Skip to content

Commit 8650188

Browse files
author
Sasha Kondrashov
committed
search
1 parent ff7a2c7 commit 8650188

File tree

5 files changed

+62
-50
lines changed

5 files changed

+62
-50
lines changed

index.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,7 @@ export {
509509
StrictRatingIconProps,
510510
} from './dist/commonjs/modules/Rating/RatingIcon'
511511

512-
export {
513-
default as Search,
514-
SearchProps,
515-
SearchResultData,
516-
StrictSearchProps,
517-
} from './dist/commonjs/modules/Search'
512+
export { default as Search, SearchProps, StrictSearchProps } from './dist/commonjs/modules/Search'
518513
export {
519514
default as SearchCategory,
520515
SearchCategoryProps,

src/modules/Search/Search.d.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -93,49 +93,60 @@ export interface StrictSearchProps {
9393
* Called on blur.
9494
*
9595
* @param {SyntheticEvent} event - React's original SyntheticEvent.
96-
* @param {object} data - All props.
96+
* @param {object} props - All props.
9797
*/
98-
onBlur?: (event: React.MouseEvent<HTMLElement>, data: SearchProps) => void
98+
onBlur?: (event: React.MouseEvent<HTMLElement>, props: SearchProps) => void
9999

100100
/**
101101
* Called on focus.
102102
*
103103
* @param {SyntheticEvent} event - React's original SyntheticEvent.
104-
* @param {object} data - All props.
104+
* @param {object} props - All props.
105105
*/
106-
onFocus?: (event: React.MouseEvent<HTMLElement>, data: SearchProps) => void
106+
onFocus?: (event: React.MouseEvent<HTMLElement>, props: SearchProps) => void
107107

108108
/**
109109
* Called on mousedown.
110110
*
111111
* @param {SyntheticEvent} event - React's original SyntheticEvent.
112-
* @param {object} data - All props.
112+
* @param {object} props - All props.
113113
*/
114-
onMouseDown?: (event: React.MouseEvent<HTMLElement>, data: SearchProps) => void
114+
onMouseDown?: (event: React.MouseEvent<HTMLElement>, props: SearchProps) => void
115115

116116
/**
117117
* Called when a result is selected.
118118
*
119119
* @param {SyntheticEvent} event - React's original SyntheticEvent.
120-
* @param {object} data - All props.
120+
* @param {object} props - All props.
121+
* @param {any} result - The search result.
121122
*/
122-
onResultSelect?: (event: React.MouseEvent<HTMLDivElement>, data: SearchResultData) => void
123+
onResultSelect?: (
124+
event: React.MouseEvent<HTMLDivElement>,
125+
props: SearchProps,
126+
result: any,
127+
) => void
123128

124129
/**
125130
* Called on search input change.
126131
*
127132
* @param {SyntheticEvent} event - React's original SyntheticEvent.
128-
* @param {object} data - All props, includes current value of search input.
133+
* @param {object} props - All props.
134+
* @param {string} query - Current value of search input.
129135
*/
130-
onSearchChange?: (event: React.MouseEvent<HTMLElement>, data: SearchProps) => void
136+
onSearchChange?: (event: React.MouseEvent<HTMLElement>, props: SearchProps, query: string) => void
131137

132138
/**
133139
* Called when the active selection index is changed.
134140
*
135141
* @param {SyntheticEvent} event - React's original SyntheticEvent.
136-
* @param {object} data - All props.
142+
* @param {object} props - All props.
143+
* @param {any} result - The search result.
137144
*/
138-
onSelectionChange?: (event: React.MouseEvent<HTMLElement>, data: SearchResultData) => void
145+
onSelectionChange?: (
146+
event: React.MouseEvent<HTMLElement>,
147+
props: SearchProps,
148+
result: any,
149+
) => void
139150

140151
// ------------------------------------
141152
// Style
@@ -166,10 +177,6 @@ export interface StrictSearchProps {
166177
placeholder?: string
167178
}
168179

169-
export interface SearchResultData extends SearchProps {
170-
result: any
171-
}
172-
173180
declare const Search: ForwardRefComponent<SearchProps, HTMLDivElement> & {
174181
Category: typeof SearchCategory
175182
Result: typeof SearchResult

src/modules/Search/Search.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,14 @@ class SearchInner extends Component {
152152
debug('handleResultSelect()')
153153
debug(result)
154154

155-
_.invoke(this.props, 'onResultSelect', e, { ...this.props, result })
155+
_.invoke(this.props, 'onResultSelect', e, this.props, result)
156156
}
157157

158158
handleSelectionChange = (e) => {
159159
debug('handleSelectionChange()')
160160

161161
const result = this.getSelectedResult()
162-
_.invoke(this.props, 'onSelectionChange', e, { ...this.props, result })
162+
_.invoke(this.props, 'onSelectionChange', e, this.props, result)
163163
}
164164

165165
closeOnEscape = (e) => {
@@ -282,7 +282,7 @@ class SearchInner extends Component {
282282
const { open } = this.state
283283
const newQuery = e.target.value
284284

285-
_.invoke(this.props, 'onSearchChange', e, { ...this.props, value: newQuery })
285+
_.invoke(this.props, 'onSearchChange', e, this.props, newQuery)
286286

287287
// open search dropdown on search query
288288
if (newQuery.length < minCharacters) {

src/modules/Search/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default, SearchProps, StrictSearchProps, SearchResultData } from './Search'
1+
export { default, SearchProps, StrictSearchProps } from './Search'

test/specs/modules/Search/Search-test.js

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ describe('Search', () => {
485485
})
486486

487487
describe('onBlur', () => {
488-
it('is called with (event, data) on search input blur', () => {
488+
it('is called on search input blur', () => {
489489
const onBlur = sandbox.spy()
490490
wrapperMount(<Search results={options} onBlur={onBlur} />).simulate('blur', nativeEvent)
491491

@@ -504,7 +504,7 @@ describe('Search', () => {
504504
})
505505

506506
describe('onFocus', () => {
507-
it('is called with (event, data) on search input focus', () => {
507+
it('is called on search input focus', () => {
508508
const onFocus = sandbox.spy()
509509
wrapperMount(<Search results={options} onFocus={onFocus} />).simulate('focus', nativeEvent)
510510

@@ -514,36 +514,41 @@ describe('Search', () => {
514514
})
515515

516516
describe('onResultSelect', () => {
517-
let spy
517+
let onResultSelect
518518
beforeEach(() => {
519-
spy = sandbox.spy()
519+
onResultSelect = sandbox.spy()
520520
})
521521

522-
it('is called with event and value on item click', () => {
522+
it('is called on item click', () => {
523523
const randomIndex = _.random(options.length - 1)
524524
const randomResult = options[randomIndex]
525-
wrapperMount(<Search results={options} minCharacters={0} onResultSelect={spy} />)
525+
wrapperMount(<Search results={options} minCharacters={0} onResultSelect={onResultSelect} />)
526526

527527
// open
528528
openSearchResults()
529529
searchResultsIsOpen()
530530

531531
wrapper.find('SearchResult').at(randomIndex).simulate('click', nativeEvent)
532532

533-
spy.should.have.been.calledOnce()
534-
spy.should.have.been.calledWithMatch(
533+
onResultSelect.should.have.been.calledOnce()
534+
onResultSelect.should.have.been.calledWithMatch(
535535
{},
536536
{
537537
minCharacters: 0,
538-
result: randomResult,
539538
results: options,
540539
},
540+
randomResult,
541541
)
542542
})
543-
it('is called with event and value when pressing enter on a selected item', () => {
543+
it('is called when pressing enter on a selected item', () => {
544544
const firstResult = options[0]
545545
wrapperMount(
546-
<Search results={options} minCharacters={0} onResultSelect={spy} selectFirstResult />,
546+
<Search
547+
results={options}
548+
minCharacters={0}
549+
onResultSelect={onResultSelect}
550+
selectFirstResult
551+
/>,
547552
)
548553

549554
// open
@@ -552,18 +557,23 @@ describe('Search', () => {
552557

553558
domEvent.keyDown(document, { key: 'Enter' })
554559

555-
spy.should.have.been.calledOnce()
556-
spy.should.have.been.calledWithMatch({}, { result: firstResult })
560+
onResultSelect.should.have.been.calledOnce()
561+
onResultSelect.should.have.been.calledWithMatch({}, {}, firstResult)
557562
})
558563
it('is not called when updating the value prop', () => {
559564
const value = _.sample(options).title
560565
const next = _.sample(_.without(options, value)).title
561566

562567
wrapperMount(
563-
<Search results={options} minCharacters={0} value={value} onResultSelect={spy} />,
568+
<Search
569+
results={options}
570+
minCharacters={0}
571+
value={value}
572+
onResultSelect={onResultSelect}
573+
/>,
564574
).setProps({ value: next })
565575

566-
spy.should.not.have.been.called()
576+
onResultSelect.should.not.have.been.called()
567577
})
568578
it('does not call onResultSelect on query change', () => {
569579
const onResultSelectSpy = sandbox.spy()
@@ -579,26 +589,26 @@ describe('Search', () => {
579589
})
580590

581591
describe('onSearchChange', () => {
582-
it('is called with (event, value) on search input change', () => {
583-
const spy = sandbox.spy()
584-
wrapperMount(<Search results={options} minCharacters={0} onSearchChange={spy} />)
592+
it('is called on search input change', () => {
593+
const onSearchChange = sandbox.spy()
594+
wrapperMount(<Search results={options} minCharacters={0} onSearchChange={onSearchChange} />)
585595
.find('input.prompt')
586596
.simulate('change', { target: { value: 'a' }, stopPropagation: _.noop })
587597

588-
spy.should.have.been.calledOnce()
589-
spy.should.have.been.calledWithMatch(
598+
onSearchChange.should.have.been.calledOnce()
599+
onSearchChange.should.have.been.calledWithMatch(
590600
{ target: { value: 'a' } },
591601
{
592602
minCharacters: 0,
593603
results: options,
594-
value: 'a',
595604
},
605+
'a',
596606
)
597607
})
598608
})
599609

600-
describe('onSearchChange', () => {
601-
it('is called with (event, data) when the active selection index is changed', () => {
610+
describe('onSelectionChange', () => {
611+
it('is called when the active selection index is changed', () => {
602612
const onSelectionChange = sandbox.spy()
603613

604614
wrapperMount(
@@ -617,9 +627,9 @@ describe('Search', () => {
617627
{},
618628
{
619629
minCharacters: 0,
620-
result: options[1],
621630
results: options,
622631
},
632+
options[1],
623633
)
624634
})
625635
})

0 commit comments

Comments
 (0)