Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose children in AsyncCreatable.js #2084

Merged
merged 2 commits into from
Oct 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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