Skip to content
Permalink
Browse files

fix(select): correctly update select when underlying field changes

  • Loading branch information...
korhaliv committed Sep 28, 2019
1 parent df2f43f commit eaf7720e9da9280a67f38bcb5357ab00aaf95130
Showing with 8 additions and 7 deletions.
  1. +8 −7 renderer/components/UI/Select.js
@@ -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),
}
: {}
}
@@ -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
@@ -156,6 +156,7 @@ const Select = props => {
}
blurInput()
}}
selectedItem={getSelectedItem(items, fieldState.value).selectedItem}
>
{({
getInputProps,

0 comments on commit eaf7720

Please sign in to comment.
You can’t perform that action at this time.