SearchInput: Focus lost after clicking clear button #21700

Closed
opened 2026-02-21 20:13:57 -05:00 by yindo · 1 comment
Owner

Originally created by @lyzno1 on GitHub (Jan 16, 2026).

Description

When clicking the clear (x) button in the SearchInput component, the input loses focus. This is a poor UX because users typically want to continue typing after clearing the search field.

Steps to Reproduce

  1. Click on a SearchInput field
  2. Type some text
  3. Click the clear (x) button
  4. Observe that the input no longer has focus

Expected Behavior

After clicking the clear button, the input should remain focused so users can immediately start typing again.

Root Cause

In app/components/base/search-input/index.tsx, the clear button's onClick handler only calls onChange('') but doesn't refocus the input element.

Proposed Fix

Add an input ref and call inputRef.current?.focus() after clearing the value.

Originally created by @lyzno1 on GitHub (Jan 16, 2026). ## Description When clicking the clear (x) button in the `SearchInput` component, the input loses focus. This is a poor UX because users typically want to continue typing after clearing the search field. ## Steps to Reproduce 1. Click on a SearchInput field 2. Type some text 3. Click the clear (x) button 4. Observe that the input no longer has focus ## Expected Behavior After clicking the clear button, the input should remain focused so users can immediately start typing again. ## Root Cause In `app/components/base/search-input/index.tsx`, the clear button's `onClick` handler only calls `onChange('')` but doesn't refocus the input element. ## Proposed Fix Add an input ref and call `inputRef.current?.focus()` after clearing the value.
yindo added the 💪 enhancement label 2026-02-21 20:13:57 -05:00
yindo closed this issue 2026-02-21 20:13:57 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Jan 16, 2026):

You're right about the root cause: the clear button's onClick only calls onChange('') and doesn't refocus the input, so the field loses focus when cleared. The input element currently doesn't have a ref for programmatic focus control, but adding one and calling inputRef.current?.focus() after clearing is straightforward and aligns with React best practices. This will keep the input focused and improve UX as expected.

Here's a quick implementation guide:

  1. Create a ref for the input:
    const inputRef = useRef<HTMLInputElement>(null);
  2. Attach it to the input element:
    <input ref={inputRef} ... />
  3. Update the clear button's onClick handler:
    onClick={() => {
      onChange('');
      inputRef.current?.focus();
    }}
    

This approach is safe and should not interfere with the existing composition event logic or focus state management, but it's a good idea to test with IME input and screen readers to ensure there are no regressions.
You can see the current implementation and where to apply these changes here.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Jan 16, 2026): <!-- Answer --> You're right about the root cause: the clear button's onClick only calls onChange('') and doesn't refocus the input, so the field loses focus when cleared. The input element currently doesn't have a ref for programmatic focus control, but adding one and calling inputRef.current?.focus() after clearing is straightforward and aligns with React best practices. This will keep the input focused and improve UX as expected. Here's a quick implementation guide: 1. Create a ref for the input: `const inputRef = useRef<HTMLInputElement>(null);` 2. Attach it to the input element: `<input ref={inputRef} ... />` 3. Update the clear button's onClick handler: ``` onClick={() => { onChange(''); inputRef.current?.focus(); }} ``` This approach is safe and should not interfere with the existing composition event logic or focus state management, but it's a good idea to test with IME input and screen readers to ensure there are no regressions. You can see the current implementation and where to apply these changes [here](https://github.com/langgenius/dify/blob/b2cc9b255d9d758d2ef395ae3142ebb13b9552b1/web/app/components/base/search-input/index.tsx#L15-L81). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/b058ce52-b03f-4aef-b51e-578f69815295?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/b058ce52-b03f-4aef-b51e-578f69815295?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/b058ce52-b03f-4aef-b51e-578f69815295?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/b058ce52-b03f-4aef-b51e-578f69815295?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/b058ce52-b03f-4aef-b51e-578f69815295?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/b058ce52-b03f-4aef-b51e-578f69815295?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/b058ce52-b03f-4aef-b51e-578f69815295?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=You%27re%20right%20about%20the%20root%20cause%3A%20the%20clear%20button%27s%20onClick%20only%20calls%20onChange%28%27%27%29%20and%20doesn%27t%20refocus%20the%20input%2C%20so%20the%20field%20loses%20focus%20when%20cleared.%20The%20input%20element%20currently%20doesn%27t%20have%20a%20ref%20for%20programmatic%20focus%20control%2C%20but%20adding%20one%20and%20calling%20inputRef.current%3F.focus%28%29%20after%20clearing%20is%20straightforward%20and%20aligns%20with%20React%20best%20practices.%20This%20will%20keep%20the%20input%20focused%20and%20improve%20UX%20as%20expected.%20%0A%0AHere%27s%20a%20quick%20implementation%20guide%3A%0A%0A1.%20Create%20a%20ref%20for%20the%20input%3A%20%20%0A%20%20%20%60const%20inputRef%20%3D%20useRef%3CHTMLInputElement%3E%28null%29%3B%60%0A2.%20Attach%20it%20to%20the%20input%20element%3A%20%20%0A%20%20%20%60%3Cinput%20ref%3D%7BinputRef%7D%20...%20/%3E%60%0A3.%20Update%20the%20clear%20button%27s%20onClick%20handler%3A%20%20%0A%20%20%20%60%60%60%0A%20%20%20onClick%3D%7B%28%29%20%3D%3E%20%7B%0A%20%20%20%20%20onChange%28%27%27%29%3B%0A%20%20%20%20%20inputRef.current%3F.focus%28%29%3B%0A%20%20%20%7D%7D%0A%20%20%20%60%60%60%0A%0AThis%20approach%20is%20safe%20and%20should%20not%20interfere%20with%20the%20existing%20composition%20event%20logic%20or%20focus%20state%20management%2C%20but%20it%27s%20a%20good%20idea%20to%20test%20with%20IME%20input%20and%20screen%20readers%20to%20ensure%20there%20are%20no%20regressions.%20%20%0AYou%20can%20see%20the%20current%20implementation%20and%20where%20to%20apply%20these%20changes%20%5Bhere%5D%28https%3A//github.com/langgenius/dify/blob/b2cc9b255d9d758d2ef395ae3142ebb13b9552b1/web/app/components/base/search-input/index.tsx%23L15-L81%29.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/31106)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#21700