Skip to content

Commit 3a10f62

Browse files
fix(app): select the source selector label on click so typing replaces it (#3696)
## Summary The source selector is a searchable Mantine Select, and clicking it puts the caret wherever the pointer lands inside the current label. Typing `Data plane` over `ControlPlane Logs` produces `ControlData planePlane Logs`, the dropdown filters to nothing, and the user has to clear the box by hand before every switch. This PR selects the whole label on the click that focuses the selector, so typing replaces it. **A second click into an already focused selector places the caret as usual**, and Escape or clicking away still restores the previous label. The handlers live in `useSelectAllOnFocus` and only `SourceSelectControlled` uses them, so the aggregation, database, table and filter dropdowns are unchanged. A unit test pins replace-on-click and the second-click caret. ### Screenshots or video Not captured: this change was made without a browser. A short video of click-then-type on the source box shows it best. | Before | After | | :----- | :---- | | <img width="800" height="450" alt="Search---Observability-ezgif com-video-to-gif-converter" src="https://github.com/user-attachments/assets/492957a0-261e-43cf-90d9-4b89dc46531f" /> | <img width="800" height="450" alt="Search---ClickStacknew-ezgif com-video-to-gif-converter" src="https://github.com/user-attachments/assets/4a5847f1-f926-4a5a-9f18-4ea52271efa4" /> | ### How to reproduce **Starting point:** https://play.hyperdx.io/search, or `yarn app:dev:local` connected to the demo server. **On `main`:** 1. Click in the middle of the source selector text (placeholder **Data Source**), between two letters of the current source name. 2. Type `tra`. 3. The box shows the old name with `tra` inserted in the middle and the dropdown has no matching source. **On this branch:** 1. Click anywhere in the source selector text. 2. The whole name is highlighted. Type `tra`. 3. The box shows only `tra` and the dropdown lists the sources whose name or section contains it. 4. Click the box again: the caret lands where you clicked instead of selecting all. 5. Press Escape: the previous source name is back. ### How to test on Vercel preview **Preview routes:** /search **Steps:** 1. Click in the middle of the source selector input with placeholder **Data Source**. 2. Type `tra`. 3. Verify the input value is exactly `tra` and the dropdown lists at least one source whose name contains "Tra". 4. Press Escape and verify the input shows the previously selected source name again. ### References - Linear Issue: n/a - Related PRs: n/a
1 parent 50129d1 commit 3a10f62

4 files changed

Lines changed: 74 additions & 1 deletion

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@hyperdx/app': patch
3+
---
4+
5+
fix: clicking the source selector selects its current label so typing replaces
6+
it instead of inserting into the middle of the old name

‎packages/app/src/components/SourceSelect.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
useFilteredSortedSourceItems,
2828
useSourceKindMap,
2929
} from '@/components/sourceSelectUtils';
30+
import { useSelectAllOnFocus } from '@/hooks/useSelectAllOnFocus';
3031
import { useSources } from '@/source';
3132

3233
import styles from '@styles/SourceSelectControlled.module.scss';
@@ -192,6 +193,7 @@ function SourceSelectControlledComponent({
192193
);
193194

194195
const sourceKindMap = useSourceKindMap(data);
196+
const selectAllOnFocus = useSelectAllOnFocus();
195197

196198
// Mantine passes `checked` to renderOption for the currently selected
197199
// option; render a trailing check so the active source is obvious in the
@@ -249,6 +251,7 @@ function SourceSelectControlledComponent({
249251
renderOption={renderOption}
250252
filter={sourceSelectFilter}
251253
searchable
254+
{...selectAllOnFocus}
252255
placeholder="Data Source"
253256
leftSection={leftIcon}
254257
maxDropdownHeight={280}

‎packages/app/src/components/__tests__/SourceSelect.test.tsx‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,14 @@ describe('SourceSelectControlled (grouped rendering + tag-style search)', () =>
183183

184184
function SelectHarness({
185185
isSourceAllowed,
186+
defaultSource,
186187
}: {
187188
isSourceAllowed?: (source: TSource) => boolean;
189+
defaultSource?: string;
188190
} = {}) {
189-
const { control } = useForm();
191+
const { control } = useForm({
192+
defaultValues: { source: defaultSource },
193+
});
190194
return (
191195
<SourceSelectControlled
192196
control={control}
@@ -233,6 +237,32 @@ describe('SourceSelectControlled (grouped rendering + tag-style search)', () =>
233237
expect(screen.queryByText('Control Plane Prod')).not.toBeInTheDocument();
234238
});
235239

240+
it('selects the current label on a focusing click so typing replaces it', async () => {
241+
asMock(useSources).mockReturnValue({ data: SECTIONED_SOURCES });
242+
renderWithMantine(<SelectHarness defaultSource="billing-logs" />);
243+
const input = screen.getByPlaceholderText<HTMLInputElement>('Data Source');
244+
expect(input).toHaveValue('Billing Logs');
245+
246+
await userEvent.click(input);
247+
expect(input.selectionStart).toBe(0);
248+
expect(input.selectionEnd).toBe('Billing Logs'.length);
249+
250+
await userEvent.keyboard('prod');
251+
expect(input).toHaveValue('prod');
252+
expect(await screen.findByText('Prod Logs')).toBeInTheDocument();
253+
expect(screen.queryByText('Billing Logs')).not.toBeInTheDocument();
254+
});
255+
256+
it('places the caret normally on a click into an already focused input', async () => {
257+
asMock(useSources).mockReturnValue({ data: SECTIONED_SOURCES });
258+
renderWithMantine(<SelectHarness defaultSource="billing-logs" />);
259+
const input = screen.getByPlaceholderText<HTMLInputElement>('Data Source');
260+
261+
await userEvent.click(input);
262+
await userEvent.click(input);
263+
expect(input.selectionStart).toBe(input.selectionEnd);
264+
});
265+
236266
it('hides sources rejected by isSourceAllowed, along with sections left empty', async () => {
237267
asMock(useSources).mockReturnValue({ data: SECTIONED_SOURCES });
238268
renderWithMantine(
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { MouseEvent, useCallback, useRef } from 'react';
2+
import type { FocusEvent } from 'react';
3+
4+
const selectAll = (input: HTMLInputElement) =>
5+
input.setSelectionRange(0, input.value.length);
6+
7+
/**
8+
* Handlers that select a text input's whole value when it gains focus, so
9+
* typing replaces the value instead of inserting at the click point.
10+
*
11+
* Browsers collapse the selection to the pointer position on the mouseup
12+
* that completes a focusing click, so that mouseup is cancelled. A click on
13+
* an input that already has focus places the caret as usual.
14+
*/
15+
export function useSelectAllOnFocus() {
16+
const focusingByMouseRef = useRef(false);
17+
18+
const onMouseDown = useCallback((event: MouseEvent<HTMLInputElement>) => {
19+
focusingByMouseRef.current = document.activeElement !== event.currentTarget;
20+
}, []);
21+
22+
const onMouseUp = useCallback((event: MouseEvent<HTMLInputElement>) => {
23+
if (!focusingByMouseRef.current) return;
24+
focusingByMouseRef.current = false;
25+
event.preventDefault();
26+
selectAll(event.currentTarget);
27+
}, []);
28+
29+
const onFocus = useCallback((event: FocusEvent<HTMLInputElement>) => {
30+
selectAll(event.currentTarget);
31+
}, []);
32+
33+
return { onFocus, onMouseDown, onMouseUp };
34+
}

0 commit comments

Comments
 (0)