222
schangxiang@126.com
2025-05-02 8472b30bf3ee867a226cc4345c13599d4d027a4e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { defineComponent, computed, SetupContext } from 'vue'
import styles from './Radio.module.scss'
import Empty from '../Empty/Empty'
import Icon from '../Icon/Icon'
interface OptionType {
  label: string
  value: string
  name: string
}
 
interface DataType {
  name?: string
  label?: string
  value?: string
  description?: string
  type?: string
}
 
interface TagProps {
  data: DataType[]
  options: OptionType[]
  modelValue: string
  [key: string]: any
}
 
export default defineComponent<TagProps>({
  // @ts-ignore
  name: 'Tag',
  emits: ['click', 'update:modelValue', 'change', 'update:data'],
  props: {
    data: {
      type: [Array, Object],
      default: null,
    },
    options: {
      type: Array,
      default: null,
    },
    modelValue: {
      type: [String, Number],
      default: '',
    },
    trigger: {
      type: String,
      default: 'hover',
    },
    showClose: {
      type: Boolean,
      default: false,
    },
    labelWidth: {
      type: String,
      default: '100px',
    },
  },
  setup(props: TagProps, { attrs, slots, emit }: SetupContext) {
    const modelData = computed({
      get() {
        return props.modelValue
      },
      set(value) {
        emit('update:modelValue', value)
      },
    })
    console.log('modelData', modelData.value)
    const data = computed({
      get() {
        return props.data
      },
      set(value) {
        emit('update:data', value)
      },
    })
    /**
     * 根据value查找label
     * @param value
     * @returns
     */
    const findOptionLabelByValue = (value: string) => {
      const item: any = props.options.find((item) => item.value === value)
      console.log('item', item)
      return item?.label || item?.name || ''
    }
    /**
     * 选择
     * @param v
     */
    const onCommand = (v: any) => {
      modelData.value = findOptionLabelByValue(v) || v
      emit('change', v)
    }
    /**
     * 当v-model:data时,生效
     */
    const onClose = (item: DataType, evt: Event) => {
      evt?.stopPropagation()
      data.value = data.value.filter((i) => i !== item)
    }
 
    /**
     * click
     * @param evt Event
     */
    const onClick = (evt: Event) => {
      evt?.stopPropagation()
      emit('click', evt)
    }
 
    console.log(props.data, props.modelValue)
 
    return () => {
      // 多tag情况,传data[]
      if (Array.isArray(props.data)) {
        return props.data.map((item: DataType, index: any) => {
          return (
            <div
              class={styles.tag}
              style="margin-right: 5px;cursor: initial;"
              key={index}
              onClick={onClick}
            >
              {item.name || item.label || item.description || item.type}
              {props.showClose ? (
                <Icon
                  class={styles.tagClose}
                  icon="tag_close"
                  width={8}
                  height={8}
                  onClick={(evt) => onClose(item, evt)}
                />
              ) : null}
            </div>
          )
        })
      }
      // 下拉选择hover
      if (Array.isArray(props.options)) {
        return (
          <el-tooltip
            effect="dark"
            content={
              modelData.value
                ? findOptionLabelByValue(modelData.value)
                : '请选择'
            }
            placement="top"
          >
            <el-dropdown
              trigger={props.trigger}
              popperClass={styles.dropdown}
              onCommand={onCommand}
              placement="bottom-start"
              max-height={500}
              vSlots={{
                dropdown: () =>
                  props.options.length ? (
                    <el-dropdown-menu>
                      {props.options.map((item: OptionType) => {
                        return (
                          <el-dropdown-item command={item.value}>
                            <div class={styles.lineTag}>
                              <div class={styles.fitTag}>
                                {' '}
                                {item.label || item.name}
                              </div>
                            </div>
                          </el-dropdown-item>
                        )
                      })}
                    </el-dropdown-menu>
                  ) : (
                    <Empty />
                  ),
              }}
            >
              {modelData.value ? (
                <div
                  class={styles.tag}
                  style={{ width: props.labelWidth }}
                  onClick={() => emit('click')}
                >
                  {findOptionLabelByValue(modelData.value)}
                </div>
              ) : (
                <div
                  class={[styles.tag, styles.pl]}
                  style={{ width: props.labelWidth }}
                >
                  请选择
                </div>
              )}
            </el-dropdown>
          </el-tooltip>
        )
      }
      // 默认只展示一个tag
      return (
        <div
          class={styles.tag}
          style={{ width: props.labelWidth }}
          onClick={() => emit('click')}
        >
          {slots.default && slots.default()}
          {/* {props.showClose ? (
            <Icon
              class={styles.tagClose}
              icon="tag_close"
              width={8}
              height={8}
              onClick={(evt) => onClose(item, evt)}
            />
          ) : null} */}
        </div>
      )
    }
  },
})