ant-design-vue/components/tag/CheckableTag.tsx

46 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-10-04 14:08:12 +00:00
import { inject, defineComponent, PropType } from 'vue';
2020-09-23 09:51:02 +00:00
import classNames from '../_util/classNames';
import { defaultConfigProvider } from '../config-provider';
2020-10-04 14:08:12 +00:00
import PropTypes from '../_util/vue-types';
2020-08-23 11:33:01 +00:00
2020-10-04 14:08:12 +00:00
const CheckableTag = defineComponent({
name: 'ACheckableTag',
props: {
prefixCls: PropTypes.string,
checked: PropTypes.looseBool,
2020-10-04 14:08:12 +00:00
onChange: {
type: Function as PropType<(checked: boolean) => void>,
},
onClick: {
type: Function as PropType<(e: MouseEvent) => void>,
},
},
emits: ['update:checked', 'change', 'click'],
setup(props, { slots, emit }) {
const { getPrefixCls } = inject('configProvider', defaultConfigProvider);
const handleClick = (e: MouseEvent) => {
const { checked } = props;
emit('update:checked', !checked);
emit('change', !checked);
emit('click', e);
};
2020-08-23 11:33:01 +00:00
2020-10-04 14:08:12 +00:00
return () => {
const { checked, prefixCls: customizePrefixCls } = props;
const prefixCls = getPrefixCls('tag', customizePrefixCls);
const cls = classNames(prefixCls, {
[`${prefixCls}-checkable`]: true,
[`${prefixCls}-checkable-checked`]: checked,
});
2020-08-23 11:33:01 +00:00
2020-10-04 14:08:12 +00:00
return (
<span class={cls} onClick={handleClick}>
{slots.default?.()}
</span>
);
};
},
});
2020-08-23 11:33:01 +00:00
export default CheckableTag;