@@ -727,15 +727,9 @@ class Choices {
727
727
if ( clearSearchFlag ) {
728
728
this . _isSearching = false ;
729
729
}
730
- const items = { } ;
731
- if ( ! replaceItems ) {
732
- this . _store . items . forEach ( ( item ) => {
733
- items [ item . value ] = item ;
734
- } ) ;
735
- }
736
730
// Clear choices if needed
737
731
if ( replaceChoices ) {
738
- this . clearChoices ( ) ;
732
+ this . clearChoices ( true , replaceItems ) ;
739
733
}
740
734
const isDefaultValue = value === 'value' ;
741
735
const isDefaultLabel = label === 'label' ;
@@ -761,9 +755,6 @@ class Choices {
761
755
} as InputChoice ;
762
756
}
763
757
const choiceFull = mapInputToChoice < InputChoice > ( choice , false ) ;
764
- if ( ! replaceItems && choiceFull . value in items ) {
765
- choiceFull . selected = true ;
766
- }
767
758
this . _addChoice ( choiceFull ) ;
768
759
if ( choiceFull . placeholder && ! this . _hasNonChoicePlaceholder ) {
769
760
this . _placeholderValue = unwrapStringForEscaped ( choiceFull . label ) ;
@@ -863,23 +854,35 @@ class Choices {
863
854
return this ;
864
855
}
865
856
866
- clearChoices ( clearOptions : boolean = true ) : this {
857
+ clearChoices ( clearOptions : boolean = true , clearItems : boolean = false ) : this {
867
858
if ( clearOptions ) {
868
- this . passedElement . element . replaceChildren ( '' ) ;
859
+ if ( clearItems ) {
860
+ this . passedElement . element . replaceChildren ( '' ) ;
861
+ } else {
862
+ this . passedElement . element . querySelectorAll ( ':not([selected])' ) . forEach ( ( el ) : void => {
863
+ el . remove ( ) ;
864
+ } ) ;
865
+ }
869
866
}
870
867
this . itemList . element . replaceChildren ( '' ) ;
871
868
this . choiceList . element . replaceChildren ( '' ) ;
872
869
this . _clearNotice ( ) ;
873
- this . _store . reset ( ) ;
874
-
870
+ this . _store . withTxn ( ( ) => {
871
+ const items = clearItems ? [ ] : this . _store . items ;
872
+ this . _store . reset ( ) ;
873
+ items . forEach ( ( item : ChoiceFull ) : void => {
874
+ this . _store . dispatch ( addChoice ( item ) ) ;
875
+ this . _store . dispatch ( addItem ( item ) ) ;
876
+ } ) ;
877
+ } ) ;
875
878
// @todo integrate with Store
876
879
this . _searcher . reset ( ) ;
877
880
878
881
return this ;
879
882
}
880
883
881
884
clearStore ( clearOptions : boolean = true ) : this {
882
- this . clearChoices ( clearOptions ) ;
885
+ this . clearChoices ( clearOptions , true ) ;
883
886
this . _stopSearch ( ) ;
884
887
this . _lastAddedChoiceId = 0 ;
885
888
this . _lastAddedGroupId = 0 ;
0 commit comments