vue3-extended-multiselect 1.0.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/.eslintrc.js +34 -34
  2. package/.github/workflows/publish.yml +41 -41
  3. package/CHANGELOG.md +109 -91
  4. package/LICENSE +21 -21
  5. package/README.md +261 -261
  6. package/index.js +2 -2
  7. package/jest.config.js +60 -57
  8. package/package.json +47 -47
  9. package/src/assets/base-arrow.svg +70 -70
  10. package/src/assets/cancel.svg +34 -34
  11. package/src/assets/circle-arrow.svg +7 -7
  12. package/src/assets/double-arrow.svg +39 -39
  13. package/src/assets/inner-arrow.svg +8 -8
  14. package/src/assets/loader.svg +5 -5
  15. package/src/assets/triangle-arrow.svg +72 -72
  16. package/src/assets/triangle-circle-arrow.svg +38 -38
  17. package/src/assets/wide-arrow.svg +72 -72
  18. package/src/components/ExtendedMultiselectCancel.vue +148 -148
  19. package/src/components/ExtendedMultiselectInput.vue +617 -617
  20. package/src/components/ExtendedMultiselectLoader.vue +182 -182
  21. package/src/components/ExtendedMultiselectMultiple.vue +295 -295
  22. package/src/components/ExtendedMultiselectOptions.vue +1092 -1091
  23. package/src/components/ExtendedMultiselectToggle.vue +261 -261
  24. package/src/components/Vue3ExtendedMultiselect.vue +1665 -1660
  25. package/src/composition/cancel.js +33 -33
  26. package/src/composition/image-path.js +16 -16
  27. package/src/composition/labels.js +39 -39
  28. package/src/composition/preselected-options.js +66 -66
  29. package/src/composition/search-value.js +33 -33
  30. package/src/composition/sizes.js +42 -42
  31. package/src/composition/toggle.js +33 -33
  32. package/src/events/emitter.js +2 -2
  33. package/src/sets/sets.js +99 -99
  34. package/src/styles/animations.css +18 -18
  35. package/src/styles/main.css +433 -432
  36. package/src/styles/variables.css +47 -47
  37. package/tests/unit/events.spec.js +135 -135
  38. package/tests/unit/props.spec.js +1040 -1040
  39. package/tests/utils/mount.js +37 -37
  40. package/tests/utils/utils.js +20 -20
  41. package/typings/vue3-extended-multiselect.d.ts +42 -42
@@ -1,618 +1,618 @@
1
- <template>
2
- <div v-if="showSearchField">
3
- <input
4
- aria-controls="extended-search-field"
5
- ref="extendedInput"
6
- type="text"
7
- :accesskey="attrs.accesskey"
8
- :autocomplete="attrs.autocomplete"
9
- :class="searchFieldClass"
10
- :disabled="disabled"
11
- :id="inputId"
12
- :name="attrs.name"
13
- :placeholder="appropriatePlaceholder"
14
- :spellcheck="attrs.spellcheck"
15
- :tabindex="attrs.tabindex"
16
- :translate="attrs.translate"
17
- v-model="searchValue"
18
- @blur.prevent="rollUp"
19
- @focus.prevent="expand"
20
- @input="search"
21
- />
22
- <div
23
- class="extended__multiselect-block"
24
- v-show="!searchFieldForwarding && !multiple"
25
- @click.stop="expand"
26
- >
27
- <slot
28
- name="labelBlock"
29
- :label-block-value="singleLabel"
30
- >
31
- <span>{{ singleLabel }}</span>
32
- </slot>
33
- </div>
34
- <extended-multiselect-multiple
35
- v-show="multiple && !!selectedOptions.length"
36
- :style="multipleBlocksMargin"
37
- :disabled="disabled"
38
- :loading="loading"
39
- :toggle-multiple-blocks-limit="toggleMultipleBlocksLimit"
40
- :empty-objects-placeholder="emptyObjectsPlaceholder"
41
- :multiple-blocks-limit-message="multipleBlocksLimitMessage"
42
- :theme-type="themeType"
43
- :increase-display-by="increaseDisplayBy"
44
- :multipleBlocksLimit="multipleBlocksLimit"
45
- :selected-options="selectedOptions"
46
- :create-custom-option-label="createCustomOptionLabel"
47
- >
48
- <template #optionBlock="{ label, deselectBlock, index }">
49
- <slot
50
- name="optionBlock"
51
- :label="label"
52
- :index="index"
53
- :deselect-block="deselectBlock"
54
- >
55
- </slot>
56
- </template>
57
- <template #maxElements>
58
- <slot name="maxElements"></slot>
59
- </template>
60
- <template
61
- v-if="toggleMultipleBlocksLimit"
62
- #showMore="{ showMoreOptions }"
63
- >
64
- <slot
65
- name="showMore"
66
- :show-more-options="showMoreOptions"
67
- >
68
- </slot>
69
- </template>
70
- </extended-multiselect-multiple>
71
- </div>
72
- <div v-else-if="!showSearchField && multiple">
73
- <extended-multiselect-multiple
74
- :disabled="disabled"
75
- :loading="loading"
76
- :toggle-multiple-blocks-limit="toggleMultipleBlocksLimit"
77
- :empty-objects-placeholder="emptyObjectsPlaceholder"
78
- :multiple-blocks-limit-message="multipleBlocksLimitMessage"
79
- :theme-type="themeType"
80
- :increase-display-by="increaseDisplayBy"
81
- :multipleBlocksLimit="multipleBlocksLimit"
82
- :selected-options="selectedOptions"
83
- :create-custom-option-label="createCustomOptionLabel"
84
- >
85
- <template #optionBlock="{ label, deselectBlock, index }">
86
- <slot
87
- name="optionBlock"
88
- :label="label"
89
- :index="index"
90
- :deselect-block="deselectBlock"
91
- >
92
- </slot>
93
- </template>
94
- <template
95
- v-if="toggleMultipleBlocksLimit"
96
- #showMore="{ showMoreOptions }"
97
- >
98
- <slot
99
- name="showMore"
100
- :show-more-options="showMoreOptions"
101
- ></slot>
102
- </template>
103
- </extended-multiselect-multiple>
104
- <input
105
- class="extended__multiselect-input--hidden"
106
- ref="extendedInput"
107
- type="text"
108
- @blur.prevent="rollUp"
109
- @focus.prevent="expand"
110
- />
111
- </div>
112
- <div
113
- class="extended__multiselect-block"
114
- v-else
115
- >
116
- <slot
117
- name="labelBlock"
118
- :label-block-value="singleLabel"
119
- >
120
- <span>{{ singleLabel }}</span>
121
- </slot>
122
- <input
123
- class="extended__multiselect-input--hidden"
124
- ref="extendedInput"
125
- type="text"
126
- @blur.prevent="rollUp"
127
- @focus.prevent="expand"
128
- />
129
- </div>
130
- </template>
131
-
132
- <script setup>
133
- import {
134
- computed,
135
- defineProps,
136
- inject,
137
- nextTick,
138
- onBeforeMount,
139
- onMounted,
140
- ref,
141
- toRefs,
142
- useAttrs,
143
- watch,
144
- } from "vue";
145
-
146
- import debounce from "lodash.debounce";
147
- import emitter from "../events/emitter";
148
-
149
- import ExtendedMultiselectMultiple from "./ExtendedMultiselectMultiple.vue";
150
-
151
- const props = defineProps({
152
- /**
153
- * Determines which placeholder to use
154
- * depending on the options creation mode
155
- * @property {boolean} createOnTheGo
156
- */
157
- createOnTheGo: {
158
- type: Boolean,
159
- required: true,
160
- },
161
-
162
- /**
163
- * Blocks search field
164
- * @property {boolean} disabled
165
- */
166
- disabled: {
167
- type: Boolean,
168
- required: true,
169
- },
170
-
171
- /**
172
- * Determines whether option list expanded or not
173
- * @property {boolean} dropdownActive
174
- */
175
- dropdownActive: {
176
- type: Boolean,
177
- required: true,
178
- },
179
-
180
- /**
181
- * Provides "loading" state of component
182
- * to ExtendedMultiselectMultiple child component
183
- * @property {boolean} loading
184
- */
185
- loading: {
186
- type: Boolean,
187
- required: true,
188
- },
189
-
190
- /**
191
- * Determines whether to use ExtendedMultiselectMultiple
192
- * child component and some other related functionality
193
- * @property {boolean} multiple
194
- */
195
- multiple: {
196
- type: Boolean,
197
- required: true,
198
- },
199
-
200
- /**
201
- * Renews the pattern of internal search for available options
202
- * @property {boolean} searchFilterActive
203
- */
204
- searchFilterActive: {
205
- type: Boolean,
206
- required: true,
207
- },
208
-
209
- /**
210
- * Determines whether to show search field
211
- * @property {boolean} showSearchField
212
- */
213
- showSearchField: {
214
- type: Boolean,
215
- required: true,
216
- },
217
-
218
- /**
219
- * Determines whether to allow user to increase limit of shown
220
- * elements with selected options by special icon
221
- * @property {boolean} toggleMultipleBlocksLimit
222
- */
223
- toggleMultipleBlocksLimit: {
224
- type: Boolean,
225
- required: true,
226
- },
227
-
228
- /**
229
- * Determines whether to roll up options list
230
- * by option selection
231
- * @property {boolean} toggleOptionsBySelect
232
- */
233
- toggleOptionsBySelect: {
234
- type: Boolean,
235
- required: true,
236
- },
237
-
238
- /**
239
- * Determines whether to clear value of search field
240
- * after next switch of search field display
241
- * @property {boolean} togglingSavesSearchValue
242
- */
243
- togglingSavesSearchValue: {
244
- type: Boolean,
245
- required: true,
246
- },
247
-
248
- /**
249
- * Placeholder which will be shown
250
- * if "createOnTheGo" prop equals true
251
- * @property {string} createOptionPlaceholder
252
- */
253
- createOptionPlaceholder: {
254
- type: String,
255
- required: true,
256
- },
257
-
258
- /**
259
- * Placeholder used in internal options filters
260
- * and provided to ExtendedMultiselectMultiple child component
261
- * @property {string} emptyObjectsPlaceholder
262
- */
263
- emptyObjectsPlaceholder: {
264
- type: String,
265
- required: true,
266
- },
267
-
268
- /**
269
- * "placeholder" attribute for search field
270
- * @property {string} placeholder
271
- */
272
- placeholder: {
273
- type: String,
274
- required: true,
275
- },
276
-
277
- /**
278
- * Provides current theme of extended multiselect
279
- * to ExtendedMultiselectMultiple child component
280
- * @property {string} themeType
281
- */
282
- themeType: {
283
- type: String,
284
- required: true,
285
- },
286
-
287
- /**
288
- * Provides limit of hidden options amount to be shown next
289
- * @property {number} increaseDisplayBy
290
- */
291
- increaseDisplayBy: {
292
- type: Number,
293
- required: true,
294
- },
295
-
296
- /**
297
- * Provides limit of option blocks amount
298
- * to ExtendedMultiselectMultiple child component
299
- * @default null
300
- * @property {number} multipleBlocksLimit
301
- */
302
- multipleBlocksLimit: {
303
- type: Number,
304
- default: null,
305
- },
306
-
307
- /**
308
- * Provides list of selected options
309
- * to ExtendedMultiselectMultiple child component
310
- * @property {Array} selectedOptions
311
- */
312
- selectedOptions: {
313
- type: Array,
314
- required: true,
315
- },
316
-
317
- /**
318
- * Provides function that creates custom label for
319
- * block with selected option to ExtendedMultiselectMultiple
320
- * child component
321
- * @property {Function} createCustomOptionLabel
322
- */
323
- createCustomOptionLabel: {
324
- type: Function,
325
- required: true,
326
- },
327
-
328
- /**
329
- * Provides function that creates limit message for maximal of selected
330
- * options to ExtendedMultiselectMultiple child component
331
- * @property {Function} multipleBlocksLimitMessage
332
- */
333
- multipleBlocksLimitMessage: {
334
- type: Function,
335
- required: true,
336
- },
337
-
338
- /**
339
- * "id" attribute of search field
340
- * @default null
341
- * @property {string|number} inputId
342
- */
343
- inputId: {
344
- type: [String, Number],
345
- default: null,
346
- },
347
- });
348
-
349
- const searchState = inject("searchState");
350
- const setSearchValue = inject("setSearchValue");
351
- const setSearchPattern = inject("setSearchPattern");
352
-
353
- const blurSkip = ref(false);
354
- const reversePrevented = ref(false);
355
- const searchFieldFocused = ref(false);
356
- const optionWillBeTriggered = ref(false);
357
- const searchValue = ref("");
358
- const singleLabel = ref("");
359
- const blurSkipByToggleIcon = ref(0);
360
- const blurSkipByBlock = ref(0);
361
- const labelBlocks = ref([]);
362
- const searchDebounce = ref(debounce(() => {
363
- const searchPattern = searchValue.value ? new RegExp(`${searchValue.value}`, "i") : null;
364
- emitter.emit("extended:search-pattern-changed", searchValue.value);
365
-
366
- setSearchPattern(searchPattern);
367
- }, 250));
368
-
369
- /**
370
- * Element references
371
- */
372
- const extendedInput = ref(null);
373
-
374
- const {
375
- createOnTheGo,
376
- createOptionPlaceholder,
377
- disabled,
378
- increaseDisplayBy,
379
- multiple,
380
- placeholder,
381
- searchFilterActive,
382
- showSearchField,
383
- toggleOptionsBySelect,
384
- togglingSavesSearchValue,
385
- } = toRefs(props);
386
-
387
- const attrs = useAttrs();
388
-
389
- /**
390
- * Defines "placeholder" attribute of search field
391
- * @function
392
- * @returns {string} placeholder
393
- */
394
- const appropriatePlaceholder = computed(() => {
395
- return createOnTheGo.value ? createOptionPlaceholder.value : placeholder.value;
396
- });
397
-
398
- /**
399
- * Sets "margin-top" css property to multiple options block
400
- * if search field is shown
401
- * @function
402
- * @returns {Object|null} margin-top
403
- */
404
- const multipleBlocksMargin = computed(() => {
405
- return searchFieldForwarding.value ? { marginTop: "4px" } : null;
406
- });
407
-
408
- /**
409
- * Defines classes of search field
410
- * @function
411
- * @returns {Array} classes
412
- */
413
- const searchFieldClass = computed(() => {
414
- const mainClass = searchFieldForwarding.value
415
- ? "extended__multiselect-input"
416
- : "extended__multiselect-input--hidden";
417
- const searchFieldClasses = [];
418
-
419
- if (optionWillBeTriggered.value) {
420
- searchFieldClasses.push("extended__multiselect-input--trigger-option");
421
- }
422
-
423
- searchFieldClasses.push(mainClass);
424
-
425
- return searchFieldClasses;
426
- });
427
-
428
- /**
429
- * Determines whether to show search field
430
- * over element with selected option
431
- * @function
432
- * @returns {boolean} display
433
- */
434
- const searchFieldForwarding = computed(() => {
435
- return searchFieldFocused.value || optionWillBeTriggered.value;
436
- });
437
-
438
- /**
439
- * Sets pattern of internal search for available options from search field to store
440
- * @function
441
- * @param {string} pattern - pattern of internal search for available options
442
- */
443
- watch(searchState, (prevState, state) => {
444
- if (state.searchPattern === prevState.searchPattern) return;
445
-
446
- if (searchFilterActive.value) {
447
- setSearchPattern(state.pattern);
448
- }
449
- }, { deep: true });
450
-
451
- /**
452
- * Sets value of search field from search field to store
453
- * @function
454
- * @param {string} value - value of search field
455
- */
456
- watch(searchValue, (value) => {
457
- if (searchFilterActive.value) {
458
- setSearchValue(value);
459
- }
460
- });
461
-
462
- /**
463
- * Expands options list
464
- * @function
465
- * @emits extended:expand-options
466
- */
467
- const expand = () => {
468
- if (disabled.value) return;
469
-
470
- searchFieldFocused.value = true;
471
- emitter.emit("extended:expand-options");
472
- };
473
-
474
- /**
475
- * Describes behavour of input blur event
476
- * when "multiple" prop equals true
477
- * @function
478
- */
479
- const multipleBlur = () => {
480
- if (!multiple.value) {
481
- searchFieldFocused.value = false;
482
- return;
483
- }
484
-
485
- if (!blurSkipByBlock.value) {
486
- searchFieldFocused.value = false;
487
- }
488
- };
489
-
490
- /**
491
- * Rolls up options list if it needed
492
- * @function
493
- * @emits extended:rollup-options
494
- */
495
- const rollUp = () => {
496
- if (disabled.value) return;
497
-
498
- multipleBlur();
499
-
500
- if (!togglingSavesSearchValue.value) {
501
- searchValue.value = "";
502
- setSearchValue(null);
503
- setSearchPattern(null);
504
- }
505
-
506
- if (blurSkipByToggleIcon.value > 0) {
507
- blurSkipByToggleIcon.value = 0;
508
- return;
509
- }
510
-
511
- if (blurSkipByBlock.value > 0) {
512
- blurSkipByBlock.value = 0;
513
- return;
514
- }
515
-
516
- blurSkip.value = true;
517
- blurSkipByBlock.value = 0;
518
- blurSkipByToggleIcon.value = 0;
519
-
520
- emitter.emit("extended:rollup-options", true);
521
- };
522
-
523
- /**
524
- * Activates debounced version of options list filter
525
- * @function
526
- */
527
- const search = () => {
528
- searchDebounce.value();
529
- };
530
-
531
- /**
532
- * "onBeforeMount" hook of the ExtendedMultiselectInput component
533
- * @listens extended:field-focus
534
- * @listens extended:rollup-options
535
- * @listens extended:select-option
536
- * @listens extended:deselect-option
537
- * @listens extended:clean-options
538
- * @listens extended:clear-field
539
- * @listens extended:skip-blur
540
- * @listens extended:skip-block-blur
541
- * @listens extended:skip-block-blur-zeroing
542
- * @listens extended:trigger-selection
543
- */
544
- onBeforeMount(() => {
545
- emitter.on("extended:rollup-options", (internalRollup) => {
546
- if (blurSkip.value === true) {
547
- blurSkip.value = false;
548
- return;
549
- }
550
-
551
- if (internalRollup) {
552
- return;
553
- }
554
-
555
- rollUp();
556
- });
557
-
558
- emitter.on("extended:select-option", (option) => {
559
- if (toggleOptionsBySelect.value) {
560
- rollUp();
561
- }
562
-
563
- if (multiple.value) {
564
- return;
565
- } else {
566
- singleLabel.value = option.label;
567
- }
568
- });
569
-
570
- emitter.on("extended:deselect-option", () => {
571
- if (multiple.value) {
572
- return;
573
- } else {
574
- singleLabel.value = "";
575
- }
576
- });
577
-
578
- emitter.on("extended:clean-options", () => {
579
- searchValue.value = null;
580
- });
581
-
582
- emitter.on("extended:clear-field", () => {
583
- searchValue.value = null;
584
- });
585
-
586
- emitter.on("extended:skip-blur", () => {
587
- blurSkipByToggleIcon.value++;
588
- });
589
-
590
- emitter.on("extended:skip-block-blur", () => {
591
- blurSkipByBlock.value++;
592
- });
593
-
594
- emitter.on("extended:skip-block-blur-zeroing", () => {
595
- blurSkipByBlock.value = 0;
596
- });
597
-
598
- emitter.on("extended:trigger-selection", (triggerState) => {
599
- optionWillBeTriggered.value = triggerState;
600
- });
601
- });
602
-
603
- /**
604
- * "onMounted" hook of the ExtendedMultiselectInput component
605
- * @listens extended:field-focus
606
- */
607
- onMounted(() => {
608
- emitter.on("extended:field-focus", () => {
609
- if (showSearchField.value) {
610
- nextTick(() => {
611
- extendedInput.value.focus();
612
- });
613
- } else {
614
- extendedInput.value.focus();
615
- }
616
- });
617
- });
1
+ <template>
2
+ <div v-if="showSearchField">
3
+ <input
4
+ aria-controls="extended-search-field"
5
+ ref="extendedInput"
6
+ type="text"
7
+ :accesskey="attrs.accesskey"
8
+ :autocomplete="attrs.autocomplete"
9
+ :class="searchFieldClass"
10
+ :disabled="disabled"
11
+ :id="inputId"
12
+ :name="attrs.name"
13
+ :placeholder="appropriatePlaceholder"
14
+ :spellcheck="attrs.spellcheck"
15
+ :tabindex="attrs.tabindex"
16
+ :translate="attrs.translate"
17
+ v-model="searchValue"
18
+ @blur.prevent="rollUp"
19
+ @focus.prevent="expand"
20
+ @input="search"
21
+ />
22
+ <div
23
+ class="extended__multiselect-block"
24
+ v-show="!searchFieldForwarding && !multiple"
25
+ @click.stop="expand"
26
+ >
27
+ <slot
28
+ name="labelBlock"
29
+ :label-block-value="singleLabel"
30
+ >
31
+ <span>{{ singleLabel }}</span>
32
+ </slot>
33
+ </div>
34
+ <extended-multiselect-multiple
35
+ v-show="multiple && !!selectedOptions.length"
36
+ :style="multipleBlocksMargin"
37
+ :disabled="disabled"
38
+ :loading="loading"
39
+ :toggle-multiple-blocks-limit="toggleMultipleBlocksLimit"
40
+ :empty-objects-placeholder="emptyObjectsPlaceholder"
41
+ :multiple-blocks-limit-message="multipleBlocksLimitMessage"
42
+ :theme-type="themeType"
43
+ :increase-display-by="increaseDisplayBy"
44
+ :multipleBlocksLimit="multipleBlocksLimit"
45
+ :selected-options="selectedOptions"
46
+ :create-custom-option-label="createCustomOptionLabel"
47
+ >
48
+ <template #optionBlock="{ label, deselectBlock, index }">
49
+ <slot
50
+ name="optionBlock"
51
+ :label="label"
52
+ :index="index"
53
+ :deselect-block="deselectBlock"
54
+ >
55
+ </slot>
56
+ </template>
57
+ <template #maxElements>
58
+ <slot name="maxElements"></slot>
59
+ </template>
60
+ <template
61
+ v-if="toggleMultipleBlocksLimit"
62
+ #showMore="{ showMoreOptions }"
63
+ >
64
+ <slot
65
+ name="showMore"
66
+ :show-more-options="showMoreOptions"
67
+ >
68
+ </slot>
69
+ </template>
70
+ </extended-multiselect-multiple>
71
+ </div>
72
+ <div v-else-if="!showSearchField && multiple">
73
+ <extended-multiselect-multiple
74
+ :disabled="disabled"
75
+ :loading="loading"
76
+ :toggle-multiple-blocks-limit="toggleMultipleBlocksLimit"
77
+ :empty-objects-placeholder="emptyObjectsPlaceholder"
78
+ :multiple-blocks-limit-message="multipleBlocksLimitMessage"
79
+ :theme-type="themeType"
80
+ :increase-display-by="increaseDisplayBy"
81
+ :multipleBlocksLimit="multipleBlocksLimit"
82
+ :selected-options="selectedOptions"
83
+ :create-custom-option-label="createCustomOptionLabel"
84
+ >
85
+ <template #optionBlock="{ label, deselectBlock, index }">
86
+ <slot
87
+ name="optionBlock"
88
+ :label="label"
89
+ :index="index"
90
+ :deselect-block="deselectBlock"
91
+ >
92
+ </slot>
93
+ </template>
94
+ <template
95
+ v-if="toggleMultipleBlocksLimit"
96
+ #showMore="{ showMoreOptions }"
97
+ >
98
+ <slot
99
+ name="showMore"
100
+ :show-more-options="showMoreOptions"
101
+ ></slot>
102
+ </template>
103
+ </extended-multiselect-multiple>
104
+ <input
105
+ class="extended__multiselect-input--hidden"
106
+ ref="extendedInput"
107
+ type="text"
108
+ @blur.prevent="rollUp"
109
+ @focus.prevent="expand"
110
+ />
111
+ </div>
112
+ <div
113
+ class="extended__multiselect-block"
114
+ v-else
115
+ >
116
+ <slot
117
+ name="labelBlock"
118
+ :label-block-value="singleLabel"
119
+ >
120
+ <span>{{ singleLabel }}</span>
121
+ </slot>
122
+ <input
123
+ class="extended__multiselect-input--hidden"
124
+ ref="extendedInput"
125
+ type="text"
126
+ @blur.prevent="rollUp"
127
+ @focus.prevent="expand"
128
+ />
129
+ </div>
130
+ </template>
131
+
132
+ <script setup>
133
+ import {
134
+ computed,
135
+ defineProps,
136
+ inject,
137
+ nextTick,
138
+ onBeforeMount,
139
+ onMounted,
140
+ ref,
141
+ toRefs,
142
+ useAttrs,
143
+ watch,
144
+ } from "vue";
145
+
146
+ import { debounce } from "lodash-es";
147
+ import emitter from "../events/emitter";
148
+
149
+ import ExtendedMultiselectMultiple from "./ExtendedMultiselectMultiple.vue";
150
+
151
+ const props = defineProps({
152
+ /**
153
+ * Determines which placeholder to use
154
+ * depending on the options creation mode
155
+ * @property {boolean} createOnTheGo
156
+ */
157
+ createOnTheGo: {
158
+ type: Boolean,
159
+ required: true,
160
+ },
161
+
162
+ /**
163
+ * Blocks search field
164
+ * @property {boolean} disabled
165
+ */
166
+ disabled: {
167
+ type: Boolean,
168
+ required: true,
169
+ },
170
+
171
+ /**
172
+ * Determines whether option list expanded or not
173
+ * @property {boolean} dropdownActive
174
+ */
175
+ dropdownActive: {
176
+ type: Boolean,
177
+ required: true,
178
+ },
179
+
180
+ /**
181
+ * Provides "loading" state of component
182
+ * to ExtendedMultiselectMultiple child component
183
+ * @property {boolean} loading
184
+ */
185
+ loading: {
186
+ type: Boolean,
187
+ required: true,
188
+ },
189
+
190
+ /**
191
+ * Determines whether to use ExtendedMultiselectMultiple
192
+ * child component and some other related functionality
193
+ * @property {boolean} multiple
194
+ */
195
+ multiple: {
196
+ type: Boolean,
197
+ required: true,
198
+ },
199
+
200
+ /**
201
+ * Renews the pattern of internal search for available options
202
+ * @property {boolean} searchFilterActive
203
+ */
204
+ searchFilterActive: {
205
+ type: Boolean,
206
+ required: true,
207
+ },
208
+
209
+ /**
210
+ * Determines whether to show search field
211
+ * @property {boolean} showSearchField
212
+ */
213
+ showSearchField: {
214
+ type: Boolean,
215
+ required: true,
216
+ },
217
+
218
+ /**
219
+ * Determines whether to allow user to increase limit of shown
220
+ * elements with selected options by special icon
221
+ * @property {boolean} toggleMultipleBlocksLimit
222
+ */
223
+ toggleMultipleBlocksLimit: {
224
+ type: Boolean,
225
+ required: true,
226
+ },
227
+
228
+ /**
229
+ * Determines whether to roll up options list
230
+ * by option selection
231
+ * @property {boolean} toggleOptionsBySelect
232
+ */
233
+ toggleOptionsBySelect: {
234
+ type: Boolean,
235
+ required: true,
236
+ },
237
+
238
+ /**
239
+ * Determines whether to clear value of search field
240
+ * after next switch of search field display
241
+ * @property {boolean} togglingSavesSearchValue
242
+ */
243
+ togglingSavesSearchValue: {
244
+ type: Boolean,
245
+ required: true,
246
+ },
247
+
248
+ /**
249
+ * Placeholder which will be shown
250
+ * if "createOnTheGo" prop equals true
251
+ * @property {string} createOptionPlaceholder
252
+ */
253
+ createOptionPlaceholder: {
254
+ type: String,
255
+ required: true,
256
+ },
257
+
258
+ /**
259
+ * Placeholder used in internal options filters
260
+ * and provided to ExtendedMultiselectMultiple child component
261
+ * @property {string} emptyObjectsPlaceholder
262
+ */
263
+ emptyObjectsPlaceholder: {
264
+ type: String,
265
+ required: true,
266
+ },
267
+
268
+ /**
269
+ * "placeholder" attribute for search field
270
+ * @property {string} placeholder
271
+ */
272
+ placeholder: {
273
+ type: String,
274
+ required: true,
275
+ },
276
+
277
+ /**
278
+ * Provides current theme of extended multiselect
279
+ * to ExtendedMultiselectMultiple child component
280
+ * @property {string} themeType
281
+ */
282
+ themeType: {
283
+ type: String,
284
+ required: true,
285
+ },
286
+
287
+ /**
288
+ * Provides limit of hidden options amount to be shown next
289
+ * @property {number} increaseDisplayBy
290
+ */
291
+ increaseDisplayBy: {
292
+ type: Number,
293
+ required: true,
294
+ },
295
+
296
+ /**
297
+ * Provides limit of option blocks amount
298
+ * to ExtendedMultiselectMultiple child component
299
+ * @default null
300
+ * @property {number} multipleBlocksLimit
301
+ */
302
+ multipleBlocksLimit: {
303
+ type: Number,
304
+ default: null,
305
+ },
306
+
307
+ /**
308
+ * Provides list of selected options
309
+ * to ExtendedMultiselectMultiple child component
310
+ * @property {Array} selectedOptions
311
+ */
312
+ selectedOptions: {
313
+ type: Array,
314
+ required: true,
315
+ },
316
+
317
+ /**
318
+ * Provides function that creates custom label for
319
+ * block with selected option to ExtendedMultiselectMultiple
320
+ * child component
321
+ * @property {Function} createCustomOptionLabel
322
+ */
323
+ createCustomOptionLabel: {
324
+ type: Function,
325
+ required: true,
326
+ },
327
+
328
+ /**
329
+ * Provides function that creates limit message for maximal of selected
330
+ * options to ExtendedMultiselectMultiple child component
331
+ * @property {Function} multipleBlocksLimitMessage
332
+ */
333
+ multipleBlocksLimitMessage: {
334
+ type: Function,
335
+ required: true,
336
+ },
337
+
338
+ /**
339
+ * "id" attribute of search field
340
+ * @default null
341
+ * @property {string|number} inputId
342
+ */
343
+ inputId: {
344
+ type: [String, Number],
345
+ default: null,
346
+ },
347
+ });
348
+
349
+ const searchState = inject("searchState");
350
+ const setSearchValue = inject("setSearchValue");
351
+ const setSearchPattern = inject("setSearchPattern");
352
+
353
+ const blurSkip = ref(false);
354
+ const reversePrevented = ref(false);
355
+ const searchFieldFocused = ref(false);
356
+ const optionWillBeTriggered = ref(false);
357
+ const searchValue = ref("");
358
+ const singleLabel = ref("");
359
+ const blurSkipByToggleIcon = ref(0);
360
+ const blurSkipByBlock = ref(0);
361
+ const labelBlocks = ref([]);
362
+ const searchDebounce = ref(debounce(() => {
363
+ const searchPattern = searchValue.value ? new RegExp(`${searchValue.value}`, "i") : null;
364
+ emitter.emit("extended:search-pattern-changed", searchValue.value);
365
+
366
+ setSearchPattern(searchPattern);
367
+ }, 250));
368
+
369
+ /**
370
+ * Element references
371
+ */
372
+ const extendedInput = ref(null);
373
+
374
+ const {
375
+ createOnTheGo,
376
+ createOptionPlaceholder,
377
+ disabled,
378
+ increaseDisplayBy,
379
+ multiple,
380
+ placeholder,
381
+ searchFilterActive,
382
+ showSearchField,
383
+ toggleOptionsBySelect,
384
+ togglingSavesSearchValue,
385
+ } = toRefs(props);
386
+
387
+ const attrs = useAttrs();
388
+
389
+ /**
390
+ * Defines "placeholder" attribute of search field
391
+ * @function
392
+ * @returns {string} placeholder
393
+ */
394
+ const appropriatePlaceholder = computed(() => {
395
+ return createOnTheGo.value ? createOptionPlaceholder.value : placeholder.value;
396
+ });
397
+
398
+ /**
399
+ * Sets "margin-top" css property to multiple options block
400
+ * if search field is shown
401
+ * @function
402
+ * @returns {Object|null} margin-top
403
+ */
404
+ const multipleBlocksMargin = computed(() => {
405
+ return searchFieldForwarding.value ? { marginTop: "4px" } : null;
406
+ });
407
+
408
+ /**
409
+ * Defines classes of search field
410
+ * @function
411
+ * @returns {Array} classes
412
+ */
413
+ const searchFieldClass = computed(() => {
414
+ const mainClass = searchFieldForwarding.value
415
+ ? "extended__multiselect-input"
416
+ : "extended__multiselect-input--hidden";
417
+ const searchFieldClasses = [];
418
+
419
+ if (optionWillBeTriggered.value) {
420
+ searchFieldClasses.push("extended__multiselect-input--trigger-option");
421
+ }
422
+
423
+ searchFieldClasses.push(mainClass);
424
+
425
+ return searchFieldClasses;
426
+ });
427
+
428
+ /**
429
+ * Determines whether to show search field
430
+ * over element with selected option
431
+ * @function
432
+ * @returns {boolean} display
433
+ */
434
+ const searchFieldForwarding = computed(() => {
435
+ return searchFieldFocused.value || optionWillBeTriggered.value;
436
+ });
437
+
438
+ /**
439
+ * Sets pattern of internal search for available options from search field to store
440
+ * @function
441
+ * @param {string} pattern - pattern of internal search for available options
442
+ */
443
+ watch(searchState, (prevState, state) => {
444
+ if (state.searchPattern === prevState.searchPattern) return;
445
+
446
+ if (searchFilterActive.value) {
447
+ setSearchPattern(state.pattern);
448
+ }
449
+ }, { deep: true });
450
+
451
+ /**
452
+ * Sets value of search field from search field to store
453
+ * @function
454
+ * @param {string} value - value of search field
455
+ */
456
+ watch(searchValue, (value) => {
457
+ if (searchFilterActive.value) {
458
+ setSearchValue(value);
459
+ }
460
+ });
461
+
462
+ /**
463
+ * Expands options list
464
+ * @function
465
+ * @emits extended:expand-options
466
+ */
467
+ const expand = () => {
468
+ if (disabled.value) return;
469
+
470
+ searchFieldFocused.value = true;
471
+ emitter.emit("extended:expand-options");
472
+ };
473
+
474
+ /**
475
+ * Describes behavour of input blur event
476
+ * when "multiple" prop equals true
477
+ * @function
478
+ */
479
+ const multipleBlur = () => {
480
+ if (!multiple.value) {
481
+ searchFieldFocused.value = false;
482
+ return;
483
+ }
484
+
485
+ if (!blurSkipByBlock.value) {
486
+ searchFieldFocused.value = false;
487
+ }
488
+ };
489
+
490
+ /**
491
+ * Rolls up options list if it needed
492
+ * @function
493
+ * @emits extended:rollup-options
494
+ */
495
+ const rollUp = () => {
496
+ if (disabled.value) return;
497
+
498
+ multipleBlur();
499
+
500
+ if (!togglingSavesSearchValue.value) {
501
+ searchValue.value = "";
502
+ setSearchValue(null);
503
+ setSearchPattern(null);
504
+ }
505
+
506
+ if (blurSkipByToggleIcon.value > 0) {
507
+ blurSkipByToggleIcon.value = 0;
508
+ return;
509
+ }
510
+
511
+ if (blurSkipByBlock.value > 0) {
512
+ blurSkipByBlock.value = 0;
513
+ return;
514
+ }
515
+
516
+ blurSkip.value = true;
517
+ blurSkipByBlock.value = 0;
518
+ blurSkipByToggleIcon.value = 0;
519
+
520
+ emitter.emit("extended:rollup-options", true);
521
+ };
522
+
523
+ /**
524
+ * Activates debounced version of options list filter
525
+ * @function
526
+ */
527
+ const search = () => {
528
+ searchDebounce.value();
529
+ };
530
+
531
+ /**
532
+ * "onBeforeMount" hook of the ExtendedMultiselectInput component
533
+ * @listens extended:field-focus
534
+ * @listens extended:rollup-options
535
+ * @listens extended:select-option
536
+ * @listens extended:deselect-option
537
+ * @listens extended:clean-options
538
+ * @listens extended:clear-field
539
+ * @listens extended:skip-blur
540
+ * @listens extended:skip-block-blur
541
+ * @listens extended:skip-block-blur-zeroing
542
+ * @listens extended:trigger-selection
543
+ */
544
+ onBeforeMount(() => {
545
+ emitter.on("extended:rollup-options", (internalRollup) => {
546
+ if (blurSkip.value === true) {
547
+ blurSkip.value = false;
548
+ return;
549
+ }
550
+
551
+ if (internalRollup) {
552
+ return;
553
+ }
554
+
555
+ rollUp();
556
+ });
557
+
558
+ emitter.on("extended:select-option", (option) => {
559
+ if (toggleOptionsBySelect.value) {
560
+ rollUp();
561
+ }
562
+
563
+ if (multiple.value) {
564
+ return;
565
+ } else {
566
+ singleLabel.value = option.label;
567
+ }
568
+ });
569
+
570
+ emitter.on("extended:deselect-option", () => {
571
+ if (multiple.value) {
572
+ return;
573
+ } else {
574
+ singleLabel.value = "";
575
+ }
576
+ });
577
+
578
+ emitter.on("extended:clean-options", () => {
579
+ searchValue.value = null;
580
+ });
581
+
582
+ emitter.on("extended:clear-field", () => {
583
+ searchValue.value = null;
584
+ });
585
+
586
+ emitter.on("extended:skip-blur", () => {
587
+ blurSkipByToggleIcon.value++;
588
+ });
589
+
590
+ emitter.on("extended:skip-block-blur", () => {
591
+ blurSkipByBlock.value++;
592
+ });
593
+
594
+ emitter.on("extended:skip-block-blur-zeroing", () => {
595
+ blurSkipByBlock.value = 0;
596
+ });
597
+
598
+ emitter.on("extended:trigger-selection", (triggerState) => {
599
+ optionWillBeTriggered.value = triggerState;
600
+ });
601
+ });
602
+
603
+ /**
604
+ * "onMounted" hook of the ExtendedMultiselectInput component
605
+ * @listens extended:field-focus
606
+ */
607
+ onMounted(() => {
608
+ emitter.on("extended:field-focus", () => {
609
+ if (showSearchField.value) {
610
+ nextTick(() => {
611
+ extendedInput.value.focus();
612
+ });
613
+ } else {
614
+ extendedInput.value.focus();
615
+ }
616
+ });
617
+ });
618
618
  </script>