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 1 commit
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>
)
}
20 changes: 19 additions & 1 deletion src/components/forms/ComboBox/ComboBox.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,25 @@ describe('ComboBox component', () => {
expect(comboBoxInput).toHaveAttribute('role', 'testing')
})

it('renders input with custom props if passed in', () => {
it('allows a custom input onChange handler to be called', () => {
const mockOnInputChange = jest.fn()

const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
name="favorite-fruit"
options={fruitOptions}
onChange={jest.fn()}
inputProps={{ onChange: mockOnInputChange }}
/>
)

const input = getByTestId('combo-box-input')
userEvent.type(input, 'x')
expect(mockOnInputChange).toHaveBeenCalled()
})

it('renders list with custom props if passed in', () => {
const { getByTestId } = render(
<ComboBox
id="favorite-fruit"
Expand Down
23 changes: 14 additions & 9 deletions src/components/forms/ComboBox/ComboBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ const Input = ({
return (
<input
type="text"
{...inputProps}
className="usa-combo-box__input"
data-testid="combo-box-input"
{...inputProps}
autoCapitalize="off"
autoComplete="off"
ref={inputRef}
Expand Down Expand Up @@ -349,35 +349,40 @@ export const ComboBox = forwardRef(
id={id}
ref={containerRef}>
<select
{...selectProps}
className="usa-select usa-sr-only usa-combo-box__select"
name={name}
aria-hidden
tabIndex={-1}
defaultValue={state.selectedOption?.value}
data-testid="combo-box-select"
disabled={isDisabled}
{...selectProps}>
disabled={isDisabled}>
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
<Input
onChange={(e): void =>
role="combobox"
{...inputProps}
onChange={(e): void => {
if (inputProps?.onChange) {
// Allow a custom input onChange handler
inputProps?.onChange(e)
}

dispatch({ type: ActionTypes.UPDATE_FILTER, value: e.target.value })
}
}}
onClick={(): void => dispatch({ type: ActionTypes.OPEN_LIST })}
onBlur={handleInputBlur}
onKeyDown={handleInputKeyDown}
value={state.inputValue}
focused={state.focusMode === FocusMode.Input}
role="combobox"
aria-owns={listID}
aria-describedby={assistiveHintID}
aria-expanded={state.isOpen}
disabled={isDisabled}
{...inputProps}
/>
<span className="usa-combo-box__clear-input__wrapper" tabIndex={-1}>
<button
Expand Down Expand Up @@ -411,13 +416,13 @@ export const ComboBox = forwardRef(
</button>
</span>
<ul
{...ulProps}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also moved the custom props for these elements up, to avoid mistakenly overwriting required props

data-testid="combo-box-option-list"
tabIndex={-1}
id={listID}
className="usa-combo-box__list"
role="listbox"
hidden={!state.isOpen}
{...ulProps}>
hidden={!state.isOpen}>
{state.filteredOptions.map((option, index) => {
const focused = option === state.focusedOption
const selected = option === state.selectedOption
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export { Grid } from './components/grid/Grid/Grid'
/** Form components */
export { CharacterCount } from './components/forms/CharacterCount/CharacterCount'
export { Checkbox } from './components/forms/Checkbox/Checkbox'
export { ComboBox } from './components/forms/ComboBox/ComboBox'
export { ComboBox, ComboBoxOption } from './components/forms/ComboBox/ComboBox'
Copy link
Contributor

@brandonlenz brandonlenz Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding here is that ComboBoxOption used mostly for internal type safety, but now with the onChange handler, we expose it in case someone wants to use that componentinterface with their handler?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was actually unrelated to the onChange addition! because the options passed into a ComboBox component have to match this type, I found it helpful to export this type as well to reference in the consumer app.

export { DateInput } from './components/forms/DateInput/DateInput'
export { DateInputGroup } from './components/forms/DateInputGroup/DateInputGroup'
export { DatePicker } from './components/forms/DatePicker/DatePicker'
Expand Down