1
1
import {
2
- inject ,
3
2
ref ,
4
3
HTMLAttributes ,
5
4
defineComponent ,
8
7
PropType ,
9
8
ExtractPropTypes ,
10
9
Plugin ,
10
+ computed ,
11
11
} from 'vue' ;
12
12
import classNames from '../_util/classNames' ;
13
13
import PropTypes from '../_util/vue-types' ;
@@ -20,8 +20,8 @@ import {
20
20
PresetStatusColorType ,
21
21
} from '../_util/colors' ;
22
22
import { LiteralUnion } from '../_util/type' ;
23
- import { defaultConfigProvider } from '../config-provider' ;
24
23
import CheckableTag from './CheckableTag' ;
24
+ import useConfigInject from '../_util/hooks/useConfigInject' ;
25
25
26
26
const PresetColorRegex = new RegExp ( `^(${ PresetColorTypes . join ( '|' ) } )(-inverse)?$` ) ;
27
27
const PresetStatusColorRegex = new RegExp ( `^(${ PresetStatusColorTypes . join ( '|' ) } )$` ) ;
@@ -46,8 +46,9 @@ const Tag = defineComponent({
46
46
name : 'ATag' ,
47
47
props : tagProps ,
48
48
emits : [ 'update:visible' , 'close' ] ,
49
+ slots : [ 'closeIcon' , 'icon' ] ,
49
50
setup ( props : TagProps , { slots, emit, attrs } ) {
50
- const { getPrefixCls } = inject ( 'configProvider ', defaultConfigProvider ) ;
51
+ const { prefixCls , direction } = useConfigInject ( 'tag ', props ) ;
51
52
52
53
const visible = ref ( true ) ;
53
54
@@ -70,26 +71,31 @@ const Tag = defineComponent({
70
71
}
71
72
} ;
72
73
73
- const isPresetColor = ( ) : boolean => {
74
+ const isPresetColor = computed ( ( ) => {
74
75
const { color } = props ;
75
76
if ( ! color ) {
76
77
return false ;
77
78
}
78
79
return PresetColorRegex . test ( color ) || PresetStatusColorRegex . test ( color ) ;
79
- } ;
80
+ } ) ;
81
+
82
+ const tagClassName = computed ( ( ) =>
83
+ classNames ( prefixCls . value , {
84
+ [ `${ prefixCls . value } -${ props . color } ` ] : isPresetColor . value ,
85
+ [ `${ prefixCls . value } -has-color` ] : props . color && ! isPresetColor . value ,
86
+ [ `${ prefixCls . value } -hidden` ] : ! visible . value ,
87
+ [ `${ prefixCls . value } -rtl` ] : direction . value === 'rtl' ,
88
+ } ) ,
89
+ ) ;
80
90
81
91
return ( ) => {
82
92
const {
83
- prefixCls : customizePrefixCls ,
84
93
icon = slots . icon ?.( ) ,
85
94
color,
86
95
closeIcon = slots . closeIcon ?.( ) ,
87
96
closable = false ,
88
97
} = props ;
89
98
90
- const presetColor = isPresetColor ( ) ;
91
- const prefixCls = getPrefixCls ( 'tag' , customizePrefixCls ) ;
92
-
93
99
const renderCloseIcon = ( ) => {
94
100
if ( closable ) {
95
101
return closeIcon ? (
@@ -104,15 +110,9 @@ const Tag = defineComponent({
104
110
} ;
105
111
106
112
const tagStyle = {
107
- backgroundColor : color && ! isPresetColor ( ) ? color : undefined ,
113
+ backgroundColor : color && ! isPresetColor . value ? color : undefined ,
108
114
} ;
109
115
110
- const tagClassName = classNames ( prefixCls , {
111
- [ `${ prefixCls } -${ color } ` ] : presetColor ,
112
- [ `${ prefixCls } -has-color` ] : color && ! presetColor ,
113
- [ `${ prefixCls } -hidden` ] : ! visible . value ,
114
- } ) ;
115
-
116
116
const iconNode = icon || null ;
117
117
const children = slots . default ?.( ) ;
118
118
const kids = iconNode ? (
@@ -127,7 +127,7 @@ const Tag = defineComponent({
127
127
const isNeedWave = 'onClick' in attrs ;
128
128
129
129
const tagNode = (
130
- < span class = { tagClassName } style = { tagStyle } >
130
+ < span class = { tagClassName . value } style = { tagStyle } >
131
131
{ kids }
132
132
{ renderCloseIcon ( ) }
133
133
</ span >
0 commit comments