Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dropdown: fix visible prop #616

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/components/Dropdown/Dropdown.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useRef, useState } from 'react';
import { Stories } from '@storybook/addon-docs';
import { ComponentStory, ComponentMeta } from '@storybook/react';
import {
Expand All @@ -10,6 +10,7 @@ import {
import { Icon, IconName } from '../Icon';
import { Dropdown } from './';
import { List } from '../List';
import { Stack } from '../Stack';

export default {
title: 'Dropdown',
Expand Down Expand Up @@ -192,6 +193,37 @@ const Dropdown_Div_Story: ComponentStory<typeof Dropdown> = (args) => {

export const Dropdown_Div = Dropdown_Div_Story.bind({});

const Dropdown_External_Story: ComponentStory<typeof Dropdown> = (args) => {
const [visible, setVisibility] = useState(false);
return (
<Stack direction="horizontal" flexGap="xxl">
<DefaultButton
alignIcon={ButtonIconAlign.Right}
checked={visible}
onClick={() => setVisibility(!visible)}
text={'External Control'}
toggle
/>
<Dropdown
{...args}
visible={visible}
onVisibleChange={(isVisible) => setVisibility(isVisible)}
>
<DefaultButton
alignIcon={ButtonIconAlign.Right}
text={'Click button start'}
iconProps={{
path: IconName.mdiChevronDown,
rotate: visible ? 180 : 0,
}}
/>
</Dropdown>
</Stack>
);
};

export const Dropdown_External = Dropdown_External_Story.bind({});

const dropdownArgs: Object = {
trigger: 'click',
classNames: 'my-dropdown-class',
Expand All @@ -218,3 +250,7 @@ Dropdown_Div.args = {
Dropdown_IconButton.args = {
...dropdownArgs,
};

Dropdown_External.args = {
...dropdownArgs,
};
54 changes: 54 additions & 0 deletions src/components/Dropdown/Dropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,38 @@ const ComplexDropdownComponent = (): JSX.Element => {
);
};

const ExternalElementDropdownComponent = (): JSX.Element => {
const [visible, setVisibility] = useState(false);

return (
<Stack direction="horizontal" flexGap="xxl">
<DefaultButton
alignIcon={ButtonIconAlign.Right}
checked={visible}
data-testid="test-external-button-id"
onClick={() => setVisibility(!visible)}
text={'External Control'}
toggle
/>
<Dropdown
{...dropdownProps}
visible={visible}
onVisibleChange={(isVisible) => setVisibility(isVisible)}
>
<DefaultButton
alignIcon={ButtonIconAlign.Right}
text={'Dropdown menu test'}
iconProps={{
path: IconName.mdiChevronDown,
rotate: visible ? 180 : 0,
}}
data-testid="test-button-id"
/>
</Dropdown>
</Stack>
);
};

describe('Dropdown', () => {
beforeAll(() => {
matchMedia = new MatchMediaMock();
Expand Down Expand Up @@ -181,4 +213,26 @@ describe('Dropdown', () => {
);
expect(dropdownAriaRef.getAttribute('aria-expanded')).toBe('true');
});

test('Should support visible prop toggle via external element', async () => {
const { container } = render(<ExternalElementDropdownComponent />);
const externalElement = screen.getByTestId('test-external-button-id');
const referenceElement = screen.getByTestId('test-button-id');
externalElement.click();
await waitFor(() => screen.getByText('User profile 1'));
const option1 = screen.getByText('User profile 1');
expect(option1).toBeTruthy();
expect(container.querySelector('.dropdown-wrapper')?.classList).toContain(
'my-dropdown-class'
);
expect(referenceElement.getAttribute('aria-expanded')).toBe('true');
externalElement.click();
await waitFor(() =>
expect(referenceElement.getAttribute('aria-expanded')).toBe('false')
);
expect(container.querySelector('.dropdown-wrapper')).toBeFalsy();
externalElement.click();
await waitFor(() => screen.getByText('User profile 1'));
expect(option1).toBeTruthy();
});
});
16 changes: 13 additions & 3 deletions src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
import { Menu } from '../Menu';
import { useMergedState } from '../../hooks/useMergedState';
import { useOnClickOutside } from '../../hooks/useOnClickOutside';
import { usePreviousState } from '../../hooks/usePreviousState';
import {
ConditionalWrapper,
eventKeys,
Expand Down Expand Up @@ -73,6 +74,8 @@ export const Dropdown: FC<DropdownProps> = React.memo(
});

const [closing, setClosing] = useState<boolean>(false);
const previouslyClosing: boolean = usePreviousState(closing);

const dropdownId: string = uniqueId('dropdown-');
const [dropdownReferenceId, setReferenceElementId] = useState<string>(
`${dropdownId}reference`
Expand All @@ -90,7 +93,7 @@ export const Dropdown: FC<DropdownProps> = React.memo(
(show: boolean, showDropdown = (show: boolean) => show): Function =>
(e: SyntheticEvent): void => {
// to control the toggle behaviour
const updatedShow = showDropdown(show);
const updatedShow: boolean = showDropdown(show);
if (PREVENT_DEFAULT_TRIGGERS.includes(trigger)) {
e.preventDefault();
}
Expand All @@ -99,6 +102,9 @@ export const Dropdown: FC<DropdownProps> = React.memo(
timeout = setTimeout(
() => {
setVisible(updatedShow);
if (!previouslyClosing) {
setClosing(false);
}
onVisibleChange?.(updatedShow);
},
!show ? ANIMATION_DURATION : 0
Expand All @@ -114,15 +120,19 @@ export const Dropdown: FC<DropdownProps> = React.memo(
(e) => {
const referenceElement: HTMLElement =
document.getElementById(dropdownReferenceId);
if (closeOnOutsideClick && closeOnReferenceClick) {
if (closeOnOutsideClick && closeOnReferenceClick && !mergedVisible) {
toggle(false)(e);
}
if (
!closeOnReferenceClick &&
!referenceElement.contains(e.target as Node)
!referenceElement.contains(e.target as Node) &&
!mergedVisible
) {
toggle(false)(e);
}
if (!mergedVisible) {
toggle(false)(e);
}
onClickOutside?.(e);
},
mergedVisible
Expand Down