1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
1
import { activateChoices , addChoice , removeChoice , filterChoices } from './actions/choices' ;
3
2
import { addGroup } from './actions/groups' ;
4
3
import { addItem , highlightItem , removeItem } from './actions/items' ;
@@ -28,7 +27,7 @@ import Store from './store/store';
28
27
import { coerceBool , mapInputToChoice } from './lib/choice-input' ;
29
28
import { ChoiceFull } from './interfaces/choice-full' ;
30
29
import { GroupFull } from './interfaces/group-full' ;
31
- import { EventType , KeyCodeMap , PassedElementType , PassedElementTypes } from './interfaces' ;
30
+ import { EventChoiceValueType , EventType , KeyCodeMap , PassedElementType , PassedElementTypes } from './interfaces' ;
32
31
import { EventChoice } from './interfaces/event-choice' ;
33
32
import { NoticeType , NoticeTypes , Templates } from './interfaces/templates' ;
34
33
import { isHtmlInputElement , isHtmlSelectElement } from './lib/html-guard-statements' ;
@@ -539,13 +538,10 @@ class Choices {
539
538
return this ;
540
539
}
541
540
542
- getValue ( valueOnly = false ) : string [ ] | EventChoice [ ] | EventChoice | string {
543
- const values = this . _store . items . reduce < any [ ] > ( ( selectedItems , item ) => {
544
- const itemValue = valueOnly ? item . value : this . _getChoiceForOutput ( item ) ;
545
- selectedItems . push ( itemValue ) ;
546
-
547
- return selectedItems ;
548
- } , [ ] ) ;
541
+ getValue < B extends boolean = false > ( valueOnly ?: B ) : EventChoiceValueType < B > | EventChoiceValueType < B > [ ] {
542
+ const values = this . _store . items . map ( ( item ) => {
543
+ return ( valueOnly ? item . value : this . _getChoiceForOutput ( item ) ) as EventChoiceValueType < B > ;
544
+ } ) ;
549
545
550
546
return this . _isSelectOneElement || this . config . singleModeForMultiSelect ? values [ 0 ] : values ;
551
547
}
@@ -788,16 +784,21 @@ class Choices {
788
784
789
785
this . clearStore ( false ) ;
790
786
791
- choicesFromOptions . forEach ( ( groupOrChoice ) => {
792
- if ( 'choices' in groupOrChoice ) {
793
- return ;
794
- }
795
- const choice = groupOrChoice ;
787
+ const updateChoice = ( choice : ChoiceFull ) : void => {
796
788
if ( deselectAll ) {
797
789
this . _store . dispatch ( removeItem ( choice ) ) ;
798
790
} else if ( existingItems [ choice . value ] ) {
799
791
choice . selected = true ;
800
792
}
793
+ } ;
794
+
795
+ choicesFromOptions . forEach ( ( groupOrChoice ) => {
796
+ if ( 'choices' in groupOrChoice ) {
797
+ groupOrChoice . choices . forEach ( updateChoice ) ;
798
+
799
+ return ;
800
+ }
801
+ updateChoice ( groupOrChoice ) ;
801
802
} ) ;
802
803
803
804
/* @todo only generate add events for the added options instead of all
@@ -984,7 +985,7 @@ class Choices {
984
985
if ( ! this . _hasNonChoicePlaceholder && ! isSearching && this . _isSelectOneElement ) {
985
986
// If we have a placeholder choice along with groups
986
987
renderChoices (
987
- activeChoices . filter ( ( choice ) => choice . placeholder && ! choice . groupId ) ,
988
+ activeChoices . filter ( ( choice ) => choice . placeholder && ! choice . group ) ,
988
989
false ,
989
990
undefined ,
990
991
) ;
@@ -995,6 +996,13 @@ class Choices {
995
996
if ( config . shouldSort ) {
996
997
activeGroups . sort ( config . sorter ) ;
997
998
}
999
+ // render Choices without group first, regardless of sort, otherwise they won't be distinguishable
1000
+ // from the last group
1001
+ renderChoices (
1002
+ activeChoices . filter ( ( choice ) => ! choice . placeholder && ! choice . group ) ,
1003
+ false ,
1004
+ undefined ,
1005
+ ) ;
998
1006
999
1007
activeGroups . forEach ( ( group ) => {
1000
1008
const groupChoices = renderableChoices ( group . choices ) ;
@@ -1160,13 +1168,8 @@ class Choices {
1160
1168
}
1161
1169
}
1162
1170
1163
- _getChoiceForOutput ( choice ?: ChoiceFull , keyCode ?: number ) : EventChoice | undefined {
1164
- if ( ! choice ) {
1165
- return undefined ;
1166
- }
1167
-
1168
- const group = choice . groupId ? this . _store . getGroupById ( choice . groupId ) : null ;
1169
-
1171
+ // eslint-disable-next-line class-methods-use-this
1172
+ _getChoiceForOutput ( choice : ChoiceFull , keyCode ?: number ) : EventChoice {
1170
1173
return {
1171
1174
id : choice . id ,
1172
1175
highlighted : choice . highlighted ,
@@ -1178,7 +1181,7 @@ class Choices {
1178
1181
label : choice . label ,
1179
1182
placeholder : choice . placeholder ,
1180
1183
value : choice . value ,
1181
- groupValue : group && group . label ? group . label : undefined ,
1184
+ groupValue : choice . group ? choice . group . label : undefined ,
1182
1185
element : choice . element ,
1183
1186
keyCode,
1184
1187
} ;
@@ -2131,7 +2134,7 @@ class Choices {
2131
2134
group . id = this . _lastAddedGroupId ;
2132
2135
2133
2136
group . choices . forEach ( ( item : ChoiceFull ) => {
2134
- item . groupId = group . id ;
2137
+ item . group = group ;
2135
2138
if ( group . disabled ) {
2136
2139
item . disabled = true ;
2137
2140
}
0 commit comments