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

[feature] add closeOnSelect on dropdown #1623

Merged
merged 1 commit into from
Aug 23, 2022
Merged
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
18 changes: 12 additions & 6 deletions @navikt/internal/react/src/dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<DropdownProps> {
Expand All @@ -19,12 +25,12 @@ export interface DropdownContextType {
setIsOpen: React.Dispatch<React.SetStateAction<boolean>>;
readonly anchorEl: Element | null;
setAnchorEl: React.Dispatch<React.SetStateAction<Element | null>>;
onSelect: DropdownElementChosenHandler;
onSelect: (element: React.MouseEvent) => void;
}

export const DropdownContext = createContext<DropdownContextType | null>(null);

export const Dropdown = (({ children, onSelect }) => {
export const Dropdown = (({ children, onSelect, closeOnSelect = true }) => {
const [isOpen, setIsOpen] = useState<boolean>(false);
const [anchorEl, setAnchorEl] = useState<Element | null>(null);

Expand All @@ -37,7 +43,7 @@ export const Dropdown = (({ children, onSelect }) => {
setAnchorEl,
onSelect: (event) => {
onSelect?.(event);
setIsOpen(false);
closeOnSelect && setIsOpen(false);
},
}}
>
Expand Down