Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui-simple-select): fix selection getting lost after options have changed #1821

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions packages/ui-simple-select/src/SimpleSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { allowedProps, propTypes, SimpleSelectState } from './props'
type OptionChild = React.ComponentElement<SimpleSelectOptionProps, Option>
type GroupChild = React.ComponentElement<SimpleSelectGroupProps, Group>

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

componentDidUpdate(prevProps: SimpleSelectProps) {
if (this.hasOptionsChanged(prevProps.children, this.props.children)) {
const option = this.getOption('value', this.state.inputValue)
// Compare current input value to children's child prop, this is put into
// state.inputValue
const option = this.getOption('children', this.state.inputValue)
this.setState({
inputValue: option ? option.props.children : undefined,
selectedOptionId: option ? option.props.id : ''
})
}

if (this.props.value !== prevProps.value) {
// if value has changed externally try to find an option with the same value
// and select it
let option = this.getOption('value', this.props.value)
if (typeof this.props.value === 'undefined') {
// preserve current value when changing from controlled to uncontrolled
Expand Down
Loading