Skip to content

Commit

Permalink
Pass through refs and props to all layers of AsyncCreatable.
Browse files Browse the repository at this point in the history
This enables users of AsyncCreatable to hook the Select which is
created and/or access it as a ref.

Previously there was no way to get at the underlying Select if
you used AsyncCreatable.
  • Loading branch information
DanielHeath committed Nov 14, 2016
1 parent d216b56 commit 1ddb8d9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
43 changes: 33 additions & 10 deletions src/AsyncCreatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,42 @@ const AsyncCreatable = React.createClass({
render () {
return (
<Select.Async {...this.props}>
{(asyncProps) => (
<Select.Creatable {...this.props}>
{(creatableProps) => (
<Select
{...asyncProps}
{...creatableProps}
/>
)}
</Select.Creatable>
)}
{({ref, ...asyncProps}) => {
const asyncRef = ref;
return <Select.Creatable {...asyncProps} >
{({ref, ...creatableProps}) => {
const creatableRef = ref;
return this.props.children({
...creatableProps,
ref: (select) => {
creatableRef(select);
asyncRef(select);
this.select = select;
}
});
}}
</Select.Creatable>;
}}
</Select.Async>
);
}
});

function defaultChildren (props) {
return (
<Select {...props} />
);
};

const propTypes = {
children: React.PropTypes.func.isRequired, // Child function responsible for creating the inner Select component; (props: Object): PropTypes.element
};

const defaultProps = {
children: defaultChildren,
};

AsyncCreatable.propTypes = propTypes;
AsyncCreatable.defaultProps = defaultProps;

module.exports = AsyncCreatable;
2 changes: 1 addition & 1 deletion src/Creatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const Creatable = React.createClass({
onInputChange (input) {
// This value may be needed in between Select mounts (when this.select is null)
this.inputValue = input;
return this.props.onInputChange && this.props.onInputChange(input)
return this.props.onInputChange && this.props.onInputChange(input);
},

onInputKeyDown (event) {
Expand Down

0 comments on commit 1ddb8d9

Please sign in to comment.