ant-design/components/tag/CheckableTag.tsx

38 lines
1.0 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;
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
}
const CheckableTag: React.FC<CheckableTagProps> = props => {
const { getPrefixCls } = React.useContext(ConfigContext);
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
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
delete (restProps as any).onChange; // TypeScript cannot check delete now.
return <span {...(restProps as any)} className={cls} onClick={handleClick} />;
};
export default CheckableTag;