2017-11-17 14:38:54 +08:00
|
|
|
import * as React from 'react';
|
2016-11-10 11:14:03 +08:00
|
|
|
import classNames from 'classnames';
|
2018-12-05 19:12:18 +08:00
|
|
|
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
|
|
|
};
|
2018-12-05 19:12:18 +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
|
|
|
|
2017-03-06 15:48: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} />;
|
|
|
|
};
|
2018-12-05 19:12:18 +08:00
|
|
|
|
|
|
|
render() {
|
2018-12-07 20:02:01 +08:00
|
|
|
return <ConfigConsumer>{this.renderCheckableTag}</ConfigConsumer>;
|
2018-12-05 19:12:18 +08:00
|
|
|
}
|
2016-11-10 11:14:03 +08:00
|
|
|
}
|