vue3-extended-multiselect 0.1.2 → 0.1.3

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