ant-design/components/tag/index.tsx

134 lines
3.3 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2016-01-27 01:47:12 +08:00
import classNames from 'classnames';
import omit from 'omit.js';
import CloseOutlined from '@ant-design/icons/CloseOutlined';
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
2016-11-10 11:14:03 +08:00
import CheckableTag from './CheckableTag';
2020-05-28 09:56:45 +08:00
import { ConfigContext } from '../config-provider';
import {
PresetColorTypes,
PresetStatusColorTypes,
PresetColorType,
PresetStatusColorType,
} from '../_util/colors';
2019-04-10 21:42:35 +08:00
import Wave from '../_util/wave';
import { LiteralUnion } from '../_util/type';
2015-08-21 17:12:42 +08:00
export { CheckableTagProps } from './CheckableTag';
export interface TagProps extends React.HTMLAttributes<HTMLSpanElement> {
2016-12-19 15:19:15 +08:00
prefixCls?: string;
className?: string;
color?: LiteralUnion<PresetColorType | PresetStatusColorType, string>;
2016-07-13 11:14:24 +08:00
closable?: boolean;
2018-06-12 22:23:04 +08:00
visible?: boolean;
2016-07-13 11:14:24 +08:00
onClose?: Function;
style?: React.CSSProperties;
icon?: React.ReactNode;
2016-07-09 10:52:16 +08:00
}
2016-07-13 11:14:24 +08:00
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`);
2020-04-29 11:29:26 +08:00
export interface TagType
extends React.ForwardRefExoticComponent<TagProps & React.RefAttributes<HTMLElement>> {
CheckableTag: typeof CheckableTag;
}
2019-08-05 18:38:10 +08:00
2020-05-28 09:56:45 +08:00
const InternalTag: React.ForwardRefRenderFunction<unknown, TagProps> = (
{
prefixCls: customizePrefixCls,
className,
style,
children,
icon,
color,
onClose,
closable = false,
...props
},
ref,
) => {
const { getPrefixCls, direction } = React.useContext(ConfigContext);
const [visible, setVisible] = React.useState(true);
React.useEffect(() => {
if ('visible' in props) {
setVisible(props.visible!);
}
}, [props.visible]);
2015-07-30 23:52:44 +08:00
const isPresetColor = (): boolean => {
if (!color) {
return false;
}
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
2018-06-12 22:23:04 +08:00
};
2020-05-28 09:56:45 +08:00
const tagStyle = {
backgroundColor: color && !isPresetColor() ? color : undefined,
...style,
};
2019-08-05 18:38:10 +08:00
2020-05-28 09:56:45 +08:00
const presetColor = isPresetColor();
const prefixCls = getPrefixCls('tag', customizePrefixCls);
const tagClassName = classNames(
prefixCls,
{
[`${prefixCls}-${color}`]: presetColor,
[`${prefixCls}-has-color`]: color && !presetColor,
[`${prefixCls}-hidden`]: !visible,
[`${prefixCls}-rtl`]: direction === 'rtl',
},
className,
);
2019-08-05 18:38:10 +08:00
2020-05-28 09:56:45 +08:00
const handleCloseClick = (e: React.MouseEvent<HTMLElement>) => {
e.stopPropagation();
2016-10-24 16:30:38 +08:00
if (onClose) {
onClose(e);
}
2018-12-22 19:20:38 +08:00
if (e.defaultPrevented) {
2018-06-12 22:23:04 +08:00
return;
}
if (!('visible' in props)) {
setVisible(false);
2016-07-13 11:14:24 +08:00
}
2018-12-07 20:02:01 +08:00
};
2018-06-12 22:23:04 +08:00
const renderCloseIcon = () => {
2020-05-28 09:56:45 +08:00
return closable ? <CloseOutlined onClick={handleCloseClick} /> : null;
};
const isNeedWave =
2020-05-28 09:56:45 +08:00
'onClick' in props || (children && (children as React.ReactElement<any>).type === 'a');
const tagProps = omit(props, ['visible']);
const iconNode = icon || null;
const kids = iconNode ? (
<>
{iconNode}
<span>{children}</span>
</>
) : (
children
);
2020-05-28 09:56:45 +08:00
const tagNode = (
<span {...tagProps} ref={ref} className={tagClassName} style={tagStyle}>
{kids}
{renderCloseIcon()}
</span>
);
2020-05-28 09:56:45 +08:00
return isNeedWave ? <Wave>{tagNode}</Wave> : tagNode;
};
2020-04-29 11:29:26 +08:00
const Tag = React.forwardRef<unknown, TagProps>(InternalTag) as TagType;
Tag.displayName = 'Tag';
Tag.CheckableTag = CheckableTag;
2018-06-13 10:57:34 +08:00
export default Tag;