|
1 | 1 | import { useStyles } from 'botframework-webchat-styles/react'; |
2 | 2 | import { validateProps } from 'botframework-webchat-react-valibot'; |
3 | 3 | import cx from 'classnames'; |
4 | | -import { object, optional, pipe, readonly, picklist } from 'valibot'; |
| 4 | +import { object, optional, picklist, pipe, readonly, type OptionalSchema, type PicklistSchema } from 'valibot'; |
5 | 5 | import React, { type ComponentType } from 'react'; |
6 | 6 |
|
7 | 7 | type Prefixes<T> = T extends `${infer P}--${string}` ? P : never; |
8 | 8 |
|
9 | 9 | type SuffixesOf<Prefix extends string, T> = T extends `${Prefix}--${infer S}` ? S : never; |
10 | 10 |
|
11 | | -type VariantMap<T> = { |
| 11 | +type ModifierMap<T> = { |
12 | 12 | [P in Prefixes<keyof T>]?: SuffixesOf<P, keyof T>; |
13 | 13 | }; |
14 | 14 |
|
15 | | -function createPropsSchema(styles: CSSModuleClasses) { |
| 15 | +function createPropsSchema< |
| 16 | + const TCSSModfuleClasses extends CSSModuleClasses, |
| 17 | + const TModifiers extends Array<keyof ModifierMap<TCSSModfuleClasses>> |
| 18 | +>(styles: TCSSModfuleClasses, modifiers: TModifiers) { |
| 19 | + type CSSModuleModifiers = ModifierMap<TCSSModfuleClasses>; |
| 20 | + |
16 | 21 | const props = Object.keys(styles).reduce((acc, key) => { |
17 | | - const [base, modifier] = key.split('--'); |
18 | | - if (modifier) { |
| 22 | + const [base, modifier] = key.split('--') as [keyof CSSModuleModifiers, string | undefined]; |
| 23 | + if (modifier && modifiers.includes(base)) { |
19 | 24 | acc.has(base) || acc.set(base, new Set()); |
20 | 25 | acc.get(base).add(modifier); |
21 | 26 | } |
22 | 27 | return acc; |
23 | | - }, new Map<string, Set<string>>()); |
| 28 | + }, new Map<keyof CSSModuleModifiers, Set<string>>()); |
| 29 | + |
24 | 30 | return pipe( |
25 | 31 | object( |
26 | 32 | Object.fromEntries( |
27 | 33 | Array.from(props.entries()).map(([base, modifiers]) => [base, optional(picklist(Array.from(modifiers)))]) |
28 | | - ) |
| 34 | + ) as unknown as { |
| 35 | + [key in TModifiers[number]]: OptionalSchema< |
| 36 | + PicklistSchema<Array<CSSModuleModifiers[key]>, undefined>, |
| 37 | + undefined |
| 38 | + >; |
| 39 | + } |
29 | 40 | ), |
30 | 41 | readonly() |
31 | 42 | ); |
32 | 43 | } |
33 | 44 |
|
34 | | -export default function createIconComponent<T extends { className?: string | undefined }, K extends CSSModuleClasses>( |
35 | | - styles: K, |
36 | | - BaseIcon: ComponentType<T> |
37 | | -) { |
38 | | - const propsSchema = createPropsSchema(styles); |
39 | | - return (props => { |
| 45 | +export default function createIconComponent< |
| 46 | + const TProps extends { className?: string | undefined }, |
| 47 | + const TModifiers extends Array<keyof ModifierMap<TCSSModfuleClasses>>, |
| 48 | + const TCSSModfuleClasses extends CSSModuleClasses |
| 49 | +>(styles: TCSSModfuleClasses, modifiers: TModifiers, BaseIcon: ComponentType<TProps>) { |
| 50 | + type CSSModuleModifiers = ModifierMap<TCSSModfuleClasses>; |
| 51 | + |
| 52 | + // Do not bail if no CSS modules TypeScript plugin is provided. |
| 53 | + type FinalCSSModuleModifiers = keyof CSSModuleModifiers extends never |
| 54 | + ? Record<keyof TModifiers[number], string> |
| 55 | + : Pick<CSSModuleModifiers, TModifiers[number]>; |
| 56 | + |
| 57 | + const modifierPropsSchema = createPropsSchema(styles, modifiers); |
| 58 | + |
| 59 | + const component = (props => { |
40 | 60 | const { className, ...rest } = props; |
41 | | - const validatedProps = validateProps(propsSchema, props); |
| 61 | + const validatedProps = validateProps(modifierPropsSchema, props); |
42 | 62 | const classNames = useStyles(styles); |
43 | 63 |
|
44 | 64 | const classes = Object.entries(validatedProps).map(([key, value]) => classNames[`${key}--${value}`]); |
45 | 65 |
|
46 | | - return <BaseIcon className={cx(className, classes)} {...(rest as T)} />; |
47 | | - }) as ComponentType<VariantMap<K> & T>; |
| 66 | + return <BaseIcon className={cx(className, classes)} {...(rest as TProps)} />; |
| 67 | + }) as ComponentType<FinalCSSModuleModifiers & TProps>; |
| 68 | + |
| 69 | + return { component, modifierPropsSchema }; |
48 | 70 | } |
0 commit comments