diff --git a/@navikt/internal/react/src/dropdown/Dropdown.tsx b/@navikt/internal/react/src/dropdown/Dropdown.tsx index 0aaaa029fd..99f09ab527 100644 --- a/@navikt/internal/react/src/dropdown/Dropdown.tsx +++ b/@navikt/internal/react/src/dropdown/Dropdown.tsx @@ -2,11 +2,17 @@ import React, { createContext, useState } from "react"; import Toggle, { ToggleType } from "./Toggle"; import Menu, { MenuType } from "./Menu"; -type DropdownElementChosenHandler = (element: React.MouseEvent) => void; - export interface DropdownProps { children: React.ReactNode; - onSelect?: DropdownElementChosenHandler; + /** + * Handler that is called when an item is selected. + */ + onSelect?: (element: React.MouseEvent) => void; + /** + * Whether the Menu closes when a selection is made. + * @default true + */ + closeOnSelect?: boolean; } export interface DropdownType extends React.FC { @@ -19,12 +25,12 @@ export interface DropdownContextType { setIsOpen: React.Dispatch>; readonly anchorEl: Element | null; setAnchorEl: React.Dispatch>; - onSelect: DropdownElementChosenHandler; + onSelect: (element: React.MouseEvent) => void; } export const DropdownContext = createContext(null); -export const Dropdown = (({ children, onSelect }) => { +export const Dropdown = (({ children, onSelect, closeOnSelect = true }) => { const [isOpen, setIsOpen] = useState(false); const [anchorEl, setAnchorEl] = useState(null); @@ -37,7 +43,7 @@ export const Dropdown = (({ children, onSelect }) => { setAnchorEl, onSelect: (event) => { onSelect?.(event); - setIsOpen(false); + closeOnSelect && setIsOpen(false); }, }} >