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

Add unselected option to select widget when no default is set #673

Merged
merged 1 commit into from
Nov 11, 2017
Merged
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
16 changes: 10 additions & 6 deletions src/components/Widgets/SelectControl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PropTypes from 'prop-types';
import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes';
import { isMap } from 'immutable';

export default class SelectControl extends React.Component {
handleChange = (e) => {
Expand All @@ -15,12 +16,15 @@ export default class SelectControl extends React.Component {
return <div>Error rendering select control for {field.get('name')}: No options</div>;
}

const options = fieldOptions.map((option) => {
if (typeof option === 'string') {
return { label: option, value: option };
}
return option;
});
const options = [
...(field.get('default', false) ? [] : [{ label: '', value: '' }]),
...fieldOptions.map((option) => {
if (typeof option === 'string') {
return { label: option, value: option };
}
return isMap(option) ? option.toJS() : option;
}),
];

return (<select id={forID} value={value || ''} onChange={this.handleChange}>
{options.map((option, idx) => <option key={idx} value={option.value}>
Expand Down