MCPcopy Create free account
hub / github.com/adobe/react-spectrum / ArrayControl

Function ArrayControl

packages/dev/s2-docs/src/VisualExampleClient.tsx:1521–1597  ·  view source on GitHub ↗
({control, valueType, value = [], onChange})

Source from the content-addressed store, hash-verified

1519}
1520
1521function ArrayControl({control, valueType, value = [], onChange}) {
1522 let ref = useRef<HTMLDivElement | null>(null);
1523 return (
1524 <Wrapper
1525 ref={ref}
1526 control={control}
1527 styles={style({gridColumnStart: 1, gridColumnEnd: -1, width: 150})}>
1528 {value.length === 0 && (
1529 <ActionButton
1530 size="S"
1531 aria-label="Add item"
1532 styles={style({alignSelf: 'start'})}
1533 onPress={() => {
1534 flushSync(() => onChange(['']));
1535 ref.current?.querySelector('input')?.focus();
1536 }}>
1537 <AddCircle />
1538 </ActionButton>
1539 )}
1540 {value.map((item, index) => {
1541 let rendered;
1542 switch (valueType.type) {
1543 case 'string':
1544 rendered = (
1545 <TextField
1546 aria-label={`${control.name}, item ${index}`}
1547 size="S"
1548 styles={style({flexGrow: 1})}
1549 value={item}
1550 onChange={newValue => {
1551 let arr: any[] = [...value];
1552 arr[index] = newValue;
1553 onChange(arr);
1554 }}
1555 />
1556 );
1557 break;
1558 default:
1559 console.warn('unknown array element type', valueType);
1560 return null;
1561 }
1562
1563 return (
1564 <div key={index} className={style({display: 'flex', gap: 4})}>
1565 {rendered}
1566 <ActionButton
1567 size="S"
1568 aria-label="Add item after"
1569 onPress={() => {
1570 let arr: any[] = [...value];
1571 arr.splice(index + 1, 0, '');
1572 flushSync(() => onChange(arr));
1573 ref.current?.querySelectorAll('input')[index + 1]?.focus();
1574 }}>
1575 <AddCircle />
1576 </ActionButton>
1577 <ActionButton
1578 size="S"

Callers

nothing calls this directly

Calls 2

focusMethod · 0.80
onChangeFunction · 0.70

Tested by

no test coverage detected