Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(numberinput): commit null on keyboard clear with hasClear (#3600)

Md Shahrier Islam Arhambc3180f93c6c
diff --git a/.changeset/numberinput-keyboard-clear.md b/.changeset/numberinput-keyboard-clear.md
new file mode 100644
index 0000000000..1a6eb5fa5c
--- /dev/null
+++ b/.changeset/numberinput-keyboard-clear.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] NumberInput with hasClear commits null when cleared from the keyboard (#3599)
+
+Deleting the text and blurring (or pressing Enter) silently reverted to the previous value — only the X button honored the clearable contract. An emptied input now commits onChange(null) on blur/Enter when hasClear is set; non-clearable inputs keep the revert behavior.
+@arham766
diff --git a/packages/core/src/NumberInput/NumberInput.test.tsx b/packages/core/src/NumberInput/NumberInput.test.tsx
index 4a7b9d45eb..617d806d18 100644
--- a/packages/core/src/NumberInput/NumberInput.test.tsx
+++ b/packages/core/src/NumberInput/NumberInput.test.tsx
@@ -867,3 +867,48 @@ describe('NumberInput', () => {
});
});
});
+
+describe('keyboard clearing with hasClear (#3599)', () => {
+ it('commits null when the input is emptied and blurred', () => {
+ const onChange = vi.fn();
+ render(
+ <NumberInput label="Qty" hasClear value={42} onChange={onChange} />,
+ );
+ const input = screen.getByLabelText('Qty');
+ fireEvent.change(input, {target: {value: ''}});
+ fireEvent.blur(input);
+ expect(onChange).toHaveBeenCalledWith(null);
+ });
+
+ it('commits null when the input is emptied and Enter is pressed', () => {
+ const onChange = vi.fn();
+ render(
+ <NumberInput label="Qty" hasClear value={42} onChange={onChange} />,
+ );
+ const input = screen.getByLabelText('Qty');
+ fireEvent.change(input, {target: {value: ''}});
+ fireEvent.keyDown(input, {key: 'Enter'});
+ expect(onChange).toHaveBeenCalledWith(null);
+ });
+
+ it('does not fire when emptied and blurred with no prior value', () => {
+ const onChange = vi.fn();
+ render(
+ <NumberInput label="Qty" hasClear value={null} onChange={onChange} />,
+ );
+ const input = screen.getByLabelText('Qty');
+ fireEvent.change(input, {target: {value: ''}});
+ fireEvent.blur(input);
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('still reverts on blur when hasClear is not set', () => {
+ const onChange = vi.fn();
+ render(<NumberInput label="Qty" value={42} onChange={onChange} />);
+ const input = screen.getByLabelText('Qty');
+ fireEvent.change(input, {target: {value: ''}});
+ fireEvent.blur(input);
+ expect(onChange).not.toHaveBeenCalled();
+ expect((input as HTMLInputElement).value).toBe('42');
+ });
+});
diff --git a/packages/core/src/NumberInput/NumberInput.tsx b/packages/core/src/NumberInput/NumberInput.tsx
index 3a97d82bda..8c70f4eef4 100644
--- a/packages/core/src/NumberInput/NumberInput.tsx
+++ b/packages/core/src/NumberInput/NumberInput.tsx
@@ -503,13 +503,21 @@ export function NumberInput({
const handleBlur = useCallback(
(e: FocusEvent<HTMLInputElement>) => {
if (pendingInput !== null) {
- const parsed = parseNumberInput(pendingInput, {
- min,
- max,
- isIntegerOnly,
- });
- if (parsed !== null && parsed !== value) {
- onChange(parsed);
+ if (hasClear && pendingInput.trim() === '') {
+ // Keyboard clearing honors the clearable contract: an emptied
+ // input commits null instead of silently reverting on blur.
+ if (value != null) {
+ onChange(null);
+ }
+ } else {
+ const parsed = parseNumberInput(pendingInput, {
+ min,
+ max,
+ isIntegerOnly,
+ });
+ if (parsed !== null && parsed !== value) {
+ onChange(parsed);
+ }
}
}
@@ -517,7 +525,7 @@ export function NumberInput({
setPendingInput(null);
onBlur?.(e);
},
- [pendingInput, value, onChange, min, max, isIntegerOnly, onBlur],
+ [pendingInput, value, onChange, min, max, isIntegerOnly, onBlur, hasClear],
);
// Handle keyboard events
@@ -526,13 +534,21 @@ export function NumberInput({
if (e.key === 'Enter') {
// Validate and commit on Enter
if (pendingInput !== null) {
- const parsed = parseNumberInput(pendingInput, {
- min,
- max,
- isIntegerOnly,
- });
- if (parsed !== null && parsed !== value) {
- onChange(parsed);
+ if (hasClear && pendingInput.trim() === '') {
+ // Same clearable contract as blur: Enter on an emptied input
+ // commits null instead of reverting.
+ if (value != null) {
+ onChange(null);
+ }
+ } else {
+ const parsed = parseNumberInput(pendingInput, {
+ min,
+ max,
+ isIntegerOnly,
+ });
+ if (parsed !== null && parsed !== value) {
+ onChange(parsed);
+ }
}
}
onEnter?.();
@@ -548,6 +564,7 @@ export function NumberInput({
isIntegerOnly,
onEnter,
onKeyDown,
+ hasClear,
],
);