Skip to content

Commit 2f8606a

Browse files
Rethink behavior on item click (#71)
* added tests to match functionality * separated logical concerns into separate useEffect hooks * Update src/use-dropdown-menu.ts Co-Authored-By: Wes Cossick <WesCossick@users.noreply.github.com> * fixed test and revised function * rethought process for closing menu on click * formatting * demo formatting * added a test for setIsOpen functionality * version changed to 2.0.0 Co-authored-by: Wes Cossick <WesCossick@users.noreply.github.com>
1 parent 3a78498 commit 2f8606a

File tree

6 files changed

+20
-6
lines changed

6 files changed

+20
-6
lines changed

demo/src/app.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import useDropdownMenu from 'react-accessible-dropdown-menu-hook';
66
// Functional component
77
const App: React.FC = () => {
88
// Use the Hook
9-
const [buttonProps, itemProps, isOpen] = useDropdownMenu(3);
9+
const { buttonProps, itemProps, isOpen } = useDropdownMenu(3);
1010

1111
// Return JSX
1212
return (

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-accessible-dropdown-menu-hook",
3-
"version": "1.1.1",
3+
"version": "2.0.0",
44
"description": "A simple Hook for creating fully accessible dropdown menus in React",
55
"main": "dist/use-dropdown-menu.js",
66
"types": "dist/use-dropdown-menu.d.ts",

src/use-dropdown-menu.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,5 +174,5 @@ export default function useDropdownMenu(itemCount: number) {
174174
}));
175175

176176
// Return a listener for the button, individual list items, and the state of the menu
177-
return [buttonProps, itemProps, isOpen] as const;
177+
return { buttonProps, itemProps, isOpen, setIsOpen } as const;
178178
}

test/puppeteer/demo.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ it('reroutes enter presses on menu items as clicks', async () => {
9696
await menuOpen();
9797

9898
// eslint-disable-next-line @typescript-eslint/no-misused-promises
99-
page.on('dialog', async dialog => {
99+
page.once('dialog', async dialog => {
100100
alertAppeared = true;
101101
await dialog.dismiss();
102102
});

test/test-component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import useDropdownMenu from '../src/use-dropdown-menu';
44

55
// A mock component for testing the Hook
66
const TestComponent: React.FC = () => {
7-
const [buttonProps, itemProps, isOpen] = useDropdownMenu(3);
7+
const { buttonProps, itemProps, isOpen, setIsOpen } = useDropdownMenu(3);
88

99
return (
1010
<React.Fragment>
@@ -13,7 +13,7 @@ const TestComponent: React.FC = () => {
1313
</button>
1414

1515
<div role='menu' id='menu'>
16-
<a {...itemProps[0]} onClick={() => null} id='menu-item-1'>
16+
<a {...itemProps[0]} onClick={() => setIsOpen(false)} id='menu-item-1'>
1717
Item 1
1818
</a>
1919

test/use-dropdown-menu.test.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ it('sets isOpen to true after pressing space while focused on the menu button',
7070
expect(span.text()).toBe('true');
7171
});
7272

73+
it('sets isOpen to false after clicking a menu item that calls the state change function', () => {
74+
const component = mount(<TestComponent />);
75+
const button = component.find('#menu-button');
76+
const itemWithHandler = component.find('#menu-item-1');
77+
const span = component.find('#is-open-indicator');
78+
79+
button.getDOMNode<HTMLButtonElement>().focus();
80+
button.simulate('keydown', { key: 'Enter' });
81+
82+
itemWithHandler.simulate('click');
83+
84+
expect(span.text()).toBe('false');
85+
});
86+
7387
it('moves the focus to the next element in the menu after pressing the down arrow', () => {
7488
const component = mount(<TestComponent />);
7589
const button = component.find('#menu-button');

0 commit comments

Comments
 (0)