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

feat(Search): prop to return the active result #1825

Merged
merged 3 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/modules/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ export default class Search extends Component {
*/
onResultSelect: PropTypes.func,

/**
* Called when the active selection index is changed.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onActiveSelectionChange: PropTypes.func,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that onActiveSelectionChange can be renamed to onSelectionChange.


/**
* Called on search input change.
*
Expand Down Expand Up @@ -289,6 +297,13 @@ export default class Search extends Component {
if (onResultSelect) onResultSelect(e, result)
}

handleActiveSelectionChange = () => {
debug('handleActiveSelectionChange()')
const { onActiveSelectionChange } = this.props
const result = this.getSelectedResult()
if (onActiveSelectionChange) onActiveSelectionChange(result)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can exclude there if-condition if we will use lodash's invoke. Also, our callbacks should return props as the second argument.

_.invoke(this.props, `onActiveSelectionChange`, e, { ...this.props, result})

}

closeOnEscape = (e) => {
if (keyboardKey.getCode(e) !== keyboardKey.Escape) return
e.preventDefault()
Expand Down Expand Up @@ -466,6 +481,7 @@ export default class Search extends Component {

this.setState({ selectedIndex: nextIndex })
this.scrollSelectedItemIntoView()
this.handleActiveSelectionChange()
}

// ----------------------------------------
Expand Down