@@ -21,43 +21,75 @@ import { type Accessor, type JSX, createMemo } from "solid-js"
2121 * }} />
2222 * ```
2323 */
24- export function Match <
25- T extends { [ k in Tag ] : PropertyKey } ,
26- Tag extends PropertyKey ,
24+ export function MatchTag <
25+ T extends { [ k in Tag ] : PropertyKey } ,
26+ Tag extends PropertyKey ,
2727> ( props : {
28- on : T | null | undefined ,
29- tag : Tag ,
30- case : { [ K in T [ Tag ] ] : ( v : Accessor < T & { [ k in Tag ] : K } > ) => JSX . Element } ,
28+ on : T | null | undefined ,
29+ tag : Tag ,
30+ case : { [ K in T [ Tag ] ] : ( v : Accessor < T & { [ k in Tag ] : K } > ) => JSX . Element } ,
3131 fallback ?: JSX . Element ,
32- partial ?: false ,
32+ partial ?: false ,
3333} ) : JSX . Element
34- export function Match <
35- T extends { type : PropertyKey } ,
34+ export function MatchTag <
35+ T extends { type : PropertyKey } ,
3636> ( props : {
37- on : T | null | undefined ,
38- case : { [ K in T [ 'type' ] ] : ( v : Accessor < T & { [ k in 'type' ] : K } > ) => JSX . Element } ,
37+ on : T | null | undefined ,
38+ case : { [ K in T [ 'type' ] ] : ( v : Accessor < T & { [ k in 'type' ] : K } > ) => JSX . Element } ,
3939 fallback ?: JSX . Element ,
40- partial ?: false ,
40+ partial ?: false ,
4141} ) : JSX . Element
42- export function Match <
43- T extends { [ k in Tag ] : PropertyKey } ,
44- Tag extends PropertyKey ,
42+ export function MatchTag <
43+ T extends { [ k in Tag ] : PropertyKey } ,
44+ Tag extends PropertyKey ,
4545> ( props : {
46- on : T | null | undefined ,
47- tag : Tag ,
48- case : { [ K in T [ Tag ] ] ?: ( v : Accessor < T & { [ k in Tag ] : K } > ) => JSX . Element } ,
46+ on : T | null | undefined ,
47+ tag : Tag ,
48+ case : { [ K in T [ Tag ] ] ?: ( v : Accessor < T & { [ k in Tag ] : K } > ) => JSX . Element } ,
4949 fallback ?: JSX . Element ,
50- partial : true ,
50+ partial : true ,
5151} ) : JSX . Element
52- export function Match <
53- T extends { type : PropertyKey } ,
52+ export function MatchTag <
53+ T extends { type : PropertyKey } ,
5454> ( props : {
55- on : T | null | undefined ,
56- case : { [ K in T [ 'type' ] ] ?: ( v : Accessor < T & { [ k in 'type' ] : K } > ) => JSX . Element } ,
55+ on : T | null | undefined ,
56+ case : { [ K in T [ 'type' ] ] ?: ( v : Accessor < T & { [ k in 'type' ] : K } > ) => JSX . Element } ,
5757 fallback ?: JSX . Element ,
58- partial : true ,
58+ partial : true ,
5959} ) : JSX . Element
60- export function Match ( props : any ) : any {
61- const kind = createMemo ( ( ) => props . on ?. [ props . tag ?? 'type' ] )
62- return createMemo ( ( ) => props . case [ kind ( ) ] ?.( ( ) => props . on ) ?? props . fallback )
60+ export function MatchTag ( props : any ) : any {
61+ const kind = createMemo ( ( ) => props . on ?. [ props . tag ?? 'type' ] )
62+ return createMemo ( ( ) => props . case [ kind ( ) ] ?.( ( ) => props . on ) ?? props . fallback )
63+ }
64+
65+ /**
66+ * Control-flow component for matching union literals.
67+ *
68+ * @example
69+ * ```tsx
70+ * type MyUnion = 'foo' | 'bar'
71+ *
72+ * const [value, setValue] = createSignal<MyUnion>('foo')
73+ *
74+ * <Match on={value()} case={{
75+ * foo: () => <p>foo</p>,
76+ * bar: () => <p>bar</p>,
77+ * }} />
78+ * ```
79+ */
80+ export function MatchValue < T extends PropertyKey > ( props : {
81+ on : T | null | undefined ,
82+ case : { [ K in T ] : ( v : K ) => JSX . Element } ,
83+ fallback ?: JSX . Element ,
84+ partial ?: false ,
85+ } ) : JSX . Element
86+ export function MatchValue < T extends PropertyKey > ( props : {
87+ on : T | null | undefined ,
88+ case : { [ K in T ] ?: ( v : K ) => JSX . Element } ,
89+ fallback ?: JSX . Element ,
90+ partial : true ,
91+ } ) : JSX . Element
92+ export function MatchValue ( props : any ) : any {
93+ const kind = createMemo ( ( ) => props . on )
94+ return createMemo ( ( ) => props . case [ kind ( ) ] ?.( kind ( ) ) ?? props . fallback )
6395}
0 commit comments