Skip to content

Commit 12da97e

Browse files
committed
Allow onChange callback to be used with internal state
The state is managed by the component but value changes can still be observed.
1 parent f5f217e commit 12da97e

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"scripts": {
1111
"build": "webpack",
1212
"build:prod": "webpack --mode=production",
13-
"serve": "webpack --watch",
13+
"serve": "webpack --watch --mode=production",
1414
"lint": "eslint src",
1515
"test": "jest --coverage",
1616
"bump-major": "npm version major",

src/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ const VerificationInput = forwardRef(
6060
if (RegExp(`^[${validChars}]{0,${length}}$`).test(newInputVal)) {
6161
if (onChange) {
6262
onChange?.(newInputVal);
63-
} else {
64-
setLocalValue(newInputVal);
6563
}
64+
setLocalValue(newInputVal);
6665
}
6766
};
6867

src/verification-input.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ describe("VerificationInput", () => {
251251
expect(spyHandleChange).toHaveBeenCalledWith("01");
252252
});
253253

254+
it("should trigger onChange callback even if value is not provided", () => {
255+
const spyHandleChange = jest.fn();
256+
257+
render(<VerificationInput onChange={spyHandleChange} />);
258+
259+
userEvent.type(screen.getByRole("textbox"), "01");
260+
261+
expect(spyHandleChange).toHaveBeenCalledTimes(2);
262+
expect(spyHandleChange).toHaveBeenCalledWith("0");
263+
expect(spyHandleChange).toHaveBeenCalledWith("01");
264+
});
265+
254266
it("should trigger onFocus and onBlur callbacks", () => {
255267
const spyHandleFocus = jest.fn();
256268
const spyHandleBlur = jest.fn();

0 commit comments

Comments
 (0)