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

137 lines
4.0 KiB
TypeScript
Raw Normal View History

2018-09-01 14:05:47 +08:00
import * as React from 'react';
2019-08-30 00:06:35 +08:00
import Icon, * as AntdIcons from '@ant-design/icons';
import { Radio, Input, Empty } from 'antd';
import type { RadioChangeEvent } from 'antd/es/radio/interface';
import { injectIntl } from 'react-intl';
2019-04-06 13:20:22 +08:00
import debounce from 'lodash/debounce';
import Category from './Category';
2019-08-22 17:53:31 +08:00
import IconPicSearcher from './IconPicSearcher';
2018-09-01 16:16:11 +08:00
import { FilledIcon, OutlinedIcon, TwoToneIcon } from './themeIcons';
import type { Categories, CategoriesKeys } from './fields';
import { categories } from './fields';
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
export enum ThemeType {
Filled = 'Filled',
Outlined = 'Outlined',
TwoTone = 'TwoTone',
}
2019-10-30 16:28:28 +08:00
use ant design icons 4.0 (#18217) * feat: use @ant-design/icons@4.0 * feat: use createFromIconfontCN to make site works * feat: update doc for Icon * feat: use icon in component Alert * feat: use icon in component Avatar * feat: use icon in component Breadcrumb * feat: use icon in component Button * feat: use icon in component Cascader * feat: use icon in component Collapse * feat: use icon in component Datepicker * feat: use icon in component Dropdown * feat: use icon in component Form * feat: use icon in component Input * feat: use icon in component InputNumber * feat: use icon in component Layout * feat: use icon in component Mention * feat: use icon in component Message * feat: use icon in component Modal * feat: use icon in component Notification * feat: use icon in component PageHeader * feat: use icon in component Pagination * feat: use icon in component Popconfirm * feat: use icon in component Progress * feat: use icon in component Rate * feat: use icon in component Result * feat: use icon in component Select * feat: use icon in component Step * feat: use icon in component Switch * feat: use icon in component Table * feat: use icon in component Tab * feat: use icon in component Tag * feat: handle rest component which using Icon * fix: remove unused vars * feat: use latest alpha ant design icons * fix: failed test in uploadlist.test.js * test: update snapshot for icons * doc: add Icon for site * doc: use @ant-design/icons in site * chore: use latest icons * fix: tslint issue * fix: test cases * fix: types for react * fix: lint rules for import orders * fix: use @ant-design/icons@4.0.0-alpha.5 to avoid insert css in server render * fix: eslint error in demo/**.md * inject antd icons * update snapshot * fix site * doc: update docs * fix: code snippets icon in site * feat: use latest @ant-design/icons * fix: icon props in message
2019-08-13 14:07:17 +08:00
const allIcons: {
[key: string]: any;
} = AntdIcons;
2018-09-01 14:05:47 +08:00
interface IconDisplayProps {
intl: any;
}
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
}
class IconDisplay extends React.PureComponent<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
2018-09-01 14:05:47 +08:00
state: IconDisplayState = {
theme: ThemeType.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
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,
}));
};
renderCategories() {
const { searchKey = '', theme } = this.state;
const categoriesResult = Object.keys(categories)
.map((key: CategoriesKeys) => {
let iconList = categories[key];
if (searchKey) {
const matchKey = searchKey
// eslint-disable-next-line prefer-regex-literals
.replace(new RegExp(`^<([a-zA-Z]*)\\s/>$`, 'gi'), (_, name) => name)
.replace(/(Filled|Outlined|TwoTone)$/, '')
.toLowerCase();
iconList = iconList.filter(iconName => iconName.toLowerCase().includes(matchKey));
}
// CopyrightCircle is same as Copyright, don't show it
iconList = iconList.filter(icon => icon !== 'CopyrightCircle');
return {
category: key,
icons: iconList.map(iconName => iconName + theme).filter(iconName => allIcons[iconName]),
};
})
2019-05-16 17:47:50 +08:00
.filter(({ icons }) => !!icons.length)
.map(({ category, icons }) => (
<Category
key={category}
title={category as CategoriesKeys}
2019-12-25 14:42:15 +08:00
theme={theme}
2019-05-16 17:47:50 +08:00
icons={icons}
newIcons={IconDisplay.newIconNames}
/>
));
return categoriesResult.length === 0 ? <Empty style={{ margin: '2em 0' }} /> : categoriesResult;
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
return (
2019-04-08 10:17:03 +08:00
<>
2019-04-06 13:20:22 +08:00
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
2019-06-19 19:09:08 +08:00
<Radio.Group
value={this.state.theme}
onChange={this.handleChangeTheme}
size="large"
buttonStyle="solid"
>
<Radio.Button value={ThemeType.Outlined}>
2019-04-06 13:20:22 +08:00
<Icon component={OutlinedIcon} /> {messages['app.docs.components.icon.outlined']}
</Radio.Button>
<Radio.Button value={ThemeType.Filled}>
2019-04-06 13:20:22 +08:00
<Icon component={FilledIcon} /> {messages['app.docs.components.icon.filled']}
</Radio.Button>
<Radio.Button value={ThemeType.TwoTone}>
2019-04-06 13:20:22 +08:00
<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']}
style={{ margin: '0 10px', flex: 1 }}
2019-04-06 13:20:22 +08:00
allowClear
onChange={e => this.handleSearchIcon(e.currentTarget.value)}
size="large"
2019-04-08 10:17:03 +08:00
autoFocus
2019-08-22 17:53:31 +08:00
suffix={<IconPicSearcher />}
2019-04-06 13:20:22 +08:00
/>
</div>
{this.renderCategories()}
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);