From 7ef1b45bf69683d3e56dc241d59265cd210073c5 Mon Sep 17 00:00:00 2001 From: Tomeshwari-02 Date: Fri, 3 Jul 2026 19:57:21 +0530 Subject: [PATCH] Clamp NumberInput numeric values --- packages/ui/src/NumberInput.test.ts | 17 ++++++++++++++++- packages/ui/src/NumberInput.ts | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/ui/src/NumberInput.test.ts b/packages/ui/src/NumberInput.test.ts index ea529aff..e3a3ac4d 100644 --- a/packages/ui/src/NumberInput.test.ts +++ b/packages/ui/src/NumberInput.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect } from 'vitest'; +import { describe, it, expect, vi } from 'vitest'; import { render } from '@termuijs/testing'; import { createElement, useRef } from '@termuijs/jsx'; import { NumberInput } from './NumberInput.js'; @@ -67,4 +67,19 @@ describe('NumberInput', () => { screen.unmount(); }); + + it('clamps typed values exposed through numericValue and submit', () => { + const onChange = vi.fn(); + const onSubmit = vi.fn(); + const input = new NumberInput({}, { min: 0, max: 10, onChange, onSubmit }); + + input.insertChar('9'); + input.insertChar('9'); + input.submit(); + + expect(input.rawValue).toBe('99'); + expect(input.numericValue).toBe(10); + expect(onChange).toHaveBeenLastCalledWith(10); + expect(onSubmit).toHaveBeenCalledWith(10); + }); }); diff --git a/packages/ui/src/NumberInput.ts b/packages/ui/src/NumberInput.ts index 1838dd7e..a0ad23b9 100644 --- a/packages/ui/src/NumberInput.ts +++ b/packages/ui/src/NumberInput.ts @@ -51,7 +51,7 @@ export class NumberInput extends Widget { get numericValue(): number | null { if (this._raw === '' || this._raw === '-') return null; const n = parseFloat(this._raw); - return isNaN(n) ? null : n; + return isNaN(n) ? null : this._clamp(n); } /** Raw text string (what the user typed). */