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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "minor",
"comment": "Proper onClick handling for MenuGridRow",
"packageName": "@fluentui/react-menu-grid-preview",
"email": "adam.samec@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '@fluentui/react-utilities';

import { useTableCompositeNavigation } from '@fluentui/react-table';
import { useMergedTabsterAttributes_unstable, useTabsterAttributes } from '@fluentui/react-tabster';
import type { MenuGridProps, MenuGridState } from './MenuGrid.types';
import { useMenuContext_unstable } from '@fluentui/react-menu';
import { useValidateNesting } from '../../utils/useValidateNesting';
Expand All @@ -23,6 +24,17 @@ export const useMenuGrid_unstable = (props: MenuGridProps, ref: React.Ref<HTMLDi
const { tableRowTabsterAttribute, tableTabsterAttribute, onTableKeyDown } = useTableCompositeNavigation();
const onKeyDown = useEventCallback(mergeCallbacks(props.onKeyDown, onTableKeyDown));

const ignoreEnterKeyAttribute = useTabsterAttributes({
focusable: {
ignoreKeydown: { Enter: true },
},
});

const mergedRowTabsterAttribute = useMergedTabsterAttributes_unstable(
tableRowTabsterAttribute,
ignoreEnterKeyAttribute,
);

return {
components: {
root: 'div',
Expand All @@ -38,6 +50,6 @@ export const useMenuGrid_unstable = (props: MenuGridProps, ref: React.Ref<HTMLDi
}),
{ elementType: 'div' },
),
tableRowTabsterAttribute,
tableRowTabsterAttribute: mergedRowTabsterAttribute,
};
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
'use client';

import * as React from 'react';
import { useMergedRefs, getIntrinsicElementProps, slot } from '@fluentui/react-utilities';
import { Enter, Space } from '@fluentui/keyboard-keys';
import {
useMergedRefs,
useEventCallback,
mergeCallbacks,
getIntrinsicElementProps,
slot,
} from '@fluentui/react-utilities';

import { useMenuGridContext_unstable } from '../../contexts/menuGridContext';
import { MenuGridRowProps, MenuGridRowState } from './MenuGridRow.types';
Expand All @@ -14,6 +21,25 @@ export function useMenuGridRow_unstable(props: MenuGridRowProps, ref: React.Ref<
const validateNestingRef = useValidateNesting('MenuGridRow');
const { tableRowTabsterAttribute } = useMenuGridContext_unstable();

const onKeyDownToClick = useEventCallback((event: React.KeyboardEvent<HTMLDivElement>) => {
if (
!event.isDefaultPrevented() &&
(event.key === Enter || event.key === Space) &&
event.target === event.currentTarget
) {
// event.preventDefault();
event.currentTarget.click();
}
});

const onKeyDown = useEventCallback(mergeCallbacks(props.onKeyDown, onKeyDownToClick));

const onClick = useEventCallback((event: React.MouseEvent<HTMLDivElement>) => {
if (event.target === event.currentTarget) {
props.onClick?.(event);
}
});

return {
components: {
root: 'div',
Expand All @@ -25,6 +51,8 @@ export function useMenuGridRow_unstable(props: MenuGridRowProps, ref: React.Ref<
tabIndex: 0,
...tableRowTabsterAttribute,
...props,
onKeyDown,
onClick,
}),
{ elementType: 'div' },
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ const items = [
];

export const Asymmetric = (): JSXElement => {
const showAlert = (name: string) => {
alert(`Item ${name} activated`);
};

return (
<Menu>
<MenuTrigger disableButtonEnhancement>
Expand Down Expand Up @@ -43,6 +47,7 @@ export const Asymmetric = (): JSXElement => {
aria-label={`Profile card for ${item.name}`}
/>
}
onClick={() => showAlert(item.name)}
aria-label={item.name}
>
{item.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { DeleteRegular, GlobePersonRegular } from '@fluentui/react-icons';
const items = ['Olivia Carter', 'Liam Thompson', 'Sophia Martinez', 'Noah Patel', 'Emma Robinson'];

export const Default = (): JSXElement => {
const showAlert = (name: string) => {
alert(`Item ${name} activated`);
};

return (
<Menu>
<MenuTrigger disableButtonEnhancement>
Expand All @@ -17,6 +21,7 @@ export const Default = (): JSXElement => {
{items.map(name => (
<MenuGridItem
key={name}
onClick={() => showAlert(name)}
aria-label={name}
icon={
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const items = {
};

export const GroupingItems = (): JSXElement => {
const showAlert = (name: string) => {
alert(`Item ${name} activated`);
};

return (
<Menu>
<MenuTrigger disableButtonEnhancement>
Expand Down Expand Up @@ -38,6 +42,7 @@ export const GroupingItems = (): JSXElement => {
aria-label={`Remove ${name}`}
/>
}
onClick={() => showAlert(name)}
aria-label={name}
>
{name}
Expand Down Expand Up @@ -65,6 +70,7 @@ export const GroupingItems = (): JSXElement => {
aria-label={`Remove ${name}`}
/>
}
onClick={() => showAlert(name)}
aria-label={name}
>
{name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import { CameraRegular, DeleteRegular, GlobePersonRegular, PhoneRegular } from '
const items = ['Olivia Carter', 'Liam Thompson', 'Sophia Martinez', 'Noah Patel', 'Emma Robinson'];

export const MoreComplexMenus = (): JSXElement => {
const showAlert = (name: string) => {
alert(`Item ${name} activated`);
};

return (
<Menu>
<MenuTrigger disableButtonEnhancement>
Expand All @@ -15,7 +19,7 @@ export const MoreComplexMenus = (): JSXElement => {
<MenuPopover>
<MenuGrid>
{items.map(name => (
<MenuGridRow key={name} aria-label={name}>
<MenuGridRow key={name} onClick={() => showAlert(name)} aria-label={name}>
<MenuGridCell>
<Button
size="small"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const Submenu = () => {
};

export const WithSubmenu = (): JSXElement => {
const showAlert = (name: string) => {
alert(`Item ${name} activated`);
};

return (
<Menu>
<MenuTrigger disableButtonEnhancement>
Expand All @@ -44,7 +48,7 @@ export const WithSubmenu = (): JSXElement => {
<MenuPopover>
<MenuGrid>
{items.map(name => (
<MenuGridItem key={name} firstSubAction={<Submenu />} aria-label={name}>
<MenuGridItem key={name} firstSubAction={<Submenu />} onClick={() => showAlert(name)} aria-label={name}>
{name}
</MenuGridItem>
))}
Expand Down