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,1145 +1,1145 @@
1
- <template>
2
- <div
3
- ref="optionsWrapper"
4
- tabindex="-1"
5
- :class="classes"
6
- :style="styles"
7
- >
8
- <slot name="listHeader"></slot>
9
- <div
10
- v-if="showCreateNewOptionBlock"
11
- class="extended__multiselect-option-create"
12
- :class="optionHighlightClasses(null)"
13
- @mousedown="createNewOption"
14
- >
15
- {{ searchState.searchValue }}
16
- </div>
17
- <slot
18
- name="moreThanLimit"
19
- v-if="maxOptionsWereSelected"
20
- >
21
- <span>Maximum limit of selected options was achieved</span>
22
- </slot>
23
- <slot
24
- name="lessThanLimit"
25
- v-if="minOptionsWereNotSelected"
26
- >
27
- <span>Minimum amount of selected options was not achieved</span>
28
- </slot>
29
- <div class="extended__multiselect-options_container">
30
- <div
31
- role="listbox"
32
- v-for="(option, index) in availableOptions"
33
- :key="index"
34
- >
35
- <div
36
- v-if="option"
37
- :aria-setsize="availableOptions.length"
38
- :aria-posinset="index"
39
- :aria-labelledby="`option-label-${index}`"
40
- :aria-disabled="optionIsDisabled(option)"
41
- :class="optionHighlightClasses(option)"
42
- :style="optionHeightByProps"
43
- :enter-locator="index"
44
- :role="optionCreateRole(option)"
45
- @click.stop="selectOption(option, $event)"
46
- @keypress.stop="selectOption(option, $event)"
47
- @mousedown="triggerOptionBeforeSelection"
48
- @mouseenter.stop="setEnterIndex"
49
- >
50
- <slot
51
- name="option"
52
- :option="option"
53
- :create-custom-option-label="createCustomOptionLabel"
54
- >
55
- <div class="extended__multiselect-marker">
56
- <slot
57
- name="marker"
58
- :option="option"
59
- >
60
- <div
61
- v-if="showMarker && showCurrentMarker(option)"
62
- :class="markerShapeClass(option)"
63
- :style="markerShapeMargin"
64
- >
65
- </div>
66
- <div
67
- v-else-if="showMarker"
68
- class="extended__multiselect-marker-shape-only"
69
- >
70
- </div>
71
- </slot>
72
- </div>
73
- <span :id="`option-label-${index}`">
74
- {{ optionCreateLabel(option) }}
75
- </span>
76
- </slot>
77
- </div>
78
- </div>
79
- </div>
80
- <slot
81
- name="noResults"
82
- v-if="emptySearchResult"
83
- >
84
- <span>No results were found by search</span>
85
- </slot>
86
- <slot
87
- name="noOptions"
88
- v-if="emptyOptionsList"
89
- >
90
- <span>Options list is empty</span>
91
- </slot>
92
- <slot name="listFooter"></slot>
93
- </div>
94
- </template>
95
-
96
- <script setup>
97
- import {
98
- computed,
99
- defineProps,
100
- inject,
101
- onBeforeMount,
102
- onMounted,
103
- ref,
104
- toRefs,
105
- } from "vue";
106
-
107
- import emitter from '../events/emitter';
108
-
109
- import usePreselectedOptions from "../composition/preselected-options";
110
- import useLabels from "../composition/labels";
111
-
112
- import { UnionPropType } from "../sets/sets";
113
-
114
- const props = defineProps({
115
- /**
116
- * Determines whether to immediately select just now created option
117
- * @property {boolean} autoSelectCreatedOption
118
- */
119
- autoSelectCreatedOption: {
120
- type: Boolean,
121
- required: true,
122
- },
123
-
124
- /**
125
- * Determines whether to show noResults slot
126
- * and block with new option's text
127
- * @property {boolean} createOnTheGo
128
- */
129
- createOnTheGo: {
130
- type: Boolean,
131
- required: true,
132
- },
133
-
134
- /**
135
- * Determines whether to emit clear event
136
- * by option selection
137
- * @property {boolean} clearBySelectWhenMultiple
138
- */
139
- clearBySelectWhenMultiple: {
140
- type: Boolean,
141
- required: true,
142
- },
143
-
144
- /**
145
- * Determines whether to define thematic class for
146
- * every option
147
- * @property {boolean} highlightOptions
148
- */
149
- highlightOptions: {
150
- type: Boolean,
151
- required: true,
152
- },
153
-
154
- /**
155
- * Determines whether to use functionality of
156
- * multiple select
157
- * @property {boolean} multiple
158
- */
159
- multiple: {
160
- type: Boolean,
161
- required: true,
162
- },
163
-
164
- /**
165
- * Determines whether to show selected options
166
- * in options list
167
- * @property {boolean} selectedOptionsShown
168
- */
169
- selectedOptionsShown: {
170
- type: Boolean,
171
- required: true,
172
- },
173
-
174
- /**
175
- * Determines whether to log warnings about incorrect props
176
- * @property {boolean} showInsertWarnings
177
- */
178
- showInsertWarnings: {
179
- type: Boolean,
180
- required: true,
181
- },
182
-
183
- /**
184
- * Determines whether to show marker slot
185
- * @property {boolean} showMarker
186
- */
187
- showMarker: {
188
- type: Boolean,
189
- required: true,
190
- },
191
-
192
- /**
193
- * Defines class of options list placement after expanse
194
- * @property {string} chosenToggleAppearanceSide
195
- */
196
- chosenToggleAppearanceSide: {
197
- type: String,
198
- required: true,
199
- },
200
-
201
- /**
202
- * Determines type of option that will be created
203
- * @property {string} createOptionType
204
- */
205
- createOptionType: {
206
- type: String,
207
- required: true,
208
- },
209
-
210
- /**
211
- * Determines a field in options of type "object"
212
- * which value will be used to disable such options.
213
- * @property {string} disableByField
214
- */
215
- disableByField: {
216
- type: String,
217
- required: true,
218
- },
219
-
220
- /**
221
- * Assigns unique identifier of extended multiselect
222
- * for each event
223
- * @property {string} emitterUniqueId
224
- */
225
- emitterUniqueId: {
226
- type: String,
227
- required: true,
228
- },
229
-
230
- /**
231
- * Sets labels for options of type "object"
232
- * or Array instances which length/keys length equals 0
233
- * @property {string} emptyObjectsPlaceholder
234
- */
235
- emptyObjectsPlaceholder: {
236
- type: String,
237
- required: true,
238
- },
239
-
240
- /**
241
- * Determines field of option which will be displayed as label
242
- * @property {string} label
243
- */
244
- label: {
245
- type: String,
246
- required: true,
247
- },
248
-
249
- /**
250
- * Determines field in options of type "object"
251
- * to use in internal search.
252
- * @property {string} searchByField
253
- */
254
- searchByField: {
255
- type: String,
256
- required: true,
257
- },
258
-
259
- /**
260
- * Current theme of extended-multiselect
261
- * used in class definition
262
- * @property {string} themeType
263
- */
264
- themeType: {
265
- type: String,
266
- required: true,
267
- },
268
-
269
- /**
270
- * Defines "height" css-property of every option element
271
- * @property {number} anyOptionWrapperBlockHeight
272
- */
273
- anyOptionWrapperBlockHeight: {
274
- type: Number,
275
- required: true,
276
- },
277
-
278
- /**
279
- * Determines maximal limit of selected options
280
- * @property {number|null} maxOptionsCount
281
- */
282
- maxOptionsCount: {
283
- type: Number,
284
- default: null,
285
- },
286
-
287
- /**
288
- * Determines minimal limit of selected options
289
- * @property {number|null} minOptionsCount
290
- */
291
- minOptionsCount: {
292
- type: Number,
293
- default: null,
294
- },
295
-
296
- /**
297
- * Defines value of "max-height" css-property of options list
298
- * @property {number} toggleMaxHeight
299
- */
300
- toggleMaxHeight: {
301
- type: Number,
302
- required: true,
303
- },
304
-
305
- /**
306
- * Defines value of "min-height" css-property of options list
307
- * @property {number} toggleMinHeight
308
- */
309
- toggleMinHeight: {
310
- type: Number,
311
- default: null,
312
- },
313
-
314
- /**
315
- * List of fields for new options of type "object" created by user
316
- * if "createOnTheGo" prop equals true
317
- * Each field will have basic value of "searchValue" prop
318
- * @property {Array} createOptionFields
319
- */
320
- createOptionFields: {
321
- type: Array,
322
- required: true,
323
- },
324
-
325
- /**
326
- * Searches for options with listed primitive values
327
- * and disables it
328
- * @property {Array} disabledPrimitiveOptionsConverted
329
- */
330
- disabledPrimitiveOptionsConverted: {
331
- type: Array,
332
- required: true,
333
- },
334
-
335
- /**
336
- * Unfiltered options list
337
- * @property {Array} options
338
- */
339
- options: {
340
- type: Array,
341
- required: true,
342
- },
343
-
344
- /**
345
- * Defines value of "padding" css-property of every option
346
- * @property {Array} optionsPadding
347
- */
348
- optionsPadding: {
349
- type: Array,
350
- required: true,
351
- },
352
-
353
- /**
354
- * List of options which should be selected by default
355
- * if "multiple" prop equals true
356
- * @property {Array} preselectedOptions
357
- */
358
- preselectedOptions: {
359
- type: Array,
360
- required: true,
361
- },
362
-
363
- /**
364
- * List of options filtered by maximal available options amount
365
- * @property {Array} restrictedOptions
366
- */
367
- restrictedOptions: {
368
- type: Array,
369
- required: true,
370
- },
371
-
372
- /**
373
- * List of selected options or list with single selected option
374
- * if "multiple" prop equals false
375
- * @property {Array} selectedOptions
376
- */
377
- selectedOptions: {
378
- type: Array,
379
- required: true,
380
- },
381
-
382
- /**
383
- * List of key names that in combination with "enter" key
384
- * should cancel option selection
385
- * @property {Array} specialKeysBlock
386
- */
387
- specialKeysBlock: {
388
- type: Array,
389
- required: true,
390
- },
391
-
392
- /**
393
- * Creates custom label for every option in options list
394
- * @property {Function} createCustomOptionLabel
395
- */
396
- createCustomOptionLabel: {
397
- type: Function,
398
- required: true,
399
- },
400
-
401
- /**
402
- * Option which should be selected by default
403
- * if "multiple" prop equals false
404
- * @property {UnionPropType|null} preselectedOption
405
- */
406
- preselectedOption: {
407
- type: UnionPropType,
408
- default: null,
409
- },
410
- });
411
-
412
- const searchState = inject("searchState");
413
- const setSearchValue = inject("setSearchValue");
414
- const setSearchPattern = inject("setSearchPattern");
415
-
416
- const fieldWasShown = ref(false);
417
- const atopWithScroll = ref(null);
418
- const enterIndex = ref(null);
419
- const heightFromMounted = ref(null);
420
- const underWithScroll = ref(null);
421
-
422
- /**
423
- * Element references
424
- */
425
- const optionsWrapper = ref(null);
426
-
427
- const {
428
- anyOptionWrapperBlockHeight,
429
- autoSelectCreatedOption,
430
- chosenToggleAppearanceSide,
431
- clearBySelectWhenMultiple,
432
- createOnTheGo,
433
- createCustomOptionLabel,
434
- createOptionFields,
435
- createOptionType,
436
- disableByField,
437
- disabledPrimitiveOptionsConverted,
438
- emitterUniqueId,
439
- emptyObjectsPlaceholder,
440
- highlightOptions,
441
- label,
442
- searchByField,
443
- maxOptionsCount,
444
- minOptionsCount,
445
- multiple,
446
- options,
447
- optionsPadding,
448
- restrictedOptions,
449
- selectedOptions,
450
- selectedOptionsShown,
451
- showInsertWarnings,
452
- showMarker,
453
- specialKeysBlock,
454
- themeType,
455
- toggleMaxHeight,
456
- toggleMinHeight,
457
- } = toRefs(props);
458
-
459
- const { createLabel, optionTypeRestrictor } = usePreselectedOptions(
460
- label,
461
- emptyObjectsPlaceholder,
462
- showInsertWarnings,
463
- );
464
- const { optionCreateLabel } = useLabels(
465
- label,
466
- createCustomOptionLabel,
467
- emptyObjectsPlaceholder,
468
- );
469
-
470
- /**
471
- * List of options filtered by "disabled" and "selected" signs
472
- * also by equality to null and undefined primitive values
473
- * or instance declarations
474
- * @function
475
- * @returns {Array} options
476
- */
477
- const availableOptions = computed(() => {
478
- let filteredOptions = restrictedOptions.value.filter((option) => {
479
- const availableOptionType = optionTypeRestrictor(option);
480
-
481
- if (!option || !availableOptionType) return false;
482
-
483
- if (!lookForSimpleOptions(option)) return false;
484
-
485
- if (selectedOptionsShown.value) return true;
486
-
487
- return lookForObjectOptions(option);
488
- });
489
-
490
- let finalOptionsList = filteredOptions;
491
-
492
- if (searchState.searchPattern) {
493
- finalOptionsList = filterBySearchPattern(filteredOptions);
494
- }
495
-
496
- return finalOptionsList;
497
- });
498
-
499
- /**
500
- * Defines a list of options list classes
501
- * @function
502
- * @returns {Array} classes
503
- */
504
- const classes = computed(() => {
505
- let theme = [];
506
-
507
- switch(themeType.value) {
508
- case "basic":
509
- theme.push("extended__multiselect-options");
510
- break;
511
- case "slate-grey":
512
- theme.push("extended__multiselect-options-slate-grey");
513
- break;
514
- case "slate-blue":
515
- theme.push("extended__multiselect-options-slate-blue");
516
- break;
517
- case "teal":
518
- theme.push("extended__multiselect-options-teal");
519
- break;
520
- case "strict":
521
- theme.push("extended__multiselect-options-strict");
522
- break;
523
- default:
524
- theme.push("extended__multiselect-options");
525
- }
526
-
527
- if (showMarker.value) {
528
- theme.push("extended__multiselect-options--marker");
529
- }
530
-
531
- if (chosenToggleAppearanceSide.value === "atop") {
532
- theme.push("extended__multiselect-options--atop");
533
- }
534
-
535
- return theme;
536
- });
537
-
538
- /**
539
- * Determines whether given options list is empty
540
- * @function
541
- * @returns {boolean} emptiness
542
- */
543
- const emptyOptionsList = computed(() => {
544
- return (!options.value.length || !options.value.filter((option) => !!option).length
545
- || !availableOptions.value.length) && !(createOnTheGo.value && searchState.searchValue);
546
- });
547
-
548
- /**
549
- * Determines whether list of internal search results is empty
550
- * @function
551
- * @returns {boolean} emptiness
552
- */
553
- const emptySearchResult = computed(() => {
554
- return !availableOptions.value.length && !createOnTheGo.value && searchState.searchValue;
555
- });
556
-
557
- /**
558
- * Defines styles for "min-heigt", "max-height"
559
- * and "top" css-properties of options wrapper
560
- * @function
561
- * @returns {Object} styles
562
- */
563
- const heightFromProps = computed(() => {
564
- let heightRecord = {};
565
-
566
- if (chosenToggleAppearanceSide.value === "atop") {
567
- Object.defineProperty(heightRecord, "top", {
568
- enumerable: true,
569
- value: `-${toggleMaxHeight.value}px`,
570
- });
571
- }
572
-
573
- Object.defineProperty(heightRecord, "maxHeight", {
574
- enumerable: true,
575
- value: `${toggleMaxHeight.value}px`,
576
- });
577
-
578
- if (toggleMinHeight.value) {
579
- if (toggleMinHeight.value > toggleMaxHeight.value) {
580
- if (showInsertWarnings.value) {
581
- let errorMessage = "vue-extended-multiselect: «toggleMinHeight» property can not be greater than «toggleMaxHeight» property.";
582
- errorMessage += "«toggleMaxHeight» property was reset to default value of 400 pixels.";
583
- console.warn(errorMessage);
584
- }
585
-
586
- return Object.keys(heightRecord).length ? heightRecord : null;
587
- }
588
-
589
- Object.defineProperty(heightRecord, "minHeight", {
590
- enumerable: true,
591
- value: `${toggleMinHeight.value}px`,
592
- });
593
- }
594
-
595
- return Object.keys(heightRecord).length ? heightRecord : null;
596
- });
597
-
598
- /**
599
- * Determines whether maximal amount of options has been selected
600
- * @function
601
- * @returns {boolean} reaching
602
- */
603
- const maxOptionsWereSelected = computed(() => {
604
- return maxOptionsCount.value && multiple.value && selectedOptions.value.length === maxOptionsCount.value;
605
- });
606
-
607
- /**
608
- * Defines styles for "margin-top" css-property of marker element
609
- * @function
610
- * @returns {Object} styles
611
- */
612
- const markerShapeMargin = computed(() => {
613
- return { marginTop: "2px" };
614
- });
615
-
616
- /**
617
- * Determines whether minimal amount of options has not been selected
618
- * @function
619
- * @returns {boolean} reaching
620
- */
621
- const minOptionsWereNotSelected = computed(() => {
622
- return minOptionsCount.value && multiple.value && selectedOptions.value.length < minOptionsCount.value;
623
- });
624
-
625
- /**
626
- * Defines styles for "padding" and "height" css-property
627
- * of every option element
628
- * @function
629
- * @returns {Object} styles
630
- */
631
- const optionHeightByProps = computed(() => {
632
- let height = {
633
- height: `${anyOptionWrapperBlockHeight.value}px`,
634
- };
635
-
636
- if (optionsPadding.value.length) {
637
- const padding = {
638
- paddingTop: optionsPadding.value[0] ? `${optionsPadding.value[0]}px` : null,
639
- paddingLeft: optionsPadding.value[1] ? `${optionsPadding.value[1]}px` : null,
640
- paddingBottom: optionsPadding.value[2] ? `${optionsPadding.value[2]}px` : null,
641
- paddingRight: optionsPadding.value[3] ? `${optionsPadding.value[3]}px` : null,
642
- };
643
-
644
- return Object.assign(height, padding);
645
- }
646
-
647
- return height;
648
- });
649
-
650
- /**
651
- * Determines whether to show element with new custom option
652
- * @function
653
- * @returns {boolean} display
654
- */
655
- const showCreateNewOptionBlock = computed(() => {
656
- return createOnTheGo.value && !availableOptions.value.length && searchState.searchValue;
657
- });
658
-
659
- /**
660
- * Summarizes styles for options wrapper in combined collection
661
- * @function
662
- * @returns {Array} styles
663
- */
664
-
665
- const styles = computed(() => {
666
- return [
667
- heightFromProps.value,
668
- heightFromMounted.value,
669
- atopWithScroll.value,
670
- underWithScroll.value
671
- ];
672
- });
673
-
674
- /**
675
- * Restricts repeated selection of option
676
- * @function
677
- * @param {string} calculatedOptionLabel - displayed label of option
678
- */
679
- const compareWithSelected = (calculatedOptionLabel) => {
680
- return !selectedOptions.value.find((selectedOption) => {
681
- let calculatedSelectedOptionLabel;
682
-
683
- if (typeof selectedOption === "object") {
684
- if (Object.keys(selectedOption).length === 0) {
685
- calculatedSelectedOptionLabel = emptyObjectsPlaceholder.value;
686
- } else if (Array.isArray(selectedOption)) {
687
- calculatedSelectedOptionLabel = selectedOption.join(", ");
688
- } else {
689
- calculatedSelectedOptionLabel = typeof selectedOption[label.value] === "object"
690
- ? JSON.stringify(selectedOption[label.value])
691
- : selectedOption[label.value].toString();
692
- }
693
- } else {
694
- calculatedSelectedOptionLabel = selectedOption.toString();
695
- }
696
-
697
- return calculatedSelectedOptionLabel === calculatedOptionLabel;
698
- });
699
- };
700
-
701
- /**
702
- * Creates new option by users input
703
- * if "createOnTheGo" prop equals true
704
- * @function
705
- * @emits extended:create-option
706
- */
707
- const createNewOption = () => {
708
- let newOption;
709
-
710
- switch(createOptionType.value) {
711
- case "primitive":
712
- newOption = searchState.searchValue;
713
- break;
714
- case "array":
715
- newOption = [searchState.searchValue];
716
- break;
717
- case "object":
718
- newOption = createObjectOption();
719
- break;
720
- }
721
-
722
- options.value.unshift(newOption);
723
- emitter.emit(`extended:create-option-${emitterUniqueId.value}`, newOption);
724
-
725
- if (autoSelectCreatedOption.value) {
726
- selectOption(newOption, { target: { id: "extended__multiselect" } });
727
- setSearchValue(null);
728
- setSearchPattern(null);
729
- }
730
- };
731
-
732
- /**
733
- * Creates option of type object
734
- * if "createOptionType" equals "object"
735
- * @function
736
- * @returns {UnionPropType} option
737
- */
738
- const createObjectOption = () => {
739
- if (!createOptionFields.value) {
740
- if (showInsertWarnings.value) {
741
- console.warn("vue-extended-multiselect: if option should be object — option fields should not be empty");
742
- }
743
-
744
- return searchState.searchValue;
745
- }
746
-
747
- const newOption = {};
748
-
749
- createOptionFields.value.forEach((field) => {
750
- Object.defineProperty(newOption, field, {
751
- enumerable: true,
752
- writable: true,
753
- configurable: true,
754
- value: `${field}-${searchState.searchValue}`,
755
- });
756
- });
757
-
758
- return newOption;
759
- };
760
-
761
- /**
762
- * Recognises if option is disabled
763
- * @function
764
- * @param {UnionPropType} option - option to recognize
765
- * @returns {boolean} prohibition
766
- */
767
- const defineDisabledOption = (option) => {
768
- if (typeof option === "object" && !Array.isArray(option)) return false;
769
-
770
- let convertedOption;
771
- if (Array.isArray(option) && !option.length) {
772
- convertedOption = emptyObjectsPlaceholder.value;
773
- } else {
774
- convertedOption = option;
775
- }
776
-
777
- if (disabledPrimitiveOptionsConverted.value.includes(convertedOption)) return true;
778
-
779
- return false;
780
- };
781
-
782
- /**
783
- * Filters options list by pattern of internal search for available options
784
- * @function
785
- * @param {Array} optionsList - pre-filtered list of oprions
786
- * @returns {boolean} filter
787
- */
788
- const filterBySearchPattern = (optionsList) => {
789
- return optionsList.filter((option) => {
790
- if (typeof option === "object") {
791
- if (Array.isArray(option)) {
792
- return searchState.searchPattern.test(option.length ? option.join(", ") : emptyObjectsPlaceholder.value);
793
- }
794
-
795
- let internalSearchResult;
796
-
797
- if (searchByField.value.length) {
798
- if (!option[searchByField.value]) return false;
799
-
800
- internalSearchResult = option[searchByField.value].toString();
801
- } else {
802
- internalSearchResult = option[label.value].toString();
803
- }
804
-
805
- return searchState.searchPattern.test(internalSearchResult);
806
- } else {
807
- return searchState.searchPattern.test(option.toString());
808
- }
809
- });
810
- };
811
-
812
- /**
813
- * Blocks keys pressed in combination with "enter" key
814
- * for not to select options by them
815
- * @function
816
- * @param {KeyboardEvent} keyboardEvent - KeyboardEvent instance
817
- * @returns {boolean} block
818
- */
819
- const keyBlocker = (keyboardEvent) => {
820
- if (!keyboardEvent) return false;
821
-
822
- const altKeyBlock = specialKeysBlock.value.includes("alt");
823
- const ctrlKeyBlock = specialKeysBlock.value.includes("ctrl");
824
- const shiftKeyBlock = specialKeysBlock.value.includes("shift");
825
-
826
- if (altKeyBlock && keyboardEvent.altKey) return true;
827
- if (ctrlKeyBlock && keyboardEvent.ctrlKey) return true;
828
- if (shiftKeyBlock && keyboardEvent.shiftKey) return true;
829
-
830
- return false;
831
- }
832
-
833
- /**
834
- * Searchs for already selected options in particular
835
- * for options of type "object"
836
- * @function
837
- * @param {UnionPropType} option - option for search
838
- * @returns {boolean} selected
839
- */
840
- const lookForObjectOptions = (option) => {
841
- if (typeof option === "object") {
842
- let calculatedOptionLabel;
843
-
844
- if (Array.isArray(option) && !!option.length) {
845
- calculatedOptionLabel = option.join(", ");
846
- } else {
847
- const hasLabel = Object.getOwnPropertyNames(option).includes("label");
848
-
849
- if (hasLabel) {
850
- calculatedOptionLabel = typeof option[label.value] === "object"
851
- ? JSON.stringify(option[label.value])
852
- : option[label.value].toString();
853
- } else {
854
- calculatedOptionLabel = JSON.stringify(option);
855
- }
856
-
857
- if (Object.keys(option).length === 0) {
858
- calculatedOptionLabel = emptyObjectsPlaceholder.value;
859
- }
860
- }
861
-
862
- return compareWithSelected(calculatedOptionLabel);
863
- } else {
864
- return !selectedOptions.value.includes(option);
865
- }
866
- };
867
-
868
- /**
869
- * Searches for already selected options in particular
870
- * for options of primitive types or of type "function"
871
- * @function
872
- * @param {UnionPropType} option - option for search
873
- * @returns {boolean} selected
874
- */
875
- const lookForSimpleOptions = (option) => {
876
- if (typeof option === "object" && Object.keys(option).length === 0) {
877
- const emptyObject = selectedOptions.value.find((emptyObject) => {
878
- return typeof emptyObject === "object" && !Object.keys(emptyObject).length;
879
- });
880
-
881
- if (!emptyObject || selectedOptionsShown.value) return true;
882
-
883
- return false;
884
- }
885
-
886
- if (
887
- typeof option === "object"
888
- && !Array.isArray(option)
889
- && !Object.keys(option).includes(label.value)
890
- ) return false;
891
-
892
- return true;
893
- };
894
-
895
- /**
896
- * Defines a class for "marker" slot content
897
- * @function
898
- * @param {UnionPropType} option - option in options list
899
- * @returns {string} class
900
- */
901
- const markerShapeClass = (option) => {
902
- if (lookForSimpleOptions(option) && lookForObjectOptions(option)) {
903
- return "extended__multiselect-marker-shape--selected";
904
- }
905
-
906
- return "extended__multiselect-marker-shape";
907
- }
908
-
909
- /**
910
- * Creates role of every option element for accessible applications
911
- * if it needed
912
- * @function
913
- * @param {UnionPropType} option - each option from options list
914
- * @returns {string|null} role
915
- */
916
- const optionCreateRole = (option) => {
917
- if (optionIsDisabled(option)) return null;
918
- if (!option) return null;
919
-
920
- return "option";
921
- };
922
-
923
- /**
924
- * Determines whether option is disabled
925
- * @function
926
- * @param {UnionPropType} option - each option from options list
927
- * @returns {boolean} blocking
928
- */
929
- const optionIsDisabled = (option) => {
930
- if (option[disableByField.value] || defineDisabledOption(option)) return true;
931
-
932
- return false;
933
- }
934
-
935
- /**
936
- * Defines various classes for options dependent from props
937
- * @function
938
- * @param {UnionPropType} option - each option from options list
939
- * @returns {Array} classes
940
- */
941
- const optionHighlightClasses = (option) => {
942
- let theme = [];
943
-
944
- if (highlightOptions.value) {
945
- switch(themeType.value) {
946
- case "basic":
947
- theme.push("extended__multiselect-options_option");
948
- break;
949
- case "slate-grey":
950
- theme.push("extended__multiselect-options_option-slate-grey");
951
- break;
952
- case "slate-blue":
953
- theme.push("extended__multiselect-options_option-slate-blue");
954
- break;
955
- case "teal":
956
- theme.push("extended__multiselect-options_option-teal");
957
- break;
958
- case "strict":
959
- theme.push("extended__multiselect-options_option-strict");
960
- break;
961
- default:
962
- theme.push("extended__multiselect-options_option");
963
- }
964
- }
965
-
966
- if (showMarker) {
967
- theme.push("extended__multiselect-options_option--marker");
968
- }
969
-
970
- if (option && option[disableByField.value]) {
971
- theme.push("extended__multiselect-options_option--disabled");
972
- }
973
-
974
- if (disabledPrimitiveOptionsConverted.value && option && defineDisabledOption(option)) {
975
- theme.push("extended__multiselect-options_option--disabled");
976
- }
977
-
978
- return theme;
979
- };
980
-
981
- /**
982
- * Selects option
983
- * @function
984
- * @emits extended:deselect-option
985
- * @param {UnionPropType} option - option to select
986
- * @param {MouseEvent|KeyboardEvent} clickEvent - MouseEvent or KeyboardEvent instance
987
- */
988
- const selectOption = (option, clickEvent) => {
989
- emitter.emit(`extended:trigger-selection-${emitterUniqueId.value}`, false);
990
-
991
- if (!clickEvent) return;
992
- if (keyBlocker(clickEvent)) return;
993
- if (maxOptionsWereSelected.value) return;
994
- if (option[disableByField.value] || defineDisabledOption(option)) return;
995
- if (
996
- clickEvent.target.id === "extended__multiselect-toggle"
997
- || clickEvent.target.id === "extended__multiselect-cancel"
998
- ) return;
999
-
1000
- if (selectedOptionsShown.value) {
1001
- const optionDeselected = lookForObjectOptions(option);
1002
-
1003
- if (!optionDeselected) {
1004
- emitter.emit(`extended:deselect-option-${emitterUniqueId.value}`);
1005
- return;
1006
- }
1007
- }
1008
-
1009
- const isObject = typeof option === "object" && !Array.isArray(option);
1010
-
1011
- if (isObject && !Object.keys(option).includes(label.value)) {
1012
- if (showInsertWarnings.value) {
1013
- console.warn("vue-extended-multiselect: wrong label type inserted");
1014
- }
1015
- }
1016
-
1017
- selectOptionEmitter(option);
1018
- };
1019
-
1020
- /**
1021
- * Emits select events or clear event
1022
- * if prop "clearBySelectWhenMultiple" equals true
1023
- * @function
1024
- * @emits extended:clear-field
1025
- * @emits extended:select-option
1026
- * @param {UnionPropType} - option to select
1027
- */
1028
- const selectOptionEmitter = (option) => {
1029
- const isObjectOrArray = typeof option === "object";
1030
- const label = createLabel(isObjectOrArray, option);
1031
-
1032
- if (multiple.value) {
1033
- if (clearBySelectWhenMultiple.value) {
1034
- emitter.emit(`extended:clear-field-${emitterUniqueId.value}`);
1035
- }
1036
-
1037
- emitter.emit(`extended:select-option-${emitterUniqueId.value}`, {
1038
- label,
1039
- option,
1040
- });
1041
- } else {
1042
- emitter.emit(`extended:select-option-${emitterUniqueId.value}`, {
1043
- label,
1044
- option,
1045
- });
1046
- }
1047
- };
1048
-
1049
- /**
1050
- * Determines an option which will be selected
1051
- * by pressing "enter" key on it
1052
- * @function
1053
- * @param {MouseEvent} event - MouseEvent instance
1054
- */
1055
- const setEnterIndex = (event) => {
1056
- enterIndex.value = event.target.getAttribute("enter-locator");
1057
- };
1058
-
1059
- /**
1060
- * Determines whether to show marker beside option by its selection
1061
- * @function
1062
- * @param {UnionPropType} option - each option from options list
1063
- * @returns {boolean} display
1064
- */
1065
- const showCurrentMarker = (option) => {
1066
- if (!lookForSimpleOptions(option) || !lookForObjectOptions(option)) {
1067
- return true;
1068
- }
1069
-
1070
- return false;
1071
- };
1072
-
1073
- /**
1074
- * Emits event which listener will add special class
1075
- * to search field before option selection
1076
- * @function
1077
- * @emits extended:trigger-selection
1078
- */
1079
- const triggerOptionBeforeSelection = () => {
1080
- if (fieldWasShown.value) {
1081
- emitter.emit(`extended:trigger-selection-${emitterUniqueId.value}`, true);
1082
- }
1083
- };
1084
-
1085
- /**
1086
- * "onBeforeMount" hook of ExtendedMultiselectOptions component
1087
- * @listens extended:select-enter
1088
- * @listens extended:renew-field-forwarding
1089
- * @listens extended:reset-index
1090
- */
1091
- onBeforeMount(() => {
1092
- const objectFields = restrictedOptions.value.filter((option) => {
1093
- if (!option) return false;
1094
-
1095
- return typeof option === "object" && typeof option[label.value] === "object";
1096
- });
1097
-
1098
- if (objectFields.length && showInsertWarnings.value) {
1099
- console.warn("vue-extended-multiselect: «label» property can not be of type «object»");
1100
- }
1101
-
1102
- emitter.on(`extended:select-enter-${emitterUniqueId.value}`, (keyboardEvent) => {
1103
- if (!enterIndex.value) return;
1104
-
1105
- const option = availableOptions.value[enterIndex.value];
1106
- selectOption(option, keyboardEvent);
1107
- });
1108
-
1109
- emitter.on(`extended:renew-field-forwarding-${emitterUniqueId.value}`, (value) => {
1110
- fieldWasShown.value = value;
1111
- });
1112
-
1113
- emitter.on(`extended:reset-index-${emitterUniqueId.value}`, () => {
1114
- enterIndex.value = null;
1115
- });
1116
- });
1117
-
1118
- /**
1119
- * "onMounted" hook of ExtendedMultiselectOptions component
1120
- */
1121
- onMounted(() => {
1122
- const offsetHeight = optionsWrapper.value.offsetHeight;
1123
- const scrollHeight = optionsWrapper.value.scrollHeight;
1124
-
1125
- if (chosenToggleAppearanceSide.value === "atop") {
1126
- if (scrollHeight > offsetHeight) {
1127
- atopWithScroll.value = {
1128
- borderTopRightRadius: 0,
1129
- };
1130
- }
1131
-
1132
- if (toggleMaxHeight.value > offsetHeight) {
1133
- heightFromMounted.value = {
1134
- top: `-${offsetHeight - 1}px`,
1135
- };
1136
- }
1137
- } else {
1138
- if (scrollHeight > offsetHeight) {
1139
- underWithScroll.value = {
1140
- borderBottomRightRadius: 0,
1141
- };
1142
- }
1143
- }
1144
- });
1
+ <template>
2
+ <div
3
+ ref="optionsWrapper"
4
+ tabindex="-1"
5
+ :class="classes"
6
+ :style="styles"
7
+ >
8
+ <slot name="listHeader"></slot>
9
+ <div
10
+ v-if="showCreateNewOptionBlock"
11
+ class="extended__multiselect-option-create"
12
+ :class="optionHighlightClasses(null)"
13
+ @mousedown="createNewOption"
14
+ >
15
+ {{ searchState.searchValue }}
16
+ </div>
17
+ <slot
18
+ name="moreThanLimit"
19
+ v-if="maxOptionsWereSelected"
20
+ >
21
+ <span>Maximum limit of selected options was achieved</span>
22
+ </slot>
23
+ <slot
24
+ name="lessThanLimit"
25
+ v-if="minOptionsWereNotSelected"
26
+ >
27
+ <span>Minimum amount of selected options was not achieved</span>
28
+ </slot>
29
+ <div class="extended__multiselect-options_container">
30
+ <div
31
+ role="listbox"
32
+ v-for="(option, index) in availableOptions"
33
+ :key="index"
34
+ >
35
+ <div
36
+ v-if="option"
37
+ :aria-setsize="availableOptions.length"
38
+ :aria-posinset="index"
39
+ :aria-labelledby="`option-label-${index}`"
40
+ :aria-disabled="optionIsDisabled(option)"
41
+ :class="optionHighlightClasses(option)"
42
+ :style="optionHeightByProps"
43
+ :enter-locator="index"
44
+ :role="optionCreateRole(option)"
45
+ @click.stop="selectOption(option, $event)"
46
+ @keypress.stop="selectOption(option, $event)"
47
+ @mousedown="triggerOptionBeforeSelection"
48
+ @mouseenter.stop="setEnterIndex"
49
+ >
50
+ <slot
51
+ name="option"
52
+ :option="option"
53
+ :create-custom-option-label="createCustomOptionLabel"
54
+ >
55
+ <div class="extended__multiselect-marker">
56
+ <slot
57
+ name="marker"
58
+ :option="option"
59
+ >
60
+ <div
61
+ v-if="showMarker && showCurrentMarker(option)"
62
+ :class="markerShapeClass(option)"
63
+ :style="markerShapeMargin"
64
+ >
65
+ </div>
66
+ <div
67
+ v-else-if="showMarker"
68
+ class="extended__multiselect-marker-shape-only"
69
+ >
70
+ </div>
71
+ </slot>
72
+ </div>
73
+ <span :id="`option-label-${index}`">
74
+ {{ optionCreateLabel(option) }}
75
+ </span>
76
+ </slot>
77
+ </div>
78
+ </div>
79
+ </div>
80
+ <slot
81
+ name="noResults"
82
+ v-if="emptySearchResult"
83
+ >
84
+ <span>No results were found by search</span>
85
+ </slot>
86
+ <slot
87
+ name="noOptions"
88
+ v-if="emptyOptionsList"
89
+ >
90
+ <span>Options list is empty</span>
91
+ </slot>
92
+ <slot name="listFooter"></slot>
93
+ </div>
94
+ </template>
95
+
96
+ <script setup>
97
+ import {
98
+ computed,
99
+ defineProps,
100
+ inject,
101
+ onBeforeMount,
102
+ onMounted,
103
+ ref,
104
+ toRefs,
105
+ } from "vue";
106
+
107
+ import emitter from '../events/emitter';
108
+
109
+ import usePreselectedOptions from "../composition/preselected-options";
110
+ import useLabels from "../composition/labels";
111
+
112
+ import { UnionPropType } from "../sets/sets";
113
+
114
+ const props = defineProps({
115
+ /**
116
+ * Determines whether to immediately select just now created option
117
+ * @property {boolean} autoSelectCreatedOption
118
+ */
119
+ autoSelectCreatedOption: {
120
+ type: Boolean,
121
+ required: true,
122
+ },
123
+
124
+ /**
125
+ * Determines whether to show noResults slot
126
+ * and block with new option's text
127
+ * @property {boolean} createOnTheGo
128
+ */
129
+ createOnTheGo: {
130
+ type: Boolean,
131
+ required: true,
132
+ },
133
+
134
+ /**
135
+ * Determines whether to emit clear event
136
+ * by option selection
137
+ * @property {boolean} clearBySelectWhenMultiple
138
+ */
139
+ clearBySelectWhenMultiple: {
140
+ type: Boolean,
141
+ required: true,
142
+ },
143
+
144
+ /**
145
+ * Determines whether to define thematic class for
146
+ * every option
147
+ * @property {boolean} highlightOptions
148
+ */
149
+ highlightOptions: {
150
+ type: Boolean,
151
+ required: true,
152
+ },
153
+
154
+ /**
155
+ * Determines whether to use functionality of
156
+ * multiple select
157
+ * @property {boolean} multiple
158
+ */
159
+ multiple: {
160
+ type: Boolean,
161
+ required: true,
162
+ },
163
+
164
+ /**
165
+ * Determines whether to show selected options
166
+ * in options list
167
+ * @property {boolean} selectedOptionsShown
168
+ */
169
+ selectedOptionsShown: {
170
+ type: Boolean,
171
+ required: true,
172
+ },
173
+
174
+ /**
175
+ * Determines whether to log warnings about incorrect props
176
+ * @property {boolean} showInsertWarnings
177
+ */
178
+ showInsertWarnings: {
179
+ type: Boolean,
180
+ required: true,
181
+ },
182
+
183
+ /**
184
+ * Determines whether to show marker slot
185
+ * @property {boolean} showMarker
186
+ */
187
+ showMarker: {
188
+ type: Boolean,
189
+ required: true,
190
+ },
191
+
192
+ /**
193
+ * Defines class of options list placement after expanse
194
+ * @property {string} chosenToggleAppearanceSide
195
+ */
196
+ chosenToggleAppearanceSide: {
197
+ type: String,
198
+ required: true,
199
+ },
200
+
201
+ /**
202
+ * Determines type of option that will be created
203
+ * @property {string} createOptionType
204
+ */
205
+ createOptionType: {
206
+ type: String,
207
+ required: true,
208
+ },
209
+
210
+ /**
211
+ * Determines a field in options of type "object"
212
+ * which value will be used to disable such options.
213
+ * @property {string} disableByField
214
+ */
215
+ disableByField: {
216
+ type: String,
217
+ required: true,
218
+ },
219
+
220
+ /**
221
+ * Assigns unique identifier of extended multiselect
222
+ * for each event
223
+ * @property {string} emitterUniqueId
224
+ */
225
+ emitterUniqueId: {
226
+ type: String,
227
+ required: true,
228
+ },
229
+
230
+ /**
231
+ * Sets labels for options of type "object"
232
+ * or Array instances which length/keys length equals 0
233
+ * @property {string} emptyObjectsPlaceholder
234
+ */
235
+ emptyObjectsPlaceholder: {
236
+ type: String,
237
+ required: true,
238
+ },
239
+
240
+ /**
241
+ * Determines field of option which will be displayed as label
242
+ * @property {string} label
243
+ */
244
+ label: {
245
+ type: String,
246
+ required: true,
247
+ },
248
+
249
+ /**
250
+ * Determines field in options of type "object"
251
+ * to use in internal search.
252
+ * @property {string} searchByField
253
+ */
254
+ searchByField: {
255
+ type: String,
256
+ required: true,
257
+ },
258
+
259
+ /**
260
+ * Current theme of extended-multiselect
261
+ * used in class definition
262
+ * @property {string} themeType
263
+ */
264
+ themeType: {
265
+ type: String,
266
+ required: true,
267
+ },
268
+
269
+ /**
270
+ * Defines "height" css-property of every option element
271
+ * @property {number} anyOptionWrapperBlockHeight
272
+ */
273
+ anyOptionWrapperBlockHeight: {
274
+ type: Number,
275
+ required: true,
276
+ },
277
+
278
+ /**
279
+ * Determines maximal limit of selected options
280
+ * @property {number|null} maxOptionsCount
281
+ */
282
+ maxOptionsCount: {
283
+ type: Number,
284
+ default: null,
285
+ },
286
+
287
+ /**
288
+ * Determines minimal limit of selected options
289
+ * @property {number|null} minOptionsCount
290
+ */
291
+ minOptionsCount: {
292
+ type: Number,
293
+ default: null,
294
+ },
295
+
296
+ /**
297
+ * Defines value of "max-height" css-property of options list
298
+ * @property {number} toggleMaxHeight
299
+ */
300
+ toggleMaxHeight: {
301
+ type: Number,
302
+ required: true,
303
+ },
304
+
305
+ /**
306
+ * Defines value of "min-height" css-property of options list
307
+ * @property {number} toggleMinHeight
308
+ */
309
+ toggleMinHeight: {
310
+ type: Number,
311
+ default: null,
312
+ },
313
+
314
+ /**
315
+ * List of fields for new options of type "object" created by user
316
+ * if "createOnTheGo" prop equals true
317
+ * Each field will have basic value of "searchValue" prop
318
+ * @property {Array} createOptionFields
319
+ */
320
+ createOptionFields: {
321
+ type: Array,
322
+ required: true,
323
+ },
324
+
325
+ /**
326
+ * Searches for options with listed primitive values
327
+ * and disables it
328
+ * @property {Array} disabledPrimitiveOptionsConverted
329
+ */
330
+ disabledPrimitiveOptionsConverted: {
331
+ type: Array,
332
+ required: true,
333
+ },
334
+
335
+ /**
336
+ * Unfiltered options list
337
+ * @property {Array} options
338
+ */
339
+ options: {
340
+ type: Array,
341
+ required: true,
342
+ },
343
+
344
+ /**
345
+ * Defines value of "padding" css-property of every option
346
+ * @property {Array} optionsPadding
347
+ */
348
+ optionsPadding: {
349
+ type: Array,
350
+ required: true,
351
+ },
352
+
353
+ /**
354
+ * List of options which should be selected by default
355
+ * if "multiple" prop equals true
356
+ * @property {Array} preselectedOptions
357
+ */
358
+ preselectedOptions: {
359
+ type: Array,
360
+ required: true,
361
+ },
362
+
363
+ /**
364
+ * List of options filtered by maximal available options amount
365
+ * @property {Array} restrictedOptions
366
+ */
367
+ restrictedOptions: {
368
+ type: Array,
369
+ required: true,
370
+ },
371
+
372
+ /**
373
+ * List of selected options or list with single selected option
374
+ * if "multiple" prop equals false
375
+ * @property {Array} selectedOptions
376
+ */
377
+ selectedOptions: {
378
+ type: Array,
379
+ required: true,
380
+ },
381
+
382
+ /**
383
+ * List of key names that in combination with "enter" key
384
+ * should cancel option selection
385
+ * @property {Array} specialKeysBlock
386
+ */
387
+ specialKeysBlock: {
388
+ type: Array,
389
+ required: true,
390
+ },
391
+
392
+ /**
393
+ * Creates custom label for every option in options list
394
+ * @property {Function} createCustomOptionLabel
395
+ */
396
+ createCustomOptionLabel: {
397
+ type: Function,
398
+ required: true,
399
+ },
400
+
401
+ /**
402
+ * Option which should be selected by default
403
+ * if "multiple" prop equals false
404
+ * @property {UnionPropType|null} preselectedOption
405
+ */
406
+ preselectedOption: {
407
+ type: UnionPropType,
408
+ default: null,
409
+ },
410
+ });
411
+
412
+ const searchState = inject("searchState");
413
+ const setSearchValue = inject("setSearchValue");
414
+ const setSearchPattern = inject("setSearchPattern");
415
+
416
+ const fieldWasShown = ref(false);
417
+ const atopWithScroll = ref(null);
418
+ const enterIndex = ref(null);
419
+ const heightFromMounted = ref(null);
420
+ const underWithScroll = ref(null);
421
+
422
+ /**
423
+ * Element references
424
+ */
425
+ const optionsWrapper = ref(null);
426
+
427
+ const {
428
+ anyOptionWrapperBlockHeight,
429
+ autoSelectCreatedOption,
430
+ chosenToggleAppearanceSide,
431
+ clearBySelectWhenMultiple,
432
+ createOnTheGo,
433
+ createCustomOptionLabel,
434
+ createOptionFields,
435
+ createOptionType,
436
+ disableByField,
437
+ disabledPrimitiveOptionsConverted,
438
+ emitterUniqueId,
439
+ emptyObjectsPlaceholder,
440
+ highlightOptions,
441
+ label,
442
+ searchByField,
443
+ maxOptionsCount,
444
+ minOptionsCount,
445
+ multiple,
446
+ options,
447
+ optionsPadding,
448
+ restrictedOptions,
449
+ selectedOptions,
450
+ selectedOptionsShown,
451
+ showInsertWarnings,
452
+ showMarker,
453
+ specialKeysBlock,
454
+ themeType,
455
+ toggleMaxHeight,
456
+ toggleMinHeight,
457
+ } = toRefs(props);
458
+
459
+ const { createLabel, optionTypeRestrictor } = usePreselectedOptions(
460
+ label,
461
+ emptyObjectsPlaceholder,
462
+ showInsertWarnings,
463
+ );
464
+ const { optionCreateLabel } = useLabels(
465
+ label,
466
+ createCustomOptionLabel,
467
+ emptyObjectsPlaceholder,
468
+ );
469
+
470
+ /**
471
+ * List of options filtered by "disabled" and "selected" signs
472
+ * also by equality to null and undefined primitive values
473
+ * or instance declarations
474
+ * @function
475
+ * @returns {Array} options
476
+ */
477
+ const availableOptions = computed(() => {
478
+ let filteredOptions = restrictedOptions.value.filter((option) => {
479
+ const availableOptionType = optionTypeRestrictor(option);
480
+
481
+ if (!option || !availableOptionType) return false;
482
+
483
+ if (!lookForSimpleOptions(option)) return false;
484
+
485
+ if (selectedOptionsShown.value) return true;
486
+
487
+ return lookForObjectOptions(option);
488
+ });
489
+
490
+ let finalOptionsList = filteredOptions;
491
+
492
+ if (searchState.searchPattern) {
493
+ finalOptionsList = filterBySearchPattern(filteredOptions);
494
+ }
495
+
496
+ return finalOptionsList;
497
+ });
498
+
499
+ /**
500
+ * Defines a list of options list classes
501
+ * @function
502
+ * @returns {Array} classes
503
+ */
504
+ const classes = computed(() => {
505
+ let theme = [];
506
+
507
+ switch(themeType.value) {
508
+ case "basic":
509
+ theme.push("extended__multiselect-options");
510
+ break;
511
+ case "slate-grey":
512
+ theme.push("extended__multiselect-options-slate-grey");
513
+ break;
514
+ case "slate-blue":
515
+ theme.push("extended__multiselect-options-slate-blue");
516
+ break;
517
+ case "teal":
518
+ theme.push("extended__multiselect-options-teal");
519
+ break;
520
+ case "strict":
521
+ theme.push("extended__multiselect-options-strict");
522
+ break;
523
+ default:
524
+ theme.push("extended__multiselect-options");
525
+ }
526
+
527
+ if (showMarker.value) {
528
+ theme.push("extended__multiselect-options--marker");
529
+ }
530
+
531
+ if (chosenToggleAppearanceSide.value === "atop") {
532
+ theme.push("extended__multiselect-options--atop");
533
+ }
534
+
535
+ return theme;
536
+ });
537
+
538
+ /**
539
+ * Determines whether given options list is empty
540
+ * @function
541
+ * @returns {boolean} emptiness
542
+ */
543
+ const emptyOptionsList = computed(() => {
544
+ return (!options.value.length || !options.value.filter((option) => !!option).length
545
+ || !availableOptions.value.length) && !(createOnTheGo.value && searchState.searchValue);
546
+ });
547
+
548
+ /**
549
+ * Determines whether list of internal search results is empty
550
+ * @function
551
+ * @returns {boolean} emptiness
552
+ */
553
+ const emptySearchResult = computed(() => {
554
+ return !availableOptions.value.length && !createOnTheGo.value && searchState.searchValue;
555
+ });
556
+
557
+ /**
558
+ * Defines styles for "min-heigt", "max-height"
559
+ * and "top" css-properties of options wrapper
560
+ * @function
561
+ * @returns {Object} styles
562
+ */
563
+ const heightFromProps = computed(() => {
564
+ let heightRecord = {};
565
+
566
+ if (chosenToggleAppearanceSide.value === "atop") {
567
+ Object.defineProperty(heightRecord, "top", {
568
+ enumerable: true,
569
+ value: `-${toggleMaxHeight.value}px`,
570
+ });
571
+ }
572
+
573
+ Object.defineProperty(heightRecord, "maxHeight", {
574
+ enumerable: true,
575
+ value: `${toggleMaxHeight.value}px`,
576
+ });
577
+
578
+ if (toggleMinHeight.value) {
579
+ if (toggleMinHeight.value > toggleMaxHeight.value) {
580
+ if (showInsertWarnings.value) {
581
+ let errorMessage = "vue-extended-multiselect: «toggleMinHeight» property can not be greater than «toggleMaxHeight» property.";
582
+ errorMessage += "«toggleMaxHeight» property was reset to default value of 400 pixels.";
583
+ console.warn(errorMessage);
584
+ }
585
+
586
+ return Object.keys(heightRecord).length ? heightRecord : null;
587
+ }
588
+
589
+ Object.defineProperty(heightRecord, "minHeight", {
590
+ enumerable: true,
591
+ value: `${toggleMinHeight.value}px`,
592
+ });
593
+ }
594
+
595
+ return Object.keys(heightRecord).length ? heightRecord : null;
596
+ });
597
+
598
+ /**
599
+ * Determines whether maximal amount of options has been selected
600
+ * @function
601
+ * @returns {boolean} reaching
602
+ */
603
+ const maxOptionsWereSelected = computed(() => {
604
+ return maxOptionsCount.value && multiple.value && selectedOptions.value.length === maxOptionsCount.value;
605
+ });
606
+
607
+ /**
608
+ * Defines styles for "margin-top" css-property of marker element
609
+ * @function
610
+ * @returns {Object} styles
611
+ */
612
+ const markerShapeMargin = computed(() => {
613
+ return { marginTop: "2px" };
614
+ });
615
+
616
+ /**
617
+ * Determines whether minimal amount of options has not been selected
618
+ * @function
619
+ * @returns {boolean} reaching
620
+ */
621
+ const minOptionsWereNotSelected = computed(() => {
622
+ return minOptionsCount.value && multiple.value && selectedOptions.value.length < minOptionsCount.value;
623
+ });
624
+
625
+ /**
626
+ * Defines styles for "padding" and "height" css-property
627
+ * of every option element
628
+ * @function
629
+ * @returns {Object} styles
630
+ */
631
+ const optionHeightByProps = computed(() => {
632
+ let height = {
633
+ height: `${anyOptionWrapperBlockHeight.value}px`,
634
+ };
635
+
636
+ if (optionsPadding.value.length) {
637
+ const padding = {
638
+ paddingTop: optionsPadding.value[0] ? `${optionsPadding.value[0]}px` : null,
639
+ paddingLeft: optionsPadding.value[1] ? `${optionsPadding.value[1]}px` : null,
640
+ paddingBottom: optionsPadding.value[2] ? `${optionsPadding.value[2]}px` : null,
641
+ paddingRight: optionsPadding.value[3] ? `${optionsPadding.value[3]}px` : null,
642
+ };
643
+
644
+ return Object.assign(height, padding);
645
+ }
646
+
647
+ return height;
648
+ });
649
+
650
+ /**
651
+ * Determines whether to show element with new custom option
652
+ * @function
653
+ * @returns {boolean} display
654
+ */
655
+ const showCreateNewOptionBlock = computed(() => {
656
+ return createOnTheGo.value && !availableOptions.value.length && searchState.searchValue;
657
+ });
658
+
659
+ /**
660
+ * Summarizes styles for options wrapper in combined collection
661
+ * @function
662
+ * @returns {Array} styles
663
+ */
664
+
665
+ const styles = computed(() => {
666
+ return [
667
+ heightFromProps.value,
668
+ heightFromMounted.value,
669
+ atopWithScroll.value,
670
+ underWithScroll.value
671
+ ];
672
+ });
673
+
674
+ /**
675
+ * Restricts repeated selection of option
676
+ * @function
677
+ * @param {string} calculatedOptionLabel - displayed label of option
678
+ */
679
+ const compareWithSelected = (calculatedOptionLabel) => {
680
+ return !selectedOptions.value.find((selectedOption) => {
681
+ let calculatedSelectedOptionLabel;
682
+
683
+ if (typeof selectedOption === "object") {
684
+ if (Object.keys(selectedOption).length === 0) {
685
+ calculatedSelectedOptionLabel = emptyObjectsPlaceholder.value;
686
+ } else if (Array.isArray(selectedOption)) {
687
+ calculatedSelectedOptionLabel = selectedOption.join(", ");
688
+ } else {
689
+ calculatedSelectedOptionLabel = typeof selectedOption[label.value] === "object"
690
+ ? JSON.stringify(selectedOption[label.value])
691
+ : selectedOption[label.value].toString();
692
+ }
693
+ } else {
694
+ calculatedSelectedOptionLabel = selectedOption.toString();
695
+ }
696
+
697
+ return calculatedSelectedOptionLabel === calculatedOptionLabel;
698
+ });
699
+ };
700
+
701
+ /**
702
+ * Creates new option by users input
703
+ * if "createOnTheGo" prop equals true
704
+ * @function
705
+ * @emits extended:create-option
706
+ */
707
+ const createNewOption = () => {
708
+ let newOption;
709
+
710
+ switch(createOptionType.value) {
711
+ case "primitive":
712
+ newOption = searchState.searchValue;
713
+ break;
714
+ case "array":
715
+ newOption = [searchState.searchValue];
716
+ break;
717
+ case "object":
718
+ newOption = createObjectOption();
719
+ break;
720
+ }
721
+
722
+ options.value.unshift(newOption);
723
+ emitter.emit(`extended:create-option-${emitterUniqueId.value}`, newOption);
724
+
725
+ if (autoSelectCreatedOption.value) {
726
+ selectOption(newOption, { target: { id: "extended__multiselect" } });
727
+ setSearchValue(null);
728
+ setSearchPattern(null);
729
+ }
730
+ };
731
+
732
+ /**
733
+ * Creates option of type object
734
+ * if "createOptionType" equals "object"
735
+ * @function
736
+ * @returns {UnionPropType} option
737
+ */
738
+ const createObjectOption = () => {
739
+ if (!createOptionFields.value) {
740
+ if (showInsertWarnings.value) {
741
+ console.warn("vue-extended-multiselect: if option should be object — option fields should not be empty");
742
+ }
743
+
744
+ return searchState.searchValue;
745
+ }
746
+
747
+ const newOption = {};
748
+
749
+ createOptionFields.value.forEach((field) => {
750
+ Object.defineProperty(newOption, field, {
751
+ enumerable: true,
752
+ writable: true,
753
+ configurable: true,
754
+ value: `${field}-${searchState.searchValue}`,
755
+ });
756
+ });
757
+
758
+ return newOption;
759
+ };
760
+
761
+ /**
762
+ * Recognises if option is disabled
763
+ * @function
764
+ * @param {UnionPropType} option - option to recognize
765
+ * @returns {boolean} prohibition
766
+ */
767
+ const defineDisabledOption = (option) => {
768
+ if (typeof option === "object" && !Array.isArray(option)) return false;
769
+
770
+ let convertedOption;
771
+ if (Array.isArray(option) && !option.length) {
772
+ convertedOption = emptyObjectsPlaceholder.value;
773
+ } else {
774
+ convertedOption = option;
775
+ }
776
+
777
+ if (disabledPrimitiveOptionsConverted.value.includes(convertedOption)) return true;
778
+
779
+ return false;
780
+ };
781
+
782
+ /**
783
+ * Filters options list by pattern of internal search for available options
784
+ * @function
785
+ * @param {Array} optionsList - pre-filtered list of oprions
786
+ * @returns {boolean} filter
787
+ */
788
+ const filterBySearchPattern = (optionsList) => {
789
+ return optionsList.filter((option) => {
790
+ if (typeof option === "object") {
791
+ if (Array.isArray(option)) {
792
+ return searchState.searchPattern.test(option.length ? option.join(", ") : emptyObjectsPlaceholder.value);
793
+ }
794
+
795
+ let internalSearchResult;
796
+
797
+ if (searchByField.value.length) {
798
+ if (!option[searchByField.value]) return false;
799
+
800
+ internalSearchResult = option[searchByField.value].toString();
801
+ } else {
802
+ internalSearchResult = option[label.value].toString();
803
+ }
804
+
805
+ return searchState.searchPattern.test(internalSearchResult);
806
+ } else {
807
+ return searchState.searchPattern.test(option.toString());
808
+ }
809
+ });
810
+ };
811
+
812
+ /**
813
+ * Blocks keys pressed in combination with "enter" key
814
+ * for not to select options by them
815
+ * @function
816
+ * @param {KeyboardEvent} keyboardEvent - KeyboardEvent instance
817
+ * @returns {boolean} block
818
+ */
819
+ const keyBlocker = (keyboardEvent) => {
820
+ if (!keyboardEvent) return false;
821
+
822
+ const altKeyBlock = specialKeysBlock.value.includes("alt");
823
+ const ctrlKeyBlock = specialKeysBlock.value.includes("ctrl");
824
+ const shiftKeyBlock = specialKeysBlock.value.includes("shift");
825
+
826
+ if (altKeyBlock && keyboardEvent.altKey) return true;
827
+ if (ctrlKeyBlock && keyboardEvent.ctrlKey) return true;
828
+ if (shiftKeyBlock && keyboardEvent.shiftKey) return true;
829
+
830
+ return false;
831
+ }
832
+
833
+ /**
834
+ * Searchs for already selected options in particular
835
+ * for options of type "object"
836
+ * @function
837
+ * @param {UnionPropType} option - option for search
838
+ * @returns {boolean} selected
839
+ */
840
+ const lookForObjectOptions = (option) => {
841
+ if (typeof option === "object") {
842
+ let calculatedOptionLabel;
843
+
844
+ if (Array.isArray(option) && !!option.length) {
845
+ calculatedOptionLabel = option.join(", ");
846
+ } else {
847
+ const hasLabel = Object.getOwnPropertyNames(option).includes("label");
848
+
849
+ if (hasLabel) {
850
+ calculatedOptionLabel = typeof option[label.value] === "object"
851
+ ? JSON.stringify(option[label.value])
852
+ : option[label.value].toString();
853
+ } else {
854
+ calculatedOptionLabel = JSON.stringify(option);
855
+ }
856
+
857
+ if (Object.keys(option).length === 0) {
858
+ calculatedOptionLabel = emptyObjectsPlaceholder.value;
859
+ }
860
+ }
861
+
862
+ return compareWithSelected(calculatedOptionLabel);
863
+ } else {
864
+ return !selectedOptions.value.includes(option);
865
+ }
866
+ };
867
+
868
+ /**
869
+ * Searches for already selected options in particular
870
+ * for options of primitive types or of type "function"
871
+ * @function
872
+ * @param {UnionPropType} option - option for search
873
+ * @returns {boolean} selected
874
+ */
875
+ const lookForSimpleOptions = (option) => {
876
+ if (typeof option === "object" && Object.keys(option).length === 0) {
877
+ const emptyObject = selectedOptions.value.find((emptyObject) => {
878
+ return typeof emptyObject === "object" && !Object.keys(emptyObject).length;
879
+ });
880
+
881
+ if (!emptyObject || selectedOptionsShown.value) return true;
882
+
883
+ return false;
884
+ }
885
+
886
+ if (
887
+ typeof option === "object"
888
+ && !Array.isArray(option)
889
+ && !Object.keys(option).includes(label.value)
890
+ ) return false;
891
+
892
+ return true;
893
+ };
894
+
895
+ /**
896
+ * Defines a class for "marker" slot content
897
+ * @function
898
+ * @param {UnionPropType} option - option in options list
899
+ * @returns {string} class
900
+ */
901
+ const markerShapeClass = (option) => {
902
+ if (lookForSimpleOptions(option) && lookForObjectOptions(option)) {
903
+ return "extended__multiselect-marker-shape--selected";
904
+ }
905
+
906
+ return "extended__multiselect-marker-shape";
907
+ }
908
+
909
+ /**
910
+ * Creates role of every option element for accessible applications
911
+ * if it needed
912
+ * @function
913
+ * @param {UnionPropType} option - each option from options list
914
+ * @returns {string|null} role
915
+ */
916
+ const optionCreateRole = (option) => {
917
+ if (optionIsDisabled(option)) return null;
918
+ if (!option) return null;
919
+
920
+ return "option";
921
+ };
922
+
923
+ /**
924
+ * Determines whether option is disabled
925
+ * @function
926
+ * @param {UnionPropType} option - each option from options list
927
+ * @returns {boolean} blocking
928
+ */
929
+ const optionIsDisabled = (option) => {
930
+ if (option[disableByField.value] || defineDisabledOption(option)) return true;
931
+
932
+ return false;
933
+ }
934
+
935
+ /**
936
+ * Defines various classes for options dependent from props
937
+ * @function
938
+ * @param {UnionPropType} option - each option from options list
939
+ * @returns {Array} classes
940
+ */
941
+ const optionHighlightClasses = (option) => {
942
+ let theme = [];
943
+
944
+ if (highlightOptions.value) {
945
+ switch(themeType.value) {
946
+ case "basic":
947
+ theme.push("extended__multiselect-options_option");
948
+ break;
949
+ case "slate-grey":
950
+ theme.push("extended__multiselect-options_option-slate-grey");
951
+ break;
952
+ case "slate-blue":
953
+ theme.push("extended__multiselect-options_option-slate-blue");
954
+ break;
955
+ case "teal":
956
+ theme.push("extended__multiselect-options_option-teal");
957
+ break;
958
+ case "strict":
959
+ theme.push("extended__multiselect-options_option-strict");
960
+ break;
961
+ default:
962
+ theme.push("extended__multiselect-options_option");
963
+ }
964
+ }
965
+
966
+ if (showMarker) {
967
+ theme.push("extended__multiselect-options_option--marker");
968
+ }
969
+
970
+ if (option && option[disableByField.value]) {
971
+ theme.push("extended__multiselect-options_option--disabled");
972
+ }
973
+
974
+ if (disabledPrimitiveOptionsConverted.value && option && defineDisabledOption(option)) {
975
+ theme.push("extended__multiselect-options_option--disabled");
976
+ }
977
+
978
+ return theme;
979
+ };
980
+
981
+ /**
982
+ * Selects option
983
+ * @function
984
+ * @emits extended:deselect-option
985
+ * @param {UnionPropType} option - option to select
986
+ * @param {MouseEvent|KeyboardEvent} clickEvent - MouseEvent or KeyboardEvent instance
987
+ */
988
+ const selectOption = (option, clickEvent) => {
989
+ emitter.emit(`extended:trigger-selection-${emitterUniqueId.value}`, false);
990
+
991
+ if (!clickEvent) return;
992
+ if (keyBlocker(clickEvent)) return;
993
+ if (maxOptionsWereSelected.value) return;
994
+ if (option[disableByField.value] || defineDisabledOption(option)) return;
995
+ if (
996
+ clickEvent.target.id === "extended__multiselect-toggle"
997
+ || clickEvent.target.id === "extended__multiselect-cancel"
998
+ ) return;
999
+
1000
+ if (selectedOptionsShown.value) {
1001
+ const optionDeselected = lookForObjectOptions(option);
1002
+
1003
+ if (!optionDeselected) {
1004
+ emitter.emit(`extended:deselect-option-${emitterUniqueId.value}`);
1005
+ return;
1006
+ }
1007
+ }
1008
+
1009
+ const isObject = typeof option === "object" && !Array.isArray(option);
1010
+
1011
+ if (isObject && !Object.keys(option).includes(label.value)) {
1012
+ if (showInsertWarnings.value) {
1013
+ console.warn("vue-extended-multiselect: wrong label type inserted");
1014
+ }
1015
+ }
1016
+
1017
+ selectOptionEmitter(option);
1018
+ };
1019
+
1020
+ /**
1021
+ * Emits select events or clear event
1022
+ * if prop "clearBySelectWhenMultiple" equals true
1023
+ * @function
1024
+ * @emits extended:clear-field
1025
+ * @emits extended:select-option
1026
+ * @param {UnionPropType} - option to select
1027
+ */
1028
+ const selectOptionEmitter = (option) => {
1029
+ const isObjectOrArray = typeof option === "object";
1030
+ const label = createLabel(isObjectOrArray, option);
1031
+
1032
+ if (multiple.value) {
1033
+ if (clearBySelectWhenMultiple.value) {
1034
+ emitter.emit(`extended:clear-field-${emitterUniqueId.value}`);
1035
+ }
1036
+
1037
+ emitter.emit(`extended:select-option-${emitterUniqueId.value}`, {
1038
+ label,
1039
+ option,
1040
+ });
1041
+ } else {
1042
+ emitter.emit(`extended:select-option-${emitterUniqueId.value}`, {
1043
+ label,
1044
+ option,
1045
+ });
1046
+ }
1047
+ };
1048
+
1049
+ /**
1050
+ * Determines an option which will be selected
1051
+ * by pressing "enter" key on it
1052
+ * @function
1053
+ * @param {MouseEvent} event - MouseEvent instance
1054
+ */
1055
+ const setEnterIndex = (event) => {
1056
+ enterIndex.value = event.target.getAttribute("enter-locator");
1057
+ };
1058
+
1059
+ /**
1060
+ * Determines whether to show marker beside option by its selection
1061
+ * @function
1062
+ * @param {UnionPropType} option - each option from options list
1063
+ * @returns {boolean} display
1064
+ */
1065
+ const showCurrentMarker = (option) => {
1066
+ if (!lookForSimpleOptions(option) || !lookForObjectOptions(option)) {
1067
+ return true;
1068
+ }
1069
+
1070
+ return false;
1071
+ };
1072
+
1073
+ /**
1074
+ * Emits event which listener will add special class
1075
+ * to search field before option selection
1076
+ * @function
1077
+ * @emits extended:trigger-selection
1078
+ */
1079
+ const triggerOptionBeforeSelection = () => {
1080
+ if (fieldWasShown.value) {
1081
+ emitter.emit(`extended:trigger-selection-${emitterUniqueId.value}`, true);
1082
+ }
1083
+ };
1084
+
1085
+ /**
1086
+ * "onBeforeMount" hook of ExtendedMultiselectOptions component
1087
+ * @listens extended:select-enter
1088
+ * @listens extended:renew-field-forwarding
1089
+ * @listens extended:reset-index
1090
+ */
1091
+ onBeforeMount(() => {
1092
+ const objectFields = restrictedOptions.value.filter((option) => {
1093
+ if (!option) return false;
1094
+
1095
+ return typeof option === "object" && typeof option[label.value] === "object";
1096
+ });
1097
+
1098
+ if (objectFields.length && showInsertWarnings.value) {
1099
+ console.warn("vue-extended-multiselect: «label» property can not be of type «object»");
1100
+ }
1101
+
1102
+ emitter.on(`extended:select-enter-${emitterUniqueId.value}`, (keyboardEvent) => {
1103
+ if (!enterIndex.value) return;
1104
+
1105
+ const option = availableOptions.value[enterIndex.value];
1106
+ selectOption(option, keyboardEvent);
1107
+ });
1108
+
1109
+ emitter.on(`extended:renew-field-forwarding-${emitterUniqueId.value}`, (value) => {
1110
+ fieldWasShown.value = value;
1111
+ });
1112
+
1113
+ emitter.on(`extended:reset-index-${emitterUniqueId.value}`, () => {
1114
+ enterIndex.value = null;
1115
+ });
1116
+ });
1117
+
1118
+ /**
1119
+ * "onMounted" hook of ExtendedMultiselectOptions component
1120
+ */
1121
+ onMounted(() => {
1122
+ const offsetHeight = optionsWrapper.value.offsetHeight;
1123
+ const scrollHeight = optionsWrapper.value.scrollHeight;
1124
+
1125
+ if (chosenToggleAppearanceSide.value === "atop") {
1126
+ if (scrollHeight > offsetHeight) {
1127
+ atopWithScroll.value = {
1128
+ borderTopRightRadius: 0,
1129
+ };
1130
+ }
1131
+
1132
+ if (toggleMaxHeight.value > offsetHeight) {
1133
+ heightFromMounted.value = {
1134
+ top: `-${offsetHeight - 1}px`,
1135
+ };
1136
+ }
1137
+ } else {
1138
+ if (scrollHeight > offsetHeight) {
1139
+ underWithScroll.value = {
1140
+ borderBottomRightRadius: 0,
1141
+ };
1142
+ }
1143
+ }
1144
+ });
1145
1145
  </script>