Skip to content
This repository was archived by the owner on Jan 15, 2021. It is now read-only.

Commit 3a5352c

Browse files
committed
feat(Filter): Add case sensitive prop
Allow case sensitive filtering
1 parent 5144d9b commit 3a5352c

File tree

8 files changed

+63
-12
lines changed

8 files changed

+63
-12
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ Picky.propTypes = {
159159
- `defaultFocusFilter` - If set to true, will focus the filter by default when opened.
160160
- `renderList` - Render prop for whole list, you can use this to add virtualization/windowing if necessary
161161
- `filterPlaceholder` - Override the filter placeholder. Defaults to 'Filter...'
162-
- `getFilterValue` - Will provide the input value of filter to the picky dropdown, so that if we have a larger list of options then we can only supply the matching options based on this value.
163-
162+
- `getFilterValue` - Will provide the input value of filter to the picky dropdown, so that if we have a larger list of options then we can only supply the matching options based on this value.
163+
- `caseSensitiveFilter` - If true options will be returned when they match case
164164

165165
## Custom rendering
166166

dist/index.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Picky.propTypes = {
160160
- `renderList` - Render prop for whole list, you can use this to add virtualization/windowing if necessary
161161
- `filterPlaceholder` - Override the filter placeholder. Defaults to 'Filter...'
162162
- `getFilterValue` - Will provide the input value of filter to the picky dropdown, so that if we have a larger list of options then we can only supply the matching options based on this value.
163+
- `caseSensitiveFilter` - If true options will be returned when they match case
163164

164165
## Custom rendering
165166

index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ declare module 'react-picky' {
380380
* Will provide the input value of filter to the picky dropdown, so that if we have a larger list of options then we can only supply the matching options based on this value.
381381
*/
382382
getFilterValue: (term: string) => any;
383+
/**
384+
* If true options will be returned when they match case, defaults to false
385+
*/
386+
caseSensitiveFilter?: boolean;
383387
}
384388

385389
export default class Picky extends React.PureComponent<PickyProps, any> {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"pretest": "npm run build",
1414
"lint": "eslint src/**/*.js",
1515
"test": "cross-env NODE_ENV=test jest",
16-
"test:watch": "cross-env NODE_ENV=test jest --watchAll",
16+
"test:watch": "cross-env NODE_ENV=test jest --watch",
1717
"semantic-release": "semantic-release",
1818
"precommit": "npm run test"
1919
},

src/Picky.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,13 @@ class Picky extends React.PureComponent {
258258
);
259259
const filteredOptions = this.props.options.filter(option => {
260260
if (isObject) {
261-
return includes(option[this.props.labelKey], term);
261+
return includes(
262+
option[this.props.labelKey],
263+
term,
264+
this.props.caseSensitiveFilter
265+
);
262266
}
263-
return includes(option, term);
267+
return includes(option, term, this.props.caseSensitiveFilter);
264268
});
265269
this.setState(
266270
{
@@ -494,6 +498,7 @@ Picky.propTypes = {
494498
filterPlaceholder: PropTypes.string,
495499
disabled: PropTypes.bool,
496500
getFilterValue: PropTypes.func,
501+
caseSensitiveFilter: PropTypes.bool,
497502
};
498503

499504
export default Picky;

src/lib/includes.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
export default (str, term) =>
2-
String(str)
3-
.toLowerCase()
4-
.indexOf(String(term).toLowerCase()) > -1;
1+
/**
2+
* Check if a string contains a value
3+
*/
4+
function includes(str, term, caseSensitive) {
5+
if (!caseSensitive) {
6+
return (
7+
String(str)
8+
.toLowerCase()
9+
.indexOf(String(term).toLowerCase()) > -1
10+
);
11+
} else {
12+
return String(str).indexOf(String(term)) > -1;
13+
}
14+
}
15+
16+
export default includes;

tests/Picky.test.js

+31-2
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,35 @@ describe('Picky', () => {
494494
expect(wrapper.state('filteredOptions')).toEqual([1]);
495495
});
496496

497+
it('should filter values when case sensitive', () => {
498+
const options = [
499+
{ label: 'Item 1', value: 1 },
500+
{ label: 'item 2', value: 2 },
501+
{ label: 'item 3', value: 3 },
502+
{ label: 'Item 4', value: 4 },
503+
];
504+
const wrapper = mount(
505+
<Picky
506+
options={options}
507+
value={[]}
508+
valueKey="value"
509+
labelKey="label"
510+
caseSensitiveFilter={true}
511+
multiple={true}
512+
filterDebounce={0}
513+
open={true}
514+
includeFilter={true}
515+
/>
516+
);
517+
const event = { target: { value: 'i' } };
518+
expect(wrapper.state('filteredOptions')).toEqual([]);
519+
wrapper.find(sel('picky__filter__input')).simulate('change', event);
520+
expect(wrapper.state('filteredOptions')).toEqual([
521+
{ label: 'item 2', value: 2 },
522+
{ label: 'item 3', value: 3 },
523+
]);
524+
});
525+
497526
it('shouldnt filter if filter query is blank or empty string', () => {
498527
const wrapper = mount(
499528
<Picky
@@ -971,7 +1000,7 @@ describe('Picky', () => {
9711000

9721001
it('should be disabled when disabled prop supplied', () => {
9731002
const options = [1, 2];
974-
const { getByTestId, rerender } = rtlRender(
1003+
const { getByTestId } = rtlRender(
9751004
<Picky
9761005
options={options}
9771006
includeSelectAll
@@ -997,7 +1026,7 @@ describe('Picky', () => {
9971026

9981027
it('should not have select all checked when there are no options', () => {
9991028
const options = [];
1000-
const { getByTestId, debug } = rtlRender(
1029+
const { getByTestId } = rtlRender(
10011030
<Picky
10021031
options={options}
10031032
includeSelectAll

0 commit comments

Comments
 (0)