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

Add onNewOptionClick handler #1364

Merged
merged 1 commit into from
Nov 11, 2016
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ Property | Type | Description
`isOptionUnique` | function | Searches for any matching option within the set of options. This function prevents duplicate options from being created. By default this is a basic, case-sensitive comparison of label and value. Expected signature: `({ option: Object, options: Array, labelKey: string, valueKey: string }): boolean` |
`isValidNewOption` | function | Determines if the current input text represents a valid option. By default any non-empty string will be considered valid. Expected signature: `({ label: string }): boolean` |
`newOptionCreator` | function | Factory to create new option. Expected signature: `({ label: string, labelKey: string, valueKey: string }): Object` |
`onNewOptionClick` | function | new option click handler, it calls when new option has been selected. `function(option) {}` |
`shouldKeyDownEventCreateNewOption` | function | Decides if a keyDown event (eg its `keyCode`) should result in the creation of a new option. ENTER, TAB and comma keys create new options by default. Expected signature: `({ keyCode: number }): boolean` |
`promptTextCreator` | function | Factory for overriding default option creator prompt label. By default it will read 'Create option "{label}"'. Expected signature: `(label: String): String` |

Expand Down
12 changes: 10 additions & 2 deletions src/Creatable.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const Creatable = React.createClass({
// input keyDown handler: function (event) {}
onInputKeyDown: React.PropTypes.func,

// new option click handler: function (option) {}
onNewOptionClick: React.PropTypes.func,

// See Select.propTypes.options
options: React.PropTypes.array,

Expand Down Expand Up @@ -70,6 +73,7 @@ const Creatable = React.createClass({
const {
isValidNewOption,
newOptionCreator,
onNewOptionClick,
options = [],
shouldKeyDownEventCreateNewOption
} = this.props;
Expand All @@ -80,9 +84,13 @@ const Creatable = React.createClass({

// Don't add the same option twice.
if (isOptionUnique) {
options.unshift(option);
if (onNewOptionClick) {
onNewOptionClick(option);
} else {
options.unshift(option);

this.select.selectValue(option);
this.select.selectValue(option);
}
}
}
},
Expand Down