You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
4.1 KiB

import * as React from 'react';
6 years ago
import manifest from '@ant-design/icons/lib/manifest';
import { ThemeType as ThemeFolderType } from '@ant-design/icons/lib/types';
import { Radio, Icon, Input } from 'antd';
import { RadioChangeEvent } from 'antd/es/radio/interface';
import { injectIntl, InjectedIntlProps } from 'react-intl';
import debounce from 'lodash/debounce';
import Category from './Category';
6 years ago
import { FilledIcon, OutlinedIcon, TwoToneIcon } from './themeIcons';
import { categories, Categories, CategoriesKeys } from './fields';
import { ThemeType } from 'antd/es/icon';
interface IconDisplayProps extends InjectedIntlProps {}
interface IconDisplayState {
theme: ThemeType;
searchKey: string;
}
6 years ago
class IconDisplay extends React.Component<IconDisplayProps, IconDisplayState> {
static categories: Categories = categories;
6 years ago
static newIconNames: string[] = [];
6 years ago
static themeTypeMapper: { [key: string]: ThemeFolderType } = {
filled: 'fill',
outlined: 'outline',
twoTone: 'twotone',
};
state: IconDisplayState = {
theme: 'outlined',
searchKey: '',
};
constructor(props: IconDisplayProps) {
super(props);
this.handleSearchIcon = debounce(this.handleSearchIcon, 300);
}
6 years ago
getComputedDisplayList() {
return Object.keys(IconDisplay.categories)
.map((category: CategoriesKeys) => ({
category,
icons: (IconDisplay.categories[category] || []).filter(
name => manifest[IconDisplay.themeTypeMapper[this.state.theme]].indexOf(name) !== -1,
),
}))
6 years ago
.filter(({ icons }) => Boolean(icons.length));
}
handleChangeTheme = (e: RadioChangeEvent) => {
this.setState({
theme: e.target.value as ThemeType,
});
};
6 years ago
handleSearchIcon = (searchKey: string) => {
this.setState(prevState => ({
...prevState,
searchKey,
}));
};
renderCategories(list: Array<{ category: CategoriesKeys; icons: string[] }>) {
const { searchKey, theme } = this.state;
const otherIcons = categories.all.filter(icon => {
return list
.filter(({ category }) => category !== 'all')
.every(item => !item.icons.includes(icon));
});
return list
.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}
/>
));
6 years ago
}
render() {
const {
intl: { messages },
} = this.props;
6 years ago
const list = this.getComputedDisplayList();
return (
<>
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<Radio.Group
value={this.state.theme}
onChange={this.handleChangeTheme}
size="large"
buttonStyle="solid"
>
<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
placeholder={messages['app.docs.components.icon.search.placeholder']}
style={{ marginLeft: 10, flex: 1 }}
allowClear
onChange={e => this.handleSearchIcon(e.currentTarget.value)}
size="large"
autoFocus
/>
</div>
6 years ago
{this.renderCategories(list)}
</>
6 years ago
);
}
}
6 years ago
export default injectIntl(IconDisplay);