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