Skip to content

Commit

Permalink
feat(Dropdown): Override toggle dropdown click inside (#205)
Browse files Browse the repository at this point in the history
* Override toggle dropdown click inside
  • Loading branch information
jkneal authored and burnumd committed Aug 15, 2019
1 parent 6daefaa commit 594f184
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/components/Dropdown/Dropdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ By default the dropdown menu is vertically aligned with the left edge of the dro
<a href="#">Item four</a>
</Dropdown>
```

### Toggle On Click Inside Modifier

By default the dropdown menu will not close when an item within the dropdown is clicked. Set the 'toggleDropdownOnClickInside' option to true to specify the dropdown should be closed in this case.

```jsx
<Dropdown label="Toggle On Click" toggleDropdownOnClickInside>
<Button variant="plain">Item one</Button>
<Button variant="plain">Item two</Button>
</Dropdown>
```
33 changes: 33 additions & 0 deletions src/components/Dropdown/Dropdown.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,39 @@ describe('<Dropdown />', () => {
});
});

describe('Toggle behavior click inside override', () => {

let cut;

const expectDropdownMenuIsOpen = () => {
expect(document.getElementsByClassName('rvt-dropdown__menu')).toHaveLength(1);
}

const expectDropdownMenuIsClosed = () => {
expect(document.getElementsByClassName('rvt-dropdown__menu')).toHaveLength(0);
}

const clickToggleButton = () => {
document.getElementsByTagName('button')[0].click();
}

beforeEach(() => {
ReactDOM.render(
<Dropdown toggleDropdownOnClickInside={true}>
<a href="#">Hello, world!</a>
</Dropdown>, root
);
});

it('should toggle the menu when clicking inside the menu', () => {
expectDropdownMenuIsClosed();
clickToggleButton();
expectDropdownMenuIsOpen();
document.getElementsByTagName('a')[0].click();
expectDropdownMenuIsClosed();
});
});

describe('Event handler registration', () => {

afterEach(() => {
Expand Down
6 changes: 4 additions & 2 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ interface DropdownProps extends ButtonProps {
* Optional CSS classes which will be applied to the dropdown menu
*/
menuClass?: string;
/** Optional flag to toggle the dropdown open state when a click is done within the dropdown contents */
toggleDropdownOnClickInside?: boolean;
}

const initialState = { open: false }
Expand Down Expand Up @@ -59,7 +61,7 @@ export class Dropdown extends React.PureComponent<DropdownProps & React.HTMLAttr
}

public render() {
const { align, children, className, label, menuClass, ...attrs } = this.props;
const { align, children, className, label, menuClass, toggleDropdownOnClickInside = false, ...attrs } = this.props;
const menuClasses = classNames({
['rvt-dropdown__menu']: true,
[`rvt-dropdown__menu--${align}`]: !!align
Expand Down Expand Up @@ -101,7 +103,7 @@ export class Dropdown extends React.PureComponent<DropdownProps & React.HTMLAttr
return false;
}

if (event.targets(ReactDOM.findDOMNode(this)) && (!event.isKeyEvent() || event.isTabKeyPress())) {
if (event.targets(ReactDOM.findDOMNode(this)) && !this.props.toggleDropdownOnClickInside && (!event.isKeyEvent() || event.isTabKeyPress())) {
// If the user clicks, touches or tabs inside the dropdown do not close the menu
return false;
}
Expand Down

0 comments on commit 594f184

Please sign in to comment.