ant-design/components/tag/CheckableTag.tsx

51 lines
1.3 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2016-11-10 11:14:03 +08:00
import classNames from 'classnames';
import { ConfigContext } from '../config-provider';
2016-11-10 11:14:03 +08:00
export interface CheckableTagProps {
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
/**
* @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;
onClick?: (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => void;
2016-11-10 11:14:03 +08:00
}
const CheckableTag: React.FC<CheckableTagProps> = ({
prefixCls: customizePrefixCls,
className,
checked,
onChange,
onClick,
...restProps
}) => {
const { getPrefixCls } = React.useContext(ConfigContext);
const handleClick = (e: React.MouseEvent<HTMLSpanElement, MouseEvent>) => {
2016-11-10 11:14:03 +08:00
if (onChange) {
onChange(!checked);
}
if (onClick) {
onClick(e);
}
2018-12-07 20:02:01 +08:00
};
2019-08-05 18:38:10 +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
return <span {...restProps} className={cls} onClick={handleClick} />;
};
export default CheckableTag;