@@ -129,7 +129,6 @@ var highlightItem = function (item, highlighted) { return ({
129129 highlighted : highlighted ,
130130} ) ; } ;
131131
132- /* eslint-disable @typescript-eslint/no-explicit-any */
133132var getRandomNumber = function ( min , max ) { return Math . floor ( Math . random ( ) * ( max - min ) + min ) ; } ;
134133var generateChars = function ( length ) {
135134 return Array . from ( { length : length } , function ( ) { return getRandomNumber ( 0 , 36 ) . toString ( 36 ) ; } ) . join ( '' ) ;
@@ -262,6 +261,7 @@ var dispatchEvent = function (element, type, customArgs) {
262261/**
263262 * Returns an array of keys present on the first but missing on the second object
264263 */
264+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
265265var diff = function ( a , b ) {
266266 var aKeys = Object . keys ( a ) . sort ( ) ;
267267 var bKeys = Object . keys ( b ) . sort ( ) ;
@@ -767,7 +767,7 @@ var mapInputToChoice = function (value, allowGroup) {
767767 var choice = groupOrChoice ;
768768 var result = {
769769 id : 0 , // actual ID will be assigned during _addChoice
770- groupId : 0 , // actual ID will be assigned during _addGroup but before _addChoice
770+ group : null , // actual group will be assigned during _addGroup but before _addChoice
771771 score : 0 , // used in search
772772 rank : 0 , // used in search, stable sort order
773773 value : choice . value ,
@@ -844,7 +844,7 @@ var WrappedSelect = /** @class */ (function (_super) {
844844 }
845845 return {
846846 id : 0 ,
847- groupId : 0 ,
847+ group : null ,
848848 score : 0 ,
849849 rank : 0 ,
850850 value : option . value ,
@@ -1005,8 +1005,8 @@ function items(s, action, context) {
10051005 break ;
10061006 }
10071007 case ActionType . REMOVE_CHOICE : {
1008- state = state . filter ( function ( item ) { return item . id !== action . choice . id ; } ) ;
10091008 removeItem ( action . choice ) ;
1009+ state = state . filter ( function ( item ) { return item . id !== action . choice . id ; } ) ;
10101010 break ;
10111011 }
10121012 case ActionType . HIGHLIGHT_ITEM : {
@@ -1059,6 +1059,9 @@ function choices(s, action, context) {
10591059 }
10601060 case ActionType . REMOVE_CHOICE : {
10611061 action . choice . choiceEl = undefined ;
1062+ if ( action . choice . group ) {
1063+ action . choice . group . choices = action . choice . group . choices . filter ( function ( obj ) { return obj . id !== action . choice . id ; } ) ;
1064+ }
10621065 state = state . filter ( function ( obj ) { return obj . id !== action . choice . id ; } ) ;
10631066 break ;
10641067 }
@@ -3162,7 +3165,6 @@ var templates = {
31623165 label = escapeForTemplate ( allowHTML , label ) ;
31633166 label += " (" . concat ( groupName , ")" ) ;
31643167 label = { trusted : label } ;
3165- div . dataset . groupId = "" . concat ( choice . groupId ) ;
31663168 }
31673169 var describedBy = div ;
31683170 if ( choice . labelClass ) {
@@ -3190,13 +3192,16 @@ var templates = {
31903192 if ( choice . placeholder ) {
31913193 addClassesToElement ( div , placeholder ) ;
31923194 }
3193- div . setAttribute ( 'role' , choice . groupId ? 'treeitem' : 'option' ) ;
3195+ div . setAttribute ( 'role' , choice . group ? 'treeitem' : 'option' ) ;
31943196 div . dataset . choice = '' ;
31953197 div . dataset . id = choice . id ;
31963198 div . dataset . value = rawValue ;
31973199 if ( selectText ) {
31983200 div . dataset . selectText = selectText ;
31993201 }
3202+ if ( choice . group ) {
3203+ div . dataset . groupId = "" . concat ( choice . group . id ) ;
3204+ }
32003205 assignCustomProperties ( div , choice , false ) ;
32013206 if ( choice . disabled ) {
32023207 addClassesToElement ( div , itemDisabled ) ;
@@ -3631,12 +3636,9 @@ var Choices = /** @class */ (function () {
36313636 } ;
36323637 Choices . prototype . getValue = function ( valueOnly ) {
36333638 var _this = this ;
3634- if ( valueOnly === void 0 ) { valueOnly = false ; }
3635- var values = this . _store . items . reduce ( function ( selectedItems , item ) {
3636- var itemValue = valueOnly ? item . value : _this . _getChoiceForOutput ( item ) ;
3637- selectedItems . push ( itemValue ) ;
3638- return selectedItems ;
3639- } , [ ] ) ;
3639+ var values = this . _store . items . map ( function ( item ) {
3640+ return ( valueOnly ? item . value : _this . _getChoiceForOutput ( item ) ) ;
3641+ } ) ;
36403642 return this . _isSelectOneElement || this . config . singleModeForMultiSelect ? values [ 0 ] : values ;
36413643 } ;
36423644 Choices . prototype . setValue = function ( items ) {
@@ -3840,17 +3842,20 @@ var Choices = /** @class */ (function () {
38403842 } ) ;
38413843 }
38423844 _this . clearStore ( false ) ;
3843- choicesFromOptions . forEach ( function ( groupOrChoice ) {
3844- if ( 'choices' in groupOrChoice ) {
3845- return ;
3846- }
3847- var choice = groupOrChoice ;
3845+ var updateChoice = function ( choice ) {
38483846 if ( deselectAll ) {
38493847 _this . _store . dispatch ( removeItem$1 ( choice ) ) ;
38503848 }
38513849 else if ( existingItems [ choice . value ] ) {
38523850 choice . selected = true ;
38533851 }
3852+ } ;
3853+ choicesFromOptions . forEach ( function ( groupOrChoice ) {
3854+ if ( 'choices' in groupOrChoice ) {
3855+ groupOrChoice . choices . forEach ( updateChoice ) ;
3856+ return ;
3857+ }
3858+ updateChoice ( groupOrChoice ) ;
38543859 } ) ;
38553860 /* @todo only generate add events for the added options instead of all
38563861 if (withEvents) {
@@ -4006,13 +4011,16 @@ var Choices = /** @class */ (function () {
40064011 }
40074012 if ( ! this . _hasNonChoicePlaceholder && ! isSearching && this . _isSelectOneElement ) {
40084013 // If we have a placeholder choice along with groups
4009- renderChoices ( activeChoices . filter ( function ( choice ) { return choice . placeholder && ! choice . groupId ; } ) , false , undefined ) ;
4014+ renderChoices ( activeChoices . filter ( function ( choice ) { return choice . placeholder && ! choice . group ; } ) , false , undefined ) ;
40104015 }
40114016 // If we have grouped options
40124017 if ( activeGroups . length && ! isSearching ) {
40134018 if ( config . shouldSort ) {
40144019 activeGroups . sort ( config . sorter ) ;
40154020 }
4021+ // render Choices without group first, regardless of sort, otherwise they won't be distinguishable
4022+ // from the last group
4023+ renderChoices ( activeChoices . filter ( function ( choice ) { return ! choice . placeholder && ! choice . group ; } ) , false , undefined ) ;
40164024 activeGroups . forEach ( function ( group ) {
40174025 var groupChoices = renderableChoices ( group . choices ) ;
40184026 if ( groupChoices . length ) {
@@ -4153,11 +4161,8 @@ var Choices = /** @class */ (function () {
41534161 }
41544162 }
41554163 } ;
4164+ // eslint-disable-next-line class-methods-use-this
41564165 Choices . prototype . _getChoiceForOutput = function ( choice , keyCode ) {
4157- if ( ! choice ) {
4158- return undefined ;
4159- }
4160- var group = choice . groupId ? this . _store . getGroupById ( choice . groupId ) : null ;
41614166 return {
41624167 id : choice . id ,
41634168 highlighted : choice . highlighted ,
@@ -4169,7 +4174,7 @@ var Choices = /** @class */ (function () {
41694174 label : choice . label ,
41704175 placeholder : choice . placeholder ,
41714176 value : choice . value ,
4172- groupValue : group && group . label ? group . label : undefined ,
4177+ groupValue : choice . group ? choice . group . label : undefined ,
41734178 element : choice . element ,
41744179 keyCode : keyCode ,
41754180 } ;
@@ -4188,7 +4193,7 @@ var Choices = /** @class */ (function () {
41884193 if ( ! items . length || ! this . config . removeItems || ! this . config . removeItemButton ) {
41894194 return ;
41904195 }
4191- var id = element && parseDataSetId ( element . parentNode ) ;
4196+ var id = element && parseDataSetId ( element . parentElement ) ;
41924197 var itemToRemove = id && items . find ( function ( item ) { return item . id === id ; } ) ;
41934198 if ( ! itemToRemove ) {
41944199 return ;
@@ -4994,7 +4999,7 @@ var Choices = /** @class */ (function () {
49944999 this . _lastAddedGroupId ++ ;
49955000 group . id = this . _lastAddedGroupId ;
49965001 group . choices . forEach ( function ( item ) {
4997- item . groupId = group . id ;
5002+ item . group = group ;
49985003 if ( group . disabled ) {
49995004 item . disabled = true ;
50005005 }
0 commit comments