Skip to content

Commit

Permalink
fix(number-input): onChange event in number input (#4907)
Browse files Browse the repository at this point in the history
* fix(number-input): onChange event in number input

* chore(number-input): remove duplicate test case
  • Loading branch information
wingkwong authored Feb 26, 2025
1 parent 2186f6d commit f55cf8b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-geese-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@heroui/number-input": patch
---

handle onChange event in number input (#4874)
29 changes: 28 additions & 1 deletion packages/components/number-input/__tests__/number-input.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,34 @@ describe("NumberInput", () => {

expect(stepperButton).toBeNull();
});

it("should emit onChange", async () => {
const onChange = jest.fn();

const {container} = render(<NumberInput label="test number input" onChange={onChange} />);

const input = container.querySelector("input") as HTMLInputElement;

await user.click(input);
await user.keyboard("1024");

expect(onChange).toHaveBeenCalledTimes(4);
});

it("should emit onChange with keyboard up & down key", async () => {
const onChange = jest.fn();

const {container} = render(<NumberInput label="test number input" onChange={onChange} />);

const input = container.querySelector("input") as HTMLInputElement;

await user.click(input);
await user.keyboard("[ArrowUp]");
await user.keyboard("[ArrowUp]");
expect(onChange).toHaveBeenCalledTimes(2);
await user.keyboard("[ArrowDown]");
expect(onChange).toHaveBeenCalledTimes(3);
});
});

describe("NumberInput with React Hook Form", () => {
Expand Down Expand Up @@ -503,7 +531,6 @@ describe("NumberInput with React Hook Form", () => {

await user.tab();
await user.keyboard("1234");
await user.tab();
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/components/number-input/src/use-number-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export function useNumberInput(originalProps: UseNumberInputProps) {
...originalProps,
validationBehavior,
locale,
onChange: onValueChange,
onChange: chain(onValueChange, onChange),
});

const {
Expand Down

0 comments on commit f55cf8b

Please sign in to comment.