Skip to content

Commit

Permalink
breaking(Search): update onSearchChange signature (#1828)
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter authored and levithomason committed Jul 7, 2017
1 parent e6cdac6 commit c9d3394
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
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

0 comments on commit c9d3394

Please sign in to comment.