Skip to content

Commit 90f9370

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 f3cadba commit 90f9370

3 files changed

Lines changed: 207 additions & 20 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';
@@ -1658,3 +1659,118 @@ describe('ListInput', () => {
16581659
);
16591660
});
16601661
});
1662+
1663+
describe('ListInput prop targets', () => {
1664+
it('forwards ref, id, data and ARIA attributes, and handlers to the group', () => {
1665+
const ref = createRef<HTMLDivElement>();
1666+
const onFocus = vi.fn();
1667+
renderListInput({
1668+
ref,
1669+
id: 'caller-id',
1670+
'data-testid': 'guests',
1671+
'aria-details': 'details-id',
1672+
onFocus,
1673+
});
1674+
const group = screen.getByRole('group', {name: 'Guests'});
1675+
expect(group).toHaveAttribute('id', 'caller-id');
1676+
expect(group).toHaveAttribute('data-testid', 'guests');
1677+
expect(group).toHaveAttribute('aria-details', 'details-id');
1678+
expect(ref.current).toBe(group);
1679+
fireEvent.focus(group);
1680+
expect(onFocus).toHaveBeenCalled();
1681+
});
1682+
1683+
it('keeps a caller aria-describedby and aria-labelledby beside its own', () => {
1684+
renderListInput({
1685+
description: 'Who comes to dinner',
1686+
'aria-describedby': 'caller-help',
1687+
'aria-labelledby': 'caller-label',
1688+
});
1689+
const group = screen.getByRole('group');
1690+
const describedBy = (group.getAttribute('aria-describedby') ?? '').split(
1691+
' ',
1692+
);
1693+
expect(describedBy).toContain('caller-help');
1694+
expect(
1695+
describedBy.map(id => document.getElementById(id)?.textContent).join(),
1696+
).toContain('Who comes to dinner');
1697+
const labelledBy = (group.getAttribute('aria-labelledby') ?? '').split(' ');
1698+
expect(labelledBy).toContain('caller-label');
1699+
expect(
1700+
labelledBy.map(id => document.getElementById(id)?.textContent).join(),
1701+
).toContain('Guests');
1702+
});
1703+
1704+
it('styles the Field box with container* and the group with control*', () => {
1705+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
1706+
const {container} = renderListInput({
1707+
containerClassName: 'caller-container',
1708+
containerStyle: {marginTop: '7px'},
1709+
controlClassName: 'caller-control',
1710+
controlStyle: {marginBottom: '9px'},
1711+
});
1712+
const group = screen.getByRole('group');
1713+
const fieldBox = container.querySelector<HTMLElement>('.caller-container');
1714+
expect(fieldBox).not.toBeNull();
1715+
expect(fieldBox).not.toBe(group);
1716+
expect(fieldBox).toContainElement(group);
1717+
expect(fieldBox?.style.marginTop).toBe('7px');
1718+
expect(group).toHaveClass('caller-control', 'astryx-list-input');
1719+
expect(group.style.marginBottom).toBe('9px');
1720+
expect(warn).not.toHaveBeenCalled();
1721+
});
1722+
1723+
it('drops root styling and warns in development', () => {
1724+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
1725+
const untyped = {
1726+
className: 'caller-root',
1727+
style: {outlineColor: 'red'},
1728+
} as unknown as Partial<ListInputProps<Guest>>;
1729+
const {container} = renderListInput(untyped);
1730+
expect(container.querySelector('.caller-root')).toBeNull();
1731+
expect(screen.getByRole('group').style.outlineColor).toBe('');
1732+
expect(
1733+
warn.mock.calls.some(call => String(call[0]).startsWith('ListInput:')),
1734+
).toBe(true);
1735+
});
1736+
1737+
it('ignores container* and warns in a horizontal-labels FormLayout', () => {
1738+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
1739+
const {container} = render(
1740+
<FormLayout direction="horizontal-labels">
1741+
<ListInput<Guest>
1742+
label="Guests"
1743+
itemName="guest"
1744+
value={guests}
1745+
onChange={() => {}}
1746+
getItemKey={guest => guest.id}
1747+
createItem={() => createdGuest}
1748+
columns={columns}
1749+
containerClassName="caller-container"
1750+
/>
1751+
</FormLayout>,
1752+
);
1753+
expect(container.querySelector('.caller-container')).toBeNull();
1754+
expect(
1755+
warn.mock.calls.some(call =>
1756+
String(call[0]).includes('no Field box renders'),
1757+
),
1758+
).toBe(true);
1759+
});
1760+
1761+
it('types no root styling props', () => {
1762+
const props: ListInputProps<Guest> = {
1763+
label: 'Guests',
1764+
value: guests,
1765+
onChange: () => {},
1766+
getItemKey: guest => guest.id,
1767+
createItem: () => createdGuest,
1768+
columns,
1769+
containerClassName: 'caller-container',
1770+
controlClassName: 'caller-control',
1771+
// @ts-expect-error Field-composed inputs take no root className.
1772+
className: 'caller-root',
1773+
};
1774+
expect(props.controlClassName).toBe('caller-control');
1775+
});
1776+
});

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

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
* primitives, the container-reveal hook, compact size context, browser
99
* scroll geometry, and programmatic theme tokens. Also core List's reorder
1010
* session and handle, run by the owner's createReorderable object through
11-
* Core's internal entry `@astryxdesign/core/internal/reorder`
11+
* Core's internal entry `@astryxdesign/core/internal/reorder`, and Field's
12+
* shared styling rule through `@astryxdesign/core/internal/selectable-card`
1213
* @output Exports ListInput and its controlled data, column, renderer, and change types
1314
* @position Lab experiment (RFC meta/astryx#4531) for editing compact repeated records
1415
*
@@ -29,16 +30,20 @@ import {
2930
type ReactNode,
3031
} from 'react';
3132
import * as stylex from '@stylexjs/stylex';
32-
import type {BaseProps} from '@astryxdesign/core';
3333
import {Button} from '@astryxdesign/core/Button';
3434
import {EmptyState} from '@astryxdesign/core/EmptyState';
35-
import {Field, type InputStatus} from '@astryxdesign/core/Field';
35+
import {
36+
Field,
37+
type FieldInputBaseProps,
38+
type InputStatus,
39+
} from '@astryxdesign/core/Field';
3640
import {FieldStatus} from '@astryxdesign/core/FieldStatus';
3741
import {Icon} from '@astryxdesign/core/Icon';
3842
import {
3943
useAnnounce,
4044
useContainerReveal,
4145
useMediaQuery,
46+
useMergedRefs,
4247
} from '@astryxdesign/core/hooks';
4348
import {IconButton} from '@astryxdesign/core/IconButton';
4449
import {useTranslator} from '@astryxdesign/core/i18n';
@@ -47,6 +52,7 @@ import {
4752
ListReorderScope,
4853
useListReorderScope,
4954
} from '@astryxdesign/core/internal/reorder';
55+
import {useFieldInputStyling} from '@astryxdesign/core/internal/selectable-card';
5056
import type {Reorderable} from '@astryxdesign/core';
5157
import {reorderHandle} from '@astryxdesign/core/internal/reorderHandle.stylex';
5258
import {SizeProvider} from '@astryxdesign/core/SizeContext';
@@ -115,10 +121,10 @@ export interface ListInputColumn<T> {
115121
}
116122

117123
export interface ListInputProps<T> extends Omit<
118-
BaseProps<HTMLDivElement>,
124+
FieldInputBaseProps<HTMLDivElement>,
119125
'onChange'
120126
> {
121-
/** Ref forwarded to the outer field element. */
127+
/** Ref forwarded to the `role="group"` element. */
122128
ref?: React.Ref<HTMLDivElement>;
123129
/** Visible and accessible label for the list. */
124130
label: string;
@@ -705,17 +711,33 @@ export function ListInput<T>({
705711
isLabelHidden = false,
706712
isOptional = false,
707713
isRequired = false,
714+
id,
715+
containerXstyle,
716+
containerClassName,
717+
containerStyle,
718+
controlXstyle,
719+
controlClassName,
720+
controlStyle,
721+
'aria-labelledby': ariaLabelledBy,
722+
'aria-describedby': ariaDescribedBy,
708723
ref,
709-
...rest
724+
...restProps
710725
}: ListInputProps<T>): ReactNode {
726+
const {rest, container} = useFieldInputStyling('ListInput', restProps, {
727+
containerXstyle,
728+
containerClassName,
729+
containerStyle,
730+
});
711731
const generatedID = useId();
712-
const groupID = `list-input-${generatedID}`;
713-
const labelID = `${groupID}-label`;
714-
const descriptionID = description ? `${groupID}-description` : undefined;
715-
const statusID = status?.message ? `${groupID}-status` : undefined;
716-
const reorderInstructionsID = `${groupID}-reorder-instructions`;
732+
const baseID = `list-input-${generatedID}`;
733+
const groupID = id ?? baseID;
734+
const labelID = `${baseID}-label`;
735+
const descriptionID = description ? `${baseID}-description` : undefined;
736+
const statusID = status?.message ? `${baseID}-status` : undefined;
737+
const reorderInstructionsID = `${baseID}-reorder-instructions`;
717738
const rowRefs = useRef(new Map<string, HTMLDivElement>());
718739
const groupRef = useRef<HTMLDivElement>(null);
740+
const mergedGroupRef = useMergedRefs(ref, groupRef);
719741
const listRef = useRef<HTMLOListElement>(null);
720742
const addActionRef = useRef<HTMLDivElement>(null);
721743
const addButtonRef = useRef<HTMLButtonElement>(null);
@@ -1199,7 +1221,6 @@ export function ListInput<T>({
11991221
return (
12001222
<SizeProvider value="md">
12011223
<Field
1202-
ref={ref}
12031224
label={label}
12041225
description={description}
12051226
inputID={groupID}
@@ -1212,15 +1233,18 @@ export function ListInput<T>({
12121233
isDisabled={isDisabled}
12131234
status={status == null ? undefined : {...status, messageID: statusID}}
12141235
statusVariant="detached"
1215-
{...rest}>
1236+
xstyle={container.xstyle}
1237+
className={container.className}
1238+
style={container.style}>
12161239
<div
1217-
ref={groupRef}
1240+
{...rest}
1241+
ref={mergedGroupRef}
12181242
id={groupID}
12191243
role="group"
1220-
aria-labelledby={labelID}
1221-
aria-describedby={joinIDs(descriptionID, statusID)}
1222-
aria-disabled={isDisabled || undefined}
1223-
aria-busy={isLoading || undefined}
1244+
aria-labelledby={joinIDs(labelID, ariaLabelledBy)}
1245+
aria-describedby={joinIDs(ariaDescribedBy, descriptionID, statusID)}
1246+
aria-disabled={isDisabled || rest['aria-disabled']}
1247+
aria-busy={isLoading || rest['aria-busy']}
12241248
{...mergeProps(
12251249
themeProps('list-input', {
12261250
state: isLoading
@@ -1235,7 +1259,10 @@ export function ListInput<T>({
12351259
showStartHandle &&
12361260
!isHandleAlwaysVisible &&
12371261
styles.groupWithHangingHandles,
1262+
controlXstyle,
12381263
),
1264+
controlClassName,
1265+
controlStyle,
12391266
)}>
12401267
<ListReorderScope reorder={reorderScope}>
12411268
<ol
@@ -1270,7 +1297,7 @@ export function ListInput<T>({
12701297
showReorderHandle && (canReorder?.(itemKey) ?? true);
12711298
const itemStatus = getItemStatus?.(item, index);
12721299
const itemStatusID = itemStatus?.message
1273-
? `${groupID}-item-${index}-status`
1300+
? `${baseID}-item-${index}-status`
12741301
: undefined;
12751302
return (
12761303
<li

0 commit comments

Comments
 (0)