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

breaking(Search): update onResultSelect and onSearchChange signature #1828

Merged
merged 1 commit into from
Jul 7, 2017
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
6 changes: 3 additions & 3 deletions src/modules/Search/Search.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ export interface SearchProps {
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onResultSelect?: (event: React.MouseEvent<HTMLDivElement>, data: SearchResultProps) => void;
onResultSelect?: (event: React.MouseEvent<HTMLDivElement>, data: SearchProps) => void;

/**
* Called on search input change.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {string} value - Current value of search input.
* @param {object} data - All props, includes current value of search input.
*/
onSearchChange?: (event: React.MouseEvent<HTMLElement>, value: string) => void;
onSearchChange?: (event: React.MouseEvent<HTMLElement>, data: SearchProps) => void;

// ------------------------------------
// Style
Expand Down
10 changes: 5 additions & 5 deletions src/modules/Search/Search.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default class Search extends Component {
* Called on search input change.
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {string} value - Current value of search input.
* @param {object} data - All props, includes current value of search input.
*/
onSearchChange: PropTypes.func,

Expand Down Expand Up @@ -285,8 +285,8 @@ export default class Search extends Component {
handleResultSelect = (e, result) => {
debug('handleResultSelect()')
debug(result)
const { onResultSelect } = this.props
if (onResultSelect) onResultSelect(e, result)

_.invoke(this.props, 'onResultSelect', e, { ...this.props, result })
}

closeOnEscape = (e) => {
Expand Down Expand Up @@ -400,11 +400,11 @@ export default class Search extends Component {
debug(e.target.value)
// prevent propagating to this.props.onChange()
e.stopPropagation()
const { onSearchChange, minCharacters } = this.props
const { minCharacters } = this.props
const { open } = this.state
const newQuery = e.target.value

if (onSearchChange) onSearchChange(e, newQuery)
_.invoke(this.props, 'onSearchChange', e, { ...this.props, value: newQuery })

// open search dropdown on search query
if (newQuery.length < minCharacters) {
Expand Down
14 changes: 11 additions & 3 deletions test/specs/modules/Search/Search-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,11 @@ describe('Search', () => {
.simulate('click', nativeEvent)

spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch({}, randomResult)
spy.should.have.been.calledWithMatch({}, {
minCharacters: 0,
result: randomResult,
results: options,
})
})
it('is called with event and value when pressing enter on a selected item', () => {
const firstResult = options[0]
Expand All @@ -559,7 +563,7 @@ describe('Search', () => {
domEvent.keyDown(document, { key: 'Enter' })

spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch({}, firstResult)
spy.should.have.been.calledWithMatch({}, { result: firstResult })
})
it('is not called when updating the value prop', () => {
const value = _.sample(options).title
Expand Down Expand Up @@ -591,7 +595,11 @@ describe('Search', () => {
.simulate('change', { target: { value: 'a' }, stopPropagation: _.noop })

spy.should.have.been.calledOnce()
spy.should.have.been.calledWithMatch({ target: { value: 'a' } }, 'a')
spy.should.have.been.calledWithMatch({ target: { value: 'a' } }, {
minCharacters: 0,
results: options,
value: 'a',
})
})
})

Expand Down