Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/ui/src/NumberInput.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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);
});
});
2 changes: 1 addition & 1 deletion packages/ui/src/NumberInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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). */
Expand Down
Loading