From 27fb2c95a3e3c77fd078fd82ad7208dc90d6147c Mon Sep 17 00:00:00 2001 From: Andrew Durber Date: Sun, 9 Dec 2018 19:47:07 +0000 Subject: [PATCH] feat(Filter): Add filterPlaceholder prop Override the default filter placeholder re #86 --- README.md | 1 + dist/index.js | 2 +- docs/README.md | 1 + index.d.ts | 9 +++++++++ src/Filter.js | 7 +++++-- src/Picky.js | 7 +++++-- tests/Picky.test.js | 22 ++++++++++++++++++++++ 7 files changed, 44 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 916a64d..11d03d3 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/dist/index.js b/dist/index.js index 7bab8bc..aae4dab 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";function _interopDefault(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var React=require("react"),React__default=_interopDefault(React);function _typeof(e){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r>>0;if("function"!=typeof e)throw new TypeError("predicate must be a function");for(var l=arguments[1],n=0;n>>0;if("function"!=typeof e)throw new TypeError("predicate must be a function");for(var r=arguments[1],n=0;n any; + + + /** + * Override the placeholder of the filter + * + * @type {string} + * @memberof PickyProps + */ + filterPlaceholder?:string; } export default class Picky extends React.PureComponent { diff --git a/src/Filter.js b/src/Filter.js index afa3b64..92c49c3 100644 --- a/src/Filter.js +++ b/src/Filter.js @@ -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)} @@ -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; diff --git a/src/Picky.js b/src/Picky.js index 49c4ef8..af05163 100644 --- a/src/Picky.js +++ b/src/Picky.js @@ -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 || @@ -337,6 +337,7 @@ class Picky extends React.PureComponent { tabIndex, dropdownHeight, renderSelectAll, + filterPlaceholder, } = this.props; const { open } = this.state; let ariaOwns = ''; @@ -388,6 +389,7 @@ class Picky extends React.PureComponent { {includeFilter && ( (this.filter = filter)} + placeholder={filterPlaceholder} onFilterChange={ filterDebounce > 0 ? debounce(this.onFilterChange, filterDebounce) @@ -482,6 +484,7 @@ Picky.propTypes = { defaultFocusFilter: PropTypes.bool, className: PropTypes.string, renderList: PropTypes.func, + filterPlaceholder: PropTypes.string, }; export default Picky; diff --git a/tests/Picky.test.js b/tests/Picky.test.js index b9fb269..d7ffede 100644 --- a/tests/Picky.test.js +++ b/tests/Picky.test.js @@ -863,5 +863,27 @@ describe('Picky', () => { expect(onChange).toHaveBeenCalledTimes(1); }); }); + + test('should override filter placeholder if supplied', () => { + const { getByTestId } = rtlRender( + + ); + const filter = getByTestId('picky__filter__input'); + + expect(filter.placeholder).toEqual('Hello...'); + }); + test('filter placeholder default to Filter... if not supplied', () => { + const { getByTestId } = rtlRender( + + ); + const filter = getByTestId('picky__filter__input'); + + expect(filter.placeholder).toEqual('Filter...'); + }); }); });