| 1 |
import { cloneElement, Fragment, useRef, useState } from 'react'; |
| 2 |
import { |
| 3 |
MenuToggle, |
| 4 |
MenuSearch, |
| 5 |
MenuSearchInput, |
| 6 |
Tooltip, |
| 7 |
Divider, |
| 8 |
SearchInput, |
| 9 |
Dropdown, |
| 10 |
DropdownGroup, |
| 11 |
DropdownList, |
| 12 |
DropdownItem |
| 13 |
} from '@patternfly/react-core'; |
| 14 |
import RhUiThumbnailViewSmallFillIcon from '@patternfly/react-icons/dist/js/icons/rh-ui-thumbnail-view-small-fill-icon'; |
| 15 |
import brandImg from '@patternfly/react-core/src/demos/assets/PF-IconLogo.svg'; |
| 16 |
|
| 17 |
const MockLink: React.FunctionComponent = ({ to, ...props }: any) => <a href={to} {...props}></a>; |
| 18 |
|
| 19 |
export const ApplicationLauncherDemo: React.FunctionComponent = () => { |
| 20 |
const [isOpen, setIsOpen] = useState<boolean>(false); |
| 21 |
const [refFullOptions, setRefFullOptions] = useState<Element[]>(); |
| 22 |
const [favorites, setFavorites] = useState<string[]>([]); |
| 23 |
const [filteredIds, setFilteredIds] = useState<string[]>(['*']); |
| 24 |
const menuRef = useRef<HTMLDivElement>(null); |
| 25 |
|
| 26 |
const onToggleClick = () => { |
| 27 |
setTimeout(() => { |
| 28 |
if (menuRef.current) { |
| 29 |
const firstElement = menuRef.current.querySelector( |
| 30 |
'li > button:not(:disabled), li > a:not(:disabled), input:not(:disabled)' |
| 31 |
); |
| 32 |
firstElement && (firstElement as HTMLElement).focus(); |
| 33 |
setRefFullOptions(Array.from(menuRef.current.querySelectorAll('li:not(li[role=separator])>*:first-child'))); |
| 34 |
} |
| 35 |
}, 0); |
| 36 |
setIsOpen(!isOpen); |
| 37 |
}; |
| 38 |
|
| 39 |
const menuItems = [ |
| 40 |
<DropdownGroup key="group1" label="Group 1"> |
| 41 |
<DropdownList> |
| 42 |
<DropdownItem value="0" id="0" isFavorited={favorites.includes('0')}> |
| 43 |
Application 1 |
| 44 |
</DropdownItem> |
| 45 |
<DropdownItem |
| 46 |
value="1" |
| 47 |
id="1" |
| 48 |
isFavorited={favorites.includes('1')} |
| 49 |
to="#default-link2" |
| 50 |
onClick={(ev) => ev.preventDefault()} |
| 51 |
> |
| 52 |
Application 2 |
| 53 |
</DropdownItem> |
| 54 |
</DropdownList> |
| 55 |
</DropdownGroup>, |
| 56 |
<Divider key="group1-divider" />, |
| 57 |
<DropdownGroup key="group2" label="Group 2"> |
| 58 |
<DropdownList> |
| 59 |
<DropdownItem |
| 60 |
value="2" |
| 61 |
id="2" |
| 62 |
isFavorited={favorites.includes('2')} |
| 63 |
component={(props) => <MockLink {...props} to="#router-link" />} |
| 64 |
> |
| 65 |
Custom component (such as @reach/router Link) |
| 66 |
</DropdownItem> |
| 67 |
<DropdownItem |
| 68 |
value="3" |
| 69 |
id="3" |
| 70 |
isFavorited={favorites.includes('3')} |
| 71 |
isExternalLink |
| 72 |
icon={<img src={brandImg} alt="" width={25} height={25} />} |
| 73 |
component={(props) => <MockLink {...props} to="#router-link2" />} |
| 74 |
> |
| 75 |
Custom component with icon |
| 76 |
</DropdownItem> |
| 77 |
</DropdownList> |
| 78 |
</DropdownGroup>, |
| 79 |
<Divider key="group2-divider" />, |
| 80 |
<DropdownList key="other-items"> |
| 81 |
<DropdownItem key="tooltip-app" isFavorited={favorites.includes('4')} value="4" id="4"> |
| 82 |
<Tooltip content={<div>Launch Application 3</div>} position="right"> |
| 83 |
<span>Application 3 with tooltip</span> |
| 84 |
</Tooltip> |
| 85 |
</DropdownItem> |
| 86 |
<DropdownItem key="disabled-app" value="5" id="5" isDisabled> |
| 87 |
Unavailable Application |
| 88 |
</DropdownItem> |
| 89 |
</DropdownList> |
| 90 |
]; |
| 91 |
|
| 92 |
const createFavorites = (favIds: string[]) => { |
| 93 |
const favorites: unknown[] = []; |
| 94 |
|
| 95 |
menuItems.forEach((item) => { |
| 96 |
if (item.type === DropdownList) { |
| 97 |
item.props.children.filter((child) => { |
| 98 |
if (favIds.includes(child.props.value)) { |
| 99 |
favorites.push(child); |
| 100 |
} |
| 101 |
}); |
| 102 |
} else if (item.type === DropdownGroup) { |
| 103 |
item.props.children.props.children.filter((child) => { |
| 104 |
if (favIds.includes(child.props.value)) { |
| 105 |
favorites.push(child); |
| 106 |
} |
| 107 |
}); |
| 108 |
} else { |
| 109 |
if (favIds.includes(item.props.value)) { |
| 110 |
favorites.push(item); |
| 111 |
} |
| 112 |
} |
| 113 |
}); |
| 114 |
|
| 115 |
return favorites; |
| 116 |
}; |
| 117 |
|
| 118 |
const filterItems = (items: any[], filteredIds: string[]) => { |
| 119 |
if (filteredIds.length === 1 && filteredIds[0] === '*') { |
| 120 |
return items; |
| 121 |
} |
| 122 |
let keepDivider = false; |
| 123 |
const filteredCopy = items |
| 124 |
.map((group) => { |
| 125 |
if (group.type === DropdownGroup) { |
| 126 |
const filteredGroup = cloneElement(group, { |
| 127 |
children: cloneElement(group.props.children, { |
| 128 |
children: group.props.children.props.children.filter((child) => { |
| 129 |
if (filteredIds.includes(child.props.value)) { |
| 130 |
return child; |
| 131 |
} |
| 132 |
}) |
| 133 |
}) |
| 134 |
}); |
| 135 |
const filteredList = filteredGroup.props.children; |
| 136 |
if (filteredList.props.children.length > 0) { |
| 137 |
keepDivider = true; |
| 138 |
return filteredGroup; |
| 139 |
} else { |
| 140 |
keepDivider = false; |
| 141 |
} |
| 142 |
} else if (group.type === DropdownList) { |
| 143 |
const filteredGroup = cloneElement(group, { |
| 144 |
children: group.props.children.filter((child) => { |
| 145 |
if (filteredIds.includes(child.props.value)) { |
| 146 |
return child; |
| 147 |
} |
| 148 |
}) |
| 149 |
}); |
| 150 |
if (filteredGroup.props.children.length > 0) { |
| 151 |
keepDivider = true; |
| 152 |
return filteredGroup; |
| 153 |
} else { |
| 154 |
keepDivider = false; |
| 155 |
} |
| 156 |
} else { |
| 157 |
if ((keepDivider && group.type === Divider) || filteredIds.includes(group.props.value)) { |
| 158 |
return group; |
| 159 |
} |
| 160 |
} |
| 161 |
}) |
| 162 |
.filter((newGroup) => newGroup); |
| 163 |
|
| 164 |
if (filteredCopy.length > 0) { |
| 165 |
const lastGroup = filteredCopy.pop(); |
| 166 |
if (lastGroup.type !== Divider) { |
| 167 |
filteredCopy.push(lastGroup); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return filteredCopy; |
| 172 |
}; |
| 173 |
|
| 174 |
const onTextChange = (textValue: string) => { |
| 175 |
if (textValue === '') { |
| 176 |
setFilteredIds(['*']); |
| 177 |
return; |
| 178 |
} |
| 179 |
|
| 180 |
const filteredIds = |
| 181 |
refFullOptions |
| 182 |
?.filter((item) => (item as HTMLElement).innerText.toLowerCase().includes(textValue.toString().toLowerCase())) |
| 183 |
.map((item) => item.id) || []; |
| 184 |
setFilteredIds(filteredIds); |
| 185 |
}; |
| 186 |
|
| 187 |
const onFavorite = (event: any, value: string, actionId: string) => { |
| 188 |
event.stopPropagation(); |
| 189 |
if (actionId === 'fav') { |
| 190 |
const isFavorite = favorites.includes(value); |
| 191 |
if (isFavorite) { |
| 192 |
setFavorites(favorites.filter((fav) => fav !== value)); |
| 193 |
} else { |
| 194 |
setFavorites([...favorites, value]); |
| 195 |
} |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
const filteredFavorites = filterItems(createFavorites(favorites), filteredIds); |
| 200 |
const filteredItems = filterItems(menuItems, filteredIds); |
| 201 |
if (filteredItems.length === 0) { |
| 202 |
filteredItems.push(<DropdownItem key="no-items">No results found</DropdownItem>); |
| 203 |
} |
| 204 |
|
| 205 |
return ( |
| 206 |
<Dropdown |
| 207 |
isOpen={isOpen} |
| 208 |
onOpenChange={(isOpen) => setIsOpen(isOpen)} |
| 209 |
onOpenChangeKeys={['Escape']} |
| 210 |
toggle={(toggleRef) => ( |
| 211 |
<MenuToggle |
| 212 |
aria-label="Toggle" |
| 213 |
ref={toggleRef} |
| 214 |
variant="plain" |
| 215 |
onClick={onToggleClick} |
| 216 |
isExpanded={isOpen} |
| 217 |
style={{ width: 'auto' }} |
| 218 |
icon={<RhUiThumbnailViewSmallFillIcon />} |
| 219 |
/> |
| 220 |
)} |
| 221 |
ref={menuRef} |
| 222 |
onActionClick={onFavorite} |
| 223 |
// eslint-disable-next-line no-console |
| 224 |
onSelect={(_ev, value) => console.log('selected', value)} |
| 225 |
> |
| 226 |
<MenuSearch> |
| 227 |
<MenuSearchInput> |
| 228 |
<SearchInput aria-label="Filter menu items" onChange={(_event, value) => onTextChange(value)} /> |
| 229 |
</MenuSearchInput> |
| 230 |
</MenuSearch> |
| 231 |
<Divider /> |
| 232 |
{filteredFavorites.length > 0 && ( |
| 233 |
<Fragment> |
| 234 |
<DropdownGroup key="favorites-group" label="Favorites"> |
| 235 |
<DropdownList>{filteredFavorites}</DropdownList> |
| 236 |
</DropdownGroup> |
| 237 |
<Divider key="favorites-divider" /> |
| 238 |
</Fragment> |
| 239 |
)} |
| 240 |
{filteredItems} |
| 241 |
</Dropdown> |
| 242 |
); |
| 243 |
}; |