Skip to content

Commit f749fb7

Browse files
authored
feat: unify allowClear configuration (#758)
1 parent 2b12adb commit f749fb7

4 files changed

Lines changed: 68 additions & 14 deletions

File tree

‎README.md‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ Then open `http://localhost:8000`.
6666

6767
| Property | Type | Default | Description |
6868
| --- | --- | --- | --- |
69-
| allowClear | `boolean \| { clearIcon?: ReactNode; disabled?: boolean; label?: string }` | `false` | Show a clear button, optionally with a custom icon, disabled state, or accessible label. |
69+
| allowClear | `boolean \| AllowClearConfig` | `false` | Show a clear button, optionally with a custom icon, disabled state, or accessible label. |
7070
| autoFocus | `boolean` | `false` | Focus the input when mounted. |
7171
| changeOnBlur | `boolean` | `true` | Commit value changes on blur. |
7272
| changeOnWheel | `boolean` | `false` | Allow value changes from the mouse wheel. |
@@ -151,3 +151,20 @@ The release flow is handled by `@rc-component/np` through the `rc-np` command af
151151
## License
152152

153153
@rc-component/input-number is released under the [MIT](./LICENSE) license.
154+
155+
### Clear configuration
156+
157+
`allowClear` accepts a boolean or an `AllowClearConfig` object from `@rc-component/util`:
158+
159+
```ts
160+
interface AllowClearConfig {
161+
clearIcon?: React.ReactNode;
162+
disabled?: boolean;
163+
tabIndex?: number;
164+
label?: string;
165+
}
166+
```
167+
168+
All fields are optional. `disabled: true` behaves like `allowClear={false}`: the clear button and space reserved solely for it are removed; the input remains editable. Existing suffixes and other content are preserved. This changes the previous hidden-button spacing behavior where applicable.
169+
170+
`tabIndex` affects only the clear button; `-1` removes it from Tab navigation while retaining mouse activation. `label` supplies its accessible name through `aria-label`. Defaults, clear values, callbacks, and component state restrictions are unchanged.

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
},
4747
"dependencies": {
4848
"@rc-component/mini-decimal": "^1.1.1",
49-
"@rc-component/util": "^1.13.0",
49+
"@rc-component/util": "^1.14.0",
5050
"clsx": "^2.1.1"
5151
},
5252
"devDependencies": {

‎src/InputNumber.tsx‎

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import getMiniDecimal, {
77
ValueType,
88
} from '@rc-component/mini-decimal';
99
import {
10+
type AllowClearConfig,
1011
type InputFocusOptions,
12+
useAllowClear,
1113
isReactRenderable,
1214
proxyObject,
1315
triggerFocus,
@@ -119,13 +121,7 @@ export interface InputNumberProps<T extends ValueType = ValueType>
119121
controls?: boolean;
120122
prefix?: React.ReactNode;
121123
suffix?: React.ReactNode;
122-
allowClear?:
123-
| boolean
124-
| {
125-
clearIcon?: React.ReactNode;
126-
disabled?: boolean;
127-
label?: string;
128-
};
124+
allowClear?: boolean | AllowClearConfig;
129125
classNames?: Partial<Record<SemanticName, string>>;
130126
styles?: Partial<Record<SemanticName, React.CSSProperties>>;
131127

@@ -731,8 +727,7 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
731727
);
732728

733729
// >>> Clear
734-
const clearConfig =
735-
allowClear && typeof allowClear === 'object' ? allowClear : { disabled: allowClear !== true };
730+
const clearConfig = useAllowClear(allowClear);
736731
const showClear =
737732
!disabled && !readOnly && clearConfig.disabled !== true && String(inputValue).length > 0;
738733
const hasSuffix = isReactRenderable(suffix);
@@ -765,10 +760,11 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
765760
onClear?.();
766761
};
767762

768-
const clearNode = allowClear && (
763+
const clearNode = !clearConfig.disabled && (
769764
<button
770765
type="button"
771766
aria-label={clearConfig.label ?? 'Clear'}
767+
tabIndex={clearConfig.tabIndex}
772768
disabled={!showClear}
773769
className={clsx(
774770
clearIconCls,
@@ -841,7 +837,7 @@ const InputNumber = React.forwardRef<InputNumberRef, InputNumberProps>((props, r
841837
{...restProps}
842838
/>
843839

844-
{(allowClear || hasSuffix) && (
840+
{(!clearConfig.disabled || hasSuffix) && (
845841
<div className={clsx(`${prefixCls}-suffix`, classNames?.suffix)} style={styles?.suffix}>
846842
{clearNode}
847843
{suffix}

‎tests/allowClear.test.tsx‎

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ describe('InputNumber.AllowClear', () => {
214214
it.each([
215215
['disabled', { disabled: true }],
216216
['readOnly', { readOnly: true }],
217-
['allowClear.disabled', { allowClear: { disabled: true } }],
218217
])('disables the clear action when %s', (_name, extraProps) => {
219218
const onChange = jest.fn();
220219
const onClear = jest.fn();
@@ -361,3 +360,45 @@ describe('InputNumber.AllowClear', () => {
361360
]);
362361
});
363362
});
363+
364+
describe('unified clear configuration', () => {
365+
it.each([undefined, 'suffix'])('removes only the clear entry with suffix %s', (suffix) => {
366+
const onChange = jest.fn();
367+
const onClear = jest.fn();
368+
const config = Object.freeze({ clearIcon: 'custom', tabIndex: -1, label: 'Erase' });
369+
const props = { defaultValue: 12, prefix: 'prefix', suffix, onChange, onClear };
370+
const { container, getByRole, queryByRole, rerender } = render(
371+
<InputNumber {...props} allowClear={false} />,
372+
);
373+
const disabledMarkup = container.innerHTML;
374+
rerender(<InputNumber {...props} allowClear={{ ...config, disabled: true }} />);
375+
expect(container.innerHTML).toBe(disabledMarkup);
376+
expect(queryByRole('button', { name: 'Erase' })).toBeNull();
377+
expect(getByRole('spinbutton')).toHaveValue('12');
378+
expect(onChange).not.toHaveBeenCalled();
379+
expect(onClear).not.toHaveBeenCalled();
380+
381+
rerender(<InputNumber {...props} allowClear={config} />);
382+
const button = getByRole('button', { name: 'Erase' });
383+
expect(button).toHaveTextContent('custom');
384+
expect(button.tabIndex).toBe(-1);
385+
fireEvent.click(button);
386+
expect(getByRole('spinbutton')).toHaveValue('');
387+
expect(onChange).toHaveBeenCalledWith(null);
388+
expect(onClear).toHaveBeenCalledTimes(1);
389+
});
390+
391+
it('updates tabIndex independently of the input and retains focus while toggling', () => {
392+
const { getByRole, rerender } = render(
393+
<InputNumber defaultValue={12} tabIndex={3} allowClear={{ tabIndex: -1 }} />,
394+
);
395+
const input = getByRole('spinbutton');
396+
act(() => input.focus());
397+
rerender(<InputNumber defaultValue={12} tabIndex={3} allowClear={{ disabled: true }} />);
398+
expect(getByRole('spinbutton')).toBe(input);
399+
expect(input).toHaveFocus();
400+
rerender(<InputNumber defaultValue={12} tabIndex={3} allowClear={{ tabIndex: 0 }} />);
401+
expect(getByRole('button', { name: 'Clear' }).tabIndex).toBe(0);
402+
expect(input.tabIndex).toBe(3);
403+
});
404+
});

0 commit comments

Comments
 (0)