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

feat: Add input onChange handler to ComboBox #1689

Merged
merged 3 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
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
36 changes: 36 additions & 0 deletions src/components/forms/ComboBox/ComboBox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,39 @@ export const externalClearSelection = (): React.ReactElement => {
</Form>
)
}

export const customInputChangeHandler = (): React.ReactElement => {
const fruitList = Object.entries(fruits).map(([value, key]) => ({
value: value,
label: key,
}))

const options = [...fruitList]

const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const { value } = e.target

if (value && fruitList.findIndex((f) => f.value === value) < 0) {
if (options.length === fruitList.length) {
// Add new option to end of list
options.push({ value, label: value })
} else {
// Rewrite the new option
options[options.length - 1] = { value, label: `Add new: ${value}` }
}
}
}

return (
<Form onSubmit={noop}>
<Label htmlFor="fruit">Select a Fruit</Label>
<ComboBox
id="fruit"
name="fruit"
options={options}
onChange={noop}
inputProps={{ onChange: handleInputChange }}
/>
</Form>
)
}
Loading