Skip to content

Commit

Permalink
Merge pull request #119 from hyjeko/onSelect-new-property
Browse files Browse the repository at this point in the history
Add new property onSelect
  • Loading branch information
Sharlaan authored Dec 25, 2017
2 parents 180c0cc + b099e81 commit a0c790d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ This component requires 3 dependencies :
| keepSearchOnSelect | bool | false | Prevents the autocomplete field's value to be reset after each selection.|
| disabled | bool | false | Include this property to disable superSelectField.|
| value | null, object, object[] | null | Selected value(s).<br>/!\ REQUIRED: each object must expose a 'value' property. |
| onChange | function | () => {} | Triggers when selecting/unselecting an option from the Menu.<br>signature: (selectedValues, name) with `selectedValues` array of selected values based on selected nodes' value property, and `name` the value of the superSelectField instance's name property |
| onChange | function | () => {} | Triggers when closing the menu. Use this if you do not want to update your component state with each selection and only on menu close. <br>signature: (selectedValues, name) with `selectedValues` array of selected values based on selected nodes' value property, and `name` the value of the superSelectField instance's name property |
| onSelect | function | () => {} | Triggers when selecting an item in the menu. Use this to update your componenet state with each selection from the menu (while still open). <br>signature: (selectedValues, name) with `selectedValues` array of selected values based on selected nodes' value property, and `name` the value of the superSelectField instance's name property |
| onMenuOpen | function | () => {} | Triggers when opening the Menu. |
| onAutoCompleteTyping | function | () => {} | Exposes the word typed in AutoComplete. Useful for triggering onType API requests. |
| children | any | [] | Datasource is an array of any type of nodes, styled at your convenience.<br>/!\ REQUIRED: each node must expose a `value` property. This value property will be used by default for both option's value and label.<br>A `label` property can be provided to specify a different value than `value`. |
Expand Down
7 changes: 6 additions & 1 deletion src/SuperSelectField.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class SelectField extends Component {
})
}

getSelected = () => {
const {onSelect, name} = this.props;
onSelect(this.state.selectedItems, name);
}

openMenu() {
if (!this.state.isOpen) this.props.onMenuOpen()
if (this.state.itemsLength || this.props.showAutocompleteThreshold === 'always') this.setState({ isOpen: true }, () => this.focusTextField())
Expand Down Expand Up @@ -146,7 +151,7 @@ class SelectField extends Component {
const updatedValues = selectedItemExists
? selectedItems.filter(obj => !areEqual(obj.value, selectedItem.value))
: selectedItems.concat(selectedItem)
this.setState({ selectedItems: updatedValues })
this.setState({ selectedItems: updatedValues }, this.getSelected())
this.clearTextField(() => this.focusTextField())
}
else {
Expand Down
1 change: 1 addition & 0 deletions src/defaultProps.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const selectFieldDefaultProps = {
},
value: null,
onChange: () => {},
onSelect: () => {},
onMenuOpen: () => {},
onAutoCompleteTyping: () => {},
children: []
Expand Down
1 change: 1 addition & 0 deletions src/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export const selectFieldTypes = {
keepSearchOnSelect: bool,
disabled: bool,
onChange: func,
onSelect: func,
onMenuOpen: func,
onAutoCompleteTyping: func
}

0 comments on commit a0c790d

Please sign in to comment.