diff --git a/dist/Combobox.d.ts b/dist/Combobox.d.ts --- a/dist/Combobox.d.ts +++ b/dist/Combobox.d.ts @@ -210,6 +210,7 @@ readonly autoHighlight: Schema.optional; readonly highlightItemOnHover: Schema.optional; readonly loopFocus: Schema.optional; + readonly clearOnEscape: Schema.optional; readonly openOnInputClick: Schema.optional; readonly inputInPopup: Schema.optional; readonly inline: Schema.optional; @@ -257,6 +258,8 @@ * True when left out. * - `loopFocus`: the arrow keys go past the ends of the list and around. * True when left out. + * - `clearOnEscape`: false lets an ancestor handle Escape when the popup + * is closed, preserving the query and value. True when left out. * - `openOnInputClick`: a press on the input opens the popup. True when * left out. * - `inputInPopup`: the input is drawn inside the popup, and the trigger @@ -358,6 +361,7 @@ readonly autoHighlight: Schema.optional; readonly highlightItemOnHover: Schema.optional; readonly loopFocus: Schema.optional; + readonly clearOnEscape: Schema.optional; readonly openOnInputClick: Schema.optional; readonly inputInPopup: Schema.optional; readonly inline: Schema.optional; @@ -639,6 +643,7 @@ readonly autoHighlight?: boolean | undefined; readonly highlightItemOnHover?: boolean | undefined; readonly loopFocus?: boolean | undefined; + readonly clearOnEscape?: boolean | undefined; readonly openOnInputClick?: boolean | undefined; readonly inputInPopup?: boolean | undefined; readonly inline?: boolean | undefined; @@ -919,6 +924,7 @@ readonly autoHighlight?: boolean | undefined; readonly highlightItemOnHover?: boolean | undefined; readonly loopFocus?: boolean | undefined; + readonly clearOnEscape?: boolean | undefined; readonly openOnInputClick?: boolean | undefined; readonly inputInPopup?: boolean | undefined; readonly inline?: boolean | undefined; diff --git a/dist/Combobox.js b/dist/Combobox.js --- a/dist/Combobox.js +++ b/dist/Combobox.js @@ -125,8 +125,9 @@ * picks the highlighted item, and with nothing highlighted it closes the * popup on its keyup, leaving the keydown to submit a form. Space picks the * highlighted item while the input is empty. Escape closes the popup, and - * on a closed combobox clears the text and the value. Typing opens the - * popup. Keys pressed while an IME composes text are left alone. + * on a closed combobox clears the text and the value unless + * `clearOnEscape` is false. Typing opens the popup. Keys pressed while an + * IME composes text are left alone. * * With `grid`, ArrowLeft and ArrowRight move between the items of a row * once one is highlighted. With chips, ArrowLeft with the caret at the @@ -368,6 +369,7 @@ autoHighlight: Schema.optional(Schema.Boolean), highlightItemOnHover: Schema.optional(Schema.Boolean), loopFocus: Schema.optional(Schema.Boolean), + clearOnEscape: Schema.optional(Schema.Boolean), openOnInputClick: Schema.optional(Schema.Boolean), inputInPopup: Schema.optional(Schema.Boolean), inline: Schema.optional(Schema.Boolean), @@ -417,6 +419,8 @@ * True when left out. * - `loopFocus`: the arrow keys go past the ends of the list and around. * True when left out. + * - `clearOnEscape`: false lets an ancestor handle Escape when the popup + * is closed, preserving the query and value. True when left out. * - `openOnInputClick`: a press on the input opens the popup. True when * left out. * - `inputInPopup`: the input is drawn inside the popup, and the trigger diff --git a/dist/Dialog.js b/dist/Dialog.js --- a/dist/Dialog.js +++ b/dist/Dialog.js @@ -261,12 +261,13 @@ const focusFirst = from => { const root = popup(); if (root.contains(from)) return; + const focused = focusedNow(); if (!current.initialFocus) { - const focused = focusedNow(); if (focused === null || !node.contains(focused)) return; if (isHTMLElement(from) && from !== document.body && from.isConnected) handOff(from);else focused.blur(); return; } + if (focused !== root && root.contains(focused)) return; const marked = Array.from(node.querySelectorAll("[autofocus]")).find(element => !inNested(node, element)); const visible = current.openMethod === "keyboard"; if (marked === undefined && current.openMethod !== "touch" && tabbables(node, root).some(element => takes(element, visible))) return; diff --git a/dist/internal/combobox.d.ts b/dist/internal/combobox.d.ts --- a/dist/internal/combobox.d.ts +++ b/dist/internal/combobox.d.ts @@ -62,6 +62,7 @@ readonly submitOnItemClick?: boolean | undefined; readonly highlightItemOnHover?: boolean | undefined; readonly loopFocus?: boolean | undefined; + readonly clearOnEscape?: boolean | undefined; readonly openOnInputClick?: boolean | undefined; readonly inputInPopup?: boolean | undefined; readonly inline?: boolean | undefined; diff --git a/dist/internal/combobox.js b/dist/internal/combobox.js --- a/dist/internal/combobox.js +++ b/dist/internal/combobox.js @@ -469,7 +469,7 @@ return highlighted === null ? step : pick(step, props, highlighted, "keyboard"); } case "Escape": - return props.open ? dismiss(step, props, "escape-key", "keyboard") : clear(step, props, "escape-key"); + return props.open ? dismiss(step, props, "escape-key", "keyboard") : props.clearOnEscape === false ? step : clear(step, props, "escape-key"); case "Backspace": return values.length > 0 ? removeChip(step, props, values[values.length - 1], "none") : step; default: @@ -1129,7 +1129,7 @@ case " ": return !readOnly && open && props.inputValue === ""; case "Escape": - return open || !readOnly && !inline && !model.mounted && (props.inputValue !== "" || filled); + return open || !readOnly && !inline && !model.mounted && props.clearOnEscape !== false && (props.inputValue !== "" || filled); case "Backspace": return !readOnly && chipsDrawn && props.inputValue === "" && values.length > 0; default: @@ -1194,7 +1194,7 @@ .on("keyup", ({ composing, key - }) => !composing && (key === "Enter" && open && !inline && highlighted === null || key === "Escape" && inline && !readOnly && (props.inputValue !== "" || filled)) ? Message.ReleasedInputKey({ + }) => !composing && (key === "Enter" && open && !inline && highlighted === null || key === "Escape" && inline && !readOnly && props.clearOnEscape !== false && (props.inputValue !== "" || filled)) ? Message.ReleasedInputKey({ key }) : undefined); const focusable = inline ? decorated.on("focusin", Message.FocusedInput()).on("focusout", Message.BlurredInput()) : decorated; diff --git a/src/Combobox.ts b/src/Combobox.ts --- a/src/Combobox.ts +++ b/src/Combobox.ts @@ -125,8 +125,9 @@ * picks the highlighted item, and with nothing highlighted it closes the * popup on its keyup, leaving the keydown to submit a form. Space picks the * highlighted item while the input is empty. Escape closes the popup, and - * on a closed combobox clears the text and the value. Typing opens the - * popup. Keys pressed while an IME composes text are left alone. + * on a closed combobox clears the text and the value unless + * `clearOnEscape` is false. Typing opens the popup. Keys pressed while an + * IME composes text are left alone. * * With `grid`, ArrowLeft and ArrowRight move between the items of a row * once one is highlighted. With chips, ArrowLeft with the caret at the @@ -431,6 +432,7 @@ autoHighlight: Schema.optional(Schema.Boolean), highlightItemOnHover: Schema.optional(Schema.Boolean), loopFocus: Schema.optional(Schema.Boolean), + clearOnEscape: Schema.optional(Schema.Boolean), openOnInputClick: Schema.optional(Schema.Boolean), inputInPopup: Schema.optional(Schema.Boolean), inline: Schema.optional(Schema.Boolean), @@ -481,6 +483,8 @@ * True when left out. * - `loopFocus`: the arrow keys go past the ends of the list and around. * True when left out. + * - `clearOnEscape`: false lets an ancestor handle Escape when the popup + * is closed, preserving the query and value. True when left out. * - `openOnInputClick`: a press on the input opens the popup. True when * left out. * - `inputInPopup`: the input is drawn inside the popup, and the trigger diff --git a/src/Dialog.ts b/src/Dialog.ts --- a/src/Dialog.ts +++ b/src/Dialog.ts @@ -671,8 +671,8 @@ const focusFirst = (from: Element | null) => { const root = popup() if (root.contains(from)) return + const focused = focusedNow() if (!current.initialFocus) { - const focused = focusedNow() if (focused === null || !node.contains(focused)) return if ( isHTMLElement(from) && from !== document.body && from.isConnected @@ -680,6 +680,7 @@ else (focused as HTMLElement).blur() return } + if (focused !== root && root.contains(focused)) return const marked = Array.from( node.querySelectorAll("[autofocus]") ).find((element) => !inNested(node, element)) diff --git a/src/internal/combobox.ts b/src/internal/combobox.ts --- a/src/internal/combobox.ts +++ b/src/internal/combobox.ts @@ -174,6 +174,7 @@ readonly submitOnItemClick?: boolean | undefined readonly highlightItemOnHover?: boolean | undefined readonly loopFocus?: boolean | undefined + readonly clearOnEscape?: boolean | undefined readonly openOnInputClick?: boolean | undefined readonly inputInPopup?: boolean | undefined readonly inline?: boolean | undefined @@ -822,6 +823,8 @@ case "Escape": return props.open ? dismiss(step, props, "escape-key", "keyboard") + : props.clearOnEscape === false + ? step : clear(step, props, "escape-key") case "Backspace": return values.length > 0 @@ -1668,7 +1671,7 @@ return !readOnly && open && props.inputValue === "" case "Escape": return open || - (!readOnly && !inline && !model.mounted && + (!readOnly && !inline && !model.mounted && props.clearOnEscape !== false && (props.inputValue !== "" || filled)) case "Backspace": return !readOnly && chipsDrawn && props.inputValue === "" && @@ -1744,7 +1747,7 @@ ({ composing, key }) => !composing && ((key === "Enter" && open && !inline && highlighted === null) || - (key === "Escape" && inline && !readOnly && + (key === "Escape" && inline && !readOnly && props.clearOnEscape !== false && (props.inputValue !== "" || filled))) ? Message.ReleasedInputKey({ key }) : undefined