|
| 1 | +import { useState } from "react" |
| 2 | +import Form from "@rjsf/core" |
| 3 | +import validator from "@rjsf/validator-ajv8" |
| 4 | +import type { RJSFSchema } from "@rjsf/utils" |
| 5 | +import { customTemplates, customWidgets } from "./rjsf-templates.tsx" |
| 6 | + |
| 7 | +interface AdditionalPropertiesEditorProps { |
| 8 | + value: Record<string, unknown> |
| 9 | + onChange: (value: Record<string, unknown>) => void |
| 10 | + readonly?: boolean |
| 11 | + title?: string |
| 12 | + description?: string |
| 13 | + required?: boolean |
| 14 | + itemSchema: RJSFSchema |
| 15 | +} |
| 16 | + |
| 17 | +export function AdditionalPropertiesEditor({ |
| 18 | + value, |
| 19 | + onChange, |
| 20 | + readonly, |
| 21 | + title, |
| 22 | + description, |
| 23 | + required, |
| 24 | + itemSchema, |
| 25 | +}: AdditionalPropertiesEditorProps) { |
| 26 | + const [newKey, setNewKey] = useState("") |
| 27 | + |
| 28 | + const keys = Object.keys(value || {}) |
| 29 | + |
| 30 | + const handleAddKey = () => { |
| 31 | + if (!newKey.trim()) return |
| 32 | + if (value && newKey in value) { |
| 33 | + alert(`Key "${newKey}" already exists`) |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + // Initialize with default value from schema |
| 38 | + const defaultValue = itemSchema.type === "object" ? {} : itemSchema.default ?? "" |
| 39 | + onChange({ ...value, [newKey]: defaultValue }) |
| 40 | + setNewKey("") |
| 41 | + } |
| 42 | + |
| 43 | + const handleRemoveKey = (key: string) => { |
| 44 | + const newValue = { ...value } |
| 45 | + delete newValue[key] |
| 46 | + onChange(newValue) |
| 47 | + } |
| 48 | + |
| 49 | + const handleValueChange = (key: string, newVal: unknown) => { |
| 50 | + onChange({ ...value, [key]: newVal }) |
| 51 | + } |
| 52 | + |
| 53 | + return ( |
| 54 | + <div className="field"> |
| 55 | + {title && ( |
| 56 | + <label className="control-label mb-2 block text-sm font-medium text-slate-700"> |
| 57 | + {title} |
| 58 | + {required && <span className="required ml-1 text-red-500">*</span>} |
| 59 | + </label> |
| 60 | + )} |
| 61 | + {description && <p className="field-description mb-3 text-xs text-slate-500">{description}</p>} |
| 62 | + |
| 63 | + <div className="space-y-3"> |
| 64 | + {keys.map((key) => ( |
| 65 | + <div key={key} className="rounded-lg border border-slate-200 bg-slate-50 p-4"> |
| 66 | + <div className="mb-2 flex items-center justify-between"> |
| 67 | + <div className="font-mono text-sm font-semibold text-slate-900">{key}</div> |
| 68 | + {!readonly && ( |
| 69 | + <button |
| 70 | + type="button" |
| 71 | + onClick={() => handleRemoveKey(key)} |
| 72 | + className="rounded-md border border-red-300 bg-white px-2 py-1 text-xs font-medium text-red-600 hover:bg-red-50" |
| 73 | + > |
| 74 | + × Remove |
| 75 | + </button> |
| 76 | + )} |
| 77 | + </div> |
| 78 | + <div className="rounded-md bg-white p-3"> |
| 79 | + <Form |
| 80 | + schema={itemSchema} |
| 81 | + formData={value[key]} |
| 82 | + validator={validator} |
| 83 | + templates={customTemplates} |
| 84 | + widgets={customWidgets} |
| 85 | + onChange={(e) => handleValueChange(key, e.formData)} |
| 86 | + disabled={readonly} |
| 87 | + liveValidate={false} |
| 88 | + showErrorList={false} |
| 89 | + uiSchema={{ |
| 90 | + "ui:submitButtonOptions": { norender: true }, |
| 91 | + }} |
| 92 | + > |
| 93 | + {/* No submit button */} |
| 94 | + </Form> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + ))} |
| 98 | + |
| 99 | + {keys.length === 0 && readonly && ( |
| 100 | + <div className="text-sm text-slate-500 italic">No entries</div> |
| 101 | + )} |
| 102 | + |
| 103 | + {!readonly && ( |
| 104 | + <div className="flex gap-2"> |
| 105 | + <input |
| 106 | + type="text" |
| 107 | + value={newKey} |
| 108 | + onChange={(e) => setNewKey(e.target.value)} |
| 109 | + onKeyDown={(e) => { |
| 110 | + if (e.key === "Enter") { |
| 111 | + e.preventDefault() |
| 112 | + handleAddKey() |
| 113 | + } |
| 114 | + }} |
| 115 | + placeholder="Enter key name..." |
| 116 | + className="flex-1 rounded-lg border border-slate-300 bg-white px-3 py-2 text-sm text-slate-900 placeholder:text-slate-400 outline-none focus:border-blue-400 focus:ring-1 focus:ring-blue-400" |
| 117 | + /> |
| 118 | + <button |
| 119 | + type="button" |
| 120 | + onClick={handleAddKey} |
| 121 | + disabled={!newKey.trim()} |
| 122 | + className="rounded-md border border-slate-300 bg-white px-3 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50 disabled:opacity-50 disabled:cursor-not-allowed" |
| 123 | + > |
| 124 | + + Add |
| 125 | + </button> |
| 126 | + </div> |
| 127 | + )} |
| 128 | + </div> |
| 129 | + </div> |
| 130 | + ) |
| 131 | +} |
0 commit comments