Skip to content

Commit

Permalink
Add keyForDisplayName prop
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickDesign committed Oct 1, 2020
1 parent da10852 commit 730e8f8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ include|[]|An array of values that should be included from the searcher's output
maxPatternLength|32|The maximum length of the pattern. The longer the pattern, the more intensive the search operation will be. Whenever the pattern exceeds the maxPatternLength, an error will be thrown.
onSelect| noop | Function to be executed on selection of any result.
width|430|width of the fuzzy searchbox
keyForDisplayName|title|The key which should be used for list item text.
keys|all[Array]|List of properties that will be searched. This also supports nested properties.
list|null|Array of properties to be filtered.
placeholder|'Search'|Placeholder of the searchbox
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function defaultResultsTemplate(props, state, styl, clickHandler) {
const style = state.selectedIndex === i ? styl.selectedResultStyle : styl.resultsStyle;
return (
<div key={i} style={style} onClick={() => clickHandler(i)}>
{val.title}
{val[props.keyForDisplayName]}
</div>
);
});
Expand All @@ -73,6 +73,7 @@ export default class FuzzySearch extends Component {
maxPatternLength: PropTypes.number,
onSelect: PropTypes.func.isRequired,
width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
keyForDisplayName: PropTypes.string,
keys: PropTypes.oneOfType([PropTypes.array, PropTypes.string]),
list: PropTypes.array.isRequired,
location: PropTypes.number,
Expand All @@ -92,6 +93,7 @@ export default class FuzzySearch extends Component {
caseSensitive: false,
distance: 100,
include: [],
keyForDisplayName: 'title',
location: 0,
width: 430,
placeholder: 'Search',
Expand Down
23 changes: 23 additions & 0 deletions src/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,27 @@ describe('<FuzzySearch />', () => {
// Each result should have a 'matches' array now with `includeMatches`
expect(wrapper.state('results')[0].matches.length).to.not.equal(0);
});

it('should display keyForDisplayName when passed in', () => {
const onChange = sinon.spy();
const wrapper = mount(
<FuzzySearch
keyForDisplayName="author"
list={list}
onSelect={onChange}
keys={['author', 'title']}
options={{ includeMatches: true }}
/>,
);

const input = wrapper.find('input');
input.simulate('change', {
target: {
value: 'f',
},
});

// Each result should have a 'matches' array now with `includeMatches`
expect(wrapper.state('results')[0].item.title).to.equal('The Great Gatsby');
});
});

0 comments on commit 730e8f8

Please sign in to comment.