// // Copyright 2026 DXOS.org // import '../../theme/index.css'; import { type Meta, type StoryObj } from '@storybook/react-vite'; import React, { useState } from 'react'; import { expect, userEvent, waitFor, within } from 'storybook/test'; import { translations } from '#translations'; import { SIZES } from '../../sizes.ts'; import { GEOMETRY, byTestId, controlSize, expectEndCell, expectScoped, sizeRow } from '../../testing/geometry.ts'; import { withLayout, withTheme } from '../../testing/index.ts'; import { SIZE_ARG_TYPES, type SizeArgs, withSizes } from '../../testing/stories.tsx'; import * as Field from '../Field/Field.tsx'; import { Input } from '../Input/Input.tsx'; import { NumberInput } from './NumberInput.tsx'; const DefaultStory = ({ size }: SizeArgs) => { const [value, setValue] = useState('8'); return ( <> Quantity Between 0 and 10: {value} Price Ratio Locked ); }; const meta = { title: 'ui/react-ui-core/components/NumberInput', render: DefaultStory, decorators: [withSizes(), withLayout({ classNames: 'p-0 w-[32rem]' }), withTheme()], args: { size: 'md' }, argTypes: SIZE_ARG_TYPES, parameters: { layout: 'centered', translations }, } satisfies Meta; export default meta; type Story = StoryObj; export const Default: Story = {}; /** * The row is a control as wide as an Input at every size, with the compact stepper buttons abutting, inset at its end; the Field label * names the input (`spinbutton`). The buttons and ArrowUp/Down step by `step` and stop at `max`/`min`; typing past * `max` clamps on blur. */ export const Test: Story = { args: { allSizes: true }, play: async ({ canvasElement }) => { for (const size of SIZES) { const row = byTestId(canvasElement, `number-${size}`); const rect = row.getBoundingClientRect(); await expect(rect.height, `${size} height`).toBeCloseTo(controlSize(size), 0); await expect(rect.width, `${size} width`).toBeCloseTo( byTestId(canvasElement, `input-${size}`).getBoundingClientRect().width, 0, ); await expect(parseFloat(getComputedStyle(row).marginTop), `${size} inset`).toBeCloseTo(GEOMETRY[size].inset, 0); const increment = within(row).getByRole('button', { name: 'Increment' }); // The last stepper centres on the end cell, the pair's other half sitting before it. await expectEndCell(increment.querySelector('svg'), rect.right, size, `${size} stepper`); // Compact steppers: the pair abuts, each narrower than a block cell. const decrement = within(row).getByRole('button', { name: 'Decrement' }); await expect(decrement, `${size} decrement compact`).toHaveAttribute('data-compact'); await expect(increment.getBoundingClientRect().left, `${size} stepper gap`).toBeCloseTo( decrement.getBoundingClientRect().right, 0, ); for (const button of [decrement, increment]) { await expect(button.getBoundingClientRect().width, `${size} stepper width`).toBeLessThan(GEOMETRY[size].block); await expect(button.getBoundingClientRect().width).toBeCloseTo( GEOMETRY[size].icon + 2 * GEOMETRY[size].inset, 0, ); } } await expectScoped(canvasElement); const md = sizeRow(canvasElement, 'md'); const canvas = within(md); const value = byTestId(md, 'number-md-value'); const input = canvas.getByRole('spinbutton', { name: 'Quantity' }); await expect(input).toHaveAccessibleDescription('Between 0 and 10: 8'); await expect(input).toHaveAttribute('aria-valuemax', '10'); await expect(getComputedStyle(input).fontVariantNumeric).toContain('tabular-nums'); const row = byTestId(md, 'number-md'); await userEvent.click(within(row).getByRole('button', { name: 'Increment' })); await waitFor(() => expect(value).toHaveTextContent('9')); await userEvent.click(within(row).getByRole('button', { name: 'Increment' })); await userEvent.click(within(row).getByRole('button', { name: 'Increment' })); await waitFor(() => expect(value).toHaveTextContent('10')); await expect(within(row).getByRole('button', { name: 'Increment' })).toBeDisabled(); await userEvent.click(input); await userEvent.keyboard('{ArrowDown}{ArrowDown}'); await waitFor(() => expect(value).toHaveTextContent('8')); await userEvent.clear(input); await userEvent.type(input, '42'); await userEvent.tab(); await waitFor(() => expect(value).toHaveTextContent('10')); await expect(canvas.getByRole('spinbutton', { name: 'Price' })).toHaveValue('$1,250.00'); await expect(within(byTestId(md, 'bare-md')).queryByRole('button')).toBeNull(); await expect(canvas.getByRole('spinbutton', { name: 'Locked' })).toBeDisabled(); await expect(getComputedStyle(byTestId(md, 'disabled-md')).opacity).toBe('0.5'); }, }; const VariantsStory = ({ size }: SizeArgs) => { const [value, setValue] = useState('40'); return ( <> Split Aligned Hover right-aligned Hover left-aligned Volume {value} ); }; /** Stepper placements (`split`, `start`), hover-revealed steppers, end-aligned figures and the slider button. */ export const Variants: Story = { render: VariantsStory, }; /** * `split` puts decrement before the input and increment after it, each inside the row; `align='end'` right-aligns the * figures; `stepperOnHover` steppers sit opposite the number and are hidden until the row is hovered or focused; the slider button opens a slider that drives the value. */ export const VariantsTest: Story = { render: VariantsStory, play: async ({ canvasElement }) => { const md = sizeRow(canvasElement, 'md'); const canvas = within(md); const split = byTestId(md, 'split-md'); const splitRect = split.getBoundingClientRect(); const input = within(split).getByRole('spinbutton'); const decrement = within(split).getByRole('button', { name: 'Decrement' }); const increment = within(split).getByRole('button', { name: 'Increment' }); await expect(decrement.getBoundingClientRect().right).toBeLessThanOrEqual(input.getBoundingClientRect().left); await expect(increment.getBoundingClientRect().left).toBeGreaterThanOrEqual(input.getBoundingClientRect().right); for (const button of [decrement, increment]) { const rect = button.getBoundingClientRect(); await expect(rect.left).toBeGreaterThanOrEqual(splitRect.left); await expect(rect.right).toBeLessThanOrEqual(splitRect.right); } await userEvent.click(decrement); await waitFor(() => expect(input).toHaveValue('4')); await userEvent.click(increment); await userEvent.click(increment); await waitFor(() => expect(input).toHaveValue('6')); await expect(getComputedStyle(canvas.getByRole('spinbutton', { name: 'Aligned' })).textAlign).toBe('end'); await expect(getComputedStyle(canvas.getByRole('spinbutton', { name: 'Split' })).textAlign).not.toBe('end'); const leading = byTestId(md, 'leading-md'); const leadingInput = within(leading).getByRole('spinbutton'); // Hidden until hovered; focus reveals the pair through the same rule. await expect(getComputedStyle(within(leading).getAllByRole('button', { hidden: true })[0]).visibility).toBe( 'hidden', ); // Clicks before the first digit, which focus overrides. await userEvent.pointer({ keys: '[MouseLeft]', target: leadingInput, offset: 0 }); const leadingIncrement = within(leading).getByRole('button', { name: 'Increment' }); await expect(leadingIncrement.getBoundingClientRect().right).toBeLessThanOrEqual( leadingInput.getBoundingClientRect().left, ); await expect( within(leading).getByRole('button', { name: 'Decrement' }).getBoundingClientRect().left, ).toBeGreaterThanOrEqual(leading.getBoundingClientRect().left); await expect(getComputedStyle(leadingInput).textAlign).toBe('end'); // Focus puts the caret after the last digit. await waitFor(() => expect(leadingInput.selectionStart).toBe(leadingInput.value.length)); await userEvent.click(leadingIncrement); await waitFor(() => expect(leadingInput).toHaveValue('13')); const hover = byTestId(md, 'hover-md'); // Hidden buttons have no accessible name, so take the pair by position. const hoverButtons = within(hover).getAllByRole('button', { hidden: true }); await expect(hoverButtons).toHaveLength(2); for (const button of hoverButtons) { await expect(getComputedStyle(button).visibility).toBe('hidden'); } // Synthetic pointer events cannot set CSS `:hover`; focus reveals the pair through the same rule. await userEvent.click(within(hover).getByRole('spinbutton')); await waitFor(() => expect(within(hover).getByRole('button', { name: 'Increment' })).toBeVisible()); await expect( within(hover).getByRole('button', { name: 'Decrement' }).getBoundingClientRect().left, ).toBeGreaterThanOrEqual(within(hover).getByRole('spinbutton').getBoundingClientRect().right); await userEvent.click(within(hover).getByRole('button', { name: 'Increment' })); await waitFor(() => expect(canvas.getByRole('spinbutton', { name: 'Hover left-aligned' })).toHaveValue('4')); const sliderRow = byTestId(md, 'slider-md'); // A trailing slider button takes the same end cell as a trailing stepper, so end-aligned figures line up. await expect(within(sliderRow).getByRole('spinbutton').getBoundingClientRect().right).toBeCloseTo( canvas.getByRole('spinbutton', { name: 'Aligned' }).getBoundingClientRect().right, 0, ); await expect(within(sliderRow).queryByRole('button', { name: 'Increment' })).toBeNull(); await userEvent.click(within(sliderRow).getByRole('button', { name: 'Adjust' })); const thumb = await within(canvasElement.ownerDocument.body).findByRole('slider', { name: 'Adjust' }); await expect(thumb).toHaveAttribute('aria-valuenow', '40'); thumb.focus(); await userEvent.keyboard('{ArrowRight}{ArrowRight}'); await waitFor(() => expect(byTestId(md, 'slider-md-value')).toHaveTextContent('50')); await userEvent.keyboard('{Escape}'); }, };