Skip to content

Commit

Permalink
added first example to docs - different bridges with antd theme
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrpospiech committed Nov 29, 2024
1 parent f751fcb commit 627fd99
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 72 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"[mdx]": {
"editor.defaultFormatter": "unifiedjs.vscode-mdx"
}
}
70 changes: 0 additions & 70 deletions website/components/CodeEditor.tsx

This file was deleted.

40 changes: 40 additions & 0 deletions website/components/CodeEditor/CodeEditor.tsx
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>
);
}
16 changes: 16 additions & 0 deletions website/components/CodeEditor/ajvValidatorFile.ts
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;
};
}`;
41 changes: 41 additions & 0 deletions website/components/CodeEditor/dependencies.ts
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,
},
};
5 changes: 5 additions & 0 deletions website/docs/examples/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"position": 3,
"label": "Examples",
"collapsible": false
}
97 changes: 97 additions & 0 deletions website/docs/examples/basic-usage.mdx
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} />
25 changes: 23 additions & 2 deletions website/docs/getting-started/basic-usage.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: 'Basic usage'
sidebar_position: 2
---

import { CodeEditor } from '../../components/CodeEditor';
import { CodeEditor } from '../../components/CodeEditor/CodeEditor';

:::info
This example uses `uniforms-bridge-zod` and `uniforms-antd` packages as this bridge and theme don't require any extra configuration.
Expand Down Expand Up @@ -56,4 +56,25 @@ That's it! Now we can render our form and see it in action.

We have created a simple form with a schema. It uses AntD theme and Zod as our schema validator. Check [advanced usage](/docs/gettings-started/advanced-usage) for more complex examples or play around with the example below.

<CodeEditor />
export const files = {
'/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={files} />

0 comments on commit 627fd99

Please sign in to comment.