//
// Copyright 2026 DXOS.org
//
import '../../theme/index.css';
import { type Meta, type StoryObj } from '@storybook/react-vite';
import React from 'react';
import { expect, within } from 'storybook/test';
import { sizeRow } from '../../testing/geometry.ts';
import { withLayout, withTheme } from '../../testing/index.ts';
import { SIZE_ARG_TYPES, type SizeArgs, withSizes } from '../../testing/stories.tsx';
import { Block } from '../Block/Block.tsx';
import { Button } from '../Button/Button.tsx';
import { Checkbox } from '../Checkbox/Checkbox.tsx';
import * as Collapsible from '../Collapsible/Collapsible.tsx';
import { Container } from '../Container/Container.tsx';
import * as Field from '../Field/Field.tsx';
import { Group } from '../Group/Group.tsx';
import * as Icon from '../Icon/Icon.tsx';
import { Input } from '../Input/Input.tsx';
import { Switch } from '../Switch/Switch.tsx';
import * as Fieldset from './Fieldset.tsx';
/**
* Valid and enabled sets, then an invalid and a disabled one, and a set whose fields span a two-column row; test ids are
* scoped by the size row.
*/
const DefaultStory = () => (
<>
Profile
NameEmailShown on your public page.Complete your profile.NotificationsAccountHandleRecovery emailComplete your account.PrivacyAddressStreetCityZIPDelivery
{/* Grid sets: subgrids of the enclosing Container at any depth, the inner one folding a subgrid Collapsible. */}
ShippingCarrierCoordinatesLatitude
>
);
const meta = {
title: 'ui/react-ui-core/components/Fieldset',
render: DefaultStory,
decorators: [withSizes(), withLayout({ classNames: 'p-0 w-[32rem]' }), withTheme()],
args: { size: 'md' },
argTypes: SIZE_ARG_TYPES,
parameters: { layout: 'centered' },
} satisfies Meta;
export default meta;
type Story = StoryObj;
const bounds = (root: HTMLElement, selector: string) => {
const element = root.querySelector(selector);
if (!element) {
throw new Error(`missing ${selector}`);
}
return element.getBoundingClientRect();
};
export const Default: Story = {};
/**
* Sets are `div` groups named by their legend row, which lines up with the fields' labels and controls; a disabled set
* disables every control inside it through context, and an invalid set shows its error and marks its fields invalid.
* Fields and sets take `span` in a multi-column row.
*/
export const Test: Story = {
args: { allSizes: true },
play: async ({ canvasElement: canvasRoot }) => {
const canvasElement = sizeRow(canvasRoot, 'md');
const canvas = within(canvasElement);
const profile = canvas.getByRole('group', { name: 'Profile' });
await expect(profile.tagName).toBe('DIV');
await expect(profile).toHaveAttribute('data-scope', 'fieldset');
await expect(profile).toHaveAccessibleDescription('Shown on your public page.');
await expect(canvas.getByRole('group', { name: 'Notifications' })).toBeInTheDocument();
await expect(getComputedStyle(profile).borderTopWidth).toBe('0px');
// The legend is a control-tall label row spanning the content track, like a Field's header.
const legend = bounds(canvasElement, '[data-testid="profile"] [data-part="legend"]');
const label = bounds(canvasElement, '[data-testid="name"] label');
const input = bounds(canvasElement, '[data-testid="name"] .dx-input');
await expect(legend.height).toBeCloseTo(28, 0);
await expect(legend.left).toBeCloseTo(label.left, 0);
// The trailing Block is inset in a block-sized cell that ends at the control's edge.
await expect(bounds(canvasElement, '[data-testid="profile-lock"]').right + 2).toBeCloseTo(input.right, 0);
// Fields are spaced by the gap token; switches and the checkbox start on the legend's edge.
const email = bounds(canvasElement, '[data-testid="email"]');
const name = bounds(canvasElement, '[data-testid="name"]');
await expect(email.top - name.bottom).toBeCloseTo(8, 0);
// Switches and the checkbox each take an icon-only Button's block-sized cell on the legend's edge, so labels align.
const notifications = bounds(canvasElement, '[data-testid="notifications"] [data-part="legend"]');
const controls = canvasElement.querySelectorAll(
'[data-testid="notifications"] :is([data-scope="switch"], [data-scope="checkbox"])[data-part="control"]',
);
const labels = canvasElement.querySelectorAll(
'[data-testid="notifications"] :is([data-scope="switch"], [data-scope="checkbox"])[data-part="label"]',
);
for (const control of controls) {
const style = getComputedStyle(control);
const rect = control.getBoundingClientRect();
const cellLeft = rect.left - parseFloat(style.marginLeft);
const cellWidth = rect.width + parseFloat(style.marginLeft) + parseFloat(style.marginRight);
await expect(cellLeft).toBeCloseTo(notifications.left, 0);
await expect(cellLeft).toBeCloseTo(legend.left, 0);
await expect(cellWidth).toBeCloseTo(32, 0);
}
for (const label of labels) {
await expect(label.getBoundingClientRect().left).toBeCloseTo(labels[0].getBoundingClientRect().left, 0);
}
await expect(canvas.queryByText('Complete your profile.')).toBeNull();
await expect(canvas.getByRole('group', { name: 'Profile' })).not.toHaveAttribute('data-invalid');
await expect(canvas.getByRole('group', { name: 'Notifications' })).not.toHaveAttribute('aria-disabled');
await expect(canvas.getByRole('group', { name: 'Privacy' })).toHaveAttribute('aria-disabled', 'true');
for (const name of ['Show online status', 'Read receipts']) {
await expect(canvas.getByRole('switch', { name })).toBeDisabled();
}
await expect(canvas.getByRole('checkbox', { name: 'Share usage data' })).toBeDisabled();
await expect(canvas.getByRole('textbox', { name: 'Alias' })).toBeDisabled();
await expect(canvas.getByRole('button', { name: 'Reset' })).toBeDisabled();
await expect(canvas.getByRole('textbox', { name: 'Name' })).toBeEnabled();
// A grid set is a `group` element (a `