-
Notifications
You must be signed in to change notification settings - Fork 82
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
FIX (searchFn)[#101] searchFn callback should only call once when typing in the search bar #131
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5a81228
Working searchFn example.
13abda6
Remove debug code.
c8b5287
Undo indentation
5e44a72
Restore changes.
b084dbd
Remove debug code.
eaa340d
Restore return.
a2fd9e5
Add test for a custom function.
9617003
Add test coverage for custom search function.
2bb3dab
Apply code review suggestions.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React from 'react'; | ||
import { Heading } from './components/Heading'; | ||
import Select from '../../../src'; | ||
|
||
import { | ||
getByPath, | ||
} from '../../../src/util'; | ||
|
||
const WithSearchFn = ({ options, title }) => { | ||
const onSearch = ({ props, state, methods }) => { | ||
console.log({ props, state, methods }); | ||
|
||
const regexp = new RegExp(methods.safeString(state.search), 'i'); | ||
return methods | ||
.sortBy() | ||
.filter((item) => | ||
regexp.test(getByPath(item, props.searchBy) || getByPath(item, props.valueField)) | ||
); | ||
}; | ||
|
||
return ( | ||
<React.Fragment> | ||
<Heading | ||
title={title} | ||
source="https://github.com/sanusart/react-dropdown-select/tree/master/docs/src/examples/WithSearchFn.js" | ||
/> | ||
<Select | ||
options={options} | ||
searchFn={onSearch} | ||
/> | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
WithSearchFn.propTypes = {}; | ||
|
||
export default WithSearchFn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,8 @@ export class Select extends Component { | |
values: props.values, | ||
search: '', | ||
selectBounds: {}, | ||
cursor: null | ||
cursor: null, | ||
searchResults: props.options, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please add the new state entry to docs at |
||
}; | ||
|
||
this.methods = { | ||
|
@@ -289,7 +290,9 @@ export class Select extends Component { | |
}); | ||
|
||
this.setState({ | ||
search: event.target.value | ||
search: event.target.value, | ||
}, () => { | ||
this.setState({ searchResults: this.searchResults() }) | ||
}); | ||
}; | ||
|
||
|
@@ -391,7 +394,7 @@ export class Select extends Component { | |
}; | ||
|
||
handleKeyDownFn = ({ event, state, props, methods, setState }) => { | ||
const { cursor } = state; | ||
const { cursor, searchResults } = state; | ||
const escape = event.key === 'Escape'; | ||
const enter = event.key === 'Enter'; | ||
const arrowUp = event.key === 'ArrowUp'; | ||
|
@@ -423,7 +426,7 @@ export class Select extends Component { | |
} | ||
|
||
if (enter) { | ||
const currentItem = methods.searchResults()[cursor]; | ||
const currentItem = searchResults[cursor]; | ||
if (currentItem && !currentItem.disabled) { | ||
if (props.create && valueExistInSelected(state.search, state.values, props)) { | ||
return null; | ||
|
@@ -433,7 +436,7 @@ export class Select extends Component { | |
} | ||
} | ||
|
||
if ((arrowDown || (tab && state.dropdown)) && methods.searchResults().length === cursor) { | ||
if ((arrowDown || (tab && state.dropdown)) && searchResults.length === cursor) { | ||
return setState({ | ||
cursor: 0 | ||
}); | ||
|
@@ -453,7 +456,7 @@ export class Select extends Component { | |
|
||
if ((arrowUp || (shiftTab && state.dropdown)) && cursor === 0) { | ||
setState({ | ||
cursor: methods.searchResults().length | ||
cursor: searchResults.length | ||
}); | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not used I think