Skip to content

Commit

Permalink
resolve #48
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrowden committed Aug 18, 2018
1 parent 1fa2fb2 commit dc12ca7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ class MuiDownshift extends Component {
focusOnClear={focusOnClear}
loading={loading}
downshiftProps={downshiftProps}
inputRef={node => (this.inputRef = node)}
inputRef={node => {
this.inputRef = node;
if (this.props.inputRef && typeof this.props.inputRef === 'function') {
this.props.inputRef(node);
}
}}
/>

<Menu
Expand Down Expand Up @@ -85,6 +90,7 @@ MuiDownshift.defaultProps = {
) : null; // TODO: should we handle this or require user to implement `getListItem` at this point (`includeFooter` or an array of null/undefined)?
},
menuItemCount: 5,
inputRef: undefined,
};

MuiDownshift.propTypes = {
Expand All @@ -97,6 +103,7 @@ MuiDownshift.propTypes = {
getInputProps: PropTypes.func,
focusOnClear: PropTypes.bool,
loading: PropTypes.bool,
inputRef: PropTypes.func,

// Menu
getListItem: PropTypes.func,
Expand Down
10 changes: 10 additions & 0 deletions stories/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ storiesOf('Input', module)
focusOnClear
onChange={action('onChange')}
/>
))
.add('blurOnSelect (using inputRef)', () => (
<StarWarsSelect
getInputProps={({ openMenu }) => ({
label: 'Star Wars character',
placeholder: 'Choose wisely',
})}
blurOnSelect
onChange={action('onChange')}
/>
));

storiesOf('List item', module)
Expand Down
19 changes: 18 additions & 1 deletion stories/components/StarWarsSelect.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import React, { Component } from 'react';
import { all as starwarsNames } from 'starwars-names';
import MuiDownshift from '../../src';
import PropTypes from 'prop-types';

const items = starwarsNames.map((label, value) => ({ label, value }));

export default class StarWarsSelect extends Component {
class StarWarsSelect extends Component {
static defaultProps = {
blurOnSelect: false,
};

state = {
filteredItems: items,
};
Expand All @@ -14,6 +19,9 @@ export default class StarWarsSelect extends Component {
const filteredItems = items.filter(item => item.label.toLowerCase().includes(changes.inputValue.toLowerCase()));
this.setState({ filteredItems });
}
if (this.input && this.props.blurOnSelect) {
this.input.blur();
}
};

render() {
Expand All @@ -25,7 +33,16 @@ export default class StarWarsSelect extends Component {
// getListItemKey={rowIndex => filteredItems[rowIndex].value}
// keyMapper={rowIndex => filteredItems[rowIndex] && filteredItems[rowIndex].label}
{...this.props}
inputRef={node => {
this.input = node;
}}
/>
);
}
}

StarWarsSelect.propTypes = {
blurOnSelect: PropTypes.bool,
};

export default StarWarsSelect;

0 comments on commit dc12ca7

Please sign in to comment.