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

[Bugfix] Dropdown: ensure reactivity of components 'selectedItem' prop #1111

Merged
merged 9 commits into from
Jun 25, 2024
11 changes: 9 additions & 2 deletions src/components/Dropdown/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ const Dropdown = ({

const [isOpen, setOpen] = useState(open)
const [availableOptions, filterAvailableOptions] = useState(items || [])
const [selectedOption, selectItem] = useState(selectedItem || { title: '', id: '' })
const [activeOption, setActiveOption] = useState(selectedItem || { title: '', id: '' })
const DEFAULT_ITEM = { title: '', id: '' }
const [selectedOption, selectItem] = useState(selectedItem || DEFAULT_ITEM)
const [activeOption, setActiveOption] = useState(selectedItem || DEFAULT_ITEM)
const [inputFieldValue, updateInputValue] = useState('')
const [keyNavPosition, setKeyNavPosition] = useState(0)

const dropdownId = id || useId()

// ensure reactivity of 'selectedItem' prop is preserved
useEffect(() => {
selectItem(selectedItem || DEFAULT_ITEM)
setActiveOption(selectedItem || DEFAULT_ITEM)
}, [selectedItem])

const filterOptions = (value) => {
updateInputValue(value)
filterAvailableOptions(items.filter((it) => it.title.toLowerCase().includes(value.toLowerCase())))
Expand Down