fix: site icons category issue (#20477)

This commit is contained in:
vagusX 2019-12-27 10:23:11 +08:00 committed by 二货机器人
parent c5df5c1547
commit b2f77f92a4
2 changed files with 34 additions and 49 deletions

View File

@ -1,6 +1,8 @@
import * as AntdIcons from '@ant-design/icons/lib/icons'; import * as AntdIcons from '@ant-design/icons/lib/icons';
const all = Object.keys(AntdIcons); const all = Object.keys(AntdIcons)
.map(n => n.replace(/(Outlined|Filled|TwoTone)$/, ''))
.filter((n, i, arr) => arr.indexOf(n) === i);
const direction = [ const direction = [
'StepBackward', 'StepBackward',
@ -200,12 +202,11 @@ const logo = [
'Sketch', 'Sketch',
]; ];
const other = all.filter( const datum = [...direction, ...suggestion, ...editor, ...data, ...logo];
n => ![...direction, ...suggestion, ...editor, ...data, ...logo].includes(n),
); const other = all.filter(n => datum.includes(n));
export const categories = { export const categories = {
all,
direction, direction,
suggestion, suggestion,
editor, editor,
@ -216,14 +217,5 @@ export const categories = {
export default categories; export default categories;
export interface Categories { export type Categories = typeof categories;
all: string[];
direction: string[];
suggestion: string[];
logo: string[];
data: string[];
editor: string[];
other: string[];
}
export type CategoriesKeys = keyof Categories; export type CategoriesKeys = keyof Categories;

View File

@ -9,7 +9,11 @@ import IconPicSearcher from './IconPicSearcher';
import { FilledIcon, OutlinedIcon, TwoToneIcon } from './themeIcons'; import { FilledIcon, OutlinedIcon, TwoToneIcon } from './themeIcons';
import { categories, Categories, CategoriesKeys } from './fields'; import { categories, Categories, CategoriesKeys } from './fields';
export type ThemeType = 'filled' | 'outlined' | 'twoTone'; export enum ThemeType {
Filled = 'Filled',
Outlined = 'Outlined',
TwoTone = 'TwoTone',
}
const allIcons: { const allIcons: {
[key: string]: any; [key: string]: any;
@ -30,7 +34,7 @@ class IconDisplay extends React.Component<IconDisplayProps, IconDisplayState> {
static newIconNames: string[] = []; static newIconNames: string[] = [];
state: IconDisplayState = { state: IconDisplayState = {
theme: 'outlined', theme: ThemeType.Outlined,
searchKey: '', searchKey: '',
}; };
@ -39,15 +43,6 @@ class IconDisplay extends React.Component<IconDisplayProps, IconDisplayState> {
this.handleSearchIcon = debounce(this.handleSearchIcon, 300); this.handleSearchIcon = debounce(this.handleSearchIcon, 300);
} }
getComputedDisplayList = () => {
return Object.keys(categories)
.map((category: CategoriesKeys) => ({
category,
icons: (IconDisplay.categories[category] || []).filter(name => !!allIcons[name]),
}))
.filter(({ icons }) => Boolean(icons.length));
};
handleChangeTheme = (e: RadioChangeEvent) => { handleChangeTheme = (e: RadioChangeEvent) => {
this.setState({ this.setState({
theme: e.target.value as ThemeType, theme: e.target.value as ThemeType,
@ -61,29 +56,28 @@ class IconDisplay extends React.Component<IconDisplayProps, IconDisplayState> {
})); }));
}; };
renderCategories(list: Array<{ category: CategoriesKeys; icons: string[] }>) { renderCategories() {
const { searchKey, theme } = this.state; const { searchKey = '', theme } = this.state;
return list return Object.keys(categories)
.filter(({ category }) => category !== 'all') .map((key: CategoriesKeys) => {
.map(({ category, icons }) => ({ let iconList = categories[key];
category, if (searchKey) {
icons: icons iconList = iconList.filter(iconName =>
.filter(name => { iconName.toLowerCase().includes(searchKey.toLowerCase()),
if (theme === 'outlined') { );
return ['filled', 'twotone'].every( }
themeName => !name.toLowerCase().includes(themeName),
); return {
} category: key,
return name.toLowerCase().includes(theme); icons: iconList.map(iconName => iconName + theme).filter(iconName => allIcons[iconName]),
}) };
.filter(name => name.toLowerCase().includes(searchKey)), })
}))
.filter(({ icons }) => !!icons.length) .filter(({ icons }) => !!icons.length)
.map(({ category, icons }) => ( .map(({ category, icons }) => (
<Category <Category
key={category} key={category}
title={category} title={category as CategoriesKeys}
theme={theme} theme={theme}
icons={icons} icons={icons}
newIcons={IconDisplay.newIconNames} newIcons={IconDisplay.newIconNames}
@ -95,7 +89,6 @@ class IconDisplay extends React.Component<IconDisplayProps, IconDisplayState> {
const { const {
intl: { messages }, intl: { messages },
} = this.props; } = this.props;
const list = this.getComputedDisplayList();
return ( return (
<> <>
<div style={{ display: 'flex', justifyContent: 'space-between' }}> <div style={{ display: 'flex', justifyContent: 'space-between' }}>
@ -105,13 +98,13 @@ class IconDisplay extends React.Component<IconDisplayProps, IconDisplayState> {
size="large" size="large"
buttonStyle="solid" buttonStyle="solid"
> >
<Radio.Button value="outlined"> <Radio.Button value={ThemeType.Outlined}>
<Icon component={OutlinedIcon} /> {messages['app.docs.components.icon.outlined']} <Icon component={OutlinedIcon} /> {messages['app.docs.components.icon.outlined']}
</Radio.Button> </Radio.Button>
<Radio.Button value="filled"> <Radio.Button value={ThemeType.Filled}>
<Icon component={FilledIcon} /> {messages['app.docs.components.icon.filled']} <Icon component={FilledIcon} /> {messages['app.docs.components.icon.filled']}
</Radio.Button> </Radio.Button>
<Radio.Button value="twotone"> <Radio.Button value={ThemeType.TwoTone}>
<Icon component={TwoToneIcon} /> {messages['app.docs.components.icon.two-tone']} <Icon component={TwoToneIcon} /> {messages['app.docs.components.icon.two-tone']}
</Radio.Button> </Radio.Button>
</Radio.Group> </Radio.Group>
@ -125,7 +118,7 @@ class IconDisplay extends React.Component<IconDisplayProps, IconDisplayState> {
suffix={<IconPicSearcher />} suffix={<IconPicSearcher />}
/> />
</div> </div>
{this.renderCategories(list)} {this.renderCategories()}
</> </>
); );
} }