Skip to content

Commit

Permalink
do not set both value and defaultValue on select (#504)
Browse files Browse the repository at this point in the history
* do not set both value and defaultValue on select

* add PR link to changelog
  • Loading branch information
nreese authored Mar 12, 2018
1 parent 749024c commit e6b0b22
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

**Bug fixes**

- `EuiSelect` do not set `defaultValue` property when `value` property is provided ([#504](https://github.com/elastic/eui/pull/504)).
- `EuiBottomBar` now uses `EuiPortal` to avoid zindex conflicts ([#487](https://github.com/elastic/eui/pull/487))
- Upped dark theme contrast on disabled buttons ([#487](https://github.com/elastic/eui/pull/487))

Expand Down
27 changes: 27 additions & 0 deletions src/components/form/select/__snapshots__/select.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,30 @@ exports[`EuiSelect props options are rendered 1`] = `
</eui-validatable-control>
</eui-form-control-layout>
`;

exports[`EuiSelect props value option is rendered 1`] = `
<eui-form-control-layout
fullwidth="false"
icon="arrowDown"
iconside="right"
isloading="false"
>
<eui-validatable-control>
<select
class="euiSelect"
>
<option
selected=""
value="1"
>
Option #1
</option>
<option
value="2"
>
Option #2
</option>
</select>
</eui-validatable-control>
</eui-form-control-layout>
`;
11 changes: 10 additions & 1 deletion src/components/form/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const EuiSelect = ({
isLoading,
hasNoInitialSelection,
defaultValue,
value,
...rest
}) => {
const classes = classNames(
Expand All @@ -39,6 +40,13 @@ export const EuiSelect = ({
);
}

// React HTML input can not have both value and defaultValue properties.
// https://reactjs.org/docs/uncontrolled-components.html#default-values
let selectDefaultValue;
if (!value) {
selectDefaultValue = defaultValue || '';
}

return (
<EuiFormControlLayout
icon="arrowDown"
Expand All @@ -52,7 +60,8 @@ export const EuiSelect = ({
name={name}
className={classes}
ref={inputRef}
defaultValue={defaultValue || ''}
defaultValue={selectDefaultValue}
value={value}
{...rest}
>
{emptyOptionNode}
Expand Down
16 changes: 16 additions & 0 deletions src/components/form/select/select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,21 @@ describe('EuiSelect', () => {
expect(component)
.toMatchSnapshot();
});

test('value option is rendered', () => {
const component = render(
<EuiSelect
options={[
{ value: '1', text: 'Option #1' },
{ value: '2', text: 'Option #2' }
]}
value={'1'}
onChange={() => {}}
/>
);

expect(component)
.toMatchSnapshot();
});
});
});

0 comments on commit e6b0b22

Please sign in to comment.