Skip to content

Commit b00f9fe

Browse files
authored
Update quick-start documentation for React forms (#26)
- Clarified that no external form library is required for the first form using FormFactory. - Updated component specifications to include optional props for `components`, `customComponents`, and `renderers`. - Enhanced the examples section with a minimal example demonstrating default usage of FormFactory. - Added new components like `FormSectionGroup` and `FormSectionGroupContainer` to improve form structure. - Refactored `FormContainer` to utilize `useScheptaFormAdapter` for form submission handling. - Updated the documentation in English, Spanish, and Portuguese to reflect these changes.
1 parent d102265 commit b00f9fe

3 files changed

Lines changed: 252 additions & 93 deletions

File tree

docs/en-US/guide/quick-start.md

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@ pnpm add @schepta/core
2121

2222
# Install React adapter and factory (for React projects)
2323
pnpm add @schepta/adapter-react @schepta/factory-react
24-
25-
# Install React Hook Form (required for forms)
26-
pnpm add react-hook-form
2724
```
2825

26+
FormFactory uses native React state by default, so **no form library is required** for your first form. To integrate with React Hook Form or Formik later, see the [React Showcase](/en-US/showcases/react) for examples.
27+
2928
## Setting Up the Provider
3029

3130
The `ScheptaProvider` is the central configuration point for your application. It manages global component registrations, middleware, and context that all factories can access.
@@ -56,7 +55,9 @@ function App() {
5655

5756
| Prop | Type | Description |
5857
|------|------|-------------|
59-
| `components` | `Record<string, ComponentSpec>` | Global component registry |
58+
| `components` | `Record<string, ComponentSpec>` | Global component registry (optional) |
59+
| `customComponents` | `Record<string, ComponentSpec>` | Custom components keyed by schema key (optional) |
60+
| `renderers` | `Partial<Record<ComponentType, RendererFn>>` | Custom renderers per component type (optional) |
6061
| `middlewares` | `Middleware[]` | Global middleware stack |
6162
| `externalContext` | `object` | Shared context (user, API, etc.) |
6263
| `debug` | `object` | Debug configuration |
@@ -70,9 +71,9 @@ Components must be registered using `createComponentSpec` before they can be use
7071
schepta supports several component types:
7172

7273
- **`field`** - Input fields (text, select, checkbox, etc.)
73-
- **`container`** - Container components (FormField, FormSection, etc.)
74-
- **`content`** - Content components (titles, buttons, etc.)
75-
- **`FormContainer`** - Root form container
74+
- **`container`** - Container components (FormField, FormSection, FormContainer, etc.)
75+
- **`content`** - Content components (titles, etc.)
76+
- **`button`** - Button components (e.g. SubmitButton)
7677

7778
### Creating Component Specs
7879

@@ -102,7 +103,7 @@ const components = {
102103
InputText: createComponentSpec({
103104
id: 'InputText',
104105
type: 'field',
105-
factory: (props, runtime) => InputText,
106+
component: (props, runtime) => InputText,
106107
}),
107108
};
108109
```
@@ -112,7 +113,7 @@ const components = {
112113
```tsx
113114
import { createComponentSpec } from '@schepta/core';
114115
import React from 'react';
115-
import { useFormContext } from 'react-hook-form';
116+
import { useScheptaFormAdapter } from '@schepta/factory-react';
116117

117118
// Define your components
118119
const InputText = React.forwardRef<HTMLInputElement, any>((props, ref) => {
@@ -144,67 +145,118 @@ const FormSectionTitle = ({ 'x-content': content, ...props }: any) => {
144145
return <h2 {...props}>{content}</h2>;
145146
};
146147

148+
const FormSectionGroup = ({ children, ...props }: any) => {
149+
return <div style={{ display: 'grid', gap: '16px' }} {...props}>{children}</div>;
150+
};
151+
152+
const FormSectionGroupContainer = ({ children, ...props }: any) => {
153+
return <div {...props}>{children}</div>;
154+
};
155+
147156
const SubmitButton = ({ 'x-content': content, ...props }: any) => {
148-
const { handleSubmit } = useFormContext();
149157
return (
150-
<button
151-
type="button"
152-
onClick={() => handleSubmit(props.onSubmit)()}
153-
{...props}
154-
>
158+
<button type="submit" {...props}>
155159
{content || 'Submit'}
156160
</button>
157161
);
158162
};
159163

160-
const FormContainer = ({ children, ...props }: any) => {
161-
return <form {...props}>{children}</form>;
164+
const FormContainer = ({ children, onSubmit, ...props }: any) => {
165+
const adapter = useScheptaFormAdapter();
166+
const handleFormSubmit = (e: React.FormEvent) => {
167+
e.preventDefault();
168+
if (onSubmit) adapter.handleSubmit(onSubmit)();
169+
};
170+
return (
171+
<form onSubmit={handleFormSubmit} {...props}>
172+
{children}
173+
</form>
174+
);
162175
};
163176

164177
// Register all components using createComponentSpec
165178
export const globalComponents = {
166-
'FormContainer': createComponentSpec({
179+
FormContainer: createComponentSpec({
167180
id: 'FormContainer',
168-
type: 'FormContainer',
169-
factory: (props, runtime) => FormContainer,
181+
type: 'container',
182+
component: (props, runtime) => FormContainer,
170183
}),
171184
InputText: createComponentSpec({
172185
id: 'InputText',
173186
type: 'field',
174-
factory: (props, runtime) => InputText,
187+
component: (props, runtime) => InputText,
175188
}),
176189
FormField: createComponentSpec({
177190
id: 'FormField',
178191
type: 'container',
179-
factory: (props, runtime) => FormField,
192+
component: (props, runtime) => FormField,
180193
}),
181194
FormSectionContainer: createComponentSpec({
182195
id: 'FormSectionContainer',
183196
type: 'container',
184-
factory: (props, runtime) => FormSectionContainer,
197+
component: (props, runtime) => FormSectionContainer,
185198
}),
186199
FormSectionTitle: createComponentSpec({
187200
id: 'FormSectionTitle',
188201
type: 'content',
189-
factory: (props, runtime) => FormSectionTitle,
202+
component: (props, runtime) => FormSectionTitle,
203+
}),
204+
FormSectionGroup: createComponentSpec({
205+
id: 'FormSectionGroup',
206+
type: 'container',
207+
component: (props, runtime) => FormSectionGroup,
208+
}),
209+
FormSectionGroupContainer: createComponentSpec({
210+
id: 'FormSectionGroupContainer',
211+
type: 'container',
212+
component: (props, runtime) => FormSectionGroupContainer,
190213
}),
191214
SubmitButton: createComponentSpec({
192215
id: 'SubmitButton',
193-
type: 'content',
194-
factory: (props, runtime) => SubmitButton,
216+
type: 'button',
217+
component: (props, runtime) => SubmitButton,
195218
}),
196219
};
197220
```
198221

199222
## Your First Form
200223

201-
Now let's create a complete example combining everything:
224+
Now let's create a complete example combining everything.
225+
226+
### Minimal Example (Using Defaults)
227+
228+
FormFactory ships with built-in components, so you can render a form with just a schema and `onSubmit`—no custom registration required:
229+
230+
```tsx
231+
import React from 'react';
232+
import { ScheptaProvider } from '@schepta/adapter-react';
233+
import { FormFactory } from '@schepta/factory-react';
234+
import formSchema from './form-schema.json';
235+
236+
function App() {
237+
const handleSubmit = (values: any) => {
238+
console.log('Form submitted:', values);
239+
};
240+
241+
return (
242+
<ScheptaProvider>
243+
<div style={{ padding: '24px', maxWidth: '800px', margin: '0 auto' }}>
244+
<h1>My First schepta Form</h1>
245+
<FormFactory schema={formSchema} onSubmit={handleSubmit} />
246+
</div>
247+
</ScheptaProvider>
248+
);
249+
}
250+
251+
export default App;
252+
```
202253

203254
### 1. Define Your Schema
204255

256+
You can use `x-component-props` or `x-ui` for field labels and placeholders. The `$schema` property is optional (useful for IDE validation when working inside the monorepo).
257+
205258
```json
206259
{
207-
"$schema": "../../packages/factories/src/schemas/form-schema.json",
208260
"$id": "my-first-form",
209261
"type": "object",
210262
"x-component": "FormContainer",
@@ -233,7 +285,7 @@ Now let's create a complete example combining everything:
233285
"firstName": {
234286
"type": "string",
235287
"x-component": "InputText",
236-
"x-ui": {
288+
"x-component-props": {
237289
"label": "First Name",
238290
"placeholder": "Enter your first name"
239291
},
@@ -258,7 +310,7 @@ Now let's create a complete example combining everything:
258310
}
259311
```
260312

261-
### 2. Complete React Example
313+
### 2. Complete React Example (With Custom Components)
262314

263315
```tsx
264316
import React from 'react';
@@ -313,4 +365,5 @@ Learn the fundamental concepts that power schepta:
313365

314366
## Resources
315367

368+
- [React Showcase](/en-US/showcases/react) - Examples with React Hook Form and Formik
316369
- [GitHub Repository](https://github.com/guynikan/schepta)

0 commit comments

Comments
 (0)