Skip to content

Commit 79e1a51

Browse files
authored
fix(text-field): Show clear button in TextField only when it is filled and hovered/focused (#1713)
<!-- How to write a good PR title: - Follow [the Conventional Commits specification](https://www.conventionalcommits.org/en/v1.0.0/). - Give as much context as necessary and as little as possible - Prefix it with [WIP] while it’s a work in progress --> ## Self Checklist - [x] I wrote a PR title in **English** and added an appropriate **label** to the PR. - [x] I wrote the commit message in **English** and to follow [**the Conventional Commits specification**](https://www.conventionalcommits.org/en/v1.0.0/). - [x] I [added the **changeset**](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md) about the changes that needed to be released. (or didn't have to) - [x] I wrote or updated **documentation** related to the changes. (or didn't have to) - [x] I wrote or updated **tests** related to the changes. (or didn't have to) - [x] I tested the changes in various browsers. (or didn't have to) - Windows: Chrome, Edge, (Optional) Firefox - macOS: Chrome, Edge, Safari, (Optional) Firefox ## Related Issue <!-- Please link to issue if one exists --> Fixes #502 ## Summary <!-- Please brief explanation of the changes made --> input에 값이 채워져있고, focus되거나 hover되었을 때만 내용 삭제 버튼이 나오도록 변경합니다. ## Details <!-- Please elaborate description of the changes --> ```tsx // TextField.tsx // before const activeClear = activeInput && allowClear // after const activeClear = activeInput && allowClear && !!normalizedValue return ( ... { activeClear && ClearComponent } ... ) ``` 기존에 `ClearComponent` 의 렌더링 조건에는 `value`에 대한 것이 없었습니다. 그래서 `!!normalizedValue`를 통해 input 태그에 값이 있어야지만 내용 삭제 버튼이 렌더링되도록 하였습니다. ### Breaking change? (Yes/No) <!-- If Yes, please describe the impact and migration path for users --> No
1 parent 8c7ce90 commit 79e1a51

File tree

3 files changed

+69
-5
lines changed

3 files changed

+69
-5
lines changed

.changeset/lazy-bikes-allow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@channel.io/bezier-react": patch
3+
---
4+
5+
Show clear button in TextField only when its value is not empty and focused/hovered.

packages/bezier-react/src/components/Forms/Inputs/TextField/TextField.test.tsx

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
import React from 'react'
22

33
import { fireEvent } from '@testing-library/dom'
4-
import { act } from '@testing-library/react'
4+
import {
5+
act,
6+
within,
7+
} from '@testing-library/react'
8+
import userEvent from '@testing-library/user-event'
59

610
import { LightFoundation } from '~/src/foundation'
711

812
import { render } from '~/src/utils/testUtils'
913

1014
import { COMMON_IME_CONTROL_KEYS } from '~/src/components/Forms/Inputs/constants/CommonImeControlKeys'
1115

12-
import TextField, { TEXT_INPUT_TEST_ID } from './TextField'
16+
import TextField, {
17+
TEXT_INPUT_CLEAR_ICON_TEST_ID,
18+
TEXT_INPUT_TEST_ID,
19+
} from './TextField'
1320
import {
1421
type TextFieldProps,
1522
TextFieldVariant,
1623
} from './TextField.types'
1724
import { getProperTextFieldBgColor } from './TextFieldUtils'
1825

1926
describe('TextField', () => {
27+
const user = userEvent.setup()
2028
let props: TextFieldProps
2129

2230
beforeEach(() => {
@@ -285,4 +293,54 @@ describe('TextField', () => {
285293
})
286294
})
287295
})
296+
297+
describe('show remove button only when it is filled and focused/hovered', () => {
298+
it('disappear when empty & focused/hovered', async () => {
299+
const { getByTestId } = renderComponent({ value: '', allowClear: true })
300+
const rendered = getByTestId(TEXT_INPUT_TEST_ID)
301+
const input = rendered.getElementsByTagName('input')[0]
302+
303+
await act(async () => {
304+
await user.hover(input)
305+
input.focus()
306+
})
307+
308+
const clearButton = within(rendered).queryByTestId(TEXT_INPUT_CLEAR_ICON_TEST_ID)
309+
expect(clearButton).toBeNull()
310+
})
311+
312+
it('disappear when filled & not focused/hovered', () => {
313+
const { getByTestId } = renderComponent({ value: 'test', allowClear: true })
314+
const rendered = getByTestId(TEXT_INPUT_TEST_ID)
315+
316+
const clearButton = within(rendered).queryByTestId(TEXT_INPUT_CLEAR_ICON_TEST_ID)
317+
expect(clearButton).toBeNull()
318+
})
319+
320+
it('appear when filled & hovered', async () => {
321+
const { getByTestId } = renderComponent({ value: 'test', allowClear: true })
322+
const rendered = getByTestId(TEXT_INPUT_TEST_ID)
323+
const input = rendered.getElementsByTagName('input')[0]
324+
325+
await act(async () => {
326+
await user.hover(input)
327+
})
328+
329+
const clearButton = within(rendered).getByTestId(TEXT_INPUT_CLEAR_ICON_TEST_ID)
330+
expect(clearButton).toBeInTheDocument()
331+
})
332+
333+
it('appear when filled & focused', () => {
334+
const { getByTestId } = renderComponent({ value: 'test', allowClear: true })
335+
const rendered = getByTestId(TEXT_INPUT_TEST_ID)
336+
const input = rendered.getElementsByTagName('input')[0]
337+
338+
act(() => {
339+
input.focus()
340+
})
341+
342+
const clearButton = within(rendered).getByTestId(TEXT_INPUT_CLEAR_ICON_TEST_ID)
343+
expect(clearButton).toBeInTheDocument()
344+
})
345+
})
288346
})

packages/bezier-react/src/components/Forms/Inputs/TextField/TextField.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
import Styled from './TextField.styled'
4646

4747
export const TEXT_INPUT_TEST_ID = 'bezier-react-text-input'
48+
export const TEXT_INPUT_CLEAR_ICON_TEST_ID = 'bezier-react-text-input-clear-icon'
4849

4950
function TextFieldComponent({
5051
name,
@@ -117,7 +118,7 @@ forwardedRef: Ref<TextFieldRef>,
117118
), [value])
118119

119120
const activeInput = !disabled && !readOnly
120-
const activeClear = activeInput && allowClear
121+
const activeClear = activeInput && allowClear && !isEmpty(normalizedValue)
121122

122123
const inputRef = useRef<HTMLInputElement | null>(null)
123124

@@ -363,8 +364,9 @@ forwardedRef: Ref<TextFieldRef>,
363364
onClick={handleClear}
364365
>
365366
{
366-
normalizedValue && normalizedValue.length > 0 && (focused || hovered) && (
367+
(focused || hovered) && (
367368
<Icon
369+
testId={TEXT_INPUT_CLEAR_ICON_TEST_ID}
368370
source={CancelCircleFilledIcon}
369371
size={IconSize.XS}
370372
/>
@@ -375,7 +377,6 @@ forwardedRef: Ref<TextFieldRef>,
375377
focused,
376378
handleClear,
377379
hovered,
378-
normalizedValue,
379380
])
380381

381382
return (

0 commit comments

Comments
 (0)