2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-11-10 11:14:03 +08:00
|
|
|
import classNames from 'classnames';
|
2020-04-27 13:14:00 +08:00
|
|
|
import { ConfigContext } from '../config-provider';
|
2016-11-10 11:14:03 +08:00
|
|
|
|
|
|
|
export interface CheckableTagProps {
|
|
|
|
prefixCls?: string;
|
|
|
|
className?: string;
|
2019-08-15 23:43:25 +08:00
|
|
|
style?: React.CSSProperties;
|
2016-11-10 11:14:03 +08:00
|
|
|
checked: boolean;
|
2017-08-11 09:30:19 +08:00
|
|
|
onChange?: (checked: boolean) => void;
|
2016-11-10 11:14:03 +08:00
|
|
|
}
|
|
|
|
|
2020-04-22 10:59:24 +08:00
|
|
|
const CheckableTag: React.FC<CheckableTagProps> = props => {
|
2020-04-27 13:14:00 +08:00
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
2020-04-22 10:59:24 +08:00
|
|
|
const handleClick = () => {
|
|
|
|
const { checked, onChange } = props;
|
2016-11-10 11:14:03 +08:00
|
|
|
if (onChange) {
|
|
|
|
onChange(!checked);
|
|
|
|
}
|
2018-12-07 20:02:01 +08:00
|
|
|
};
|
2019-08-05 18:38:10 +08:00
|
|
|
|
2020-04-27 13:14:00 +08:00
|
|
|
const { prefixCls: customizePrefixCls, className, checked, ...restProps } = props;
|
|
|
|
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
|
|
|
const cls = classNames(
|
|
|
|
prefixCls,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-checkable`]: true,
|
|
|
|
[`${prefixCls}-checkable-checked`]: checked,
|
|
|
|
},
|
|
|
|
className,
|
|
|
|
);
|
2016-11-10 11:14:03 +08:00
|
|
|
|
2020-04-27 13:14:00 +08:00
|
|
|
delete (restProps as any).onChange; // TypeScript cannot check delete now.
|
|
|
|
return <span {...(restProps as any)} className={cls} onClick={handleClick} />;
|
2020-04-22 10:59:24 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default CheckableTag;
|