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

Commit

Permalink
feat(callbacks): Added additional callbacks
Browse files Browse the repository at this point in the history
Added onFiltered, onClose, onOpen callbacks

closes #4
  • Loading branch information
Aidurber committed Dec 26, 2017
1 parent 5a3c77d commit 5c9a3f2
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/Picky.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,32 @@ class Picky extends React.Component {
const filteredOptions = this.props.options.filter(option =>
String(option).includes(value)
);
this.setState({
filtered: true,
filteredOptions
});
this.setState(
{
filtered: true,
filteredOptions
},
() => {
if (this.props.onFiltered) {
this.props.onFiltered(filteredOptions);
}
}
);
}
toggleDropDown() {
this.setState({
open: !this.state.open
});
this.setState(
{
open: !this.state.open
},
() => {
const isOpen = this.state.open;
if (isOpen && this.props.onOpen) {
this.props.onOpen();
} else if (!isOpen && this.props.onClose) {
this.props.onClose();
}
}
);
}
render() {
const {
Expand Down Expand Up @@ -216,7 +233,10 @@ Picky.propTypes = {
includeSelectAll: PropTypes.bool,
includeFilter: PropTypes.bool,
filterDebounce: PropTypes.number,
dropdownHeight: PropTypes.number
dropdownHeight: PropTypes.number,
onFiltered: PropTypes.func,
onOpen: PropTypes.func,
onClose: PropTypes.func
};

export default Picky;
56 changes: 56 additions & 0 deletions tests/Picky.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,60 @@ describe('Picky', () => {
expect(wrapper.state('filteredOptions')).toEqual([1]);
});
});

describe('Callbacks', () => {
it('should call onFiltered callback', () => {
const onFilteredMock = jest.fn();
const wrapper = mount(
<Picky
options={[1, 2, 3, 4]}
value={[]}
multiple={true}
filterDebounce={0}
open={true}
includeFilter={true}
onFiltered={onFilteredMock}
/>
);

const event = { target: { value: '1' } };
wrapper.find('.picky__filter__input').simulate('change', event);
expect(onFilteredMock).toHaveBeenCalledWith([1]);
});

it('should call onOpen', () => {
const onOpenMock = jest.fn();
const wrapper = mount(
<Picky
options={[1, 2, 3, 4]}
value={[]}
multiple={true}
filterDebounce={0}
open={false}
includeFilter={true}
onOpen={onOpenMock}
/>
);

wrapper.find('.picky__input').simulate('click');
expect(onOpenMock).toHaveBeenCalled();
});
it('should call onClose', () => {
const onCloseMock = jest.fn();
const wrapper = mount(
<Picky
options={[1, 2, 3, 4]}
value={[]}
multiple={true}
filterDebounce={0}
open={true}
includeFilter={true}
onClose={onCloseMock}
/>
);

wrapper.find('.picky__input').simulate('click');
expect(onCloseMock).toHaveBeenCalled();
});
});
});

0 comments on commit 5c9a3f2

Please sign in to comment.