Add unselected option to select widget when no default is set

This commit is contained in:
Benaiah Mischenko 2017-10-10 11:32:47 -07:00 committed by Shawn Erquhart
parent 6071ea746f
commit e00c396697

View File

@ -1,6 +1,7 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import React from 'react'; import React from 'react';
import ImmutablePropTypes from 'react-immutable-proptypes'; import ImmutablePropTypes from 'react-immutable-proptypes';
import { isMap } from 'immutable';
export default class SelectControl extends React.Component { export default class SelectControl extends React.Component {
handleChange = (e) => { handleChange = (e) => {
@ -15,12 +16,15 @@ export default class SelectControl extends React.Component {
return <div>Error rendering select control for {field.get('name')}: No options</div>; return <div>Error rendering select control for {field.get('name')}: No options</div>;
} }
const options = fieldOptions.map((option) => { const options = [
...(field.get('default', false) ? [] : [{ label: '', value: '' }]),
...fieldOptions.map((option) => {
if (typeof option === 'string') { if (typeof option === 'string') {
return { label: option, value: option }; return { label: option, value: option };
} }
return option; return isMap(option) ? option.toJS() : option;
}); }),
];
return (<select id={forID} value={value || ''} onChange={this.handleChange}> return (<select id={forID} value={value || ''} onChange={this.handleChange}>
{options.map((option, idx) => <option key={idx} value={option.value}> {options.map((option, idx) => <option key={idx} value={option.value}>