| | import * as React from 'react'; |
| | import classnames from 'classnames'; |
| |
|
| | import { ValidChar } from './interface'; |
| |
|
| | type TemplateSemanticClassNames<T extends string> = Partial<Record<T, string>>; |
| |
|
| | export type SemanticSchema = { |
| | _default?: string; |
| | } & { |
| | [key: `${ValidChar}${string}`]: SemanticSchema; |
| | }; |
| |
|
| | |
| | export function mergeClassNames< |
| | T extends string, |
| | SemanticClassNames extends Partial<Record<T, any>> = TemplateSemanticClassNames<T>, |
| | >(schema: SemanticSchema | undefined, ...classNames: (SemanticClassNames | undefined)[]) { |
| | const mergedSchema = schema || {}; |
| |
|
| | return classNames.reduce((acc: any, cur) => { |
| | |
| | Object.keys(cur || {}).forEach((key) => { |
| | const keySchema = mergedSchema[key as keyof SemanticSchema] as SemanticSchema; |
| | const curVal = (cur as SemanticClassNames)[key as keyof SemanticClassNames]; |
| |
|
| | if (keySchema && typeof keySchema === 'object') { |
| | if (curVal && typeof curVal === 'object') { |
| | |
| | acc[key] = mergeClassNames(keySchema, acc[key], curVal); |
| | } else { |
| | |
| | const { _default: defaultField } = keySchema; |
| | acc[key] = acc[key] || {}; |
| | acc[key][defaultField!] = classnames(acc[key][defaultField!], curVal); |
| | } |
| | } else { |
| | |
| | acc[key] = classnames(acc[key], curVal); |
| | } |
| | }); |
| | return acc; |
| | }, {} as SemanticClassNames) as SemanticClassNames; |
| | } |
| |
|
| | function useSemanticClassNames<ClassNamesType extends object>( |
| | schema: SemanticSchema | undefined, |
| | ...classNames: (Partial<ClassNamesType> | undefined)[] |
| | ): Partial<ClassNamesType> { |
| | return React.useMemo( |
| | () => mergeClassNames(schema, ...classNames), |
| | [classNames], |
| | ) as ClassNamesType; |
| | } |
| |
|
| | |
| | function useSemanticStyles<StylesType extends object>( |
| | ...styles: (Partial<StylesType> | undefined)[] |
| | ) { |
| | return React.useMemo(() => { |
| | return styles.reduce( |
| | (acc, cur = {}) => { |
| | Object.keys(cur).forEach((key) => { |
| | acc[key] = { ...acc[key], ...(cur as Record<string, React.CSSProperties>)[key] }; |
| | }); |
| | return acc; |
| | }, |
| | {} as Record<string, React.CSSProperties>, |
| | ); |
| | }, [styles]) as StylesType; |
| | } |
| |
|
| | |
| | function fillObjectBySchema<T extends object>(obj: T, schema: SemanticSchema): T { |
| | const newObj: any = { ...obj }; |
| |
|
| | Object.keys(schema).forEach((key) => { |
| | if (key !== '_default') { |
| | const nestSchema = (schema as any)[key] as SemanticSchema; |
| | const nextValue = newObj[key] || {}; |
| |
|
| | newObj[key] = nestSchema ? fillObjectBySchema(nextValue, nestSchema) : nextValue; |
| | } |
| | }); |
| |
|
| | return newObj; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | export default function useMergeSemantic<ClassNamesType extends object, StylesType extends object>( |
| | classNamesList: (ClassNamesType | undefined)[], |
| | stylesList: (StylesType | undefined)[], |
| | schema?: SemanticSchema, |
| | ) { |
| | const mergedClassNames = useSemanticClassNames(schema, ...classNamesList) as ClassNamesType; |
| | const mergedStyles = useSemanticStyles(...stylesList) as StylesType; |
| |
|
| | return React.useMemo(() => { |
| | return [ |
| | fillObjectBySchema(mergedClassNames, schema!) as ClassNamesType, |
| | fillObjectBySchema(mergedStyles, schema!) as StylesType, |
| | ] as const; |
| | }, [mergedClassNames, mergedStyles]); |
| | } |
| |
|