Skip to content

Commit efb4306

Browse files
committed
Add support for schema for additionalProperties key
1 parent 997173d commit efb4306

File tree

2 files changed

+54
-9
lines changed

2 files changed

+54
-9
lines changed

src/components/containers.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ export function GroupTitle(props) {
66
return null;
77

88
return (
9-
<div className="rjf-form-group-title">{props.children}</div>
9+
<div className="rjf-form-group-title">
10+
{props.editable ?
11+
<span>{props.children} <Button className="edit" onClick={props.onEdit} title="Edit">Edit</Button></span>
12+
:
13+
props.children
14+
}
15+
</div>
1016
);
1117
}
1218

@@ -119,11 +125,12 @@ export function FormGroup(props) {
119125
? ""
120126
: "rjf-form-group-inner";
121127

128+
122129
return (
123130
<div className="rjf-form-group">
124-
{props.level === 0 && <GroupTitle>{props.schema.title}</GroupTitle>}
131+
{props.level === 0 && <GroupTitle editable={props.editable} onEdit={props.onEdit}>{props.schema.title}</GroupTitle>}
125132
<div className={innerClassName}>
126-
{props.level > 0 && <GroupTitle>{props.schema.title}</GroupTitle>}
133+
{props.level > 0 && <GroupTitle editable={props.editable} onEdit={props.onEdit}>{props.schema.title}</GroupTitle>}
127134
{props.children}
128135
{props.addable &&
129136
<Button

src/ui.js

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,11 +225,24 @@ export function getArrayFormRow(args) {
225225
schema={schema}
226226
addable={addable}
227227
onAdd={() => onAdd(getBlankData(schema.items), coords)}
228+
editable={args.editable}
229+
onEdit={args.onEdit}
228230
key={'row_group_' + name}
229231
>
230232
{rows}
231233
</FormGroup>
232234
);
235+
236+
if (args.parentType === 'object' && args.removable) {
237+
rows = (
238+
<div className="rjf-form-group-wrapper" key={'row_group_wrapper_' + name}>
239+
<FormRowControls
240+
onRemove={(e) => onRemove(name)}
241+
/>
242+
{rows}
243+
</div>
244+
);
245+
}
233246
}
234247

235248
if (groups.length) {
@@ -282,7 +295,15 @@ export function getObjectFormRow(args) {
282295
let key = keys[i];
283296
let value = data[key];
284297
let childName = name + '-' + key;
285-
let schemaValue = schema_keys[key] || {type: 'string'};
298+
let schemaValue = schema_keys[key]; // || {type: 'string'};
299+
300+
if (typeof schemaValue === 'undefined') {
301+
// for keys added through additionalProperties
302+
if (typeof schema.additionalProperties === 'boolean')
303+
schemaValue = {type: 'string'};
304+
else
305+
schemaValue = {...schema.additionalProperties};
306+
}
286307

287308
let type = schemaValue.type;
288309

@@ -311,13 +332,14 @@ export function getObjectFormRow(args) {
311332
parentType: 'object',
312333
};
313334

335+
nextArgs.onEdit = () => handleKeyEdit(data, key, value, childName, onAdd, onRemove);
336+
nextArgs.editable = removable;
337+
314338
if (type === 'array') {
315339
rows.push(getArrayFormRow(nextArgs));
316340
} else if (type === 'object') {
317341
rows.push(getObjectFormRow(nextArgs));
318342
} else {
319-
nextArgs.onEdit = () => handleKeyEdit(data, key, value, childName, onAdd, onRemove);
320-
nextArgs.editable = removable;
321343
rows.push(getStringFormRow(nextArgs));
322344
}
323345
}
@@ -334,30 +356,46 @@ export function getObjectFormRow(args) {
334356
level={level}
335357
schema={schema}
336358
addable={schema.additionalProperties}
337-
onAdd={() => handleKeyValueAdd(data, coords, onAdd)}
359+
onAdd={() => handleKeyValueAdd(data, coords, onAdd, schema.additionalProperties)}
360+
editable={args.editable}
361+
onEdit={args.onEdit}
338362
key={'row_group_' + name}
339363
>
340364
{rows}
341365
</FormGroup>
342366
);
367+
if (args.parentType === 'object' && args.removable) {
368+
rows = (
369+
<div className="rjf-form-group-wrapper" key={'row_group_wrapper_' + name}>
370+
<FormRowControls
371+
onRemove={(e) => onRemove(name)}
372+
/>
373+
{rows}
374+
</div>
375+
);
376+
}
377+
343378
}
344379

345380
return rows;
346381
}
347382

348383

349-
function handleKeyValueAdd(data, coords, onAdd) {
384+
function handleKeyValueAdd(data, coords, onAdd, newSchema) {
350385
let key = prompt("Add new key");
351386
if (key === null) // clicked cancel
352387
return;
353388

389+
if (newSchema === true)
390+
newSchema = {type: 'string'};
391+
354392
key = key.trim();
355393
if (!key)
356394
alert("(!) Can't add empty key.\r\n\r\n‎");
357395
else if (data.hasOwnProperty(key))
358396
alert("(!) Duplicate keys not allowed. This key already exists.\r\n\r\n‎");
359397
else
360-
onAdd("", coords + '-' + key);
398+
onAdd(getBlankData(newSchema), coords + '-' + key);
361399
}
362400

363401

0 commit comments

Comments
 (0)