/
TypeScriptExamples.tsx
407 lines (373 loc) · 12.4 KB
/
TypeScriptExamples.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import * as React from 'react';
import {
Interactive,
createInteractive,
InteractiveProps,
InteractiveExtendableProps,
InteractiveState,
InteractiveStateChange,
} from 'react-interactive';
import { BrowserRouter, Link } from 'react-router-dom';
import { styled } from '../stitches.config';
////////////////////////////////////
// TS demos in this file:
//
// <DemoCreateInteractiveAs />
// <DemoOnStateChangeAndChildren />
// <DemoPropsForInteractive />
// <DemoWrapAsTagName />
// <DemoWrapAsComponent />
// <DemoWrapAsUnion />
////////////////////////////////////
////////////////////////////////////
// using createInteractive(as) to extend <Interactive>
// and predefined Interactive.Tagname components
const InteractiveNav = createInteractive('nav');
const InteractiveRouterLink = createInteractive(Link);
export const DemoCreateInteractiveAs: React.VFC = () => (
<>
<Interactive.Button
// as="button" // should error
type="submit" // button specific prop
ref={(element) => {}}
hoverStyle={{ fontWeight: 'bold' }}
// foo // should error
>
Interactive.Button
</Interactive.Button>
<Interactive.A
// as="a" // should error
href="https://rafgraph.dev"
ref={(element) => {}}
hoverStyle={{ fontWeight: 'bold' }}
>
Interactive.A
</Interactive.A>
<Interactive.Div
ref={(element) => {}}
hoverStyle={{ fontWeight: 'bold' }}
onStateChange={({ state, prevState }) => {}}
>
Interactive.Div
</Interactive.Div>
<InteractiveNav
// as="nav" // should error
ref={(element) => {}}
hoverStyle={{ fontWeight: 'bold' }}
onStateChange={({ state, prevState }) => {}}
>
InteractiveNav
</InteractiveNav>
<InteractiveRouterLink
// as={Link} // should error
to="/some-path"
ref={(element) => {}}
hoverStyle={{ fontWeight: 'bold' }}
onStateChange={({ state, prevState }) => {}}
>
InteractiveRouterLink
</InteractiveRouterLink>
</>
);
////////////////////////////////////
// typing onStateChange and children as a function
const DemoOnStateChangeAndChildren: React.VFC = () => {
// use the InteractiveStateChange type to type the argument
// passed to the onStateChange callback
const handleInteractiveStateChange = React.useCallback(
({ state, prevState }: InteractiveStateChange) => {
// your logic here...
},
[],
);
// use the InteractiveState type to type the argument
// passed to children (when children is a function)
const childrenAsAFunction = React.useCallback(
({ hover, active, focus }: InteractiveState) =>
`Current state - active: ${active}, hover: ${hover}, focus: ${focus}`,
[],
);
return (
<Interactive as="button" onStateChange={handleInteractiveStateChange}>
{childrenAsAFunction}
</Interactive>
);
};
////////////////////////////////////
// typing props passed to <Interactive>
// use type InteractiveProps
// props object passed to <Interactive>, InteractiveProps includes types for `as` and `ref`
const propsForInteractiveButton: InteractiveProps<'button'> = {
as: 'button',
type: 'submit', // button specific prop
children: 'propsForInteractiveButton',
ref: (element) => {},
hoverStyle: { fontWeight: 'bold' },
onStateChange: ({ state, prevState }) => {},
// foo: true, // should error
};
const propsForInteractiveRouterLink: InteractiveProps<typeof Link> = {
as: Link,
to: '/some-path', // Link specific prop
children: 'propsForInteractiveRouterLink',
ref: (element) => {},
hoverStyle: { fontWeight: 'bold' },
onStateChange: ({ state, prevState }) => {},
// foo: true, // should error
};
const DemoPropsForInteractive: React.VFC = () => (
<>
<Interactive {...propsForInteractiveButton} />
<Interactive {...propsForInteractiveRouterLink} />
<Interactive
as="button"
type="submit" // button specific prop
ref={(element: HTMLButtonElement | null) => {}} // element type is not inferred, see https://github.com/kripod/react-polymorphic-types/issues/5
hoverStyle={{ fontWeight: 'bold' }}
onStateChange={({ state, prevState }) => {}}
>
Interactive-as-button
</Interactive>
<Interactive
as={Link}
to="/some-path"
ref={(element: React.ElementRef<typeof Link> | null) => {}} // element type is not inferred, see https://github.com/kripod/react-polymorphic-types/issues/5
hoverStyle={{ fontWeight: 'bold' }}
onStateChange={({ state, prevState }) => {}}
>
Interactive-as-Link
</Interactive>
</>
);
////////////////////////////////////
// wrapping as="button" with pass through props
// use type InteractiveExtendableProps
// the same props interface is used for wrapping with and without forwardRef
// note that InteractiveExtendableProps doesn't include `as` or `ref` props
// when using forwardRef the ref type will be added by the forwardRef function
interface WrapAsTagNameProps extends InteractiveExtendableProps<'button'> {
additionalProp?: string;
}
// as="button" without ref
const WrapAsTagNameWithoutRef: React.VFC<WrapAsTagNameProps> = ({
additionalProp,
...props
}) => {
// component logic here...
return <Interactive {...props} as="button" />;
};
// as="button" with ref
const WrapAsTagNameWithRef = React.forwardRef<
HTMLButtonElement,
WrapAsTagNameProps
>(({ additionalProp, ...props }, ref) => {
// component logic here...
return <Interactive {...props} as="button" ref={ref} />;
});
// type for props with ref, use for typing props passed to WrapAsTagNameWithRef
type WrapAsTagNameWithRefProps = React.ComponentPropsWithRef<
typeof WrapAsTagNameWithRef
>;
const propsForWrapAsTagNameWithRef: WrapAsTagNameWithRefProps = {
additionalProp: 'something',
children: 'propsForWrapAsTagNameWithRef',
ref: () => {},
type: 'submit', // button specific prop
hoverStyle: { fontWeight: 'bold' },
};
const DemoWrapAsTagName: React.VFC = () => (
<>
<WrapAsTagNameWithoutRef
// as="button" // should error
// ref={(element) => {}} // should error
type="submit" // button specific prop
hoverStyle={{ fontWeight: 'bold' }}
>
WrapAsTagNameWithoutRef
</WrapAsTagNameWithoutRef>
<WrapAsTagNameWithRef
// as="button" // should error
ref={(element) => {}}
type="submit" // button specific prop
hoverStyle={{ fontWeight: 'bold' }}
>
WrapAsTagNameWithRef
</WrapAsTagNameWithRef>
<WrapAsTagNameWithRef {...propsForWrapAsTagNameWithRef} />
<Interactive
as="button"
// ref={(element: HTMLButtonElement | null) => {}}
ref={{ current: null }}
type="submit" // button specific prop
hoverStyle={{ fontWeight: 'bold' }}
>
Interactive-as-button
</Interactive>
</>
);
////////////////////////////////////
// wrapping as={Component} with pass through props
// use type InteractiveExtendableProps
// first create a composable component to use for the `as` prop
// (or use a component from a composable library such as React Router's <Link>)
// the composable component needs to pass through props and ref using React.forwardRef
interface MyComponentProps extends React.ComponentPropsWithoutRef<'button'> {
someMyComponentProp?: string;
}
const MyComponent = React.forwardRef<HTMLButtonElement, MyComponentProps>(
({ someMyComponentProp, ...props }, ref) => {
// component logic here...
return <button {...props} ref={ref} />;
},
);
// next create the interface for the component that wraps the <Interactive> component,
// the same props interface is used for wrapping with and without forwardRef
// note that InteractiveExtendableProps doesn't include `as` or `ref` props
// when using forwardRef the ref type will be added by the forwardRef function
interface WrapAsComponentProps
extends InteractiveExtendableProps<typeof MyComponent> {
additionalProp?: string;
}
// as={Component} without ref
const WrapAsComponentWithoutRef: React.VFC<WrapAsComponentProps> = ({
additionalProp,
...props
}) => {
// component logic here...
return <Interactive {...props} as={MyComponent} />;
};
// as={Component} with ref
const WrapAsComponentWithRef = React.forwardRef<
React.ElementRef<typeof MyComponent>,
WrapAsComponentProps
>(({ additionalProp, ...props }, ref) => {
// component logic here...
return <Interactive {...props} as={MyComponent} ref={ref} />;
});
// type for props with ref, use for typing props passed to WrapAsComponentWithRef
type WrapAsComponentWithRefProps = React.ComponentPropsWithRef<
typeof WrapAsComponentWithRef
>;
const propsForWrapAsComponentWithRef: WrapAsComponentWithRefProps = {
additionalProp: 'something',
someMyComponentProp: 'something else',
children: 'propsForWrapAsTagNameWithRef',
ref: () => {},
hoverStyle: { fontWeight: 'bold' },
};
const DemoWrapAsComponent: React.VFC = () => (
<>
<WrapAsComponentWithoutRef
additionalProp="something"
someMyComponentProp="something else"
// ref={(element) => {}} // should error
hoverStyle={{ fontWeight: 'bold' }}
>
WrapAsComponentWithoutRef
</WrapAsComponentWithoutRef>
<WrapAsComponentWithRef
additionalProp="something"
someMyComponentProp="something else"
ref={(element) => {}}
hoverStyle={{ fontWeight: 'bold' }}
>
WrapAsComponentWithRef
</WrapAsComponentWithRef>
<WrapAsComponentWithRef {...propsForWrapAsComponentWithRef} />
<Interactive
as={MyComponent}
someMyComponentProp="something else"
hoverStyle={{ fontWeight: 'bold' }}
ref={(element: React.ElementRef<typeof MyComponent> | null) => {}}
>
Interactive-as-MyComponent
</Interactive>
</>
);
////////////////////////////////////
// wrapping as a union with ref
// this example is a union of an Anchor element and React Router's <Link> component
// when an href prop is passed to the component, an Anchor element is rendered
// when a to prop is passed to the component, a <Link> component is rendered
type WrapAsUnionProps =
| (InteractiveExtendableProps<typeof Link> & { href?: never })
| (InteractiveExtendableProps<'a'> & { to?: never; replace?: never });
const WrapAsUnionWithRef = React.forwardRef<
HTMLAnchorElement,
WrapAsUnionProps
>((props, ref) => {
// React Router's <Link> component doesn't have a disabled state
// so when disabled always render as="a" and remove router specific props
const As = props.to && !props.disabled ? Link : 'a';
let passThroughProps = props;
if (props.disabled) {
const { to, replace, ...propsWithoutRouterProps } = props;
passThroughProps = propsWithoutRouterProps;
}
return <Interactive {...passThroughProps} as={As} ref={ref} />;
});
type WrapAsUnionWithRefProps = React.ComponentPropsWithRef<
typeof WrapAsUnionWithRef
>;
const propsForWrapAsUnionWithRef: WrapAsUnionWithRefProps = {
ref: (element) => {},
href: 'https://rafgraph.dev',
// to: '/some-path', // should error
children: 'propsForWrapAsUnionWithRef',
hoverStyle: { fontWeight: 'bold' },
};
const DemoWrapAsUnionWithRef: React.VFC = () => (
<>
<WrapAsUnionWithRef
to="/some-path"
replace // replace is a Router Link prop
// href="https://rafgraph.dev" // should error
ref={(element) => {}}
hoverStyle={{ fontWeight: 'bold' }}
>
WrapAsUnionWithRef-RouterLink
</WrapAsUnionWithRef>
<WrapAsUnionWithRef
disabled
to="/some-path"
replace // replace is a Router Link prop
ref={(element) => {}}
hoverStyle={{ fontWeight: 'bold' }}
disabledStyle={{ opacity: 0.5 }}
>
WrapAsUnionWithRef-RouterLink-disabled
</WrapAsUnionWithRef>
<WrapAsUnionWithRef
href="https://rafgraph.dev"
// to="/some-path" // should error b/c to is a Router Link prop
// replace // should error b/c replace is a Router Link prop
ref={(element) => {}}
hoverStyle={{ fontWeight: 'bold' }}
>
WrapAsUnionWithRef-AnchorLink
</WrapAsUnionWithRef>
<WrapAsUnionWithRef {...propsForWrapAsUnionWithRef} />
</>
);
////////////////////////////////////
const ContainerDiv = styled('div', {
'&>*': {
display: 'block',
},
'&>h1': {
fontSize: '20px',
},
});
export const TypeScriptExamples: React.VFC = () => (
<BrowserRouter>
<ContainerDiv>
<h1>TypeScript Examples</h1>
<DemoCreateInteractiveAs />
<DemoOnStateChangeAndChildren />
<DemoPropsForInteractive />
<DemoWrapAsTagName />
<DemoWrapAsComponent />
<DemoWrapAsUnionWithRef />
</ContainerDiv>
</BrowserRouter>
);