From 221a67b6f3e1287dfabfaac7cd6b263bf7ed7220 Mon Sep 17 00:00:00 2001 From: Javier Ortiz Date: Tue, 17 Nov 2026 17:03:43 +0100 Subject: [PATCH] fix(app): select the source selector label on click so typing replaces it --- .../source-select-select-all-on-focus.md | 6 ++++ packages/app/src/components/SourceSelect.tsx | 3 ++ .../__tests__/SourceSelect.test.tsx | 32 ++++++++++++++++- packages/app/src/hooks/useSelectAllOnFocus.ts | 34 +++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 .changeset/source-select-select-all-on-focus.md create mode 100644 packages/app/src/hooks/useSelectAllOnFocus.ts diff --git a/.changeset/source-select-select-all-on-focus.md b/.changeset/source-select-select-all-on-focus.md new file mode 100644 index 000000000..ef5c4aca4 --- /dev/null +++ b/.changeset/source-select-select-all-on-focus.md @@ -0,0 +1,6 @@ +--- +'@hyperdx/app': patch +--- + +fix: clicking the source selector selects its current label so typing replaces +it instead of inserting into the middle of the old name diff --git a/packages/app/src/components/SourceSelect.tsx b/packages/app/src/components/SourceSelect.tsx index c17bfae30..2d5baeb78 100644 --- a/packages/app/src/components/SourceSelect.tsx +++ b/packages/app/src/components/SourceSelect.tsx @@ -27,6 +27,7 @@ import { useFilteredSortedSourceItems, useSourceKindMap, } from '@/components/sourceSelectUtils'; +import { useSelectAllOnFocus } from '@/hooks/useSelectAllOnFocus'; import { useSources } from '@/source'; import styles from '@styles/SourceSelectControlled.module.scss'; @@ -192,6 +193,7 @@ function SourceSelectControlledComponent({ ); const sourceKindMap = useSourceKindMap(data); + const selectAllOnFocus = useSelectAllOnFocus(); // Mantine passes `checked` to renderOption for the currently selected // option; render a trailing check so the active source is obvious in the @@ -249,6 +251,7 @@ function SourceSelectControlledComponent({ renderOption={renderOption} filter={sourceSelectFilter} searchable + {...selectAllOnFocus} placeholder="Data Source" leftSection={leftIcon} maxDropdownHeight={280} diff --git a/packages/app/src/components/__tests__/SourceSelect.test.tsx b/packages/app/src/components/__tests__/SourceSelect.test.tsx index 85414ef9a..de098a16c 100644 --- a/packages/app/src/components/__tests__/SourceSelect.test.tsx +++ b/packages/app/src/components/__tests__/SourceSelect.test.tsx @@ -183,10 +183,14 @@ describe('SourceSelectControlled (grouped rendering + tag-style search)', () => function SelectHarness({ isSourceAllowed, + defaultSource, }: { isSourceAllowed?: (source: TSource) => boolean; + defaultSource?: string; } = {}) { - const { control } = useForm(); + const { control } = useForm({ + defaultValues: { source: defaultSource }, + }); return ( expect(screen.queryByText('Control Plane Prod')).not.toBeInTheDocument(); }); + it('selects the current label on a focusing click so typing replaces it', async () => { + asMock(useSources).mockReturnValue({ data: SECTIONED_SOURCES }); + renderWithMantine(); + const input = screen.getByPlaceholderText('Data Source'); + expect(input).toHaveValue('Billing Logs'); + + await userEvent.click(input); + expect(input.selectionStart).toBe(0); + expect(input.selectionEnd).toBe('Billing Logs'.length); + + await userEvent.keyboard('prod'); + expect(input).toHaveValue('prod'); + expect(await screen.findByText('Prod Logs')).toBeInTheDocument(); + expect(screen.queryByText('Billing Logs')).not.toBeInTheDocument(); + }); + + it('places the caret normally on a click into an already focused input', async () => { + asMock(useSources).mockReturnValue({ data: SECTIONED_SOURCES }); + renderWithMantine(); + const input = screen.getByPlaceholderText('Data Source'); + + await userEvent.click(input); + await userEvent.click(input); + expect(input.selectionStart).toBe(input.selectionEnd); + }); + it('hides sources rejected by isSourceAllowed, along with sections left empty', async () => { asMock(useSources).mockReturnValue({ data: SECTIONED_SOURCES }); renderWithMantine( diff --git a/packages/app/src/hooks/useSelectAllOnFocus.ts b/packages/app/src/hooks/useSelectAllOnFocus.ts new file mode 100644 index 000000000..2761bf4b3 --- /dev/null +++ b/packages/app/src/hooks/useSelectAllOnFocus.ts @@ -0,0 +1,34 @@ +import { MouseEvent, useCallback, useRef } from 'react'; +import type { FocusEvent } from 'react'; + +const selectAll = (input: HTMLInputElement) => + input.setSelectionRange(0, input.value.length); + +/** + * Handlers that select a text input's whole value when it gains focus, so + * typing replaces the value instead of inserting at the click point. + * + * Browsers collapse the selection to the pointer position on the mouseup + * that completes a focusing click, so that mouseup is cancelled. A click on + * an input that already has focus places the caret as usual. + */ +export function useSelectAllOnFocus() { + const focusingByMouseRef = useRef(false); + + const onMouseDown = useCallback((event: MouseEvent) => { + focusingByMouseRef.current = document.activeElement !== event.currentTarget; + }, []); + + const onMouseUp = useCallback((event: MouseEvent) => { + if (!focusingByMouseRef.current) return; + focusingByMouseRef.current = false; + event.preventDefault(); + selectAll(event.currentTarget); + }, []); + + const onFocus = useCallback((event: FocusEvent) => { + selectAll(event.currentTarget); + }, []); + + return { onFocus, onMouseDown, onMouseUp }; +}