Skip to content

Commit

Permalink
Add onNewOptionClick handler
Browse files Browse the repository at this point in the history
This is useful when we want to customize the click action of the new option instead of creating the option immediately.
  • Loading branch information
Chan Lee Siong committed Nov 11, 2016
1 parent bd5e52e commit d46f960
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
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

0 comments on commit d46f960

Please sign in to comment.