Skip to content
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
8 changes: 5 additions & 3 deletions src/formSelect/FormSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,17 @@ export const FormSelect = ({
style,
nullOption,
containerProps,
defaultValue = '',
defaultValue,
tabIndex = 0,
variant = 'normal',
disabled = false,
selectProps,
}: FormSelectProps) => {
let initialValue: string | number = '';

if (value !== undefined) {
if (defaultValue !== undefined) {
initialValue = defaultValue;
} else if (value !== undefined) {
initialValue = value;
} else if (nullOption !== undefined) {
initialValue = nullOption;
Expand Down Expand Up @@ -234,7 +236,7 @@ export const FormSelect = ({
/>
)}
<select
value={defaultValue !== '' ? defaultValue : selectValue}
value={selectValue}
name={name || 'formSelect'}
id={id || 'formSelect'}
className={selectClassName}
Expand Down
10 changes: 10 additions & 0 deletions src/formSelect/__test__/FormSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,16 @@ describe('FormSelect', () => {
expect(option.selected).toBe(true);
});

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);
});

it('should read the containerProps', () => {
render(
<FormSelect
Expand Down
7 changes: 4 additions & 3 deletions stories/liveEdit/FormSelectLive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@ import { FormSelect } from '../../src/formSelect/FormSelect';
const FormSelectDemo = `
function FormSelectDemo() {
const [value, setValue] = React.useState('')

const handleChange = event => {
setValue(event.currentTarget.value);
}

return (
<FormSelect
id="select"
nullOption="Select..."
disabled={false}
selectProps={{}}
value=""
value={value}
defaultValue="option3"
label="Basic"
labelProps={{
style: {
Expand All @@ -40,7 +43,6 @@ function FormSelectDemo() {
labelClassName=""
containerClassName=""
style={{}}

ariaLabel=""
labelProps={{}}
hint={{
Expand All @@ -56,7 +58,6 @@ function FormSelectDemo() {
className: "",
}}
errorMessageId=""
defaultValue=""
tabIndex={0}
containerErrorClassName="containerErrorClassName"
containerFilledClassName="containerFilledClassName"
Expand Down