import { useCallback , useContext } from 'react' ; import classNames from 'classnames' ; import { prefix as addPrefix } from './prefix' ; import TableContext from '../TableContext' ; export type ClassValue = string | number | ClassDictionary | ClassArray | undefined | null | boolean ; export interface ClassArray extends Array < ClassValue > { } export interface ClassDictionary { [ id ] : any ; } param str returns { withClassPrefix function useClassNames ( str , controlled ? ) { const { classPrefix : contextClassPrefix = 'rs' } = useContext ( TableContext ) || { } ; const componentName = controlled ? str : addPrefix ( contextClassPrefix , str ) ; example const prefix = useCallback ( ( ... classes ) => { const mergeClasses = classes . length ? classNames ( ... classes ) . split ( ' ' ) . map ( item => addPrefix ( componentName , item ) ) : [ ] ; return mergeClasses . filter ( cls => cls ) . join ( ' ' ) ; } , [ componentName ] ) ; example const withClassPrefix = useCallback ( ( ... classes ) => { const mergeClasses = prefix ( classes ) ; return mergeClasses ? `${ componentName } ${ mergeClasses }` : componentName ; } , [ componentName , prefix ] ) ; example const rootPrefix = ( ... classes ) => { const mergeClasses = classes . length ? classNames ( ... classes ) . split ( ' ' ) . map ( item => addPrefix ( contextClassPrefix , item ) ) : [ ] ; return mergeClasses . filter ( cls => cls ) . join ( ' ' ) ; } ; return { withClassPrefix , merge : classNames , prefix , rootPrefix } ; } export default useClassNames ;