|
| 1 | +import { |
| 2 | + useState, useEffect, type CSSProperties, |
| 3 | +} from "react"; |
| 4 | +import { useFormFieldContext } from "../hooks/form-field-context"; |
| 5 | +import { useCustomize } from "../hooks/customization-context"; |
| 6 | + |
| 7 | +type KeyValuePair = { |
| 8 | + key: string; |
| 9 | + value: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export function ControlObject() { |
| 13 | + const formFieldContextProps = useFormFieldContext(); |
| 14 | + const { |
| 15 | + id, onChange, prop, value, |
| 16 | + } = formFieldContextProps; |
| 17 | + const { |
| 18 | + getProps, theme, |
| 19 | + } = useCustomize(); |
| 20 | + |
| 21 | + // Initialize pairs from the current value |
| 22 | + const initializePairs = (): KeyValuePair[] => { |
| 23 | + if (!value || typeof value !== "object" || Array.isArray(value)) { |
| 24 | + return [ |
| 25 | + { |
| 26 | + key: "", |
| 27 | + value: "", |
| 28 | + }, |
| 29 | + ]; |
| 30 | + } |
| 31 | + |
| 32 | + const pairs = Object.entries(value).map(([ |
| 33 | + k, |
| 34 | + v, |
| 35 | + ]) => ({ |
| 36 | + key: k, |
| 37 | + value: typeof v === "string" |
| 38 | + ? v |
| 39 | + : JSON.stringify(v), |
| 40 | + })); |
| 41 | + |
| 42 | + return pairs.length > 0 |
| 43 | + ? pairs |
| 44 | + : [ |
| 45 | + { |
| 46 | + key: "", |
| 47 | + value: "", |
| 48 | + }, |
| 49 | + ]; |
| 50 | + }; |
| 51 | + |
| 52 | + const [ |
| 53 | + pairs, |
| 54 | + setPairs, |
| 55 | + ] = useState<KeyValuePair[]>(initializePairs); |
| 56 | + |
| 57 | + // Update pairs when value changes externally |
| 58 | + useEffect(() => { |
| 59 | + setPairs(initializePairs()); |
| 60 | + }, [ |
| 61 | + value, |
| 62 | + ]); |
| 63 | + |
| 64 | + const updateObject = (newPairs: KeyValuePair[]) => { |
| 65 | + // Filter out empty pairs |
| 66 | + const validPairs = newPairs.filter((p) => p.key.trim() !== ""); |
| 67 | + |
| 68 | + if (validPairs.length === 0) { |
| 69 | + onChange(undefined); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + // Convert to object |
| 74 | + const obj: Record<string, any> = {}; |
| 75 | + validPairs.forEach((pair) => { |
| 76 | + if (pair.key.trim()) { |
| 77 | + // Try to parse the value as JSON, fallback to string |
| 78 | + try { |
| 79 | + obj[pair.key] = JSON.parse(pair.value); |
| 80 | + } catch { |
| 81 | + obj[pair.key] = pair.value; |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + onChange(obj); |
| 87 | + }; |
| 88 | + |
| 89 | + const handlePairChange = (index: number, field: "key" | "value", newValue: string) => { |
| 90 | + const newPairs = [ |
| 91 | + ...pairs, |
| 92 | + ]; |
| 93 | + newPairs[index] = { |
| 94 | + ...newPairs[index], |
| 95 | + [field]: newValue, |
| 96 | + }; |
| 97 | + setPairs(newPairs); |
| 98 | + updateObject(newPairs); |
| 99 | + }; |
| 100 | + |
| 101 | + const addPair = () => { |
| 102 | + const newPairs = [ |
| 103 | + ...pairs, |
| 104 | + { |
| 105 | + key: "", |
| 106 | + value: "", |
| 107 | + }, |
| 108 | + ]; |
| 109 | + setPairs(newPairs); |
| 110 | + }; |
| 111 | + |
| 112 | + const removePair = (index: number) => { |
| 113 | + const newPairs = pairs.filter((_, i) => i !== index); |
| 114 | + setPairs(newPairs.length > 0 |
| 115 | + ? newPairs |
| 116 | + : [ |
| 117 | + { |
| 118 | + key: "", |
| 119 | + value: "", |
| 120 | + }, |
| 121 | + ]); |
| 122 | + updateObject(newPairs); |
| 123 | + }; |
| 124 | + |
| 125 | + const containerStyles: CSSProperties = { |
| 126 | + gridArea: "control", |
| 127 | + display: "flex", |
| 128 | + flexDirection: "column", |
| 129 | + gap: "0.5rem", |
| 130 | + }; |
| 131 | + |
| 132 | + const pairStyles: CSSProperties = { |
| 133 | + display: "flex", |
| 134 | + gap: "0.5rem", |
| 135 | + alignItems: "center", |
| 136 | + }; |
| 137 | + |
| 138 | + const inputStyles: CSSProperties = { |
| 139 | + color: theme.colors.neutral60, |
| 140 | + border: "1px solid", |
| 141 | + borderColor: theme.colors.neutral20, |
| 142 | + padding: 6, |
| 143 | + borderRadius: theme.borderRadius, |
| 144 | + boxShadow: theme.boxShadow.input, |
| 145 | + flex: 1, |
| 146 | + }; |
| 147 | + |
| 148 | + const buttonStyles: CSSProperties = { |
| 149 | + color: theme.colors.neutral60, |
| 150 | + display: "inline-flex", |
| 151 | + alignItems: "center", |
| 152 | + padding: `${theme.spacing.baseUnit}px ${theme.spacing.baseUnit * 1.5}px ${ |
| 153 | + theme.spacing.baseUnit |
| 154 | + }px ${theme.spacing.baseUnit * 2.5}px`, |
| 155 | + border: `1px solid ${theme.colors.neutral30}`, |
| 156 | + borderRadius: theme.borderRadius, |
| 157 | + cursor: "pointer", |
| 158 | + fontSize: "0.8125rem", |
| 159 | + fontWeight: 450, |
| 160 | + gap: theme.spacing.baseUnit * 2, |
| 161 | + textWrap: "nowrap", |
| 162 | + backgroundColor: "white", |
| 163 | + }; |
| 164 | + |
| 165 | + const removeButtonStyles: CSSProperties = { |
| 166 | + ...buttonStyles, |
| 167 | + flex: "0 0 auto", |
| 168 | + padding: "6px 8px", |
| 169 | + }; |
| 170 | + |
| 171 | + return ( |
| 172 | + <div {...getProps("controlObject", containerStyles, formFieldContextProps)}> |
| 173 | + {pairs.map((pair, index) => ( |
| 174 | + <div key={index} style={pairStyles}> |
| 175 | + <input |
| 176 | + type="text" |
| 177 | + value={pair.key} |
| 178 | + onChange={(e) => handlePairChange(index, "key", e.target.value)} |
| 179 | + placeholder="Key" |
| 180 | + style={inputStyles} |
| 181 | + required={!prop.optional && index === 0} |
| 182 | + /> |
| 183 | + <input |
| 184 | + type="text" |
| 185 | + value={pair.value} |
| 186 | + onChange={(e) => handlePairChange(index, "value", e.target.value)} |
| 187 | + placeholder="Value" |
| 188 | + style={inputStyles} |
| 189 | + /> |
| 190 | + {pairs.length > 1 && ( |
| 191 | + <button |
| 192 | + type="button" |
| 193 | + onClick={() => removePair(index)} |
| 194 | + style={removeButtonStyles} |
| 195 | + aria-label="Remove pair" |
| 196 | + > |
| 197 | + × |
| 198 | + </button> |
| 199 | + )} |
| 200 | + </div> |
| 201 | + ))} |
| 202 | + <button |
| 203 | + type="button" |
| 204 | + onClick={addPair} |
| 205 | + style={{ |
| 206 | + ...buttonStyles, |
| 207 | + alignSelf: "flex-start", |
| 208 | + paddingRight: `${theme.spacing.baseUnit * 2}px`, |
| 209 | + }} |
| 210 | + > |
| 211 | + <span>+</span> |
| 212 | + <span>Add more</span> |
| 213 | + </button> |
| 214 | + </div> |
| 215 | + ); |
| 216 | +} |
0 commit comments