fix: tag props
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 classNames from '../_util/classNames';
|
||||||
import { defaultConfigProvider } from '../config-provider';
|
import { defaultConfigProvider } from '../config-provider';
|
||||||
|
import PropTypes from '../_util/vue-types';
|
||||||
|
|
||||||
export interface CheckableTagProps {
|
const CheckableTag = defineComponent({
|
||||||
prefixCls?: string;
|
name: 'ACheckableTag',
|
||||||
class?: string;
|
props: {
|
||||||
style?: CSSProperties;
|
prefixCls: PropTypes.string,
|
||||||
checked: boolean;
|
checked: PropTypes.bool,
|
||||||
onChange?: (checked: boolean) => void;
|
onChange: {
|
||||||
onClick?: (e: MouseEvent) => void;
|
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) => {
|
return () => {
|
||||||
const { getPrefixCls } = inject('configProvider', defaultConfigProvider);
|
const { checked, prefixCls: customizePrefixCls } = props;
|
||||||
const handleClick = (e: MouseEvent) => {
|
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
||||||
const { checked, onChange, onClick } = props;
|
const cls = classNames(prefixCls, {
|
||||||
if (onChange) {
|
[`${prefixCls}-checkable`]: true,
|
||||||
onChange(!checked);
|
[`${prefixCls}-checkable-checked`]: checked,
|
||||||
}
|
});
|
||||||
if (onClick) {
|
|
||||||
onClick(e);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const { prefixCls: customizePrefixCls, checked } = props;
|
return (
|
||||||
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
<span class={cls} onClick={handleClick}>
|
||||||
const cls = classNames(prefixCls, {
|
{slots.default?.()}
|
||||||
[`${prefixCls}-checkable`]: true,
|
</span>
|
||||||
[`${prefixCls}-checkable-checked`]: checked,
|
);
|
||||||
});
|
};
|
||||||
|
},
|
||||||
return (
|
});
|
||||||
<span class={cls} onClick={handleClick}>
|
|
||||||
{slots.default?.()}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default CheckableTag;
|
export default CheckableTag;
|
||||||
|
|
|
@ -1,15 +1,6 @@
|
||||||
import {
|
import { inject, ref, HTMLAttributes, defineComponent, App, VNodeTypes, watchEffect } from 'vue';
|
||||||
inject,
|
|
||||||
ref,
|
|
||||||
HTMLAttributes,
|
|
||||||
defineComponent,
|
|
||||||
SetupContext,
|
|
||||||
App,
|
|
||||||
VNodeTypes,
|
|
||||||
CSSProperties,
|
|
||||||
} from 'vue';
|
|
||||||
import classNames from '../_util/classNames';
|
import classNames from '../_util/classNames';
|
||||||
import omit from 'omit.js';
|
import PropTypes from '../_util/vue-types';
|
||||||
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
import CloseOutlined from '@ant-design/icons-vue/CloseOutlined';
|
||||||
import Wave from '../_util/wave';
|
import Wave from '../_util/wave';
|
||||||
import {
|
import {
|
||||||
|
@ -27,11 +18,9 @@ const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')}
|
||||||
|
|
||||||
export interface TagProps extends HTMLAttributes {
|
export interface TagProps extends HTMLAttributes {
|
||||||
prefixCls?: string;
|
prefixCls?: string;
|
||||||
class?: string;
|
|
||||||
color?: LiteralUnion<PresetColorType | PresetStatusColorType, string>;
|
color?: LiteralUnion<PresetColorType | PresetStatusColorType, string>;
|
||||||
closable?: boolean;
|
closable?: boolean;
|
||||||
closeIcon?: VNodeTypes;
|
closeIcon?: VNodeTypes;
|
||||||
style?: CSSProperties;
|
|
||||||
visible?: boolean;
|
visible?: boolean;
|
||||||
onClose?: (e: MouseEvent) => void;
|
onClose?: (e: MouseEvent) => void;
|
||||||
icon?: VNodeTypes;
|
icon?: VNodeTypes;
|
||||||
|
@ -39,50 +28,48 @@ export interface TagProps extends HTMLAttributes {
|
||||||
|
|
||||||
const Tag = defineComponent({
|
const Tag = defineComponent({
|
||||||
name: 'ATag',
|
name: 'ATag',
|
||||||
inheritAttrs: false,
|
emits: ['update:visible', 'close'],
|
||||||
setup(_: TagProps, { slots, attrs }: SetupContext) {
|
setup(props: TagProps, { slots, emit, attrs }) {
|
||||||
const { getPrefixCls } = inject('configProvider', defaultConfigProvider);
|
const { getPrefixCls } = inject('configProvider', defaultConfigProvider);
|
||||||
|
|
||||||
const visible = ref(true);
|
const visible = ref(true);
|
||||||
const props = attrs as TagProps;
|
|
||||||
|
watchEffect(() => {
|
||||||
|
if (props.visible !== undefined) {
|
||||||
|
visible.value = props.visible!;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const handleCloseClick = (e: MouseEvent) => {
|
||||||
|
e.stopPropagation();
|
||||||
|
emit('update:visible', false);
|
||||||
|
emit('close', e);
|
||||||
|
|
||||||
|
if (e.defaultPrevented) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (props.visible === undefined) {
|
||||||
|
visible.value = false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const isPresetColor = (): boolean => {
|
||||||
|
const { color } = props;
|
||||||
|
if (!color) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
|
||||||
|
};
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
const {
|
const {
|
||||||
prefixCls: customizePrefixCls,
|
prefixCls: customizePrefixCls,
|
||||||
icon,
|
icon,
|
||||||
color,
|
color,
|
||||||
closeIcon,
|
closeIcon = slots.closeIcon?.(),
|
||||||
class: className,
|
|
||||||
style = {},
|
|
||||||
closable = false,
|
closable = false,
|
||||||
...restProps
|
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
if ('visible' in props) {
|
|
||||||
visible.value = props.visible!;
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleCloseClick = (e: MouseEvent) => {
|
|
||||||
e.stopPropagation();
|
|
||||||
if (props.onClose) {
|
|
||||||
props.onClose(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (e.defaultPrevented) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!('visible' in props)) {
|
|
||||||
visible.value = false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const isPresetColor = (): boolean => {
|
|
||||||
if (!color) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
|
|
||||||
};
|
|
||||||
|
|
||||||
const presetColor = isPresetColor();
|
const presetColor = isPresetColor();
|
||||||
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
const prefixCls = getPrefixCls('tag', customizePrefixCls);
|
||||||
|
|
||||||
|
@ -101,20 +88,14 @@ const Tag = defineComponent({
|
||||||
|
|
||||||
const tagStyle = {
|
const tagStyle = {
|
||||||
backgroundColor: color && !isPresetColor() ? color : undefined,
|
backgroundColor: color && !isPresetColor() ? color : undefined,
|
||||||
...style,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const tagClassName = classNames(
|
const tagClassName = classNames(prefixCls, {
|
||||||
prefixCls,
|
[`${prefixCls}-${color}`]: presetColor,
|
||||||
{
|
[`${prefixCls}-has-color`]: color && !presetColor,
|
||||||
[`${prefixCls}-${color}`]: presetColor,
|
[`${prefixCls}-hidden`]: !visible.value,
|
||||||
[`${prefixCls}-has-color`]: color && !presetColor,
|
});
|
||||||
[`${prefixCls}-hidden`]: !visible.value,
|
|
||||||
},
|
|
||||||
className,
|
|
||||||
);
|
|
||||||
|
|
||||||
const tagProps = omit(restProps, ['visible']);
|
|
||||||
const iconNode = icon || null;
|
const iconNode = icon || null;
|
||||||
const children = slots.default?.();
|
const children = slots.default?.();
|
||||||
const kids = iconNode ? (
|
const kids = iconNode ? (
|
||||||
|
@ -126,10 +107,10 @@ const Tag = defineComponent({
|
||||||
children
|
children
|
||||||
);
|
);
|
||||||
|
|
||||||
const isNeedWave = 'onClick' in props;
|
const isNeedWave = 'onClick' in attrs;
|
||||||
|
|
||||||
const tagNode = (
|
const tagNode = (
|
||||||
<span {...tagProps} class={tagClassName} style={tagStyle}>
|
<span v-show={visible.value} class={tagClassName} style={tagStyle}>
|
||||||
{kids}
|
{kids}
|
||||||
{renderCloseIcon()}
|
{renderCloseIcon()}
|
||||||
</span>
|
</span>
|
||||||
|
@ -140,6 +121,19 @@ const Tag = defineComponent({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Tag.props = {
|
||||||
|
prefixCls: PropTypes.string,
|
||||||
|
color: PropTypes.string,
|
||||||
|
closable: PropTypes.bool.def(false),
|
||||||
|
closeIcon: PropTypes.any,
|
||||||
|
visible: {
|
||||||
|
type: Boolean,
|
||||||
|
default: undefined,
|
||||||
|
},
|
||||||
|
onClose: PropTypes.func,
|
||||||
|
icon: PropTypes.any,
|
||||||
|
};
|
||||||
|
|
||||||
Tag.CheckableTag = CheckableTag;
|
Tag.CheckableTag = CheckableTag;
|
||||||
|
|
||||||
Tag.install = (app: App) => {
|
Tag.install = (app: App) => {
|
||||||
|
|
Loading…
Reference in New Issue