Skip to content

Commit

Permalink
Merge pull request #2084 from JedWatson/fix/expose-select-to-async-cr…
Browse files Browse the repository at this point in the history
…eatable

expose children in AsyncCreatable.js
  • Loading branch information
JedWatson committed Oct 23, 2017
2 parents 2275ada + e5b1c66 commit b6fd3e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
58 changes: 31 additions & 27 deletions src/AsyncCreatable.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import React from 'react';
import PropTypes from 'prop-types';
import Select from './Select';
import Async from './Async';
import Creatable from './Creatable';

function reduce(obj, props = {}){
return Object.keys(obj)
.reduce((props, key) => {
const value = obj[key];
if (value !== undefined) props[key] = value;
return props;
}, props);
}

class AsyncCreatableSelect extends React.Component {

focus () {
Expand All @@ -21,27 +13,39 @@ class AsyncCreatableSelect extends React.Component {
render () {
return (
<Async {...this.props}>
{(asyncProps) => (
<Creatable {...this.props}>
{(creatableProps) => (
<Select
{...reduce(asyncProps, reduce(creatableProps, {}))}
onInputChange={(input) => {
creatableProps.onInputChange(input);
return asyncProps.onInputChange(input);
}}
ref={(ref) => {
this.select = ref;
creatableProps.ref(ref);
asyncProps.ref(ref);
}}
/>
)}
</Creatable>
)}
{({ ref, ...asyncProps }) => {
const asyncRef = ref;
return (<Creatable {...asyncProps} >
{({ ref, ...creatableProps }) => {
const creatableRef = ref;
return this.props.children({
...creatableProps,
ref: (select) => {
creatableRef(select);
asyncRef(select);
this.select = select;
}
});
}}
</Creatable>);
}}
</Async>
);
}
};

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

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

AsyncCreatableSelect.defaultProps = {
children: defaultChildren,
};

export default AsyncCreatableSelect;
2 changes: 1 addition & 1 deletion src/Creatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class CreatableSelect extends React.Component {
this.inputValue = input;

if (onInputChange) {
this.inputValue = onInputChange(input);
this.inputValue = onInputChange(input);
}

return this.inputValue;
Expand Down

0 comments on commit b6fd3e7

Please sign in to comment.