Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2909 from korhaliv/fix/open-dialog
Browse files Browse the repository at this point in the history
fix(select): correctly update select when underlying field changes
  • Loading branch information
mrfelton authored Oct 2, 2019
2 parents df2f43f + eaf7720 commit 26f3ab7
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions renderer/components/UI/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ const StyledInput = styled(BasicInput)`

const itemToString = item => (item ? item.value : '')

const getInitialSelectedItem = (items, initialSelectedItem) => {
return initialSelectedItem
const getSelectedItem = (items, key) => {
return key
? {
initialInputValue: itemToString(initialSelectedItem),
initialSelectedItem: items.find(i => i.key === initialSelectedItem),
inputValue: itemToString(key),
selectedItem: items.find(i => i.key === key),
}
: {}
}
Expand Down Expand Up @@ -129,22 +129,22 @@ const Select = props => {
} = props
const { setValue, setTouched } = fieldApi

const { initialInputValue, initialSelectedItem } = getInitialSelectedItem(
const { inputValue, selectedItem: initialSelectedItem } = getSelectedItem(
items,
initialSelectedItemOriginal || fieldState.value
)

return (
<Downshift
initialInputValue={initialInputValue}
initialInputValue={inputValue}
initialSelectedItem={initialSelectedItem}
itemToString={itemToString}
// When an item is selected, set the item in the Informed form state.
onInputValueChange={(inputValue, stateAndHelpers) => {
if (inputValue && inputValue !== itemToString(stateAndHelpers.selectedItem)) {
fieldApi.setValue(itemToString(stateAndHelpers.selectedItem))
}
}}
// When an item is selected, set the item in the Informed form state.
onSelect={item => {
if (!item) {
return
Expand All @@ -156,6 +156,7 @@ const Select = props => {
}
blurInput()
}}
selectedItem={getSelectedItem(items, fieldState.value).selectedItem}
>
{({
getInputProps,
Expand Down

0 comments on commit 26f3ab7

Please sign in to comment.