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

Commit

Permalink
feat(Filter): Add filterPlaceholder prop
Browse files Browse the repository at this point in the history
Override the default filter placeholder

re #86
  • Loading branch information
Aidurber committed Dec 9, 2018
1 parent e4edfeb commit 27fb2c9
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Picky.propTypes = {
- `renderSelectAll` - Used for rendering a custom select all
- `defaultFocusFilter` - If set to true, will focus the filter by default when opened.
- `renderList` - Render prop for whole list, you can use this to add virtualization/windowing if necessary
- `filterPlaceholder` - Override the filter placeholder. Defaults to 'Filter...'

## Custom rendering

Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ Picky.propTypes = {
- `renderSelectAll` - Used for rendering a custom select all
- `defaultFocusFilter` - If set to true, will focus the filter by default when opened.
- `renderList` - Render prop for whole list, you can use this to add virtualization/windowing if necessary
- `filterPlaceholder` - Override the filter placeholder. Defaults to 'Filter...'

## Custom rendering

Expand Down
9 changes: 9 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,15 @@ declare module 'react-picky' {
* @memberof PickyProps
*/
renderList?: (props: RenderListProps) => any;


/**
* Override the placeholder of the filter
*
* @type {string}
* @memberof PickyProps
*/
filterPlaceholder?:string;
}

export default class Picky extends React.PureComponent<PickyProps, any> {
Expand Down
7 changes: 5 additions & 2 deletions src/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Filter extends PureComponent {
ref={input => (this.filterInput = input)}
className="picky__filter__input"
data-testid="picky__filter__input"
placeholder="Filter..."
placeholder={this.props.placeholder}
tabIndex={this.props.tabIndex}
aria-label="filter options"
onChange={e => this.props.onFilterChange(e.target.value)}
Expand All @@ -18,10 +18,13 @@ class Filter extends PureComponent {
);
}
}

Filter.defaultProps = {
placeholder: 'Filter...',
};
Filter.propTypes = {
onFilterChange: PropTypes.func.isRequired,
tabIndex: PropTypes.number,
placeholder: PropTypes.string,
};

export default Filter;
7 changes: 5 additions & 2 deletions src/Picky.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class Picky extends React.PureComponent {
componentDidMount() {
this.focusFilterInput(this.state.open);
}

componentWillUnmount() {
document.removeEventListener('click', this.handleOutsideClick, false);
}

UNSAFE_componentWillReceiveProps(nextProps) {
if (
this.props.options !== nextProps.options ||
Expand Down Expand Up @@ -337,6 +337,7 @@ class Picky extends React.PureComponent {
tabIndex,
dropdownHeight,
renderSelectAll,
filterPlaceholder,
} = this.props;
const { open } = this.state;
let ariaOwns = '';
Expand Down Expand Up @@ -388,6 +389,7 @@ class Picky extends React.PureComponent {
{includeFilter && (
<Filter
ref={filter => (this.filter = filter)}
placeholder={filterPlaceholder}
onFilterChange={
filterDebounce > 0
? debounce(this.onFilterChange, filterDebounce)
Expand Down Expand Up @@ -482,6 +484,7 @@ Picky.propTypes = {
defaultFocusFilter: PropTypes.bool,
className: PropTypes.string,
renderList: PropTypes.func,
filterPlaceholder: PropTypes.string,
};

export default Picky;
22 changes: 22 additions & 0 deletions tests/Picky.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,5 +863,27 @@ describe('Picky', () => {
expect(onChange).toHaveBeenCalledTimes(1);
});
});

test('should override filter placeholder if supplied', () => {
const { getByTestId } = rtlRender(
<Picky
options={[1, 2, 3]}
open={true}
includeFilter
filterPlaceholder="Hello..."
/>
);
const filter = getByTestId('picky__filter__input');

expect(filter.placeholder).toEqual('Hello...');
});
test('filter placeholder default to Filter... if not supplied', () => {
const { getByTestId } = rtlRender(
<Picky options={[1, 2, 3]} open={true} includeFilter />
);
const filter = getByTestId('picky__filter__input');

expect(filter.placeholder).toEqual('Filter...');
});
});
});

0 comments on commit 27fb2c9

Please sign in to comment.