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

Commit

Permalink
feat(Filter): Focus filter by default on open if defaultFocusFilter s…
Browse files Browse the repository at this point in the history
…upplied

fix #38
  • Loading branch information
Aidurber committed Jan 20, 2018
1 parent 8fa9855 commit 5e639e5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ import 'react-picky/dist/picky.css'; // Include CSS
**Note** If you check the network tag in your dev tools, notice how all images aren't loaded, this is because it's a virtual list.

### With and without virtual list example
[![Edit [No virtual list] Simple example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/9462xq75wy)

An example of a large multiselect with no virtual list, note the performance difference.
[![Edit [No virtual list] Simple example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/9462xq75wy)

An example of a large multiselect with no virtual list, note the performance difference.

## Props

Expand Down Expand Up @@ -130,7 +131,8 @@ Picky.propTypes = {
manySelectedPlaceholder: PropTypes.string,
allSelectedPlaceholder: PropTypes.string,
selectAllText: PropTypes.string,
renderSelectAll: PropTypes.func
renderSelectAll: PropTypes.func,
defaultFocusFilter: PropTypes.bool
};
```

Expand Down Expand Up @@ -161,6 +163,7 @@ Picky.propTypes = {
* `allSelectedPlaceholder` - Default "%s selected" where %s is the number of items selected. This gets used when all options are selected.
* `selectAllText` - Default "Select all", use this to override "Select all" text from top of dropdown when included with `includeSelectAll`.
* `renderSelectAll` - Used for rendering a custom select all
* `defaultFocusFilter` - If set to true, will focus the filter by default when opened.

## Custom rendering

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

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,10 @@ import 'react-picky/dist/picky.css'; // Include CSS
**Note** If you check the network tag in your dev tools, notice how all images aren't loaded, this is because it's a virtual list.

### With and without virtual list example
[![Edit [No virtual list] Simple example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/9462xq75wy)

An example of a large multiselect with no virtual list, note the performance difference.
[![Edit [No virtual list] Simple example](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/9462xq75wy)

An example of a large multiselect with no virtual list, note the performance difference.

## Props

Expand Down Expand Up @@ -130,7 +131,8 @@ Picky.propTypes = {
manySelectedPlaceholder: PropTypes.string,
allSelectedPlaceholder: PropTypes.string,
selectAllText: PropTypes.string,
renderSelectAll: PropTypes.func
renderSelectAll: PropTypes.func,
defaultFocusFilter: PropTypes.bool
};
```

Expand Down Expand Up @@ -161,6 +163,7 @@ Picky.propTypes = {
* `allSelectedPlaceholder` - Default "%s selected" where %s is the number of items selected. This gets used when all options are selected.
* `selectAllText` - Default "Select all", use this to override "Select all" text from top of dropdown when included with `includeSelectAll`.
* `renderSelectAll` - Used for rendering a custom select all
* `defaultFocusFilter` - If set to true, will focus the filter by default when opened.

## Custom rendering

Expand Down
1 change: 1 addition & 0 deletions src/Filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Filter extends Component {
<div className="picky__filter">
<input
type="text"
ref={input => (this.filterInput = input)}
className="picky__filter__input"
data-test="picky__filter__input"
placeholder="Filter..."
Expand Down
19 changes: 18 additions & 1 deletion src/Picky.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,20 @@ class Picky extends React.PureComponent {
this.allSelected = this.allSelected.bind(this);
this.handleOutsideClick = this.handleOutsideClick.bind(this);
this.isItemSelected = this.isItemSelected.bind(this);
this.focusFilterInput = this.focusFilterInput.bind(this);
}
componentWillMount() {
const allSelected = this.allSelected();

this.setState({
allSelected
});
}

componentDidMount() {
this.focusFilterInput(this.state.open);
}

componentWillReceiveProps(nextProps) {
if (
this.props.options !== nextProps.options ||
Expand Down Expand Up @@ -333,6 +339,14 @@ class Picky extends React.PureComponent {
}
this.toggleDropDown();
}

focusFilterInput(isOpen) {
if (isOpen && this.props.defaultFocusFilter) {
if (this.filter && this.filter.filterInput) {
this.filter.filterInput.focus();
}
}
}
/**
* Toggle state of dropdown
*
Expand All @@ -355,6 +369,7 @@ class Picky extends React.PureComponent {
() => {
const isOpen = this.state.open;
// Prop callbacks
this.focusFilterInput(isOpen);
if (isOpen && this.props.onOpen) {
this.props.onOpen();
} else if (!isOpen && this.props.onClose) {
Expand Down Expand Up @@ -430,6 +445,7 @@ class Picky extends React.PureComponent {
>
{includeFilter && (
<Filter
ref={filter => (this.filter = filter)}
onFilterChange={
filterDebounce > 0
? debounce(this.onFilterChange, filterDebounce)
Expand Down Expand Up @@ -525,7 +541,8 @@ Picky.propTypes = {
manySelectedPlaceholder: PropTypes.string,
allSelectedPlaceholder: PropTypes.string,
selectAllText: PropTypes.string,
renderSelectAll: PropTypes.func
renderSelectAll: PropTypes.func,
defaultFocusFilter: PropTypes.bool
};

export default Picky;
15 changes: 15 additions & 0 deletions tests/Picky.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,21 @@ describe('Picky', () => {
expect(wrapper.find(Filter)).toHaveLength(1);
});

it('should be focused by default if defaultFocusFilter is true', () => {
//picky__filter__input
const wrapper = mount(
<Picky
includeFilter={true}
value={[]}
defaultFocusFilter={true}
open={true}
/>
);
const input = wrapper.find(sel('picky__filter__input'));

expect(input.instance()).toEqual(document.activeElement);
});

it('should call onFilterChange prop when text has changed', () => {
const onChange = jest.fn();
const wrapper = mount(<Filter onFilterChange={onChange} />);
Expand Down

0 comments on commit 5e639e5

Please sign in to comment.