refactor: tag

pull/4199/head
tangjinzhou 4 years ago
parent 8c1979ef1b
commit 02e134f81d

@ -1,7 +1,7 @@
import { inject, defineComponent, PropType } from 'vue'; import { defineComponent, PropType, computed } from 'vue';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
import { defaultConfigProvider } from '../config-provider';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
import useConfigInject from '../_util/hooks/useConfigInject';
const CheckableTag = defineComponent({ const CheckableTag = defineComponent({
name: 'ACheckableTag', name: 'ACheckableTag',
@ -17,7 +17,7 @@ const CheckableTag = defineComponent({
}, },
emits: ['update:checked', 'change', 'click'], emits: ['update:checked', 'change', 'click'],
setup(props, { slots, emit }) { setup(props, { slots, emit }) {
const { getPrefixCls } = inject('configProvider', defaultConfigProvider); const { prefixCls } = useConfigInject('tag', props);
const handleClick = (e: MouseEvent) => { const handleClick = (e: MouseEvent) => {
const { checked } = props; const { checked } = props;
emit('update:checked', !checked); emit('update:checked', !checked);
@ -25,16 +25,16 @@ const CheckableTag = defineComponent({
emit('click', e); emit('click', e);
}; };
return () => { const cls = computed(() =>
const { checked, prefixCls: customizePrefixCls } = props; classNames(prefixCls.value, {
const prefixCls = getPrefixCls('tag', customizePrefixCls); [`${prefixCls.value}-checkable`]: true,
const cls = classNames(prefixCls, { [`${prefixCls.value}-checkable-checked`]: props.checked,
[`${prefixCls}-checkable`]: true, }),
[`${prefixCls}-checkable-checked`]: checked, );
});
return () => {
return ( return (
<span class={cls} onClick={handleClick}> <span class={cls.value} onClick={handleClick}>
{slots.default?.()} {slots.default?.()}
</span> </span>
); );

@ -1,5 +1,4 @@
import { import {
inject,
ref, ref,
HTMLAttributes, HTMLAttributes,
defineComponent, defineComponent,
@ -8,6 +7,7 @@ import {
PropType, PropType,
ExtractPropTypes, ExtractPropTypes,
Plugin, Plugin,
computed,
} from 'vue'; } from 'vue';
import classNames from '../_util/classNames'; import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types'; import PropTypes from '../_util/vue-types';
@ -20,8 +20,8 @@ import {
PresetStatusColorType, PresetStatusColorType,
} from '../_util/colors'; } from '../_util/colors';
import { LiteralUnion } from '../_util/type'; import { LiteralUnion } from '../_util/type';
import { defaultConfigProvider } from '../config-provider';
import CheckableTag from './CheckableTag'; import CheckableTag from './CheckableTag';
import useConfigInject from '../_util/hooks/useConfigInject';
const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`); const PresetColorRegex = new RegExp(`^(${PresetColorTypes.join('|')})(-inverse)?$`);
const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`); const PresetStatusColorRegex = new RegExp(`^(${PresetStatusColorTypes.join('|')})$`);
@ -46,8 +46,9 @@ const Tag = defineComponent({
name: 'ATag', name: 'ATag',
props: tagProps, props: tagProps,
emits: ['update:visible', 'close'], emits: ['update:visible', 'close'],
slots: ['closeIcon', 'icon'],
setup(props: TagProps, { slots, emit, attrs }) { setup(props: TagProps, { slots, emit, attrs }) {
const { getPrefixCls } = inject('configProvider', defaultConfigProvider); const { prefixCls, direction } = useConfigInject('tag', props);
const visible = ref(true); const visible = ref(true);
@ -70,26 +71,31 @@ const Tag = defineComponent({
} }
}; };
const isPresetColor = (): boolean => { const isPresetColor = computed(() => {
const { color } = props; const { color } = props;
if (!color) { if (!color) {
return false; return false;
} }
return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color); return PresetColorRegex.test(color) || PresetStatusColorRegex.test(color);
}; });
const tagClassName = computed(() =>
classNames(prefixCls.value, {
[`${prefixCls.value}-${props.color}`]: isPresetColor.value,
[`${prefixCls.value}-has-color`]: props.color && !isPresetColor.value,
[`${prefixCls.value}-hidden`]: !visible.value,
[`${prefixCls.value}-rtl`]: direction.value === 'rtl',
}),
);
return () => { return () => {
const { const {
prefixCls: customizePrefixCls,
icon = slots.icon?.(), icon = slots.icon?.(),
color, color,
closeIcon = slots.closeIcon?.(), closeIcon = slots.closeIcon?.(),
closable = false, closable = false,
} = props; } = props;
const presetColor = isPresetColor();
const prefixCls = getPrefixCls('tag', customizePrefixCls);
const renderCloseIcon = () => { const renderCloseIcon = () => {
if (closable) { if (closable) {
return closeIcon ? ( return closeIcon ? (
@ -104,15 +110,9 @@ const Tag = defineComponent({
}; };
const tagStyle = { const tagStyle = {
backgroundColor: color && !isPresetColor() ? color : undefined, backgroundColor: color && !isPresetColor.value ? color : undefined,
}; };
const tagClassName = classNames(prefixCls, {
[`${prefixCls}-${color}`]: presetColor,
[`${prefixCls}-has-color`]: color && !presetColor,
[`${prefixCls}-hidden`]: !visible.value,
});
const iconNode = icon || null; const iconNode = icon || null;
const children = slots.default?.(); const children = slots.default?.();
const kids = iconNode ? ( const kids = iconNode ? (
@ -127,7 +127,7 @@ const Tag = defineComponent({
const isNeedWave = 'onClick' in attrs; const isNeedWave = 'onClick' in attrs;
const tagNode = ( const tagNode = (
<span class={tagClassName} style={tagStyle}> <span class={tagClassName.value} style={tagStyle}>
{kids} {kids}
{renderCloseIcon()} {renderCloseIcon()}
</span> </span>

@ -16,13 +16,8 @@
background: @tag-default-bg; background: @tag-default-bg;
border: @border-width-base @border-style-base @border-color-base; border: @border-width-base @border-style-base @border-color-base;
border-radius: @border-radius-base; border-radius: @border-radius-base;
cursor: default;
opacity: 1; opacity: 1;
transition: all 0.3s @ease-in-out-circ; transition: all 0.3s;
&:hover {
opacity: 0.85;
}
&, &,
a, a,
@ -36,14 +31,12 @@
padding: 0 8px; padding: 0 8px;
} }
.@{iconfont-css-prefix}-close { &-close-icon {
.iconfont-size-under-12px(10px);
margin-left: 3px; margin-left: 3px;
color: @text-color-secondary; color: @text-color-secondary;
font-weight: bold; font-size: 10px;
cursor: pointer; cursor: pointer;
transition: all 0.3s @ease-in-out-circ; transition: all 0.3s;
&:hover { &:hover {
color: @heading-color; color: @heading-color;
@ -91,8 +84,9 @@
@lightColor: '@{color}-1'; @lightColor: '@{color}-1';
@lightBorderColor: '@{color}-3'; @lightBorderColor: '@{color}-3';
@darkColor: '@{color}-6'; @darkColor: '@{color}-6';
@textColor: '@{color}-7';
&-@{color} { &-@{color} {
color: @@darkColor; color: @@textColor;
background: @@lightColor; background: @@lightColor;
border-color: @@lightBorderColor; border-color: @@lightBorderColor;
} }
@ -127,3 +121,5 @@
margin-left: 7px; margin-left: 7px;
} }
} }
@import './rtl';

@ -0,0 +1,28 @@
@import '../../style/themes/index';
@import '../../style/mixins/index';
@tag-prefix-cls: ~'@{ant-prefix}-tag';
.@{tag-prefix-cls} {
&&-rtl {
margin-right: 0;
margin-left: 8px;
direction: rtl;
text-align: right;
}
&-close-icon {
.@{tag-prefix-cls}-rtl & {
margin-right: 3px;
margin-left: 0;
}
}
> .@{iconfont-css-prefix} + span,
> span + .@{iconfont-css-prefix} {
.@{tag-prefix-cls}-rtl& {
margin-right: 7px;
margin-left: 0;
}
}
}
Loading…
Cancel
Save