diff --git a/packages/react-examples/src/react/ContextualMenu/ContextualMenu.CustomMenuList.Example.tsx b/packages/react-examples/src/react/ContextualMenu/ContextualMenu.CustomMenuList.Example.tsx index 68962c4e631ddf..cfa12b450dd7b7 100644 --- a/packages/react-examples/src/react/ContextualMenu/ContextualMenu.CustomMenuList.Example.tsx +++ b/packages/react-examples/src/react/ContextualMenu/ContextualMenu.CustomMenuList.Example.tsx @@ -4,6 +4,7 @@ import { ISearchBoxStyles, SearchBox } from '@fluentui/react/lib/SearchBox'; import { Icon } from '@fluentui/react/lib/Icon'; import { IContextualMenuListProps, IContextualMenuItem } from '@fluentui/react/lib/ContextualMenu'; import { IRenderFunction } from '@fluentui/react/lib/Utilities'; +import { KeyCodes } from '@fluentui/react'; export const ContextualMenuWithCustomMenuListExample: React.FunctionComponent = () => { const [items, setItems] = React.useState(menuItems); @@ -32,6 +33,21 @@ export const ContextualMenuWithCustomMenuListExample: React.FunctionComponent = setItems(filteredItems); }, []); + const onKeyDown = React.useCallback((e, ...args) => { + /* Key Up, but we are not at the beginning of the text: stop event propagation to prevent ContextualMenu to focus */ + if (e.target.selectionStart > 0 && e.which === KeyCodes.up) { + e.stopPropagation(); + } + /* Key Down, but we are not at the end of the text: stop event propagation to prevent ContextualMenu to focus */ + if (e.target.selectionStart !== e.target.value.length && e.which === KeyCodes.down) { + e.stopPropagation(); + } + }, []); + + const onDismiss = React.useCallback(() => { + setItems(menuItems); + }, []); + const renderMenuList = React.useCallback( (menuListProps: IContextualMenuListProps, defaultRender: IRenderFunction) => { return ( @@ -42,6 +58,7 @@ export const ContextualMenuWithCustomMenuListExample: React.FunctionComponent = placeholder="Filter actions" onAbort={onAbort} onChange={onChange} + onKeyDown={onKeyDown} styles={searchBoxStyles} /> @@ -49,7 +66,7 @@ export const ContextualMenuWithCustomMenuListExample: React.FunctionComponent = ); }, - [onAbort, onChange], + [onAbort, onChange, onKeyDown], ); const menuProps = React.useMemo( @@ -58,8 +75,12 @@ export const ContextualMenuWithCustomMenuListExample: React.FunctionComponent = title: 'Actions', shouldFocusOnMount: true, items, + focusZoneProps: { + shouldInputLoseFocusOnArrowKey: () => true /* Allow up and down arrows to move focus out of the SearchBox */, + }, + onDismiss, }), - [items, renderMenuList], + [items, renderMenuList, onDismiss], ); return ;