-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathchip.vue
More file actions
222 lines (201 loc) · 4.6 KB
/
chip.vue
File metadata and controls
222 lines (201 loc) · 4.6 KB
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
218
219
220
221
222
<template>
<span class="d-chip">
<component
:is="interactive ? 'button' : 'span'"
:id="id"
:aria-label="ariaLabel"
:aria-labelledby="ariaLabel ? undefined : `${id}-content`"
:class="chipClasses()"
:type="interactive && 'button'"
data-qa="dt-chip"
v-on="chipListeners"
>
<span
v-if="$slots.icon"
class="d-chip__icon"
data-qa="dt-chip-icon"
>
<!-- @slot slot for Chip icon -->
<slot name="icon" />
</span>
<span
v-else-if="$slots.avatar"
data-qa="dt-chip-avatar"
>
<!-- @slot slot for Chip avatar -->
<slot name="avatar" />
</span>
<span
v-if="$slots.default"
:id="`${id}-content`"
:class="['d-chip__text', contentClass]"
data-qa="dt-chip-label"
>
<!-- @slot slot for Content within chip -->
<slot />
</span>
</component>
<dt-button
v-if="!hideClose"
:class="chipCloseButtonClasses()"
data-qa="dt-chip-close"
v-bind="$ta('CLOSE_BUTTON')"
@click="$emit('close')"
>
<template #icon>
<dt-icon-close
:size="closeButtonIconSize"
/>
</template>
</dt-button>
</span>
</template>
<script>
import { DtButton } from '@/components/button';
import { DtIconClose } from '@dialpad/dialtone-icons/vue2';
import {
CHIP_CLOSE_BUTTON_SIZE_MODIFIERS,
CHIP_SIZE_MODIFIERS,
CHIP_ICON_SIZES,
} from './chip_constants';
import { getUniqueString } from '@/common/utils';
import { DtLocalizationMixin } from '@/common/mixins';
/**
* A chip is a compact UI element that provides brief, descriptive information about an element.
* It is terse, ideally one word. It is important a button is identifiable, consistent, and
* communicates its actions clearly, and is appropriately sized to its action.
* @see https://dialtone.dialpad.com/components/chip.html
*/
export default {
name: 'DtChip',
components: {
DtButton,
DtIconClose,
},
mixins: [DtLocalizationMixin],
props: {
/**
* Hides the close button on the chip
* @values true, false
*/
hideClose: {
type: Boolean,
default: false,
},
/**
* The size of the chip.
* @values xs, sm, md
*/
size: {
type: String,
default: 'md',
validator: (s) => Object.keys(CHIP_SIZE_MODIFIERS).includes(s),
},
/**
* The interactivity of the chip.
* Makes chip clickable, apply hover/focus/active style, emit keyboard events etc.
* @values true, false
*/
interactive: {
type: Boolean,
default: true,
},
/**
* Id to use for the dialog's aria-labelledby.
*/
id: {
type: String,
default: function () { return getUniqueString(); },
},
/**
* Descriptive label for the chip content.
* If this prop is unset the content in the default slot will be used as an aria-label.
*/
ariaLabel: {
type: String,
default: '',
},
/**
* Additional class name for the chip element.
*/
contentClass: {
type: [String, Array, Object],
default: '',
},
/**
* Additional class name for the span element.
*/
labelClass: {
type: [String, Array, Object],
default: '',
},
},
emits: [
/**
* Native chip click event
*
* @event click
* @type {PointerEvent | KeyboardEvent}
*/
'click',
/**
* Close button click event
*
* @event close
*/
'close',
/**
* Native chip key up event
*
* @event keyup
* @type {KeyboardEvent}
*/
'keyup',
],
data () {
return {
isActive: false,
};
},
computed: {
chipListeners () {
return {
...this.$listeners,
click: event => {
if (this.interactive) this.$emit('click', event);
},
keyup: event => {
if (event.code?.toLowerCase() === 'delete') {
this.onClose();
} else {
this.$emit('keyup', event);
}
},
};
},
closeButtonIconSize () {
return CHIP_ICON_SIZES[this.size];
},
},
methods: {
chipClasses () {
return [
this.$attrs['grouped-chip'] ? 'd-chip' : 'd-chip__label',
CHIP_SIZE_MODIFIERS[this.size],
this.labelClass,
];
},
chipCloseButtonClasses () {
return [
'd-chip__close',
CHIP_CLOSE_BUTTON_SIZE_MODIFIERS[this.size],
];
},
onClose () {
if (!this.hideClose) {
this.$emit('close');
}
},
},
};
</script>