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;
|
2020-11-13 12:05:20 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description it is an absolute controlled component and has no uncontrolled mode.
|
|
|
|
|
* @description.zh-CN 该组件为完全受控组件,不支持非受控用法。
|
|
|
|
|
*/
|
2016-11-10 11:14:03 +08:00
|
|
|
|
checked: boolean;
|
2017-08-11 09:30:19 +08:00
|
|
|
|
onChange?: (checked: boolean) => void;
|
2020-10-12 15:07:47 +08:00
|
|
|
|
onClick?: (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => void;
|
2016-11-10 11:14:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-12 15:07:47 +08:00
|
|
|
|
const CheckableTag: React.FC<CheckableTagProps> = ({
|
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
className,
|
|
|
|
|
checked,
|
|
|
|
|
onChange,
|
|
|
|
|
onClick,
|
|
|
|
|
...restProps
|
|
|
|
|
}) => {
|
2020-04-27 13:14:00 +08:00
|
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
2020-10-12 15:07:47 +08:00
|
|
|
|
|
|
|
|
|
const handleClick = (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => {
|
2016-11-10 11:14:03 +08:00
|
|
|
|
if (onChange) {
|
|
|
|
|
onChange(!checked);
|
|
|
|
|
}
|
2020-06-05 12:23:44 +08:00
|
|
|
|
if (onClick) {
|
|
|
|
|
onClick(e);
|
|
|
|
|
}
|
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 = getPrefixCls('tag', customizePrefixCls);
|
|
|
|
|
const cls = classNames(
|
|
|
|
|
prefixCls,
|
|
|
|
|
{
|
|
|
|
|
[`${prefixCls}-checkable`]: true,
|
|
|
|
|
[`${prefixCls}-checkable-checked`]: checked,
|
|
|
|
|
},
|
|
|
|
|
className,
|
|
|
|
|
);
|
2016-11-10 11:14:03 +08:00
|
|
|
|
|
2020-10-12 15:07:47 +08:00
|
|
|
|
return <span {...restProps} className={cls} onClick={handleClick} />;
|
2020-04-22 10:59:24 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CheckableTag;
|