Skip to content

Commit baf6e57

Browse files
committed
2536 - JSON Schema Builder - Differentiate root element from the rest
1 parent bb0fe24 commit baf6e57

File tree

3 files changed

+38
-14
lines changed

3 files changed

+38
-14
lines changed

client/src/components/JsonSchemaBuilder/JsonSchemaBuilder.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const JsonSchemaBuilder = ({locale = 'en', onChange, schema}: JsonSchemaBuilderP
5454
...schema,
5555
});
5656
}}
57+
root
5758
schema={curSchema}
5859
/>
5960
);

client/src/components/JsonSchemaBuilder/components/SchemaControls.tsx

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,18 @@ import {
1717
import {SchemaRecordType} from '../utils/types';
1818

1919
interface SchemaControlsProps {
20-
schema: SchemaRecordType;
21-
schemakey: string;
2220
isCollapsed?: boolean;
23-
onDelete?: () => void;
2421
onAdd?: () => void;
25-
onCollapse?: () => void;
2622
onChangeKey?: (key: string) => void;
2723
onChange: (schema: SchemaRecordType) => void;
24+
onCollapse?: () => void;
25+
onDelete?: () => void;
26+
root?: boolean;
27+
schema: SchemaRecordType;
28+
schemakey: string;
2829
}
2930

30-
const SchemaControls = ({onAdd, onChange, onChangeKey, onDelete, schema, schemakey}: SchemaControlsProps) => {
31+
const SchemaControls = ({onAdd, onChange, onChangeKey, onDelete, root, schema, schemakey}: SchemaControlsProps) => {
3132
const [isMenuOpen, setIsMenuOpen] = useState<boolean>(false);
3233

3334
const isObjectSchema = getSchemaType(schema) === 'object';
@@ -41,8 +42,8 @@ const SchemaControls = ({onAdd, onChange, onChangeKey, onDelete, schema, schemak
4142
}, [schema, onChange]);
4243

4344
return (
44-
<div className="flex w-full flex-row">
45-
<div className="flex flex-1 gap-2">
45+
<div className="flex w-full items-end">
46+
<div className={twMerge('flex gap-2', root ? 'mr-1' : 'flex-1')}>
4647
<SchemaTypesSelect
4748
onChange={(translation) => onChange(setSchemaTypeAndRemoveWrongFields(translation, schema))}
4849
type={getSchemaType(schema)}
@@ -60,7 +61,12 @@ const SchemaControls = ({onAdd, onChange, onChangeKey, onDelete, schema, schemak
6061
)}
6162
</div>
6263

63-
<div className="ml-auto grid shrink-0 grid-flow-col items-center gap-1">
64+
<div
65+
className={twMerge(
66+
'ml-auto grid shrink-0 grid-flow-col items-center gap-1',
67+
root && 'ml-0 flex-1 justify-between'
68+
)}
69+
>
6470
<SchemaMenuPopover
6571
onChange={onChange}
6672
onClose={() => setIsMenuOpen(false)}
@@ -70,11 +76,13 @@ const SchemaControls = ({onAdd, onChange, onChangeKey, onDelete, schema, schemak
7076
<Button
7177
className={twMerge('group px-2.5', isMenuOpen && 'bg-surface-brand-secondary')}
7278
onClick={() => setIsMenuOpen((open) => !open)}
73-
title="Extra options"
79+
title="Extra fields"
7480
variant="ghost"
7581
>
7682
<CircleEllipsisIcon className={twMerge(isMenuOpen && 'text-content-brand-primary')} />
7783

84+
{root && <span>Extra fields</span>}
85+
7886
{extraFields.length > 0 && (
7987
<Badge
8088
className={twMerge(
@@ -103,13 +111,18 @@ const SchemaControls = ({onAdd, onChange, onChangeKey, onDelete, schema, schemak
103111

104112
{(typeof onAdd === 'function' || isObjectSchema) && (
105113
<Button
114+
className={twMerge(
115+
root && 'ml-auto bg-surface-brand-primary hover:bg-surface-brand-primary-hover'
116+
)}
106117
disabled={!isObjectSchema}
107118
onClick={onAdd || (() => onChange(addSchemaProperty(schema)))}
108-
size="icon"
119+
size={root ? 'default' : 'icon'}
109120
title="Add Property"
110-
variant="ghost"
121+
variant={root ? 'default' : 'ghost'}
111122
>
112123
<PlusIcon />
124+
125+
{root && <span>Add a pill</span>}
113126
</Button>
114127
)}
115128
</div>
@@ -120,10 +133,11 @@ const SchemaControls = ({onAdd, onChange, onChangeKey, onDelete, schema, schemak
120133
interface SchemaArrayControlsProps {
121134
onAdd?: () => void;
122135
onChange: (schema: SchemaRecordType) => void;
136+
root?: boolean;
123137
schema: SchemaRecordType;
124138
}
125139

126-
const SchemaArrayControls = ({onAdd, onChange, schema}: SchemaArrayControlsProps) => {
140+
const SchemaArrayControls = ({onAdd, onChange, root, schema}: SchemaArrayControlsProps) => {
127141
const [isMenuOpen, setIsMenuOpen] = useState<boolean>(false);
128142

129143
const extraFields = Object.keys(schema).filter((key) => key !== 'type' && key !== 'items' && key !== 'properties');
@@ -145,11 +159,13 @@ const SchemaArrayControls = ({onAdd, onChange, schema}: SchemaArrayControlsProps
145159
<Button
146160
className={twMerge('group px-2.5', isMenuOpen && 'bg-surface-brand-secondary')}
147161
onClick={() => setIsMenuOpen((open) => !open)}
148-
title="Extra options"
162+
title="Extra fields"
149163
variant="ghost"
150164
>
151165
<CircleEllipsisIcon className={twMerge(isMenuOpen && 'text-content-brand-primary')} />
152166

167+
{root && <span>Extra fields</span>}
168+
153169
{extraFields.length > 0 && (
154170
<Badge
155171
className={twMerge(

client/src/components/JsonSchemaBuilder/components/SchemaCreator.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ interface SchemaCreatorProps {
2525
onChangeKey?: (key: string) => void;
2626
onChange?: (schema: SchemaRecordType) => void;
2727
onDelete?: (key: string) => void;
28+
root?: boolean;
2829
schema: SchemaRecordType;
2930
schemakey?: string;
3031
}
@@ -34,6 +35,7 @@ const SchemaCreator = ({
3435
onChange = () => {},
3536
onChangeKey = () => {},
3637
onDelete = () => {},
38+
root = false,
3739
schema,
3840
schemakey = '__root__',
3941
}: SchemaCreatorProps) => {
@@ -50,6 +52,7 @@ const SchemaCreator = ({
5052
onChange={onChange}
5153
onChangeKey={schemakey !== '__root__' ? onChangeKey : undefined}
5254
onDelete={schemakey !== '__root__' ? () => onDelete(schemakey) : undefined}
55+
root={root}
5356
schema={schema}
5457
schemakey={schemakey}
5558
/>
@@ -71,6 +74,7 @@ const SchemaCreator = ({
7174
<SchemaBox>
7275
<SchemaArrayItems
7376
onChange={(s) => onChange(setSchemaItems(s, schema))}
77+
root={schemakey === '__root__'}
7478
schema={getSchemaItems(schema)}
7579
/>
7680
</SchemaBox>
@@ -82,14 +86,16 @@ const SchemaCreator = ({
8286

8387
interface SchemaArrayItemsProps {
8488
schema: SchemaRecordType;
89+
root?: boolean;
8590
onChange: (schema: SchemaRecordType) => void;
8691
}
8792

88-
const SchemaArrayItems = ({onChange, schema}: SchemaArrayItemsProps) => (
93+
const SchemaArrayItems = ({onChange, root, schema}: SchemaArrayItemsProps) => (
8994
<>
9095
<SchemaArrayControls
9196
onAdd={isSchemaObject(schema) ? () => onChange(addSchemaProperty(schema)) : undefined}
9297
onChange={onChange}
98+
root={root}
9399
schema={schema}
94100
/>
95101

@@ -108,6 +114,7 @@ const SchemaArrayItems = ({onChange, schema}: SchemaArrayItemsProps) => (
108114
<SchemaBox>
109115
<SchemaArrayItems
110116
onChange={(s) => onChange(setSchemaItems(s, schema))}
117+
root={root}
111118
schema={getSchemaItems(schema)}
112119
/>
113120
</SchemaBox>

0 commit comments

Comments
 (0)