ant-design/components/tag/CheckableTag.tsx

39 lines
1.1 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
2016-11-10 11:14:03 +08:00
import classNames from 'classnames';
import { ConfigConsumer, ConfigConsumerProps } from '../config-provider';
2016-11-10 11:14:03 +08:00
export interface CheckableTagProps {
prefixCls?: string;
className?: string;
checked: boolean;
2017-08-11 09:30:19 +08:00
onChange?: (checked: boolean) => void;
2016-11-10 11:14:03 +08:00
}
2017-07-31 00:42:25 +08:00
export default class CheckableTag extends React.Component<CheckableTagProps> {
2016-11-10 11:14:03 +08:00
handleClick = () => {
const { checked, onChange } = this.props;
if (onChange) {
onChange(!checked);
}
2018-12-07 20:02:01 +08:00
};
renderCheckableTag = ({ getPrefixCls }: ConfigConsumerProps) => {
const { prefixCls: customizePrefixCls, className, checked, ...restProps } = this.props;
const prefixCls = getPrefixCls('tag', customizePrefixCls);
2018-12-07 20:02:01 +08:00
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.
2018-12-07 20:02:01 +08:00
return <div {...restProps as any} className={cls} onClick={this.handleClick} />;
};
render() {
2018-12-07 20:02:01 +08:00
return <ConfigConsumer>{this.renderCheckableTag}</ConfigConsumer>;
}
2016-11-10 11:14:03 +08:00
}