Skip to content

Commit

Permalink
feat: add Visual JSON Schema Editor (#905)
Browse files Browse the repository at this point in the history
Co-authored-by: samz <[email protected]>
  • Loading branch information
princerajpoot20 and Amzani authored Mar 26, 2024
1 parent 011a0d4 commit 9c3773c
Show file tree
Hide file tree
Showing 13 changed files with 792 additions and 2 deletions.
284 changes: 284 additions & 0 deletions apps/design-system/src/components/VisualJsonSchemaEditor.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
import React, { useState } from 'react';
import { VisualEditor, CodeEditor, Examples } from '@asyncapi/studio-ui';

export default {
component: VisualEditor,
parameters: {
layout: 'fullscreen',
},
};
type TemplateProps = {
initialSchema: string;
};

const Template: React.FC<TemplateProps> = ({ initialSchema }) => {
const [schema, setSchema] = useState<string>(initialSchema);
const [editorType, setEditorType] = useState<'visual' | 'code' | 'examples'>('visual');


return (
<div>
<div style={{ width: '45vw', minWidth:'550px', background: '#0F172A', color: '#CBD5E1', fontFamily: 'Inter, sans-serif' }}>
<button style={{padding: 5}} onClick={() => setEditorType('visual')}>Visual Editor </button>
<button style={{padding: 5}} onClick={() => setEditorType('code')}>Code Editor </button>
<button style={{padding: 5}} onClick={() => setEditorType('examples')}>Examples </button>
</div>
<div>
{editorType === 'visual' && (
<VisualEditor schema={schema} onSchemaChange={setSchema} />
)}
{editorType === 'code' && (
<CodeEditor schema={schema} onSchemaChange={setSchema} />
)}
{editorType === 'examples' && (
<Examples/>
)}
</div>
</div>
);
};

export const Sample_Schema = () => (
<Template
initialSchema={JSON.stringify({
"type": "object",
"properties": {

"age": {
"type": "integer"
},
"address": {
"type": "object",
"properties": {
"street": {
"type": "string"
},
"city": {
"type": "string"
},
"pincode":{
"type": "number"
}
},
"required": [
"street",
"city"
]
}
}
}, null, 2)}
/>
);

export const Schema_with_array_object = () => (
<Template
initialSchema={JSON.stringify({
"type": "object",
"properties": {
"firstName": {
"type": "string"
},
"lastName": {
"type": "string"
},
"age": {
"type": "boolean"
},
"height": {
"type": ["integer", "null"]
},
"friends": {
"type": "array",
"items": {
"type": "object",
"properties": {
"firstName": {
"type": "string"
}
},
"required": ["firstName"]
}
}
},
"required": ["firstName", "lastName"]
}
, null, 2)}
/>
);

export const WithArray_obj = () => (
<Template
initialSchema={JSON.stringify({
"type": "object",
"properties": {
"books": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {
"type": "string"
},
"author": {
"type": "string"
}
}
}
}
}
}, null, 2)}
/>
);

export const WithArray_obj_and_obj = () => (
<Template
initialSchema={JSON.stringify({
"type": "object",
"properties": {
"books": {
"type": "array",
"items": {
"type": "object",
"properties": {
"title": {"type": "string"},
"author": {"type": "string"}
},
"required": ["title"]
}
},
"list": {
"type": "object",
"properties": {
"hii": {"type": "string"}
}
}
},
"required": ["books", "list"]
}, null, 2)}
/>
);

export const Array_of_string = () => (
<Template
initialSchema={JSON.stringify({
"type": "object",
"properties": {
"numbers": {
"type": "array",
"items": {
"type": "number"
}
},
"aString": {
"type": "string"
},
"aNumber": {
"type": "number"
}
},
"required": ["numbers", "aString", "aNumber"]
}, null, 2)}
/>
);

export const Root_Array_of_Object = () => (
<Template
initialSchema={JSON.stringify({
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"isActive": {
"type": "boolean"
}
},
"required": ["id", "name"]
}
}, null, 2)}
/>
);

export const two_property_having_same_name = () => (
<Template
initialSchema={JSON.stringify({

"type": "object",
"properties": {
"address": {
"type": "object",
"properties": {
"address": { "type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" }
}
}
}
}
}
}

, null, 2)}
/>
);

export const nestead_array = () => (
<Template
initialSchema={JSON.stringify(
{
"type": "object",
"properties": {
"age": {
"type": "integer"
},
"address": {
"type": "object",
"properties": {
"street": {
"type": "string"
},
"city": {
"type": "string"
},
"pincode": {
"type": "number"
},
"contact_number": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"mobile":{
"type": "stirng"
},
"address": {
"type": "object",
"properties":{
"city":{
"type": "string"
}
}
}
}
}
}
},
"required": [
"street",
"city"
]
}
}
}, null, 2)}
/>
);

3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
],
"packageManager": "[email protected]",
"dependencies": {
"@changesets/cli": "^2.26.2"
"@changesets/cli": "^2.26.2",
"lodash": "^4.17.21"
}
}
Loading

0 comments on commit 9c3773c

Please sign in to comment.