-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathAutosuggest.vue
364 lines (337 loc) · 7.63 KB
/
Autosuggest.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
<template>
<span class="-screenReader">{{ selectedLabel }}</span>
<span v-if="!currentSelected.length" class="-screenReader">
{{ t('common.none') }}
</span>
<PkpBadge
v-for="item in currentSelected"
v-else
:key="item.value"
class="pkpAutosuggest__selection"
>
{{ item.label }}
<slot v-if="item.hasSlot" name="input-slot" />
<button
v-if="!isDisabled"
class="pkpAutosuggest__deselect text-negative hover:text-on-dark"
@click.stop.prevent="deselect(item)"
>
<Icon icon="Cancel" class="h-3 w-3" />
<span class="-screenReader">
{{ t('common.removeItem', {item: item.label}) }}
</span>
</button>
</PkpBadge>
<Combobox
v-if="!isDisabled"
:id="id"
:key="id"
:model-value="null"
class="pkpAutosuggest__autosuggester"
as="div"
@update:model-value="selectSuggestion"
>
<ComboboxInput
v-show="!(hideIfDisabled && (isDisabled || !enableSuggestions))"
class="pkpAutosuggest__input"
v-bind="inputProps"
autocomplete="off"
:disabled="isDisabled || !enableSuggestions"
@change="(event) => handleChange(event)"
@focus="() => handleFocus(true)"
@blur="() => handleFocus(false)"
/>
<ComboboxOptions
v-if="
suggestions.length ||
(allowCustom && localInputValue?.length) ||
isLoading
"
class="autosuggest__results-container autosuggest__results max-h-80 overflow-auto"
>
<ComboboxOption
v-if="isLoading"
v-slot="{active}"
as="template"
:disabled="true"
>
<li
class="autosuggest__results-item"
:class="active && 'autosuggest__results-item--highlighted'"
>
<Spinner />
{{ t('common.loading') }}
</li>
</ComboboxOption>
<ComboboxOption
v-if="
allowCustom &&
localInputValue?.length &&
!suggestions.includes(localInputValue)
"
v-slot="{active}"
as="template"
>
<li
class="autosuggest__results-item"
:class="active && 'autosuggest__results-item--highlighted'"
>
{{ localInputValue }}
</li>
</ComboboxOption>
<template v-if="!isLoading && enableSuggestions">
<ComboboxOption
v-for="suggestion in suggestions"
:key="suggestion.value"
v-slot="{active}"
:value="suggestion"
as="template"
>
<li
class="autosuggest__results-item flex items-center"
:class="active && 'autosuggest__results-item--highlighted'"
>
<slot
v-if="slots['option']"
name="option"
:suggestion="suggestion"
/>
<ul v-else>
<li>
{{ suggestion.label }}
</li>
<li v-if="suggestion.identifier">
<a
v-if="suggestion.identifier.match(/^http/)"
:href="suggestion.identifier"
target="_blank"
@click.stop
>
{{ suggestion.identifier }}
</a>
<template v-else>
{{ suggestion.identifier }}
</template>
</li>
<li
v-for="(extraItem, extraItemKey) in suggestion.extraItems ?? {}"
:key="extraItemKey"
>
{{ extraItem }}
</li>
</ul>
</li>
</ComboboxOption>
</template>
</ComboboxOptions>
</Combobox>
</template>
<script setup>
import {useSlots, ref, computed} from 'vue';
import {
Combobox,
ComboboxInput,
ComboboxOption,
ComboboxOptions,
} from '@headlessui/vue';
import PkpBadge from '@/components/Badge/Badge.vue';
import Icon from '@/components/Icon/Icon.vue';
import Spinner from '@/components/Spinner/Spinner.vue';
const slots = useSlots();
const props = defineProps({
/* ID to be assigned to the Combobox component */
id: {
type: String,
required: true,
},
/* Array of suggestions to the autosuggest component */
suggestions: {
type: Array,
default: () => [],
},
/* Label to be used for the selected items */
selectedLabel: {
type: String,
required: true,
},
/* Array of currently selected items */
currentSelected: {
type: Array,
default: () => [],
},
/* The input model value */
inputValue: {
type: String,
default: () => '',
},
/* If the autosuggest component is disabled */
isDisabled: {
type: Boolean,
default: () => false,
},
/* If the list of suggestions is loading */
isLoading: {
type: Boolean,
default: () => false,
},
/* If multiple entries are allowed to be selected */
isMultiple: {
type: Boolean,
default: () => true,
},
/** If custom items can be selected */
allowCustom: {
type: Boolean,
default: () => false,
},
/** Hide input value field if disabled */
hideIfDisabled: {
type: Boolean,
default: () => false
},
/** Field input id, usually used to connect with FormFieldLabel */
inputId: {type: String, required: false, default: null},
/** aria-describedby ids */
describedBy: {type: String, required: false, default: ''},
});
/**
* Props to pass to the input field
*/
const inputProps = {
'aria-describedby': `${props.describedBy}`,
class: 'pkpAutosuggest__input',
id: props.inputId,
disabled: props.isDisabled,
};
const enableSuggestions = computed(() => {
if (props.isMultiple) {
return true;
}
// disable multiple selections if only a single item is allowed and an item is already selected.
return !props.currentSelected.length;
});
const emit = defineEmits([
'update:inputValue',
'focus-changed',
'select-suggestion',
'deselect',
]);
const localInputValue = ref('');
const isFocused = ref(false);
function handleChange(event) {
localInputValue.value = event.target.value.trim();
emit('update:inputValue', localInputValue.value);
}
function handleFocus(state) {
isFocused.value = state;
emit('focus-changed', state);
}
function selectSuggestion(suggestion) {
emit('select-suggestion', suggestion);
}
function deselect(item) {
emit('deselect', item);
}
defineExpose({handleFocus});
</script>
<style lang="less">
@import '../../../styles/_import';
// Space between items in the input wrapper
.pkpAutosuggest__selection,
.pkpAutosuggest__autosuggester {
margin-top: 0.125rem;
margin-bottom: 0.125rem;
margin-inline-end: 0.25rem;
}
.pkpAutosuggest__selection {
position: relative;
padding-right: 2.5em;
}
.pkpAutosuggest__deselect {
position: absolute;
top: 0;
right: 0;
bottom: 0;
width: 2em;
padding: 0;
background: transparent;
border: 1px solid transparent;
border-left-color: @bg-border-color-light;
border-top-right-radius: 1.2em; // matches radius on button in Badge.vue
border-bottom-right-radius: 1.2em;
color: @no;
&:hover,
&:focus {
outline: 0;
border-color: @no;
background: @no;
color: #fff;
}
}
.pkpAutosuggest__autosuggester {
position: relative;
line-height: 1.6rem; // prevent jank when value is added or removed
flex-grow: 1;
}
.autosuggest__results {
border: 1px solid @primary;
border-bottom-right-radius: @radius;
border-bottom-left-radius: @radius;
background: @lift;
box-shadow: 0 0.75rem 0.75rem rgba(0, 0, 0, 0.2);
font-size: @font-sml;
&:after {
content: '';
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
height: 100%;
width: 3px;
background: @primary;
}
margin: 0;
padding: 0;
list-style: none;
.autosuggest__results-item {
position: relative;
padding: 0.5rem 1rem;
line-height: 1.5em;
&:before {
content: '';
position: absolute;
top: 50%;
left: 0;
transform: translateY(-50%);
width: 3px;
height: 100%;
background: @primary;
transition: width 0.3s;
}
& ul li {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}
.autosuggest__results-item--highlighted {
background: @bg;
}
}
.autosuggest__results-container {
position: absolute;
top: 100%;
margin-top: -2px;
width: 100%;
max-width: 100%;
min-width: 20rem;
z-index: 9999;
}
.pkpAutosuggest__input {
border: none;
width: 100%;
&:focus {
outline: none;
}
}
</style>