/* Copyright 2022 The Matrix.org Foundation C.I.C. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import classNames from "classnames"; import React, { FunctionComponent, Key, PropsWithChildren, ReactNode } from "react"; import { MenuItemRadio } from "../../accessibility/context_menu/MenuItemRadio"; import { ButtonEvent } from "../views/elements/AccessibleButton"; import ContextMenu, { aboveLeftOf, ChevronFace, ContextMenuButton, useContextMenu } from "./ContextMenu"; export type GenericDropdownMenuOption = { key: T; label: ReactNode; description?: ReactNode; adornment?: ReactNode; }; export type GenericDropdownMenuGroup = GenericDropdownMenuOption & { options: GenericDropdownMenuOption[]; }; export type GenericDropdownMenuItem = GenericDropdownMenuGroup | GenericDropdownMenuOption; export function GenericDropdownMenuOption({ label, description, onClick, isSelected, adornment, }: GenericDropdownMenuOption & { onClick: (ev: ButtonEvent) => void; isSelected: boolean; }): JSX.Element { return (
{label} {description}
{adornment}
); } export function GenericDropdownMenuGroup({ label, description, adornment, children, }: PropsWithChildren>): JSX.Element { return ( <>
{label} {description}
{adornment}
{children} ); } function isGenericDropdownMenuGroupArray( items: readonly GenericDropdownMenuItem[], ): items is GenericDropdownMenuGroup[] { return isGenericDropdownMenuGroup(items[0]); } function isGenericDropdownMenuGroup(item: GenericDropdownMenuItem): item is GenericDropdownMenuGroup { return "options" in item; } type WithKeyFunction = T extends Key ? { toKey?: (key: T) => Key; } : { toKey: (key: T) => Key; }; type IProps = WithKeyFunction & { value: T; options: readonly GenericDropdownMenuOption[] | readonly GenericDropdownMenuGroup[]; onChange: (option: T) => void; selectedLabel: (option: GenericDropdownMenuItem | null | undefined) => ReactNode; onOpen?: (ev: ButtonEvent) => void; onClose?: (ev: ButtonEvent) => void; className?: string; AdditionalOptions?: FunctionComponent<{ menuDisplayed: boolean; closeMenu: () => void; openMenu: () => void; }>; }; export function GenericDropdownMenu({ value, onChange, options, selectedLabel, onOpen, onClose, toKey, className, AdditionalOptions, }: IProps): JSX.Element { const [menuDisplayed, button, openMenu, closeMenu] = useContextMenu(); const selected: GenericDropdownMenuItem | null = options .flatMap((it) => (isGenericDropdownMenuGroup(it) ? [it, ...it.options] : [it])) .find((option) => (toKey ? toKey(option.key) === toKey(value) : option.key === value)); let contextMenuOptions: JSX.Element; if (options && isGenericDropdownMenuGroupArray(options)) { contextMenuOptions = ( <> {options.map((group) => ( {group.options.map((option) => ( { onChange(option.key); closeMenu(); onClose?.(ev); }} adornment={option.adornment} isSelected={option === selected} /> ))} ))} ); } else { contextMenuOptions = ( <> {options.map((option) => ( { onChange(option.key); closeMenu(); onClose?.(ev); }} adornment={option.adornment} isSelected={option === selected} /> ))} ); } const contextMenu = menuDisplayed && button.current ? ( {contextMenuOptions} {AdditionalOptions && ( )} ) : null; return ( <> { openMenu(); onOpen?.(ev); }} > {selectedLabel(selected)} {contextMenu} ); }