-
Notifications
You must be signed in to change notification settings - Fork 1
/
FormPills.vue
256 lines (208 loc) · 6.04 KB
/
FormPills.vue
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<template>
<label
class="form-control"
:for="id"
>
<ul class="list-unstyled d-flex align-content-start flex-wrap mb-0">
<li
v-for="(item, i) in modelValue"
:key="i"
class="form-pill d-inline-flex align-items-center mw-100"
>
<div class="form-pill-text">
{{ item }}
</div>
<div
class="form-pill-remove"
@click="removeItem(i)"
/>
</li>
<li class="form-pill-input-item">
<div class="form-pill-input-sizer overflow-hidden">
{{ value }}
</div>
<input
:id="id"
v-model="value"
class="form-pill-input w-100"
type="text"
:placeholder="items.length ? '' : placeholder"
@blur="blur($event)"
@keydown="keydown($event)"
@paste="paste($event)"
>
</li>
</ul>
</label>
</template>
<script lang="ts">
import {PropType, computed, ref, watch} from 'vue';
import {idProps} from '@/composables/useId';
</script>
<script lang="ts" setup>
const props = defineProps({
/**
* Defines if items should be removed when pressing backspace
*/
editItemOnBackspace: {
type: Boolean,
default: false,
},
...idProps,
/**
* The maximym amount of items that can be added to the list
*/
maxItems: {
type: Number,
default: Infinity,
},
/**
* The model value
*/
modelValue: {
type: Array as PropType<string[]>,
default: () => [],
},
/**
* The placeholder for the input
*/
placeholder: {
type: String,
default: '',
},
/**
* The separator that checks if an item should be separated from the next one
*/
separator: {
type: [String, RegExp],
// For some reason it does not allow RegExp to be used as default value.
// eslint-disable-next-line vue/require-valid-default-prop
default: /[\s\n]+/,
},
/**
* The key to press to submit an item
*/
submitKeys: {
type: Array as PropType<string[]>,
default: () => ['Enter'],
},
/**
* The matcher to check if an item is valid
*/
valueMatcher: {
type: [String, RegExp],
// For some reason it does not allow RegExp to be used as default value.
// eslint-disable-next-line vue/require-valid-default-prop
default: /^.+$/,
},
});
const emit = defineEmits([
/**
* Fired when the model value is updated
* @param {string[]} value The new value
*/
'update:modelValue',
]);
const value = ref('');
const clearValue = () => value.value = '';
const items = computed({
get: () => props.modelValue,
set: (v: string[]) => emit('update:modelValue', v),
});
const maxItemsReached = computed(() => items.value.length >= props.maxItems);
const matchItem = (item: string) => !!item.match(props.valueMatcher);
const splitItem = (item: string) => item.split(props.separator);
const removeItem = (index: number) => items.value.splice(index, 1);
const validateValue = (value: string|string[]) => {
if (Array.isArray(value)) {
return value.every(validateValue);
}
return matchItem(value);
};
const addItem = (item: string) => {
if (maxItemsReached.value) {
return false;
}
const newItems = splitItem(item).filter(matchItem);
if (newItems.length === 0) {
return false;
}
const allowedItems = newItems.slice(0, props.maxItems - items.value.length);
items.value = items.value.concat(allowedItems);
return true;
};
watch(value, (v) => {
const splitItems = splitItem(v);
if (
splitItems.length > 1
&& splitItems.filter(validateValue).length === splitItems.filter(v => v !== '').length
) {
addItem(v);
clearValue();
}
});
// We need to pass the Focus Event to the blur method in order for the test to
// be able to mock the blur method.
const blur = (event: FocusEvent) => {
const splitItems = splitItem(value.value);
if (validateValue(splitItems)) {
addItem(value.value);
clearValue();
}
};
const keydown = (event: KeyboardEvent) => {
if (event.code === 'Backspace') {
if (value.value !== '') {
return;
}
const [removedItem] = removeItem(items.value.length - 1);
if (props.editItemOnBackspace && removedItem) {
value.value = removedItem;
event.preventDefault();
}
return;
}
// If the max allowed items has been reached, don't allow any more typing.
if (maxItemsReached.value) {
event.preventDefault();
return;
}
if (props.submitKeys.includes(event.code)) {
const splitItems = splitItem(value.value);
if (validateValue(splitItems)) {
addItem(value.value);
clearValue();
}
event.preventDefault();
}
};
const paste = (event: ClipboardEvent) => {
event.preventDefault();
const pasteValue = event.clipboardData.getData('text');
if (!pasteValue) {
return;
}
const newValue = value.value + pasteValue;
const splitItems = splitItem(newValue);
// If the input value together with the paste value are valid, add the items,
// otherwise concat the paste value to the input value.
if (validateValue(splitItems)) {
addItem(newValue);
clearValue();
} else {
value.value = newValue;
}
};
</script>
<docs>
```vue
<FormPills
v-model="emails"
:value-matcher="/.+@.+\.\w+/"
/>
```
## Imported Props
| Prop name | Description | Type | Values | Default |
| --------- | ----------------------------- | ------ | ------ | ------- |
| id | The id used for the component | string | - | `uuid` |
</docs>