Skip to content

Commit

Permalink
feat(Dropdown): Add onOpen and onClose props (carbon-design-system#660)
Browse files Browse the repository at this point in the history
  • Loading branch information
s100 authored and marijohannessen committed Mar 1, 2018
1 parent eaa3bc5 commit 699c621
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/components/Dropdown/Dropdown-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ storiesOf('Dropdown', module)
<Dropdown
{...dropdownEvents}
onChange={action('onChange')}
onOpen={action('onOpen')}
onClose={action('onClose')}
defaultText="Dropdown label">
<DropdownItem itemText="Option 1" value="option1" />
<DropdownItem itemText="Option 2" value="option2" />
Expand All @@ -41,6 +43,8 @@ storiesOf('Dropdown', module)
<Dropdown
{...dropdownEvents}
onChange={selectedItemInfo => console.log(selectedItemInfo)}
onOpen={action('onOpen')}
onClose={action('onClose')}
defaultText="Option 1"
value="all">
<DropdownItem itemText="Option 1" value="option1" />
Expand All @@ -61,6 +65,8 @@ storiesOf('Dropdown', module)
<Dropdown
{...dropdownEvents}
onChange={selectedItemInfo => console.log(selectedItemInfo)}
onOpen={action('onOpen')}
onClose={action('onClose')}
defaultText="Dropdown label"
disabled>
<DropdownItem itemText="Option 1" value="option1" />
Expand All @@ -81,6 +87,8 @@ storiesOf('Dropdown', module)
<Dropdown
{...dropdownEvents}
onChange={selectedItemInfo => console.log(selectedItemInfo)}
onOpen={action('onOpen')}
onClose={action('onClose')}
defaultText="Dropdown label"
value="all"
selectedText="Option 4">
Expand Down
15 changes: 14 additions & 1 deletion src/components/Dropdown/Dropdown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ describe('Dropdown', () => {

describe('events', () => {
const onClick = jest.fn();
const onOpen = jest.fn();
const onClose = jest.fn();

const wrapper = mount(
<Dropdown onClick={onClick}>
<Dropdown onClick={onClick} onOpen={onOpen} onClose={onClose}>
<DropdownItem
className="test-child"
itemText="test-child"
Expand Down Expand Up @@ -160,6 +162,17 @@ describe('Dropdown', () => {
expect(wrapper.state().open).toBe(false);
});

it('fires open and close callbacks when the dropdown is clicked', () => {
onOpen.mockReset();
onClose.mockReset();
dropdown.simulate('click');
expect(onOpen).toHaveBeenCalled();
expect(onClose).not.toHaveBeenCalled();
dropdown.simulate('click');
expect(onOpen).toHaveBeenCalled();
expect(onClose).toHaveBeenCalled();
});

it('should not open when disabled', () => {
const wrapper = mount(
<Dropdown onClick={onClick} disabled>
Expand Down
15 changes: 15 additions & 0 deletions src/components/Dropdown/Dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export default class Dropdown extends PureComponent {
tabIndex: PropTypes.number,
onClick: PropTypes.func,
onChange: PropTypes.func.isRequired,
onOpen: PropTypes.func,
onClose: PropTypes.func,
selectedText: PropTypes.string,
open: PropTypes.bool,
iconDescription: PropTypes.string,
Expand All @@ -25,6 +27,8 @@ export default class Dropdown extends PureComponent {
disabled: false,
iconDescription: 'open list of options',
onChange: () => {},
onOpen: () => {},
onClose: () => {},
};

constructor(props) {
Expand All @@ -36,6 +40,15 @@ export default class Dropdown extends PureComponent {
this.setState(this.resetState(nextProps));
}

componentDidUpdate(prevProps, prevState) {
if (!prevState.open && this.state.open) {
this.props.onOpen();
}
if (prevState.open && !this.state.open) {
this.props.onClose();
}
}

resetState(props) {
const { children, selectedText, value, defaultText, open } = props;

Expand Down Expand Up @@ -93,6 +106,8 @@ export default class Dropdown extends PureComponent {
iconDescription,
disabled,
selectedText, // eslint-disable-line no-unused-vars
onOpen, // eslint-disable-line no-unused-vars
onClose, // eslint-disable-line no-unused-vars
...other
} = this.props;

Expand Down

0 comments on commit 699c621

Please sign in to comment.