vue3-extended-multiselect 1.2.4 → 1.4.1

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