Skip to content

Commit 220a5e3

Browse files
fix: use button tag and native tabindex for clear button (#63)
Co-authored-by: afc163 <afc163@gmail.com>
1 parent c0dd6ed commit 220a5e3

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

assets/index.less

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
}
2323

2424
&-clear-icon {
25+
padding: 0;
2526
font-size: 12px;
26-
cursor: pointer;
27+
background: none;
28+
border: none;
2729

2830
&-hidden {
2931
display: none;

src/BaseInput.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
8080
: '✖';
8181

8282
clearIcon = (
83-
<span
83+
<button
8484
onClick={(event) => {
8585
handleReset?.(event);
8686
onClear?.();
@@ -92,11 +92,9 @@ const BaseInput = React.forwardRef<HolderRef, BaseInputProps>((props, ref) => {
9292
[`${clearIconCls}-hidden`]: !needClear,
9393
[`${clearIconCls}-has-suffix`]: !!suffix,
9494
})}
95-
role="button"
96-
tabIndex={-1}
9795
>
9896
{iconNode}
99-
</span>
97+
</button>
10098
);
10199
}
102100

tests/BaseInput.test.tsx

+48-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { fireEvent, render } from '@testing-library/react';
2+
import userEvent from '@testing-library/user-event';
23
import type { ChangeEvent, FC } from 'react';
34
import React, { useState } from 'react';
45
import BaseInput, { type HolderRef } from '../src/BaseInput';
@@ -45,11 +46,13 @@ describe('BaseInput', () => {
4546
expect(container).toMatchSnapshot();
4647
});
4748

48-
it('allowClear should work', () => {
49+
describe('allowClear should work', () => {
4950
const onChange = jest.fn();
5051
const onBlur = jest.fn();
5152
const onFocus = jest.fn();
5253

54+
const user = userEvent.setup();
55+
5356
const Demo: FC = () => {
5457
const [value, setValue] = useState<string>('');
5558

@@ -74,23 +77,53 @@ describe('BaseInput', () => {
7477
);
7578
};
7679

77-
const { container } = render(<Demo />);
80+
it('By click', () => {
81+
const { container } = render(<Demo />);
7882

79-
const inputEl = container.querySelector('input');
80-
fireEvent.focus(inputEl!);
81-
expect(onFocus).toHaveBeenCalledTimes(1);
83+
const inputEl = container.querySelector('input');
84+
fireEvent.focus(inputEl!);
85+
expect(onFocus).toHaveBeenCalledTimes(1);
8286

83-
fireEvent.change(inputEl!, { target: { value: 'some text' } });
84-
expect(onChange).toHaveBeenCalledTimes(1);
85-
expect(inputEl!.value).toBe('some text');
87+
fireEvent.change(inputEl!, { target: { value: 'some text' } });
88+
expect(onChange).toHaveBeenCalledTimes(1);
89+
expect(inputEl!.value).toBe('some text');
8690

87-
const clearIcon = container.querySelector('.rc-input-clear-icon');
88-
fireEvent.mouseDown(clearIcon!);
89-
fireEvent.click(clearIcon!);
90-
fireEvent.mouseUp(clearIcon!);
91-
expect(onBlur).not.toHaveBeenCalled();
92-
expect(onChange).toHaveBeenCalledTimes(1);
93-
expect(inputEl!.value).toBe('');
91+
const clearIcon = container.querySelector('.rc-input-clear-icon');
92+
fireEvent.mouseDown(clearIcon!);
93+
fireEvent.click(clearIcon!);
94+
fireEvent.mouseUp(clearIcon!);
95+
expect(onBlur).not.toHaveBeenCalled();
96+
expect(onChange).toHaveBeenCalledTimes(1);
97+
expect(inputEl!.value).toBe('');
98+
});
99+
100+
it('By focus and Space', async () => {
101+
const { container } = render(<Demo />);
102+
103+
const inputEl = container.querySelector('input');
104+
await user.click(inputEl!);
105+
106+
await user.type(inputEl!, 'some text');
107+
expect(inputEl!.value).toBe('some text');
108+
109+
await user.tab();
110+
await user.keyboard('[Space]');
111+
expect(inputEl!.value).toBe('');
112+
});
113+
114+
it('By focus and Enter', async () => {
115+
const { container } = render(<Demo />);
116+
117+
const inputEl = container.querySelector('input');
118+
await user.click(inputEl!);
119+
120+
await user.type(inputEl!, 'some text');
121+
expect(inputEl!.value).toBe('some text');
122+
123+
await user.tab();
124+
await user.keyboard('[Enter]');
125+
expect(inputEl!.value).toBe('');
126+
});
94127
});
95128

96129
it('should display clearIcon correctly', () => {

tests/__snapshots__/index.test.tsx.snap

+12-24
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ exports[`Input allowClear should change type when click 1`] = `
115115
<span
116116
class="rc-input-suffix"
117117
>
118-
<span
118+
<button
119119
class="rc-input-clear-icon"
120-
role="button"
121-
tabindex="-1"
122120
>
123121
124-
</span>
122+
</button>
125123
</span>
126124
</span>
127125
</div>
@@ -140,13 +138,11 @@ exports[`Input allowClear should change type when click 2`] = `
140138
<span
141139
class="rc-input-suffix"
142140
>
143-
<span
141+
<button
144142
class="rc-input-clear-icon rc-input-clear-icon-hidden"
145-
role="button"
146-
tabindex="-1"
147143
>
148144
149-
</span>
145+
</button>
150146
</span>
151147
</span>
152148
</div>
@@ -165,13 +161,11 @@ exports[`Input allowClear should not show icon if defaultValue is undefined or e
165161
<span
166162
class="rc-input-suffix"
167163
>
168-
<span
164+
<button
169165
class="rc-input-clear-icon rc-input-clear-icon-hidden"
170-
role="button"
171-
tabindex="-1"
172166
>
173167
174-
</span>
168+
</button>
175169
</span>
176170
</span>
177171
</div>
@@ -190,13 +184,11 @@ exports[`Input allowClear should not show icon if defaultValue is undefined or e
190184
<span
191185
class="rc-input-suffix"
192186
>
193-
<span
187+
<button
194188
class="rc-input-clear-icon rc-input-clear-icon-hidden"
195-
role="button"
196-
tabindex="-1"
197189
>
198190
199-
</span>
191+
</button>
200192
</span>
201193
</span>
202194
</div>
@@ -215,13 +207,11 @@ exports[`Input allowClear should not show icon if value is undefined or empty st
215207
<span
216208
class="rc-input-suffix"
217209
>
218-
<span
210+
<button
219211
class="rc-input-clear-icon rc-input-clear-icon-hidden"
220-
role="button"
221-
tabindex="-1"
222212
>
223213
224-
</span>
214+
</button>
225215
</span>
226216
</span>
227217
</div>
@@ -240,13 +230,11 @@ exports[`Input allowClear should not show icon if value is undefined or empty st
240230
<span
241231
class="rc-input-suffix"
242232
>
243-
<span
233+
<button
244234
class="rc-input-clear-icon rc-input-clear-icon-hidden"
245-
role="button"
246-
tabindex="-1"
247235
>
248236
249-
</span>
237+
</button>
250238
</span>
251239
</span>
252240
</div>

0 commit comments

Comments
 (0)