Jiarenxiang | 38dcb05 | 2025-03-13 16:40:09 +0800 | [diff] [blame^] | 1 | import * as React from 'react'; |
| 2 | import CopyableIcon from './CopyableIcon'; |
| 3 | import type { ThemeType } from './index'; |
| 4 | import type { CategoriesKeys } from './fields'; |
| 5 | import { useIntl } from '@umijs/max'; |
| 6 | import styles from './style.less'; |
| 7 | |
| 8 | interface CategoryProps { |
| 9 | title: CategoriesKeys; |
| 10 | icons: string[]; |
| 11 | theme: ThemeType; |
| 12 | newIcons: string[]; |
| 13 | onSelect: (type: string, name: string) => any; |
| 14 | } |
| 15 | |
| 16 | const Category: React.FC<CategoryProps> = props => { |
| 17 | |
| 18 | const { icons, title, newIcons, theme } = props; |
| 19 | const intl = useIntl(); |
| 20 | const [justCopied, setJustCopied] = React.useState<string | null>(null); |
| 21 | const copyId = React.useRef<NodeJS.Timeout | null>(null); |
| 22 | const onSelect = React.useCallback((type: string, text: string) => { |
| 23 | const { onSelect } = props; |
| 24 | if (onSelect) { |
| 25 | onSelect(type, text); |
| 26 | } |
| 27 | setJustCopied(type); |
| 28 | copyId.current = setTimeout(() => { |
| 29 | setJustCopied(null); |
| 30 | }, 2000); |
| 31 | }, []); |
| 32 | React.useEffect( |
| 33 | () => () => { |
| 34 | if (copyId.current) { |
| 35 | clearTimeout(Number(copyId.current)); |
| 36 | } |
| 37 | }, |
| 38 | [], |
| 39 | ); |
| 40 | |
| 41 | return ( |
| 42 | <div> |
| 43 | <h4>{intl.formatMessage({ |
| 44 | id: `app.docs.components.icon.category.${title}`, |
| 45 | defaultMessage: '信息', |
| 46 | })}</h4> |
| 47 | <ul className={styles.anticonsList}> |
| 48 | {icons.map(name => ( |
| 49 | <CopyableIcon |
| 50 | key={name} |
| 51 | name={name} |
| 52 | theme={theme} |
| 53 | isNew={newIcons.includes(name)} |
| 54 | justCopied={justCopied} |
| 55 | onSelect={onSelect} |
| 56 | /> |
| 57 | ))} |
| 58 | </ul> |
| 59 | </div> |
| 60 | ); |
| 61 | }; |
| 62 | |
| 63 | export default Category; |