vue3-extended-multiselect 1.7.0 → 1.7.2

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