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 select to async creatable #1360

Closed
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
13 changes: 5 additions & 8 deletions src/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@ export default class Async extends Component {
}

componentWillUpdate (nextProps, nextState) {
const propertiesToSync = ['options'];
propertiesToSync.forEach((prop) => {
if (this.props[prop] !== nextProps[prop]) {
this.setState({
[prop]: nextProps[prop]
});
}
});
if (this.props.options !== nextProps.options) {
this.setState({
options: nextProps.options
});
}
}

clearOptions() {
Expand Down
47 changes: 33 additions & 14 deletions src/AsyncCreatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,42 @@ const AsyncCreatable = React.createClass({
render () {
return (
<Select.Async {...this.props}>
{(asyncProps) => (
<Select.Creatable {...this.props}>
{(creatableProps) => (
<Select
{...asyncProps}
{...creatableProps}
onInputChange={(input) => {
creatableProps.onInputChange(input);
return asyncProps.onInputChange(input);
}}
/>
)}
</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;
10 changes: 10 additions & 0 deletions src/Creatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,19 @@ const Creatable = React.createClass({
});
},

componentWillUpdate (nextProps, nextState) {
// Tricky: Async clears the select inputValue when deleting
// an option, but it doesn't fire onInputChange which is
// how we normally keep inputValue in sync
if (this.props.options !== nextProps.options) {
this.inputValue = this.select.state.inputValue;
}
},

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);
},

onInputKeyDown (event) {
Expand Down