Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not set both value and defaultValue on select #504

Merged
merged 2 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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.
- `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();
});
});
});