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

docs(custom menu): add example with search input inline filtering #9687

Merged
merged 2 commits into from
Nov 15, 2023
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
6 changes: 6 additions & 0 deletions packages/react-core/src/demos/CustomMenus/CustomMenus.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<MenuItem>` components, [panel](/components/panel) allows more flexible control and customization.
Expand Down
Original file line number Diff line number Diff line change
@@ -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<any>();
const menuRef = React.useRef<any>();

const onSelect = (_event: React.MouseEvent<Element, 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) => (
<SelectOption key={currentValue} itemId={index}>
{currentValue}
</SelectOption>
));
if (input && menuListItems.length === 0) {
menuListItems.push(
<SelectOption isDisabled key="no result">
No results found
</SelectOption>
);
}

const toggle = (
<MenuToggle ref={toggleRef} onClick={() => setIsOpen(!isOpen)} isExpanded={isOpen}>
{isOpen ? 'Expanded' : 'Collapsed'}
</MenuToggle>
);

const menu = (
<Menu ref={menuRef} onSelect={onSelect} activeItemId={activeItem} isScrollable>
<MenuSearch>
<MenuSearchInput>
<SearchInput
value={input}
aria-label="Filter menu items"
onChange={(_event, value) => handleTextInputChange(value)}
/>
</MenuSearchInput>
</MenuSearch>
<Divider />
<MenuContent maxMenuHeight="200px">
<MenuList>{menuListItems}</MenuList>
</MenuContent>
</Menu>
);

return (
<MenuContainer
menu={menu}
menuRef={menuRef}
toggle={toggle}
toggleRef={toggleRef}
isOpen={isOpen}
onOpenChange={(isOpen) => setIsOpen(isOpen)}
onOpenChangeKeys={['Escape']}
/>
);
};
Loading