From a7075e0adc51ed22e0f5f2e2802fd6ffa44eb5c1 Mon Sep 17 00:00:00 2001 From: adamviktora <84135613+adamviktora@users.noreply.github.com> Date: Wed, 15 Nov 2023 20:45:47 +0100 Subject: [PATCH] docs(custom menu): add example with search input inline filtering (#9687) * docs(custom menu): add example with search input inline filtering * refactor(demo menu): rename to InlineSearchFilterMenuDemo --- .../src/demos/CustomMenus/CustomMenus.md | 6 ++ .../examples/InlineSearchFilterMenuDemo.tsx | 102 ++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 packages/react-core/src/demos/CustomMenus/examples/InlineSearchFilterMenuDemo.tsx diff --git a/packages/react-core/src/demos/CustomMenus/CustomMenus.md b/packages/react-core/src/demos/CustomMenus/CustomMenus.md index 779c345e64b..5b734d87904 100644 --- a/packages/react-core/src/demos/CustomMenus/CustomMenus.md +++ b/packages/react-core/src/demos/CustomMenus/CustomMenus.md @@ -50,6 +50,12 @@ Additionally, menu components may be connected to each other manually through ou ``` +### With inline search filter + +```ts file="./examples/InlineSearchFilterMenuDemo.tsx" + +``` + ### Tree view menu When rendering a menu-like element that does not contain `` components, [panel](/components/panel) allows more flexible control and customization. diff --git a/packages/react-core/src/demos/CustomMenus/examples/InlineSearchFilterMenuDemo.tsx b/packages/react-core/src/demos/CustomMenus/examples/InlineSearchFilterMenuDemo.tsx new file mode 100644 index 00000000000..d561f52581b --- /dev/null +++ b/packages/react-core/src/demos/CustomMenus/examples/InlineSearchFilterMenuDemo.tsx @@ -0,0 +1,102 @@ +import React from 'react'; +import { + Menu, + MenuList, + MenuContent, + MenuSearch, + MenuSearchInput, + Divider, + SearchInput, + SelectOption, + MenuToggle, + MenuContainer +} from '@patternfly/react-core'; + +export const InlineSearchFilterMenuDemo: React.FunctionComponent = () => { + const [activeItem, setActiveItem] = React.useState(0); + const [input, setInput] = React.useState(''); + const [isOpen, setIsOpen] = React.useState(false); + const toggleRef = React.useRef(); + const menuRef = React.useRef(); + + const onSelect = (_event: React.MouseEvent | undefined, itemId: number | string | undefined) => { + const item = itemId as number; + // eslint-disable-next-line no-console + console.log(`clicked ${itemId}`); + setActiveItem(item); + }; + + const handleTextInputChange = (value: string) => { + if (!isOpen) { + setIsOpen(true); + } + setInput(value); + }; + + const menuListItemsText = [ + 'Action 1', + 'Action 2', + 'Action 3', + 'My project', + 'OpenShift cluster', + 'Production Ansible', + 'AWS', + 'Azure', + 'My project 2', + 'OpenShift cluster ', + 'Production Ansible 2 ', + 'AWS 2', + 'Azure 2' + ]; + + const menuListItems = menuListItemsText + .filter((item) => !input || item.toLowerCase().includes(input.toString().toLowerCase())) + .map((currentValue, index) => ( + + {currentValue} + + )); + if (input && menuListItems.length === 0) { + menuListItems.push( + + No results found + + ); + } + + const toggle = ( + setIsOpen(!isOpen)} isExpanded={isOpen}> + {isOpen ? 'Expanded' : 'Collapsed'} + + ); + + const menu = ( + + + + handleTextInputChange(value)} + /> + + + + + {menuListItems} + + + ); + + return ( + setIsOpen(isOpen)} + onOpenChangeKeys={['Escape']} + /> + ); +};