parent
13413df0d1
commit
183be1ba38
@ -1,40 +1,45 @@
|
||||
import { inject, CSSProperties, SetupContext } from 'vue';
|
||||
import { inject, defineComponent, PropType } from 'vue';
|
||||
import classNames from '../_util/classNames';
|
||||
import { defaultConfigProvider } from '../config-provider';
|
||||
import PropTypes from '../_util/vue-types';
|
||||
|
||||
export interface CheckableTagProps {
|
||||
prefixCls?: string;
|
||||
class?: string;
|
||||
style?: CSSProperties;
|
||||
checked: boolean;
|
||||
onChange?: (checked: boolean) => void;
|
||||
onClick?: (e: MouseEvent) => void;
|
||||
}
|
||||
const CheckableTag = defineComponent({
|
||||
name: 'ACheckableTag',
|
||||
props: {
|
||||
prefixCls: PropTypes.string,
|
||||
checked: PropTypes.bool,
|
||||
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);
|
||||
};
|
||||
|
||||
const CheckableTag = (props: CheckableTagProps, { slots }: SetupContext) => {
|
||||
const { getPrefixCls } = inject('configProvider', defaultConfigProvider);
|
||||
const handleClick = (e: MouseEvent) => {
|
||||
const { checked, onChange, onClick } = props;
|
||||
if (onChange) {
|
||||
onChange(!checked);
|
||||
}
|
||||
if (onClick) {
|
||||
onClick(e);
|
||||
}
|
||||
};
|
||||
return () => {
|
||||
const { checked, prefixCls: customizePrefixCls } = props;
|
||||
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
||||
const cls = classNames(prefixCls, {
|
||||
[`${prefixCls}-checkable`]: true,
|
||||
[`${prefixCls}-checkable-checked`]: checked,
|
||||
});
|
||||
|
||||
const { prefixCls: customizePrefixCls, checked } = props;
|
||||
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
||||
const cls = classNames(prefixCls, {
|
||||
[`${prefixCls}-checkable`]: true,
|
||||
[`${prefixCls}-checkable-checked`]: checked,
|
||||
});
|
||||
|
||||
return (
|
||||
<span class={cls} onClick={handleClick}>
|
||||
{slots.default?.()}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
return (
|
||||
<span class={cls} onClick={handleClick}>
|
||||
{slots.default?.()}
|
||||
</span>
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
export default CheckableTag;
|
||||
|
Loading…
Reference in new issue