Skip to content

Commit a04fc85

Browse files
committed
fix(ListInput): base props reach the group, and styling only named parts
ListInput composes Field, but it put a caller's ref, id, data and ARIA attributes, and handlers on the Field box, an unlabelled wrapper around the labelled role="group" element, and its type took root xstyle, className, and style. As in every Field-composed input, base props and ref now reach the group, which joins a caller's aria-labelledby and aria-describedby to its own. The type takes no root styling: containerXstyle, containerClassName, and containerStyle style the Field box where one renders, and controlXstyle, controlClassName, and controlStyle style the group. An untyped caller's root styling is dropped with a development warning.
1 parent cf3302c commit a04fc85

3 files changed

Lines changed: 209 additions & 24 deletions

File tree

‎packages/lab/src/ListInput/ListInput.doc.mjs‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,42 @@ export const docs = {
138138
'Displays a required indicator beside the collection label and exposes the requirement to assistive technology. Mutually exclusive with isOptional.',
139139
default: 'false',
140140
},
141+
{
142+
name: 'containerXstyle',
143+
type: 'StyleXStyles',
144+
description:
145+
'StyleX styles for the field container (label, list, and status together). No effect inside InputGroup (style the InputGroup instead) or in a `horizontal-labels` FormLayout. Root `xstyle`, `className`, and `style` are not accepted.',
146+
},
147+
{
148+
name: 'containerClassName',
149+
type: 'string',
150+
description:
151+
'Class name for the field container. Same targeting as `containerXstyle`.',
152+
},
153+
{
154+
name: 'containerStyle',
155+
type: 'CSSProperties',
156+
description:
157+
'Inline styles for the field container. Same targeting as `containerXstyle`.',
158+
},
159+
{
160+
name: 'controlXstyle',
161+
type: 'StyleXStyles',
162+
description:
163+
'StyleX styles for the role="group" element, which holds the records and the Add action. That element also takes `ref`, `id`, `aria-*`, `data-*`, and event handlers.',
164+
},
165+
{
166+
name: 'controlClassName',
167+
type: 'string',
168+
description:
169+
'Class name for the role="group" element. Same targeting as `controlXstyle`.',
170+
},
171+
{
172+
name: 'controlStyle',
173+
type: 'CSSProperties',
174+
description:
175+
'Inline styles for the role="group" element. Same targeting as `controlXstyle`.',
176+
},
141177
],
142178
usage: {
143179
description:
@@ -624,6 +660,14 @@ export const docsDense = {
624660
isOptional: 'Shows Optional; exclusive with isRequired.',
625661
isRequired:
626662
'Shows Required + accessible requirement; exclusive with isOptional.',
663+
containerXstyle:
664+
'StyleX styles for the Field box; no root xstyle/className/style',
665+
containerClassName: 'class name for the Field box',
666+
containerStyle: 'inline styles for the Field box',
667+
controlXstyle:
668+
'StyleX styles for the role="group" element; ref, id, aria-*, data-*, and handlers reach it too',
669+
controlClassName: 'class name for the role="group" element',
670+
controlStyle: 'inline styles for the role="group" element',
627671
},
628672
usage: {
629673
description:

‎packages/lab/src/ListInput/ListInput.test.tsx‎

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import {readFileSync} from 'node:fs';
1818
import path from 'node:path';
1919
import {afterEach, describe, expect, it, vi} from 'vitest';
20-
import {useMemo, useState} from 'react';
20+
import {createRef, useMemo, useState} from 'react';
2121
import userEvent from '@testing-library/user-event';
2222
import {createReorderable, type ReorderHandleOptions} from '@astryxdesign/core';
2323
import {
@@ -29,6 +29,7 @@ import {
2929
waitFor,
3030
within,
3131
} from '@testing-library/react';
32+
import {FormLayout} from '@astryxdesign/core/FormLayout';
3233
import {useAnnounce, useContainerReveal} from '@astryxdesign/core/hooks';
3334
import {InternationalizationProvider} from '@astryxdesign/core/i18n';
3435
import {TextInput} from '@astryxdesign/core/TextInput';
@@ -1834,3 +1835,118 @@ describe('ListInput', () => {
18341835
);
18351836
});
18361837
});
1838+
1839+
describe('ListInput prop targets', () => {
1840+
it('forwards ref, id, data and ARIA attributes, and handlers to the group', () => {
1841+
const ref = createRef<HTMLDivElement>();
1842+
const onFocus = vi.fn();
1843+
renderListInput({
1844+
ref,
1845+
id: 'caller-id',
1846+
'data-testid': 'guests',
1847+
'aria-details': 'details-id',
1848+
onFocus,
1849+
});
1850+
const group = screen.getByRole('group', {name: 'Guests'});
1851+
expect(group).toHaveAttribute('id', 'caller-id');
1852+
expect(group).toHaveAttribute('data-testid', 'guests');
1853+
expect(group).toHaveAttribute('aria-details', 'details-id');
1854+
expect(ref.current).toBe(group);
1855+
fireEvent.focus(group);
1856+
expect(onFocus).toHaveBeenCalled();
1857+
});
1858+
1859+
it('keeps a caller aria-describedby and aria-labelledby beside its own', () => {
1860+
renderListInput({
1861+
description: 'Who comes to dinner',
1862+
'aria-describedby': 'caller-help',
1863+
'aria-labelledby': 'caller-label',
1864+
});
1865+
const group = screen.getByRole('group');
1866+
const describedBy = (group.getAttribute('aria-describedby') ?? '').split(
1867+
' ',
1868+
);
1869+
expect(describedBy).toContain('caller-help');
1870+
expect(
1871+
describedBy.map(id => document.getElementById(id)?.textContent).join(),
1872+
).toContain('Who comes to dinner');
1873+
const labelledBy = (group.getAttribute('aria-labelledby') ?? '').split(' ');
1874+
expect(labelledBy).toContain('caller-label');
1875+
expect(
1876+
labelledBy.map(id => document.getElementById(id)?.textContent).join(),
1877+
).toContain('Guests');
1878+
});
1879+
1880+
it('styles the Field box with container* and the group with control*', () => {
1881+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
1882+
const {container} = renderListInput({
1883+
containerClassName: 'caller-container',
1884+
containerStyle: {marginTop: '7px'},
1885+
controlClassName: 'caller-control',
1886+
controlStyle: {marginBottom: '9px'},
1887+
});
1888+
const group = screen.getByRole('group');
1889+
const fieldBox = container.querySelector<HTMLElement>('.caller-container');
1890+
expect(fieldBox).not.toBeNull();
1891+
expect(fieldBox).not.toBe(group);
1892+
expect(fieldBox).toContainElement(group);
1893+
expect(fieldBox?.style.marginTop).toBe('7px');
1894+
expect(group).toHaveClass('caller-control', 'astryx-list-input');
1895+
expect(group.style.marginBottom).toBe('9px');
1896+
expect(warn).not.toHaveBeenCalled();
1897+
});
1898+
1899+
it('drops root styling and warns in development', () => {
1900+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
1901+
const untyped = {
1902+
className: 'caller-root',
1903+
style: {outlineColor: 'red'},
1904+
} as unknown as Partial<ListInputProps<Guest>>;
1905+
const {container} = renderListInput(untyped);
1906+
expect(container.querySelector('.caller-root')).toBeNull();
1907+
expect(screen.getByRole('group').style.outlineColor).toBe('');
1908+
expect(
1909+
warn.mock.calls.some(call => String(call[0]).startsWith('ListInput:')),
1910+
).toBe(true);
1911+
});
1912+
1913+
it('ignores container* and warns in a horizontal-labels FormLayout', () => {
1914+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
1915+
const {container} = render(
1916+
<FormLayout direction="horizontal-labels">
1917+
<ListInput<Guest>
1918+
label="Guests"
1919+
itemName="guest"
1920+
value={guests}
1921+
onChange={() => {}}
1922+
getItemKey={guest => guest.id}
1923+
createItem={() => createdGuest}
1924+
columns={columns}
1925+
containerClassName="caller-container"
1926+
/>
1927+
</FormLayout>,
1928+
);
1929+
expect(container.querySelector('.caller-container')).toBeNull();
1930+
expect(
1931+
warn.mock.calls.some(call =>
1932+
String(call[0]).includes('no Field box renders'),
1933+
),
1934+
).toBe(true);
1935+
});
1936+
1937+
it('types no root styling props', () => {
1938+
const props: ListInputProps<Guest> = {
1939+
label: 'Guests',
1940+
value: guests,
1941+
onChange: () => {},
1942+
getItemKey: guest => guest.id,
1943+
createItem: () => createdGuest,
1944+
columns,
1945+
containerClassName: 'caller-container',
1946+
controlClassName: 'caller-control',
1947+
// @ts-expect-error Field-composed inputs take no root className.
1948+
className: 'caller-root',
1949+
};
1950+
expect(props.controlClassName).toBe('caller-control');
1951+
});
1952+
});

‎packages/lab/src/ListInput/ListInput.tsx‎

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
/**
66
* @file ListInput.tsx
7-
* @input React, StyleX, Astryx field/list/button/icon/empty-state
8-
* primitives, the container-reveal hook, compact size context, browser
9-
* scroll geometry, and programmatic theme tokens. Also core List's reorder
10-
* session and handle, run by the owner's createReorderable object through
11-
* Core's internal entry `@astryxdesign/core/internal/reorder`
7+
* @input React, StyleX, Astryx Field, list, button, icon, and empty-state
8+
* primitives, size context, scroll geometry, theme tokens, and, through
9+
* Core's internal `reorder` and `selectable-card` entries, List's reorder
10+
* session and handle and Field's shared styling rule
1211
* @output Exports ListInput and its controlled data, column, renderer, and change types
1312
* @position Lab experiment (RFC meta/astryx#4531) for editing compact repeated records
1413
*
@@ -29,16 +28,20 @@ import {
2928
type ReactNode,
3029
} from 'react';
3130
import * as stylex from '@stylexjs/stylex';
32-
import type {BaseProps} from '@astryxdesign/core';
3331
import {Button} from '@astryxdesign/core/Button';
3432
import {EmptyState} from '@astryxdesign/core/EmptyState';
35-
import {Field, type InputStatus} from '@astryxdesign/core/Field';
33+
import {
34+
Field,
35+
type FieldInputBaseProps,
36+
type InputStatus,
37+
} from '@astryxdesign/core/Field';
3638
import {FieldStatus} from '@astryxdesign/core/FieldStatus';
3739
import {Icon} from '@astryxdesign/core/Icon';
3840
import {
3941
useAnnounce,
4042
useContainerReveal,
4143
useMediaQuery,
44+
useMergedRefs,
4245
} from '@astryxdesign/core/hooks';
4346
import {IconButton} from '@astryxdesign/core/IconButton';
4447
import {useTranslator} from '@astryxdesign/core/i18n';
@@ -47,6 +50,7 @@ import {
4750
ListReorderScope,
4851
useListReorderScope,
4952
} from '@astryxdesign/core/internal/reorder';
53+
import {useFieldInputStyling} from '@astryxdesign/core/internal/selectable-card';
5054
import type {Reorderable} from '@astryxdesign/core';
5155
import {reorderHandle} from '@astryxdesign/core/internal/reorderHandle.stylex';
5256
import {SizeProvider} from '@astryxdesign/core/SizeContext';
@@ -119,10 +123,10 @@ export interface ListInputColumn<T> {
119123
}
120124

121125
export interface ListInputProps<T> extends Omit<
122-
BaseProps<HTMLDivElement>,
126+
FieldInputBaseProps<HTMLDivElement>,
123127
'onChange'
124128
> {
125-
/** Ref forwarded to the outer field element. */
129+
/** Ref forwarded to the `role="group"` element. */
126130
ref?: React.Ref<HTMLDivElement>;
127131
/** Visible and accessible label for the list. */
128132
label: string;
@@ -741,17 +745,33 @@ export function ListInput<T>({
741745
isLabelHidden = false,
742746
isOptional = false,
743747
isRequired = false,
748+
id,
749+
containerXstyle,
750+
containerClassName,
751+
containerStyle,
752+
controlXstyle,
753+
controlClassName,
754+
controlStyle,
755+
'aria-labelledby': ariaLabelledBy,
756+
'aria-describedby': ariaDescribedBy,
744757
ref,
745-
...rest
758+
...restProps
746759
}: ListInputProps<T>): ReactNode {
760+
const {rest, container} = useFieldInputStyling('ListInput', restProps, {
761+
containerXstyle,
762+
containerClassName,
763+
containerStyle,
764+
});
747765
const generatedID = useId();
748-
const groupID = `list-input-${generatedID}`;
749-
const labelID = `${groupID}-label`;
750-
const descriptionID = description ? `${groupID}-description` : undefined;
751-
const statusID = status?.message ? `${groupID}-status` : undefined;
752-
const reorderInstructionsID = `${groupID}-reorder-instructions`;
766+
const baseID = `list-input-${generatedID}`;
767+
const groupID = id ?? baseID;
768+
const labelID = `${baseID}-label`;
769+
const descriptionID = description ? `${baseID}-description` : undefined;
770+
const statusID = status?.message ? `${baseID}-status` : undefined;
771+
const reorderInstructionsID = `${baseID}-reorder-instructions`;
753772
const rowRefs = useRef(new Map<string, HTMLDivElement>());
754773
const groupRef = useRef<HTMLDivElement>(null);
774+
const mergedGroupRef = useMergedRefs(ref, groupRef);
755775
const listRef = useRef<HTMLOListElement>(null);
756776
const addActionRef = useRef<HTMLDivElement>(null);
757777
const addButtonRef = useRef<HTMLButtonElement>(null);
@@ -1244,7 +1264,6 @@ export function ListInput<T>({
12441264
return (
12451265
<SizeProvider value="md">
12461266
<Field
1247-
ref={ref}
12481267
label={label}
12491268
description={description}
12501269
inputID={groupID}
@@ -1257,15 +1276,18 @@ export function ListInput<T>({
12571276
isDisabled={isDisabled}
12581277
status={status == null ? undefined : {...status, messageID: statusID}}
12591278
statusVariant="detached"
1260-
{...rest}>
1279+
xstyle={container.xstyle}
1280+
className={container.className}
1281+
style={container.style}>
12611282
<div
1262-
ref={groupRef}
1283+
{...rest}
1284+
ref={mergedGroupRef}
12631285
id={groupID}
12641286
role="group"
1265-
aria-labelledby={labelID}
1266-
aria-describedby={joinIDs(descriptionID, statusID)}
1267-
aria-disabled={isDisabled || undefined}
1268-
aria-busy={isLoading || undefined}
1287+
aria-labelledby={joinIDs(labelID, ariaLabelledBy)}
1288+
aria-describedby={joinIDs(ariaDescribedBy, descriptionID, statusID)}
1289+
aria-disabled={isDisabled || rest['aria-disabled']}
1290+
aria-busy={isLoading || rest['aria-busy']}
12691291
{...mergeProps(
12701292
themeProps('list-input', {
12711293
state: isLoading
@@ -1281,7 +1303,10 @@ export function ListInput<T>({
12811303
(isHandleAlwaysVisible
12821304
? styles.groupWithShownStartHandles
12831305
: styles.groupWithHangingHandles),
1306+
controlXstyle,
12841307
),
1308+
controlClassName,
1309+
controlStyle,
12851310
)}>
12861311
<ListReorderScope reorder={reorderScope}>
12871312
<ol
@@ -1316,7 +1341,7 @@ export function ListInput<T>({
13161341
showReorderHandle && (canReorder?.(itemKey) ?? true);
13171342
const itemStatus = getItemStatus?.(item, index);
13181343
const itemStatusID = itemStatus?.message
1319-
? `${groupID}-item-${index}-status`
1344+
? `${baseID}-item-${index}-status`
13201345
: undefined;
13211346
return (
13221347
<li

0 commit comments

Comments
 (0)