-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
299 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* eslint-disable testing-library/no-node-access */ | ||
import React from 'react'; | ||
|
||
import userEvent from '@testing-library/user-event'; | ||
|
||
import {render, screen, within} from '../../../../test-utils/utils'; | ||
import {Select} from '../Select'; | ||
|
||
describe('Select form', () => { | ||
it('should submit empty option by default', async () => { | ||
let value; | ||
const onSubmit = jest.fn((e) => { | ||
e.preventDefault(); | ||
const formData = new FormData(e.currentTarget); | ||
value = formData.getAll('select'); | ||
}); | ||
render( | ||
<form data-qa="form" onSubmit={onSubmit}> | ||
<Select name="select" label="Test"> | ||
<Select.Option value="one">One</Select.Option> | ||
<Select.Option value="two">Two</Select.Option> | ||
<Select.Option value="three">Three</Select.Option> | ||
</Select> | ||
<button type="submit" data-qa="submit"> | ||
submit | ||
</button> | ||
</form>, | ||
); | ||
await userEvent.click(screen.getByTestId('submit')); | ||
expect(onSubmit).toHaveBeenCalledTimes(1); | ||
expect(value).toEqual(['']); | ||
}); | ||
|
||
it('should submit default option', async () => { | ||
let value; | ||
const onSubmit = jest.fn((e) => { | ||
e.preventDefault(); | ||
const formData = new FormData(e.currentTarget); | ||
value = formData.getAll('select'); | ||
}); | ||
render( | ||
<form data-qa="form" onSubmit={onSubmit}> | ||
<Select defaultValue={['one']} name="select"> | ||
<Select.Option value="one">One</Select.Option> | ||
<Select.Option value="two">Two</Select.Option> | ||
<Select.Option value="three">Three</Select.Option> | ||
</Select> | ||
<button type="submit" data-qa="submit"> | ||
submit | ||
</button> | ||
</form>, | ||
); | ||
await userEvent.click(screen.getByTestId('submit')); | ||
expect(onSubmit).toHaveBeenCalledTimes(1); | ||
expect(value).toEqual(['one']); | ||
}); | ||
|
||
it('should submit multiple option', async () => { | ||
let value; | ||
const onSubmit = jest.fn((e) => { | ||
e.preventDefault(); | ||
const formData = new FormData(e.currentTarget); | ||
value = formData.getAll('select'); | ||
}); | ||
render( | ||
<form data-qa="form" onSubmit={onSubmit}> | ||
<Select defaultValue={['one', 'three']} name="select" multiple> | ||
<Select.Option value="one">One</Select.Option> | ||
<Select.Option value="two">Two</Select.Option> | ||
<Select.Option value="three">Three</Select.Option> | ||
</Select> | ||
<button type="submit" data-qa="submit"> | ||
submit | ||
</button> | ||
</form>, | ||
); | ||
await userEvent.click(screen.getByTestId('submit')); | ||
expect(onSubmit).toHaveBeenCalledTimes(1); | ||
expect(value).toEqual(['one', 'three']); | ||
}); | ||
|
||
it('supports form reset', async () => { | ||
function Test() { | ||
const [value, setValue] = React.useState(['one']); | ||
return ( | ||
<form> | ||
<Select name="select" value={value} onUpdate={setValue} qa="select"> | ||
<Select.Option value="one">One</Select.Option> | ||
<Select.Option value="two">Two</Select.Option> | ||
<Select.Option value="three">Three</Select.Option> | ||
</Select> | ||
<input type="reset" data-qa="reset" /> | ||
</form> | ||
); | ||
} | ||
|
||
render(<Test />); | ||
const select = screen.getByTestId('select'); | ||
let inputs = document.querySelectorAll('[name=select]'); | ||
expect(inputs.length).toBe(1); | ||
expect(inputs[0]).toHaveValue('one'); | ||
|
||
await userEvent.click(select); | ||
|
||
const listbox = screen.getByRole('listbox'); | ||
const items = within(listbox).getAllByRole('option'); | ||
expect(items.length).toBe(3); | ||
|
||
await userEvent.click(items[1]); | ||
inputs = document.querySelectorAll('[name=select]'); | ||
expect(inputs.length).toBe(1); | ||
expect(inputs[0]).toHaveValue('two'); | ||
|
||
const button = screen.getByTestId('reset'); | ||
await userEvent.click(button); | ||
inputs = document.querySelectorAll('[name=select]'); | ||
expect(inputs.length).toBe(1); | ||
expect(inputs[0]).toHaveValue('one'); | ||
}); | ||
}); |
53 changes: 53 additions & 0 deletions
53
src/components/Select/components/HiddenSelect/HiddenSelect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
import {useFormResetHandler} from '../../../../hooks/private'; | ||
|
||
interface HiddenSelectProps { | ||
name?: string; | ||
value: string[]; | ||
disabled?: boolean; | ||
form?: string; | ||
onReset: (value: string[]) => void; | ||
} | ||
//FIXME: current implementation is not accessible to screen readers and does not support browser autofill and | ||
// form validation | ||
export function HiddenSelect(props: HiddenSelectProps) { | ||
const {name, value, disabled, form, onReset} = props; | ||
|
||
const ref = useFormResetHandler({onReset, initialValue: value}); | ||
|
||
if (!name || disabled) { | ||
return null; | ||
} | ||
|
||
if (value.length === 0) { | ||
return ( | ||
<input | ||
ref={ref} | ||
type="hidden" | ||
name={name} | ||
value={value} | ||
form={form} | ||
disabled={disabled} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<React.Fragment> | ||
{value.map((v, i) => ( | ||
<input | ||
key={v} | ||
ref={i === 0 ? ref : undefined} | ||
value={v} | ||
type="hidden" | ||
name={name} | ||
form={form} | ||
disabled={disabled} | ||
/> | ||
))} | ||
</React.Fragment> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.