@@ -187,10 +187,15 @@ export interface GenerateConfig<OptionsType extends object[]> {
187
187
/** Convert single raw value into { label, value } format. Will be called by each value */
188
188
getLabeledValue : GetLabeledValue < FlattenOptionsType < OptionsType > > ;
189
189
filterOptions : FilterOptions < OptionsType > ;
190
- findValueOption : (
191
- values : RawValueType [ ] ,
192
- options : FlattenOptionsType < OptionsType > ,
193
- ) => OptionsType ;
190
+ findValueOption :
191
+ | ( // Need still support legacy ts api
192
+ ( values : RawValueType [ ] , options : FlattenOptionsType < OptionsType > ) => OptionsType )
193
+ | ( // New API add prevValueOptions support
194
+ (
195
+ values : RawValueType [ ] ,
196
+ options : FlattenOptionsType < OptionsType > ,
197
+ info ?: { prevValueOptions ?: OptionsType [ ] } ,
198
+ ) => OptionsType ) ;
194
199
/** Check if a value is disabled */
195
200
isValueDisabled : ( value : RawValueType , options : FlattenOptionsType < OptionsType > ) => boolean ;
196
201
warningProps ?: ( props : any ) => void ;
@@ -515,6 +520,9 @@ export default function generateSelector<
515
520
}
516
521
} ;
517
522
523
+ // We need cache options here in case user update the option list
524
+ const [ prevValueOptions , setPrevValueOptions ] = useState ( [ ] ) ;
525
+
518
526
const triggerChange = ( newRawValues : RawValueType [ ] ) => {
519
527
if ( useInternalProps && internalProps . skipTriggerChange ) {
520
528
return ;
@@ -531,7 +539,18 @@ export default function generateSelector<
531
539
const outValue : ValueType = ( isMultiple ? outValues : outValues [ 0 ] ) as ValueType ;
532
540
// Skip trigger if prev & current value is both empty
533
541
if ( onChange && ( mergedRawValue . length !== 0 || outValues . length !== 0 ) ) {
534
- const outOptions = findValueOption ( newRawValues , newRawValuesOptions ) ;
542
+ const outOptions = findValueOption ( newRawValues , newRawValuesOptions , { prevValueOptions } ) ;
543
+
544
+ // We will cache option in case it removed by ajax
545
+ setPrevValueOptions (
546
+ outOptions . map ( ( option , index ) => {
547
+ const clone = { ...option } ;
548
+ Object . defineProperty ( clone , '_INTERNAL_OPTION_VALUE_' , {
549
+ get : ( ) => newRawValues [ index ] ,
550
+ } ) ;
551
+ return clone ;
552
+ } ) ,
553
+ ) ;
535
554
536
555
onChange ( outValue , isMultiple ? outOptions : outOptions [ 0 ] ) ;
537
556
}
0 commit comments