Capgemini / dcx-react-library

React Library UI/UX agnostic
https://main--6069a6f47f4b9f002171f8e1.chromatic.com
MIT License
107 stars 7 forks source link

Select - when you pass the default value it doesn't allow to change on select #572

Closed daniele-zurico closed 7 months ago

daniele-zurico commented 7 months ago

If I pass the default value when I change the value from the drop-down list it stays on the default value:

function FormSelectDemo() {
  const [value, setValue] = React.useState('')
  const handleChange = event => {
    setValue(event.currentTarget.value);
  }
  return (
    <FormSelect
      id="select"
      disabled={false}
      value={value}
      defaultValue="option3"
      onChange={handleChange}
      options={['option1','option2', 'option3']}
    />
  );
}

Write the following test to check if your change is correct:

  it('should allow to change the default value on select', async () => {
    const user = userEvent.setup();
    render(
      <FormSelect options={['first', 'second', 'third']} defaultValue="third" />
    );
    await user.selectOptions(screen.getByRole('combobox'), 'first');
    const option: any = screen.getByRole('option', { name: 'first' });
    expect(option.selected).toBe(true);
  });

Follow this to create the new branch:

git checkout release/1.0.0
git pull
git checkout -b 'fix/select-default'
daniele-zurico commented 7 months ago

closed by #573