ant-design/site/theme/template/IconDisplay/index.tsx

129 lines
4.0 KiB
TypeScript
Raw Normal View History

2018-09-01 14:05:47 +08:00
import * as React from 'react';
2018-09-01 16:16:11 +08:00
import manifest from '@ant-design/icons/lib/manifest';
2018-09-01 16:48:48 +08:00
import { ThemeType as ThemeFolderType } from '@ant-design/icons/lib/types';
2019-04-06 13:20:22 +08:00
import { Radio, Icon, Input } from 'antd';
2018-09-01 16:16:11 +08:00
import { RadioChangeEvent } from 'antd/lib/radio/interface';
2019-04-06 13:20:22 +08:00
import { injectIntl, InjectedIntlProps } from 'react-intl';
import debounce from 'lodash/debounce';
import Category from './Category';
2018-09-01 16:16:11 +08:00
import { FilledIcon, OutlinedIcon, TwoToneIcon } from './themeIcons';
import { categories, Categories, CategoriesKeys } from './fields';
import { ThemeType } from 'antd/lib/icon';
2018-09-01 14:05:47 +08:00
2018-12-07 20:02:01 +08:00
interface IconDisplayProps extends InjectedIntlProps {}
2018-09-01 14:05:47 +08:00
interface IconDisplayState {
theme: ThemeType;
2019-04-06 13:20:22 +08:00
searchKey: string;
2018-09-01 14:05:47 +08:00
}
2018-09-01 16:16:11 +08:00
class IconDisplay extends React.Component<IconDisplayProps, IconDisplayState> {
2019-04-06 13:20:22 +08:00
static categories: Categories = categories;
2018-09-01 16:16:11 +08:00
2019-02-17 12:18:40 +08:00
static newIconNames: string[] = [];
2018-09-01 16:16:11 +08:00
static themeTypeMapper: { [key: string]: ThemeFolderType } = {
filled: 'fill',
outlined: 'outline',
twoTone: 'twotone',
};
2018-09-01 14:05:47 +08:00
state: IconDisplayState = {
theme: 'outlined',
2019-04-06 13:20:22 +08:00
searchKey: '',
2018-09-01 14:05:47 +08:00
};
2019-04-08 10:17:03 +08:00
constructor(props: IconDisplayProps) {
super(props);
this.handleSearchIcon = debounce(this.handleSearchIcon, 300);
}
2018-09-01 16:16:11 +08:00
getComputedDisplayList() {
2019-04-06 13:20:22 +08:00
return Object.keys(IconDisplay.categories)
2018-12-07 20:02:01 +08:00
.map((category: CategoriesKeys) => ({
category,
icons: (IconDisplay.categories[category] || []).filter(
2018-12-07 20:02:01 +08:00
name => manifest[IconDisplay.themeTypeMapper[this.state.theme]].indexOf(name) !== -1,
),
}))
2018-09-01 16:16:11 +08:00
.filter(({ icons }) => Boolean(icons.length));
}
handleChangeTheme = (e: RadioChangeEvent) => {
this.setState({
theme: e.target.value as ThemeType,
});
2018-12-07 20:02:01 +08:00
};
2018-09-01 16:16:11 +08:00
2019-04-06 13:20:22 +08:00
handleSearchIcon = (searchKey: string) => {
this.setState(prevState => ({
...prevState,
searchKey,
}));
};
2018-12-07 20:02:01 +08:00
renderCategories(list: Array<{ category: CategoriesKeys; icons: string[] }>) {
2019-05-16 17:47:50 +08:00
const { searchKey, theme } = this.state;
const otherIcons = categories.all.filter(icon => {
2019-05-16 17:47:50 +08:00
return list
.filter(({ category }) => category !== 'all')
.every(item => !item.icons.includes(icon));
});
2019-04-06 13:20:22 +08:00
return list
2019-05-16 17:47:50 +08:00
.filter(({ category }) => category !== 'all')
.concat({ category: 'other', icons: otherIcons })
.map(({ category, icons }) => ({
category,
icons: icons
.filter(name => name.includes(searchKey))
.filter(name => manifest[IconDisplay.themeTypeMapper[theme]].includes(name)),
}))
.filter(({ icons }) => !!icons.length)
.map(({ category, icons }) => (
<Category
key={category}
title={category}
icons={icons}
theme={this.state.theme}
newIcons={IconDisplay.newIconNames}
/>
));
2018-09-01 16:16:11 +08:00
}
2018-09-01 14:05:47 +08:00
render() {
2018-12-07 20:02:01 +08:00
const {
intl: { messages },
} = this.props;
2018-09-01 16:16:11 +08:00
const list = this.getComputedDisplayList();
return (
2019-04-08 10:17:03 +08:00
<>
2019-04-06 13:20:22 +08:00
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Radio.Group value={this.state.theme} onChange={this.handleChangeTheme} size="large">
<Radio.Button value="outlined">
<Icon component={OutlinedIcon} /> {messages['app.docs.components.icon.outlined']}
</Radio.Button>
<Radio.Button value="filled">
<Icon component={FilledIcon} /> {messages['app.docs.components.icon.filled']}
</Radio.Button>
<Radio.Button value="twoTone">
<Icon component={TwoToneIcon} /> {messages['app.docs.components.icon.two-tone']}
</Radio.Button>
</Radio.Group>
<Input.Search
2019-04-08 10:17:03 +08:00
placeholder={messages['app.docs.components.icon.search.placeholder']}
2019-04-06 13:20:22 +08:00
style={{ marginLeft: 10, flex: 1 }}
allowClear
onChange={e => this.handleSearchIcon(e.currentTarget.value)}
size="large"
2019-04-08 10:17:03 +08:00
autoFocus
2019-04-06 13:20:22 +08:00
/>
</div>
2018-09-01 16:16:11 +08:00
{this.renderCategories(list)}
2019-04-08 10:17:03 +08:00
</>
2018-09-01 16:16:11 +08:00
);
2018-09-01 14:05:47 +08:00
}
}
2018-09-01 16:16:11 +08:00
export default injectIntl(IconDisplay);