From b6ae67ff4ddddd4d13a9382a9070f07141ead3c9 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela <47495861+HassanAbouelela@users.noreply.github.com> Date: Sun, 17 Jan 2021 02:26:25 +0300 Subject: Closes Select Menu On Click Adds logic to close the select menu when clicked, if the menu was already open. Signed-off-by: Hassan Abouelela <47495861+HassanAbouelela@users.noreply.github.com> --- src/components/InputTypes/Select.tsx | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/components/InputTypes/Select.tsx b/src/components/InputTypes/Select.tsx index a4db3dd..e753357 100644 --- a/src/components/InputTypes/Select.tsx +++ b/src/components/InputTypes/Select.tsx @@ -157,14 +157,35 @@ class Select extends React.Component { selected_option.current.textContent = option_container.textContent; } + handle_click( + container: React.RefObject, + selected_option: React.RefObject, + event: React.MouseEvent | React.KeyboardEvent + ): void { + if (!container.current || !selected_option.current || (event.type === "keydown" && (event as React.KeyboardEvent).code !== "Space")) { + return; + } + + // Check if menu is open + if (container.current.contains(document.activeElement)) { + // Close menu + selected_option.current.focus(); + selected_option.current.blur(); + event.preventDefault(); + } + } + render(): JSX.Element { + const container_ref: React.RefObject = React.createRef(); const selected_option_ref: React.RefObject = React.createRef(); + const handle_click = (event: React.MouseEvent | React.KeyboardEvent) => this.handle_click(container_ref, selected_option_ref, event); + return ( -
+
-
...
+
...
-- cgit v1.2.3