({ value, onChange, disabled = false, suggestionItems }: StateKeyValueInputProps)
| 24 | * Keys are plain text; values support variable syntax via VariableInput. |
| 25 | */ |
| 26 | export function StateKeyValueInput({ value, onChange, disabled = false, suggestionItems }: StateKeyValueInputProps) { |
| 27 | const theme = useTheme() |
| 28 | |
| 29 | const entries = useMemo(() => (Array.isArray(value) ? value : []), [value]) |
| 30 | const { keys: stableKeys, removeKey } = useStableKeys(entries.length, 'state-kv') |
| 31 | |
| 32 | const handleKeyChange = useCallback( |
| 33 | (index: number, newKey: string) => { |
| 34 | const updated = entries.map((entry, i) => (i === index ? { ...entry, key: newKey } : entry)) |
| 35 | onChange(updated) |
| 36 | }, |
| 37 | [entries, onChange] |
| 38 | ) |
| 39 | |
| 40 | const handleValueChange = useCallback( |
| 41 | (index: number, newValue: string) => { |
| 42 | const updated = entries.map((entry, i) => (i === index ? { ...entry, value: newValue } : entry)) |
| 43 | onChange(updated) |
| 44 | }, |
| 45 | [entries, onChange] |
| 46 | ) |
| 47 | |
| 48 | const handleAdd = useCallback(() => { |
| 49 | onChange([...entries, { key: '', value: '' }]) |
| 50 | }, [entries, onChange]) |
| 51 | |
| 52 | const handleRemove = useCallback( |
| 53 | (index: number) => { |
| 54 | removeKey(index) |
| 55 | onChange(entries.filter((_, i) => i !== index)) |
| 56 | }, |
| 57 | [entries, onChange, removeKey] |
| 58 | ) |
| 59 | |
| 60 | return ( |
| 61 | <Box data-testid='state-key-value-input'> |
| 62 | {entries.map((entry, index) => ( |
| 63 | <Box |
| 64 | key={stableKeys[index]} |
| 65 | sx={{ |
| 66 | p: 2, |
| 67 | mt: 1, |
| 68 | mb: 1, |
| 69 | border: 1, |
| 70 | borderColor: theme.palette.divider, |
| 71 | borderRadius: 2, |
| 72 | position: 'relative' |
| 73 | }} |
| 74 | > |
| 75 | <IconButton |
| 76 | title='Remove' |
| 77 | onClick={() => handleRemove(index)} |
| 78 | disabled={disabled} |
| 79 | sx={{ |
| 80 | position: 'absolute', |
| 81 | height: 30, |
| 82 | width: 30, |
| 83 | right: 8, |
nothing calls this directly
no test coverage detected