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

Refactor how Dropdown Button track focus. #8766

Merged
merged 1 commit into from
May 24, 2024
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
13 changes: 8 additions & 5 deletions components/frontend/src/widgets/Button.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { array, arrayOf, bool, func, string } from "prop-types"
import { useRef, useState } from "react"
import { useState } from "react"
import { Icon, Input } from "semantic-ui-react"

import { Button, Checkbox, Dropdown, Label, Popup } from "../semantic_ui_react_wrappers"
Expand Down Expand Up @@ -66,14 +66,14 @@ export function AddDropdownButton({ itemSubtypes, itemType, onClick, allItemSubt
const [query, setQuery] = useState("") // Search query to filter item subtypes
const [menuOpen, setMenuOpen] = useState(false) // Is the menu open?
const [popupTriggered, setPopupTriggered] = useState(false) // Is the popup triggered by hover or focus?
const [inputHasFocus, setInputHasFocus] = useState(false) // Does the input have focus?
const [showUnsupportedItems, setShowUnsupportedItems] = useState(false) // Show only supported itemSubTypes or also unsupported itemSubTypes?
const [hideUsedItems, setHideUsedItems] = useState(false) // Hide itemSubTypes already used?
let items = showUnsupportedItems ? allItemSubtypes : itemSubtypes
if (hideUsedItems) {
items = items.filter((item) => !usedItemSubtypeKeys.includes(item.key))
}
const options = items.filter((itemSubtype) => itemSubtype.text.toLowerCase().includes(query.toLowerCase()))
const inputRef = useRef(null)
return (
<Popup
content={`Add a new ${itemType} here`}
Expand All @@ -86,7 +86,7 @@ export function AddDropdownButton({ itemSubtypes, itemType, onClick, allItemSubt
basic
className="button icon primary"
floating
onBlur={() => setQuery("")}
//onBlur={() => setQuery("")}
onClose={() => setMenuOpen(false)}
onKeyDown={(event) => {
if (!menuOpen) {
Expand All @@ -95,7 +95,7 @@ export function AddDropdownButton({ itemSubtypes, itemType, onClick, allItemSubt
if (event.key === "Escape") {
setQuery("")
}
if (inputRef.current?.inputRef?.current !== document.activeElement) {
if (!inputHasFocus) {
// Allow for editing the query without the input having focus
if (event.key === "Backspace") {
setQuery(query.slice(0, query.length - 1))
Expand Down Expand Up @@ -141,14 +141,17 @@ export function AddDropdownButton({ itemSubtypes, itemType, onClick, allItemSubt
icon="search"
iconPosition="left"
onBlur={(event) => {
setInputHasFocus(false)
if (allItemSubtypes) {
event.stopPropagation()
} // Prevent tabbing to the checkbox from clearing the input
}}
onChange={(_event, { value }) => setQuery(value)}
onClick={stopEventPropagation}
onFocus={() => {
setInputHasFocus(true)
}}
onKeyDown={stopEventPropagationOnSpace}
ref={inputRef}
placeholder={`Filter ${itemType} types`}
value={query}
/>
Expand Down