-
-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added first example to docs - different bridges with antd theme
- Loading branch information
1 parent
f751fcb
commit 627fd99
Showing
8 changed files
with
227 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"[mdx]": { | ||
"editor.defaultFormatter": "unifiedjs.vscode-mdx" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
SandpackCodeEditor, | ||
SandpackLayout, | ||
SandpackPreview, | ||
SandpackProvider, | ||
} from '@codesandbox/sandpack-react'; | ||
import { | ||
dependencies, | ||
Bridges, | ||
} from '@site/components/CodeEditor/dependencies'; | ||
|
||
type CodeEditorProps = { | ||
files: Record<string, string>; | ||
bridge: Bridges; | ||
}; | ||
|
||
export function CodeEditor(props: CodeEditorProps) { | ||
const { files, bridge } = props; | ||
|
||
return ( | ||
<SandpackProvider | ||
template="react-ts" | ||
customSetup={{ dependencies: dependencies[bridge] }} | ||
files={files} | ||
options={{ | ||
recompileMode: 'delayed', | ||
recompileDelay: 800, | ||
}} | ||
> | ||
<SandpackLayout style={{ height: '460px' }}> | ||
<SandpackCodeEditor | ||
showTabs | ||
showInlineErrors | ||
style={{ height: '100%' }} | ||
/> | ||
<SandpackPreview style={{ height: '100%' }} /> | ||
</SandpackLayout> | ||
</SandpackProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const ajvValidatorFile = `import Ajv, { JSONSchemaType } from 'ajv'; | ||
const ajv = new Ajv({ | ||
allErrors: true, | ||
useDefaults: true, | ||
keywords: ['uniforms'], | ||
}); | ||
export function createValidator<T>(schema: JSONSchemaType<T>) { | ||
const validator = ajv.compile(schema); | ||
return (model: Record<string, unknown>) => { | ||
validator(model); | ||
return validator.errors?.length ? { details: validator.errors } : null; | ||
}; | ||
}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
export enum Bridges { | ||
Zod = 'zod', | ||
JSONSchema = 'json-schema', | ||
SimpleSchema = 'simpl-schema', | ||
} | ||
|
||
const defaultDependencies = { | ||
antd: '^5', | ||
uniforms: '4.0.0-beta.2', | ||
'uniforms-antd': '4.0.0-beta.2', | ||
}; | ||
|
||
const zodDependencies = { | ||
'uniforms-bridge-zod': '4.0.0-beta.2', | ||
zod: '^3', | ||
}; | ||
|
||
const jsonSchemaDependencies = { | ||
ajv: '^8', | ||
'uniforms-bridge-json-schema': '4.0.0-beta.2', | ||
}; | ||
|
||
const simpleSchemaDependencies = { | ||
'simpl-schema': '^3', | ||
'uniforms-bridge-simple-schema-2': '4.0.0-beta.2', | ||
}; | ||
|
||
export const dependencies = { | ||
[Bridges.Zod]: { | ||
...defaultDependencies, | ||
...zodDependencies, | ||
}, | ||
[Bridges.JSONSchema]: { | ||
...defaultDependencies, | ||
...jsonSchemaDependencies, | ||
}, | ||
[Bridges.SimpleSchema]: { | ||
...defaultDependencies, | ||
...simpleSchemaDependencies, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"position": 3, | ||
"label": "Examples", | ||
"collapsible": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
title: 'Basic usage' | ||
sidebar_position: 1 | ||
--- | ||
|
||
import { ajvValidatorFile } from '@site/components/CodeEditor/ajvValidatorFile'; | ||
import { CodeEditor } from '@site/components/CodeEditor/CodeEditor'; | ||
|
||
### Zod | ||
|
||
export const zodFiles = { | ||
'/App.tsx': `import { AutoForm } from 'uniforms-antd'; | ||
import { ZodBridge } from 'uniforms-bridge-zod'; | ||
import { z } from 'zod'; | ||
const userSchema = z.object({ | ||
username: z.string(), | ||
}); | ||
const schema = new ZodBridge({ schema: userSchema }); | ||
export default function App() { | ||
return ( | ||
<AutoForm | ||
schema={schema} | ||
onSubmit={model => window.alert(JSON.stringify(model))} | ||
/> | ||
); | ||
}` | ||
}; | ||
|
||
<CodeEditor bridge="zod" files={zodFiles} /> | ||
|
||
### JSON Schema | ||
|
||
export const jsonSchemaFiles = { | ||
'/App.tsx': `import { AutoForm } from 'uniforms-antd'; | ||
import { schema } from './userSchema'; | ||
export default function App() { | ||
return ( | ||
<AutoForm | ||
schema={schema} | ||
onSubmit={model => window.alert(JSON.stringify(model))} | ||
/> | ||
); | ||
}`, | ||
'/userSchema.ts': `import { JSONSchemaBridge } from 'uniforms-bridge-json-schema'; | ||
import { JSONSchemaType } from 'ajv' | ||
import { createValidator } from './validator'; | ||
type FormData = { | ||
username: string; | ||
}; | ||
const userSchema: JSONSchemaType<FormData> = { | ||
type: 'object', | ||
properties: { | ||
username: { type: 'string' }, | ||
}, | ||
required: ['username'], | ||
}; | ||
export const schema = new JSONSchemaBridge({ | ||
schema: userSchema, | ||
validator: createValidator(userSchema), | ||
}); | ||
`, | ||
'/validator.ts': ajvValidatorFile | ||
}; | ||
|
||
<CodeEditor bridge="json-schema" files={jsonSchemaFiles} /> | ||
|
||
### SimpleSchema | ||
|
||
export const simpleSchemaFiles = { | ||
'/App.tsx': `import { AutoForm } from 'uniforms-antd'; | ||
import { SimpleSchema2Bridge } from 'uniforms-bridge-simple-schema-2'; | ||
import SimpleSchema from 'simpl-schema'; | ||
const userSchema = new SimpleSchema({ | ||
username: String | ||
}); | ||
const schema = new SimpleSchema2Bridge({ schema: userSchema }); | ||
export default function App() { | ||
return ( | ||
<AutoForm | ||
schema={schema} | ||
onSubmit={model => window.alert(JSON.stringify(model))} | ||
/> | ||
); | ||
}` | ||
} | ||
|
||
<CodeEditor bridge="simpl-schema" files={simpleSchemaFiles} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters