1- /* eslint-disable @typescript-eslint/no-explicit-any */
21import { activateChoices , addChoice , removeChoice , filterChoices } from './actions/choices' ;
32import { addGroup } from './actions/groups' ;
43import { addItem , highlightItem , removeItem } from './actions/items' ;
@@ -28,7 +27,7 @@ import Store from './store/store';
2827import { coerceBool , mapInputToChoice } from './lib/choice-input' ;
2928import { ChoiceFull } from './interfaces/choice-full' ;
3029import { GroupFull } from './interfaces/group-full' ;
31- import { EventType , KeyCodeMap , PassedElementType , PassedElementTypes } from './interfaces' ;
30+ import { EventChoiceValueType , EventType , KeyCodeMap , PassedElementType , PassedElementTypes } from './interfaces' ;
3231import { EventChoice } from './interfaces/event-choice' ;
3332import { NoticeType , NoticeTypes , Templates } from './interfaces/templates' ;
3433import { isHtmlInputElement , isHtmlSelectElement } from './lib/html-guard-statements' ;
@@ -539,13 +538,10 @@ class Choices {
539538 return this ;
540539 }
541540
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+ } ) ;
549545
550546 return this . _isSelectOneElement || this . config . singleModeForMultiSelect ? values [ 0 ] : values ;
551547 }
@@ -788,16 +784,21 @@ class Choices {
788784
789785 this . clearStore ( false ) ;
790786
791- choicesFromOptions . forEach ( ( groupOrChoice ) => {
792- if ( 'choices' in groupOrChoice ) {
793- return ;
794- }
795- const choice = groupOrChoice ;
787+ const updateChoice = ( choice : ChoiceFull ) : void => {
796788 if ( deselectAll ) {
797789 this . _store . dispatch ( removeItem ( choice ) ) ;
798790 } else if ( existingItems [ choice . value ] ) {
799791 choice . selected = true ;
800792 }
793+ } ;
794+
795+ choicesFromOptions . forEach ( ( groupOrChoice ) => {
796+ if ( 'choices' in groupOrChoice ) {
797+ groupOrChoice . choices . forEach ( updateChoice ) ;
798+
799+ return ;
800+ }
801+ updateChoice ( groupOrChoice ) ;
801802 } ) ;
802803
803804 /* @todo only generate add events for the added options instead of all
@@ -984,7 +985,7 @@ class Choices {
984985 if ( ! this . _hasNonChoicePlaceholder && ! isSearching && this . _isSelectOneElement ) {
985986 // If we have a placeholder choice along with groups
986987 renderChoices (
987- activeChoices . filter ( ( choice ) => choice . placeholder && ! choice . groupId ) ,
988+ activeChoices . filter ( ( choice ) => choice . placeholder && ! choice . group ) ,
988989 false ,
989990 undefined ,
990991 ) ;
@@ -995,6 +996,13 @@ class Choices {
995996 if ( config . shouldSort ) {
996997 activeGroups . sort ( config . sorter ) ;
997998 }
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+ ) ;
9981006
9991007 activeGroups . forEach ( ( group ) => {
10001008 const groupChoices = renderableChoices ( group . choices ) ;
@@ -1160,13 +1168,8 @@ class Choices {
11601168 }
11611169 }
11621170
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 {
11701173 return {
11711174 id : choice . id ,
11721175 highlighted : choice . highlighted ,
@@ -1178,7 +1181,7 @@ class Choices {
11781181 label : choice . label ,
11791182 placeholder : choice . placeholder ,
11801183 value : choice . value ,
1181- groupValue : group && group . label ? group . label : undefined ,
1184+ groupValue : choice . group ? choice . group . label : undefined ,
11821185 element : choice . element ,
11831186 keyCode,
11841187 } ;
@@ -2131,7 +2134,7 @@ class Choices {
21312134 group . id = this . _lastAddedGroupId ;
21322135
21332136 group . choices . forEach ( ( item : ChoiceFull ) => {
2134- item . groupId = group . id ;
2137+ item . group = group ;
21352138 if ( group . disabled ) {
21362139 item . disabled = true ;
21372140 }
0 commit comments