1
|
|
|
export {} |
2
|
|
|
|
3
|
|
|
type ClassnamesProp<T extends Record<string, unknown>> = {classnames: T} |
4
|
|
|
|
5
|
|
|
type WithClassNamed<K extends string = string> = {classnames: Record<K, unknown>} |
6
|
|
|
interface Keying<T extends WithClassNamed> { |
7
|
|
|
(source: Partial<T["classnames"]>) : string |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
it("+ rename with function generic inheritance", () => { |
11
|
|
|
function keying<T extends WithClassNamed>(source: Partial<T["classnames"]>) { |
12
|
|
|
return Object.keys(source).join(" ") |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
function keyingCreator<T extends WithClassNamed>() { |
16
|
|
|
const lambda: Keying<T> = source => Object.keys(source).join(" ") |
17
|
|
|
return lambda |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
type ParentProps = ChildProps & ClassnamesProp<{ |
21
|
|
|
parent1: string |
22
|
|
|
}> |
23
|
|
|
|
24
|
|
|
function Parent({classnames, classnames: { |
25
|
|
|
parent1, |
26
|
|
|
renamed: child1 |
27
|
|
|
}}: ParentProps) { |
28
|
|
|
return `${keying<ParentProps>({ |
29
|
|
|
parent1, |
30
|
|
|
renamed: child1 |
31
|
|
|
})} ${Child({classnames})}` |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
type ChildProps = ClassnamesProp<{ |
35
|
|
|
renamed: string |
36
|
|
|
child2: string |
37
|
|
|
}> |
38
|
|
|
|
39
|
|
|
function Child({classnames: { |
40
|
|
|
renamed: child1, |
41
|
|
|
child2 |
42
|
|
|
}}: ChildProps) { |
43
|
|
|
const k = keyingCreator<ChildProps>() |
44
|
|
|
return k({ |
45
|
|
|
renamed: child1, |
46
|
|
|
child2 |
47
|
|
|
}) |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
expect( |
51
|
|
|
Parent({classnames: {}} as ParentProps) |
52
|
|
|
).toBe( |
53
|
|
|
"parent1 renamed renamed child2" |
54
|
|
|
) |
55
|
|
|
}) |
56
|
|
|
|
57
|
|
|
it("- rename from bind", () => { |
58
|
|
|
function keyingCtx<K extends string, T extends WithClassNamed<K>>(this: T, source: Partial<T["classnames"]>) { |
59
|
|
|
return Object.keys(source).join(" ") |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
type ParentProps = ChildProps & { |
63
|
|
|
classnames: { |
64
|
|
|
parent1: string |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
function Parent(props: ParentProps) { |
69
|
|
|
const k = keyingCtx.bind(props) |
70
|
|
|
, { |
71
|
|
|
parent1, |
72
|
|
|
renamed_whatever: child1 |
73
|
|
|
} = props.classnames |
74
|
|
|
|
75
|
|
|
return `${k({ |
76
|
|
|
parent1, |
77
|
|
|
renamed: child1 |
78
|
|
|
})} ${Child(props)}` |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
type ChildProps = { |
82
|
|
|
classnames: { |
83
|
|
|
renamed_whatever: string |
84
|
|
|
child2: string |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
function Child(props: ChildProps) { |
89
|
|
|
const k = keyingCtx.bind(props) |
90
|
|
|
, { |
91
|
|
|
renamed_whatever: child1, |
92
|
|
|
child2 |
93
|
|
|
} = props.classnames |
94
|
|
|
|
95
|
|
|
return k({ |
96
|
|
|
renamed: child1, |
97
|
|
|
child2 |
98
|
|
|
}) |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
expect( |
102
|
|
|
Parent({classnames: {}} as ParentProps) |
103
|
|
|
).toBe( |
104
|
|
|
"parent1 renamed renamed child2" |
105
|
|
|
) |
106
|
|
|
}) |
107
|
|
|
|
108
|
|
|
it("rename keys as string(-) or same shape object(+)", () => { |
109
|
|
|
function keyer<T extends WithClassNamed>(...keys: (keyof T["classnames"])[]) { |
110
|
|
|
return keys.join(" ") |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
function keyShape<T extends WithClassNamed>(toggler: {[K in keyof T["classnames"]]?: boolean}) { |
114
|
|
|
return Object.keys(toggler) |
115
|
|
|
.filter(key => toggler[key]) |
116
|
|
|
.join(" ") |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
type ParentProps = ChildProps & ClassnamesProp<{ |
121
|
|
|
parent1: string |
122
|
|
|
}> |
123
|
|
|
|
124
|
|
|
function Parent(props: ParentProps) { |
125
|
|
|
return `${keyShape<ParentProps>({ |
126
|
|
|
parent1: true, |
127
|
|
|
renamed: true, |
128
|
|
|
child2: false |
129
|
|
|
})} ${Child(props)}` |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
type ChildProps = ClassnamesProp<{ |
133
|
|
|
renamed: string |
134
|
|
|
child2: string |
135
|
|
|
}> |
136
|
|
|
|
137
|
|
|
function Child(_: ChildProps) { |
138
|
|
|
return keyer<ChildProps>( |
139
|
|
|
//@ts-expect-error |
140
|
|
|
"child1", |
141
|
|
|
"child2" |
142
|
|
|
) |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
expect( |
146
|
|
|
Parent({classnames: {}} as ParentProps) |
147
|
|
|
).toBe( |
148
|
|
|
"parent1 renamed child1 child2" |
149
|
|
|
) |
150
|
|
|
}) |
151
|
|
|
|