Skip to content

Commit

Permalink
Add allowBlank to Dropdown
Browse files Browse the repository at this point in the history
Since @nickw was having issues with the dropdown not defaulting to the
first entry if the value was null (even though the options had a null value in it)
this seems like a good time to add an option to default to the first element of the supplied options
(the prop is called allowBlank) in case the value is not found in the options.

The handling of null/undefined values has been fixed too and if there actually is a value of null
in the options it is correctly selected now.
  • Loading branch information
TobiasBales authored and javivelasco committed Apr 5, 2016
1 parent 5b6452e commit 542b58a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
16 changes: 7 additions & 9 deletions components/dropdown/Dropdown.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import style from './style';

class Dropdown extends React.Component {
static propTypes = {
allowBlank: React.PropTypes.bool,
auto: React.PropTypes.bool,
className: React.PropTypes.string,
disabled: React.PropTypes.bool,
Expand All @@ -26,6 +27,7 @@ class Dropdown extends React.Component {
static defaultProps = {
auto: true,
className: '',
allowBlank: true,
disabled: false
};

Expand All @@ -52,11 +54,6 @@ class Dropdown extends React.Component {
}
}

valueIsPresent () {
const value = this.props.value;
return value !== null && value !== undefined && value !== '' && !Number.isNaN(value);
}

close = () => {
if (this.state.active) {
this.setState({active: false});
Expand Down Expand Up @@ -87,10 +84,11 @@ class Dropdown extends React.Component {
};

getSelectedItem = () => {
if (this.valueIsPresent()) {
for (const item of this.props.source) {
if (item.value === this.props.value) return item;
}
for (const item of this.props.source) {
if (item.value === this.props.value) return item;
}
if (!this.props.allowBlank) {
return this.props.source[0];
}
};

Expand Down
1 change: 1 addition & 0 deletions components/dropdown/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ class DropdownTest extends React.Component {
| `source` | `Array` | | Array of data objects with the data to represent in the dropdown.
| `template` | `Function` | | Callback function that returns a JSX template to represent the element.
| `value` | `String` | | Default value using JSON data.
| `allowBlank` | `Boolean` | `true` | If true the dropdown will preselect the first item if the supplied value matches none of the options' values

0 comments on commit 542b58a

Please sign in to comment.