Skip to content
Open
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
16 changes: 15 additions & 1 deletion packages/react-core/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ export interface DropdownProps extends MenuProps, OUIAProps {
ouiaId?: number | string;
/** Set the value of data-ouia-safe. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
ouiaSafe?: boolean;
/** When applied, wraps dropdown in a container with a data-ouia-component-id.*/
containerOuiaId?: number | string;
/** Set the value of data-ouia-safe for the container when containerOuiaId is applied. Only set to true when the component is in a static state, i.e. no animations are occurring. At all other times, this value must be false. */
containerOuiaSafe?: boolean;
/** Sets the base component to render for the container. Defaults to <span> */
containerComponent?: React.ReactNode;
/** z-index of the dropdown menu */
zIndex?: number;
/** Additional properties to pass to the Popper */
Expand Down Expand Up @@ -101,11 +107,16 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
shouldFocusFirstItemOnOpen = false,
shouldPreventScrollOnItemFocus = true,
focusTimeoutDelay = 0,
containerOuiaId,
containerOuiaSafe = true,
containerComponent = 'span',
...props
}: DropdownProps) => {
const localMenuRef = useRef<HTMLDivElement>(undefined);
const localToggleRef = useRef<HTMLButtonElement>(undefined);
const ouiaProps = useOUIAProps(Dropdown.displayName, ouiaId, ouiaSafe);
const ContainerComponent = containerComponent as any;
const containerOuiaProps = useOUIAProps('Dropdown container', containerOuiaId, containerOuiaSafe);

const menuRef = (innerRef as React.RefObject<HTMLDivElement | null>) || localMenuRef;
const toggleRef =
Expand Down Expand Up @@ -200,7 +211,8 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
</MenuContent>
</Menu>
);
return (

const popper = (
<Popper
trigger={typeof toggle === 'function' ? toggle(toggleRef) : toggle.toggleNode}
triggerRef={toggleRef}
Expand All @@ -211,6 +223,8 @@ const DropdownBase: React.FunctionComponent<DropdownProps> = ({
{...popperProps}
/>
);

return containerOuiaId ? <ContainerComponent {...containerOuiaProps}>{popper}</ContainerComponent> : popper;
};

export const Dropdown = forwardRef((props: DropdownProps, ref: React.Ref<any>) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,32 @@ test('onOpenChange is called when passed and user presses esc key', async () =>
expect(onOpenChange).toBeCalledTimes(1);
});

test('applies containerOuiaId to parent element', () => {
render(
<Dropdown containerOuiaId="test-dropdown" toggle={(toggleRef) => toggle(toggleRef)}>
{dropdownChildren}
</Dropdown>
);

const dropdownToggle = screen.getByRole('button', { name: 'Dropdown' });
const dropdownParent = dropdownToggle?.parentNode?.parentNode;
expect(dropdownParent).toHaveAttribute('data-ouia-component-id', 'test-dropdown');
expect(dropdownParent).toHaveAttribute('data-ouia-component-type', 'PF6/Dropdown container');
expect(dropdownParent?.tagName).toBe('SPAN');
});

test('applies containerOuiaId to parent element', () => {
render(
<Dropdown containerOuiaId="test-dropdown" containerComponent="div" toggle={(toggleRef) => toggle(toggleRef)}>
{dropdownChildren}
</Dropdown>
);

const dropdownToggle = screen.getByRole('button', { name: 'Dropdown' });
const dropdownParent = dropdownToggle?.parentNode?.parentNode;
expect(dropdownParent?.tagName).toBe('DIV');
});

test('match snapshot', () => {
const { asFragment } = render(
<Dropdown
Expand Down
Loading