2016-11-10 11:14:03 +08:00
|
|
|
|
import classNames from 'classnames';
|
2023-05-06 15:49:37 +08:00
|
|
|
|
import * as React from 'react';
|
2020-04-27 13:14:00 +08:00
|
|
|
|
import { ConfigContext } from '../config-provider';
|
2022-03-15 15:33:50 +08:00
|
|
|
|
import useStyle from './style';
|
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
|
|
|
|
/**
|
2020-12-28 15:30:18 +08:00
|
|
|
|
* It is an absolute controlled component and has no uncontrolled mode.
|
|
|
|
|
*
|
|
|
|
|
* .zh-cn 该组件为完全受控组件,不支持非受控用法。
|
2020-11-13 12:05:20 +08:00
|
|
|
|
*/
|
2016-11-10 11:14:03 +08:00
|
|
|
|
checked: boolean;
|
2022-04-18 18:39:23 +08:00
|
|
|
|
children?: React.ReactNode;
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-03-21 13:02:27 +08:00
|
|
|
|
const CheckableTag: React.FC<CheckableTagProps> = (props) => {
|
|
|
|
|
const {
|
|
|
|
|
prefixCls: customizePrefixCls,
|
|
|
|
|
className,
|
|
|
|
|
checked,
|
|
|
|
|
onChange,
|
|
|
|
|
onClick,
|
|
|
|
|
...restProps
|
|
|
|
|
} = props;
|
2022-04-07 22:19:56 +08:00
|
|
|
|
const { getPrefixCls } = React.useContext(ConfigContext);
|
2020-10-12 15:07:47 +08:00
|
|
|
|
|
|
|
|
|
const handleClick = (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => {
|
2021-02-19 18:26:53 +08:00
|
|
|
|
onChange?.(!checked);
|
|
|
|
|
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);
|
2022-03-15 15:33:50 +08:00
|
|
|
|
// Style
|
2022-04-07 22:19:56 +08:00
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
2022-03-15 15:33:50 +08:00
|
|
|
|
|
2020-04-27 13:14:00 +08:00
|
|
|
|
const cls = classNames(
|
|
|
|
|
prefixCls,
|
|
|
|
|
{
|
|
|
|
|
[`${prefixCls}-checkable`]: true,
|
|
|
|
|
[`${prefixCls}-checkable-checked`]: checked,
|
|
|
|
|
},
|
|
|
|
|
className,
|
2022-03-15 15:33:50 +08:00
|
|
|
|
hashId,
|
2020-04-27 13:14:00 +08:00
|
|
|
|
);
|
2016-11-10 11:14:03 +08:00
|
|
|
|
|
2022-03-15 15:33:50 +08:00
|
|
|
|
return wrapSSR(<span {...restProps} className={cls} onClick={handleClick} />);
|
2020-04-22 10:59:24 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default CheckableTag;
|