2023-01-11 21:37:12 +08:00
|
|
|
import React, { useCallback, useMemo, useState } from 'react';
|
|
|
|
import type { CSSProperties } from 'react';
|
2019-08-30 00:06:35 +08:00
|
|
|
import Icon, * as AntdIcons from '@ant-design/icons';
|
2023-01-11 21:37:12 +08:00
|
|
|
import type { SegmentedProps } from 'antd';
|
|
|
|
import type { IntlShape } from 'react-intl';
|
2023-01-04 18:49:55 +08:00
|
|
|
import { Segmented, Input, Empty, Affix } from 'antd';
|
2023-01-11 21:37:12 +08:00
|
|
|
import { css } from '@emotion/react';
|
2022-11-09 12:28:04 +08:00
|
|
|
import { useIntl } from 'dumi';
|
2019-04-06 13:20:22 +08:00
|
|
|
import debounce from 'lodash/debounce';
|
|
|
|
import Category from './Category';
|
2018-09-01 16:16:11 +08:00
|
|
|
import { FilledIcon, OutlinedIcon, TwoToneIcon } from './themeIcons';
|
2022-08-04 10:04:37 +08:00
|
|
|
import type { CategoriesKeys } from './fields';
|
2022-05-07 14:31:54 +08:00
|
|
|
import { categories } from './fields';
|
2023-01-04 18:49:55 +08:00
|
|
|
import useSiteToken from '../../../hooks/useSiteToken';
|
2019-08-13 14:07:17 +08:00
|
|
|
|
2019-12-27 10:23:11 +08:00
|
|
|
export enum ThemeType {
|
|
|
|
Filled = 'Filled',
|
|
|
|
Outlined = 'Outlined',
|
|
|
|
TwoTone = 'TwoTone',
|
|
|
|
}
|
2019-10-30 16:28:28 +08:00
|
|
|
|
2022-08-04 10:04:37 +08:00
|
|
|
const allIcons: { [key: string]: any } = AntdIcons;
|
2018-09-01 14:05:47 +08:00
|
|
|
|
2023-01-11 21:37:12 +08:00
|
|
|
const useStyle = () => ({
|
|
|
|
iconSearchAffix: css`
|
|
|
|
display: flex;
|
|
|
|
transition: all 0.3s;
|
|
|
|
justify-content: space-between;
|
|
|
|
`,
|
|
|
|
});
|
|
|
|
|
|
|
|
const options = (intl: IntlShape): SegmentedProps['options'] => [
|
|
|
|
{
|
|
|
|
value: ThemeType.Outlined,
|
|
|
|
icon: <Icon component={OutlinedIcon} />,
|
|
|
|
label: intl.formatMessage({ id: 'app.docs.components.icon.outlined' }),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: ThemeType.Filled,
|
|
|
|
icon: <Icon component={FilledIcon} />,
|
|
|
|
label: intl.formatMessage({ id: 'app.docs.components.icon.filled' }),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: ThemeType.TwoTone,
|
|
|
|
icon: <Icon component={TwoToneIcon} />,
|
|
|
|
label: intl.formatMessage({ id: 'app.docs.components.icon.two-tone' }),
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
interface IconSearchState {
|
2018-09-01 14:05:47 +08:00
|
|
|
theme: ThemeType;
|
2019-04-06 13:20:22 +08:00
|
|
|
searchKey: string;
|
2018-09-01 14:05:47 +08:00
|
|
|
}
|
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
const IconSearch: React.FC = () => {
|
|
|
|
const intl = useIntl();
|
2023-01-11 21:37:12 +08:00
|
|
|
const { iconSearchAffix } = useStyle();
|
|
|
|
const [displayState, setDisplayState] = useState<IconSearchState>({
|
2019-04-06 13:20:22 +08:00
|
|
|
searchKey: '',
|
2023-01-11 21:37:12 +08:00
|
|
|
theme: ThemeType.Outlined,
|
2022-08-04 10:04:37 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const newIconNames: string[] = [];
|
2022-08-05 10:22:55 +08:00
|
|
|
|
2023-01-11 21:37:12 +08:00
|
|
|
const handleSearchIcon = debounce((e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setDisplayState((prevState) => ({ ...prevState, searchKey: e.target.value }));
|
|
|
|
}, 300);
|
2022-08-05 10:22:55 +08:00
|
|
|
|
2023-01-11 21:37:12 +08:00
|
|
|
const handleChangeTheme = useCallback((value) => {
|
2023-01-04 18:49:55 +08:00
|
|
|
setDisplayState((prevState) => ({ ...prevState, theme: value as ThemeType }));
|
2022-08-05 10:22:55 +08:00
|
|
|
}, []);
|
|
|
|
|
2023-01-11 21:37:12 +08:00
|
|
|
const renderCategories = useMemo<React.ReactNode | React.ReactNode[]>(() => {
|
2022-08-04 10:04:37 +08:00
|
|
|
const { searchKey = '', theme } = displayState;
|
2019-12-27 10:23:11 +08:00
|
|
|
|
2021-09-09 10:58:32 +08:00
|
|
|
const categoriesResult = Object.keys(categories)
|
2022-11-19 13:47:33 +08:00
|
|
|
.map((key) => {
|
2022-11-09 12:28:04 +08:00
|
|
|
let iconList = categories[key as CategoriesKeys];
|
2019-12-27 10:23:11 +08:00
|
|
|
if (searchKey) {
|
2021-09-09 10:58:32 +08:00
|
|
|
const matchKey = searchKey
|
2021-11-26 12:18:21 +08:00
|
|
|
// eslint-disable-next-line prefer-regex-literals
|
2021-09-09 10:58:32 +08:00
|
|
|
.replace(new RegExp(`^<([a-zA-Z]*)\\s/>$`, 'gi'), (_, name) => name)
|
|
|
|
.replace(/(Filled|Outlined|TwoTone)$/, '')
|
|
|
|
.toLowerCase();
|
2022-11-19 13:47:33 +08:00
|
|
|
iconList = iconList.filter((iconName) => iconName.toLowerCase().includes(matchKey));
|
2019-12-27 10:23:11 +08:00
|
|
|
}
|
2019-05-07 23:54:08 +08:00
|
|
|
|
2020-07-25 15:19:33 +08:00
|
|
|
// CopyrightCircle is same as Copyright, don't show it
|
2022-11-19 13:47:33 +08:00
|
|
|
iconList = iconList.filter((icon) => icon !== 'CopyrightCircle');
|
2020-07-25 15:19:33 +08:00
|
|
|
|
2019-12-27 10:23:11 +08:00
|
|
|
return {
|
|
|
|
category: key,
|
2022-11-19 13:47:33 +08:00
|
|
|
icons: iconList
|
|
|
|
.map((iconName) => iconName + theme)
|
|
|
|
.filter((iconName) => allIcons[iconName]),
|
2019-12-27 10:23:11 +08:00
|
|
|
};
|
|
|
|
})
|
2019-05-16 17:47:50 +08:00
|
|
|
.filter(({ icons }) => !!icons.length)
|
|
|
|
.map(({ category, icons }) => (
|
|
|
|
<Category
|
|
|
|
key={category}
|
2019-12-27 10:23:11 +08:00
|
|
|
title={category as CategoriesKeys}
|
2019-12-25 14:42:15 +08:00
|
|
|
theme={theme}
|
2019-05-16 17:47:50 +08:00
|
|
|
icons={icons}
|
2022-08-04 10:04:37 +08:00
|
|
|
newIcons={newIconNames}
|
2019-05-16 17:47:50 +08:00
|
|
|
/>
|
|
|
|
));
|
2023-01-11 21:37:12 +08:00
|
|
|
return categoriesResult.length ? categoriesResult : <Empty style={{ margin: '2em 0' }} />;
|
2022-08-05 10:22:55 +08:00
|
|
|
}, [displayState.searchKey, displayState.theme]);
|
2023-01-04 18:49:55 +08:00
|
|
|
|
2023-01-11 21:37:12 +08:00
|
|
|
const [searchBarAffixed, setSearchBarAffixed] = useState<boolean>(false);
|
2023-01-04 18:49:55 +08:00
|
|
|
const { token } = useSiteToken();
|
|
|
|
const { borderRadius, colorBgContainer } = token;
|
2023-01-11 21:37:12 +08:00
|
|
|
|
|
|
|
const affixedStyle: CSSProperties = {
|
|
|
|
boxShadow: 'rgba(50, 50, 93, 0.25) 0 6px 12px -2px, rgba(0, 0, 0, 0.3) 0 3px 7px -3px',
|
|
|
|
padding: 8,
|
|
|
|
margin: -8,
|
|
|
|
borderRadius,
|
|
|
|
backgroundColor: colorBgContainer,
|
|
|
|
};
|
2023-01-04 18:49:55 +08:00
|
|
|
|
2022-08-04 10:04:37 +08:00
|
|
|
return (
|
2022-11-09 12:28:04 +08:00
|
|
|
<div className="markdown">
|
2023-01-11 21:37:12 +08:00
|
|
|
<Affix offsetTop={24} onChange={setSearchBarAffixed}>
|
|
|
|
<div css={iconSearchAffix} style={searchBarAffixed ? affixedStyle : {}}>
|
2023-01-04 18:49:55 +08:00
|
|
|
<Segmented
|
2023-01-11 21:37:12 +08:00
|
|
|
size="large"
|
2023-01-04 18:49:55 +08:00
|
|
|
value={displayState.theme}
|
2023-01-11 21:37:12 +08:00
|
|
|
options={options(intl)}
|
2023-01-04 18:49:55 +08:00
|
|
|
onChange={handleChangeTheme}
|
|
|
|
/>
|
|
|
|
<Input.Search
|
|
|
|
placeholder={intl.formatMessage({ id: 'app.docs.components.icon.search.placeholder' })}
|
|
|
|
style={{ flex: 1, marginInlineStart: 16 }}
|
|
|
|
allowClear
|
|
|
|
autoFocus
|
2023-01-11 21:37:12 +08:00
|
|
|
size="large"
|
|
|
|
onChange={handleSearchIcon}
|
2023-01-04 18:49:55 +08:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</Affix>
|
2022-08-05 10:22:55 +08:00
|
|
|
{renderCategories}
|
2022-11-09 12:28:04 +08:00
|
|
|
</div>
|
2022-08-04 10:04:37 +08:00
|
|
|
);
|
|
|
|
};
|
2018-09-01 16:16:11 +08:00
|
|
|
|
2022-11-09 12:28:04 +08:00
|
|
|
export default IconSearch;
|