Skip to content

Commit

Permalink
fix: keep correct dropdown value on re-render #1154
Browse files Browse the repository at this point in the history
  • Loading branch information
aalencar committed Jan 4, 2022
1 parent 955c30d commit 1e63bbe
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions ui/src/dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import * as Fluent from '@fluentui/react'
import { B, Id, S, U } from 'h2o-wave'
import React from 'react'
import React, { useEffect, useRef } from 'react'
import { stylesheet } from 'typestyle'
import { Choice } from './choice_group'
import { fuzzysearch } from './parts/utils'
Expand Down Expand Up @@ -86,7 +86,9 @@ const
isMultivalued = !!values,
selection = React.useMemo(() => isMultivalued ? new Set<S>(values) : null, [isMultivalued, values]),
[selectedOptions, setSelectedOptions] = React.useState(Array.from(selection || [])),
options = (choices || []).map(({ name, label, disabled }): Fluent.IDropdownOption => ({ key: name, text: label || name, disabled })),
mapChoices = () => (choices || []).map(({ name, label, disabled }): Fluent.IDropdownOption => ({ key: name, text: label || name, disabled })),
choicesRef = useRef(mapChoices()),
options = choicesRef.current,
onChange = (_e?: React.FormEvent<HTMLElement>, option?: Fluent.IDropdownOption) => {
if (option) {
const optionKey = option.key as S
Expand Down Expand Up @@ -123,6 +125,13 @@ const

onChange()
}

useEffect(() => {
const newChoices = mapChoices()
if (JSON.stringify(options) !== JSON.stringify(newChoices)) {
choicesRef.current = newChoices
}
}, [options, mapChoices])

return (
<>
Expand Down

0 comments on commit 1e63bbe

Please sign in to comment.