Skip to content

Commit 4e07f9a

Browse files
committed
fix(ui-simple-select): fix selection getting lost after options have changed
Closes: INSTUI-4402 This issue came up when the Option's value and children differed. TEST PLAN: make a SimpleSelect where the Option's value and children are not the same. Select a child and remove a different element from the child list. Selection should stay
1 parent e1142e4 commit 4e07f9a

File tree

1 file changed

+6
-3
lines changed
  • packages/ui-simple-select/src/SimpleSelect

1 file changed

+6
-3
lines changed

packages/ui-simple-select/src/SimpleSelect/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import { allowedProps, propTypes, SimpleSelectState } from './props'
5151
type OptionChild = React.ComponentElement<SimpleSelectOptionProps, Option>
5252
type GroupChild = React.ComponentElement<SimpleSelectGroupProps, Group>
5353

54-
type GetOption = <F extends 'id' | 'value'>(
54+
type GetOption = <F extends keyof SimpleSelectOptionProps>(
5555
field: F,
5656
value?: SimpleSelectOptionProps[F]
5757
) => OptionChild | undefined
@@ -150,14 +150,17 @@ class SimpleSelect extends Component<SimpleSelectProps, SimpleSelectState> {
150150

151151
componentDidUpdate(prevProps: SimpleSelectProps) {
152152
if (this.hasOptionsChanged(prevProps.children, this.props.children)) {
153-
const option = this.getOption('value', this.state.inputValue)
153+
// Compare current input value to children's child prop, this is put into
154+
// state.inputValue
155+
const option = this.getOption('children', this.state.inputValue)
154156
this.setState({
155157
inputValue: option ? option.props.children : undefined,
156158
selectedOptionId: option ? option.props.id : ''
157159
})
158160
}
159-
160161
if (this.props.value !== prevProps.value) {
162+
// if value has changed externally try to find an option with the same value
163+
// and select it
161164
let option = this.getOption('value', this.props.value)
162165
if (typeof this.props.value === 'undefined') {
163166
// preserve current value when changing from controlled to uncontrolled

0 commit comments

Comments
 (0)