Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changeset/source-select-select-all-on-focus.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions packages/app/src/components/SourceSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -249,6 +251,7 @@ function SourceSelectControlledComponent({
renderOption={renderOption}
filter={sourceSelectFilter}
searchable
{...selectAllOnFocus}
placeholder="Data Source"
leftSection={leftIcon}
maxDropdownHeight={280}
Expand Down
32 changes: 31 additions & 1 deletion packages/app/src/components/__tests__/SourceSelect.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<SourceSelectControlled
control={control}
Expand Down Expand Up @@ -233,6 +237,32 @@ describe('SourceSelectControlled (grouped rendering + tag-style search)', () =>
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(<SelectHarness defaultSource="billing-logs" />);
const input = screen.getByPlaceholderText<HTMLInputElement>('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(<SelectHarness defaultSource="billing-logs" />);
const input = screen.getByPlaceholderText<HTMLInputElement>('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(
Expand Down
34 changes: 34 additions & 0 deletions packages/app/src/hooks/useSelectAllOnFocus.ts
Original file line number Diff line number Diff line change
@@ -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<HTMLInputElement>) => {
focusingByMouseRef.current = document.activeElement !== event.currentTarget;
}, []);

const onMouseUp = useCallback((event: MouseEvent<HTMLInputElement>) => {
if (!focusingByMouseRef.current) return;
focusingByMouseRef.current = false;
event.preventDefault();
selectAll(event.currentTarget);
}, []);

const onFocus = useCallback((event: FocusEvent<HTMLInputElement>) => {
selectAll(event.currentTarget);
}, []);

return { onFocus, onMouseDown, onMouseUp };
}
Loading