Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #305 from uktrade/fix/select-option
Browse files Browse the repository at this point in the history
fix: correctly select a <select> option
  • Loading branch information
rafenden authored Jun 19, 2020
2 parents 922838b + d98b191 commit 793fc9e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 42 deletions.
19 changes: 7 additions & 12 deletions src/forms/elements/FieldSelect.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const FieldSelect = ({
meta={{ error, touched }}
input={{
id: name,
defaultValue: value,
value,
readOnly: true,
...rest,
}}
>
Expand All @@ -41,17 +42,11 @@ const FieldSelect = ({
{emptyOption}
</option>
)}
{options.map(({ label: optionLabel, value: optionValue }) => {
const additionalProps = {}
if (value === optionValue) {
additionalProps.selected = 'selected'
}
return (
<option key={optionValue} value={optionValue} {...additionalProps}>
{optionLabel}
</option>
)
})}
{options.map(({ label: optionLabel, value: optionValue }) => (
<option key={optionValue} value={optionValue}>
{optionLabel}
</option>
))}
</Select>
{options.find((o) => o.value === value)?.children}
</FieldWrapper>
Expand Down
3 changes: 2 additions & 1 deletion src/forms/elements/__stories__/FieldSelect.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ storiesOf('Forms', module).add('FieldSelect', () => (
name="testField"
label="Test select"
hint="Some hint"
initialValue={text('Initial value', 'testOptionValue2')}
emptyOption={text('emptyOption', 'Please select')}
options={object('options', [
{ label: 'testOptionLabel1', value: 'testOptionValue1' },
Expand All @@ -32,7 +33,7 @@ storiesOf('Forms', module).add('FieldSelect', () => (
/>

<FieldSelect
name="testField"
name="testField2"
label="Test select with children"
hint="Some hint"
emptyOption={text('emptyOption', 'Please select')}
Expand Down
35 changes: 6 additions & 29 deletions src/forms/elements/__tests__/FieldSelect.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,31 +113,6 @@ describe('FieldSelect', () => {
})
})

describe('when the field specifies a preselected option', () => {
let options

beforeAll(() => {
wrapper = mount(
<FormStateful>
<FieldSelect
name="testField"
initialValue="testOptionValue2"
options={[
{ label: 'testOptionLabel1', value: 'testOptionValue1' },
{ label: 'testOptionLabel2', value: 'testOptionValue2' },
{ label: 'testOptionLabel3', value: 'testOptionValue3' },
]}
/>
</FormStateful>
)
options = wrapper.find('option')
})

test('should preselect the second option', () => {
expect(options.at(2).prop('selected')).toEqual('selected')
})
})

describe('when the field validation fails', () => {
beforeAll(() => {
wrapper = mount(
Expand Down Expand Up @@ -200,10 +175,12 @@ describe('FieldSelect', () => {
.simulate('change', { target: { value: 'testOptionValue2' } })
})

test('should update field value', () => {
expect(wrapper.find('select').prop('defaultValue')).toEqual(
'testOptionValue2'
)
test('should update the field value', () => {
expect(wrapper.find('select').prop('value')).toEqual('testOptionValue2')
})

test('should set the readonly flag', () => {
expect(wrapper.find('select').prop('readOnly')).toEqual(true)
})

test('should update value in form state', () => {
Expand Down

0 comments on commit 793fc9e

Please sign in to comment.