ant-design/components/tag/CheckableTag.tsx

54 lines
1.3 KiB
TypeScript
Raw Normal View History

2016-11-10 11:14:03 +08:00
import classNames from 'classnames';
2022-06-22 14:57:09 +08:00
import * as React from 'react';
import { ConfigContext } from '../config-provider';
import useStyle from './style';
2016-11-10 11:14:03 +08:00
export interface CheckableTagProps {
prefixCls?: string;
className?: string;
style?: React.CSSProperties;
/**
* It is an absolute controlled component and has no uncontrolled mode.
*
* .zh-cn
*/
2016-11-10 11:14:03 +08:00
checked: boolean;
children?: React.ReactNode;
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>) => {
onChange?.(!checked);
onClick?.(e);
2018-12-07 20:02:01 +08:00
};
2019-08-05 18:38:10 +08:00
const prefixCls = getPrefixCls('tag', customizePrefixCls);
// Style
const [wrapSSR, hashId] = useStyle(prefixCls);
const cls = classNames(
prefixCls,
{
[`${prefixCls}-checkable`]: true,
[`${prefixCls}-checkable-checked`]: checked,
},
className,
hashId,
);
2016-11-10 11:14:03 +08:00
return wrapSSR(<span {...restProps} className={cls} onClick={handleClick} />);
};
export default CheckableTag;