Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,15 +170,45 @@ import {formSchema} from '@sanity/form-toolkit/form-schema'

export default defineConfig({
//...
plugins: [formSchema()],
plugins: [
formSchema({
// Optionally, use your own schemas for additional formFields
fields: [
defineField({
name: 'myField',
type: 'myObjectType',
}),
],
}),
],
})
```

This will create a "form" document type.
Then, add a field to your schema with type `form`

```ts
// ./src/schemaTypes/page.ts
import {defineField, defineType} from 'sanity'

export default defineType({
name: 'page',
type: 'document',
fields: [
defineField({
name: 'form',
type: 'reference',
to: [{type: 'form'}],
}),
],
})
```

Then pass a `form` document to the `FormRenderer` component
Finally, pass a `form` document to the `FormRenderer` component

```tsx
import React, {type FC} from 'react'
import {FormRenderer, type FormDataProps} from '@sanity/form-toolkit/form-schema'
import {FormRenderer, type FormDataProps} from '@sanity/form-toolkit/form-renderer'

interface NativeFormExampleProps {
formData: FormDataProps
Expand Down
7,544 changes: 5,130 additions & 2,414 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
"import": "./dist/form-schema/index.mjs",
"default": "./dist/form-schema/index.js"
},
"./form-renderer": {
"source": "./src/form-renderer/index.ts",
"import": "./dist/form-renderer/index.mjs",
"default": "./dist/form-renderer/index.js"
},
"./package.json": "./package.json"
},
"main": "./dist/index.js",
Expand All @@ -55,6 +60,9 @@
],
"form-schema": [
"./dist/form-schema/index.d.ts"
],
"form-renderer": [
"./dist/form-renderer/index.d.ts"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import type {FieldComponentProps} from './types'

export const DefaultField: FC<FieldComponentProps> = ({field, fieldState, error}) => {
const {type, label, name, options = {}, choices = []} = field
const {type, label, name, options = {}, choices = [], validation = []} = field
if (!type || !name) return null

const validationRules = validation.reduce((acc: Record<string, string>, v) => {
acc[v.type] = v.value
return acc
}, {})
const {value, onChange, onBlur, ref} = fieldState

const handleChange = (
Expand Down Expand Up @@ -34,10 +37,11 @@
<textarea
ref={ref as LegacyRef<HTMLTextAreaElement>}
name={name}
value={value ?? ''}
onChange={handleChange}

Check warning on line 40 in src/form-renderer/components/default-field.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

JSX props should not use arrow functions
onBlur={onBlur}
placeholder={options.placeholder}
{...validationRules}
value={value ?? ''}
/>
)

Expand All @@ -47,11 +51,12 @@
ref={ref as LegacyRef<HTMLSelectElement>}
name={name}
value={value ?? ''}
onChange={handleChange}

Check warning on line 54 in src/form-renderer/components/default-field.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

JSX props should not use arrow functions
{...validationRules}
onBlur={onBlur}
>
{choices?.map((choice, i) => (
<option key={i} value={choice.value}>

Check warning on line 59 in src/form-renderer/components/default-field.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Do not use Array index in keys
{choice.label}
</option>
))}
Expand All @@ -60,15 +65,16 @@

case 'radio':
return choices?.map((choice, i) => (
<label key={i}>

Check warning on line 68 in src/form-renderer/components/default-field.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Do not use Array index in keys
<input
type="radio"
name={name}
ref={ref as LegacyRef<HTMLInputElement>}
value={choice.value}
checked={value === choice.value}
onChange={handleChange}

Check warning on line 75 in src/form-renderer/components/default-field.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

JSX props should not use arrow functions
onBlur={onBlur}
{...validationRules}
/>
{choice.label}
</label>
Expand All @@ -76,15 +82,16 @@

case 'checkbox':
return choices?.map((choice, i) => (
<label key={i}>

Check warning on line 85 in src/form-renderer/components/default-field.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Do not use Array index in keys
<input
type="checkbox"
name={name}
ref={ref as LegacyRef<HTMLInputElement>}
value={choice.value}
checked={Array.isArray(value) ? value.includes(choice.value) : value === choice.value}
onChange={(e) => handleCheckboxChange(e, choice.value)}

Check warning on line 92 in src/form-renderer/components/default-field.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

JSX props should not use arrow functions
onBlur={onBlur}
{...validationRules}
/>
{choice.label}
</label>
Expand All @@ -97,7 +104,8 @@
ref={ref as LegacyRef<HTMLInputElement>}
name={name}
value={value ?? options.defaultValue ?? ''}
onChange={handleChange}

Check warning on line 107 in src/form-renderer/components/default-field.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

JSX props should not use arrow functions
{...validationRules}
onBlur={onBlur}
placeholder={options.placeholder}
/>
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// types.ts
export type ValidationRule = {
type: string
value: string
Expand Down Expand Up @@ -32,7 +31,6 @@ export type FormDataProps = {
current: string
}
fields?: FormField[]

submitButton?: {
text: string
position: 'left' | 'center' | 'right'
Expand Down
4 changes: 4 additions & 0 deletions src/form-renderer/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {FormRenderer} from './components/form-renderer'
import type {FormDataProps} from './components/types'
export type {FormDataProps}
export {FormRenderer}
14 changes: 14 additions & 0 deletions src/form-schema/components/validation-type.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import type {StringInputProps} from 'sanity'
import {useFormValue} from 'sanity'

import type {FormField} from '../../form-renderer/components/types'
import {validationTypesByFieldType} from '../schema-types/form-field'

export const ValidationType = (props: StringInputProps) => {

Check warning on line 7 in src/form-schema/components/validation-type.tsx

View workflow job for this annotation

GitHub Actions / Lint & Build

Missing return type on function
const {type} = useFormValue([...props.path.slice(0, 2)]) as FormField
if (!type) return props.renderDefault(props)
if (props.schemaType?.options) {
props.schemaType.options.list = validationTypesByFieldType[type]
}
return props.renderDefault(props)
}
18 changes: 12 additions & 6 deletions src/form-schema/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {definePlugin} from 'sanity'
import {definePlugin, type FieldDefinition} from 'sanity'

// import {structureTool} from 'sanity/structure'
import {FormRenderer} from './components/form-renderer'
// import {FormRenderer} from './components/form-renderer'
import {schema} from './schema-types'
// import {defaultDocumentNode} from './structure'

Expand All @@ -18,12 +18,18 @@ import {schema} from './schema-types'
* })
* ```
*/
export type {FormDataProps} from './components/types'
export {FormRenderer}
export const formSchema = definePlugin(() => {
export type FieldsOption = Array<FieldDefinition>
interface FormSchemaPluginOptions {
/**
* Array of field definitions to be used in the form schema.
*/
fields?: FieldsOption
}

export const formSchema = definePlugin(({fields = []}: FormSchemaPluginOptions) => {
return {
name: 'form-toolkit_form-schema',
schema,
schema: schema(fields),
// plugins: [structureTool({defaultDocumentNode})],
}
})
118 changes: 66 additions & 52 deletions src/form-schema/schema-types/form-field.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
import {LuTextCursorInput} from 'react-icons/lu'
import {defineField, defineType} from 'sanity'

import {ValidationType} from '../components/validation-type'
interface ValidationContextDocument {
fields?: Array<{
name: string
type?: string
}>
}

// Validation options by field type
export const validationTypesByFieldType = {
checkbox: ['minSelectedCount', 'maxSelectedCount', 'custom'],
color: ['custom'],
date: ['minDate', 'maxDate', 'custom'],
'datetime-local': ['minDate', 'maxDate', 'custom'],
email: ['pattern', 'custom'],
file: ['maxSize', 'fileType', 'custom'],
hidden: ['custom'],
number: ['min', 'max', 'custom'],
// password: ['minLength', 'pattern', 'custom'],
radio: ['custom'],
range: ['min', 'max', 'step', 'custom'],
select: ['custom'],
tel: ['pattern', 'custom'],
text: ['minLength', 'maxLength', 'pattern', 'custom'],
textarea: ['minLength', 'maxLength', 'custom'],
time: ['custom'],
url: ['pattern', 'custom'],
export const validationTypesByFieldType: Record<string, string[]> = {
checkbox: ['minSelectedCount', 'maxSelectedCount'],
color: [],
date: ['minDate', 'maxDate'],
'datetime-local': ['minDate', 'maxDate'],
email: ['pattern'],
file: ['maxSize', 'fileType'],
hidden: [],
number: ['min', 'max'],
// password: ['minLength', 'pattern'],
radio: [],
range: ['min', 'max', 'step'],
select: [],
tel: ['pattern'],
text: ['minLength', 'maxLength', 'pattern'],
textarea: ['minLength', 'maxLength'],
time: [],
url: ['pattern'],
}
export const formFieldType = defineType({
name: 'formField',
Expand Down Expand Up @@ -114,40 +116,52 @@ export const formFieldType = defineType({
type: 'boolean',
initialValue: false,
}),
// defineField({
// name: 'validation',
// title: 'Validation Rules',
// type: 'array',
// of: [
// {
// type: 'object',
// fields: [
// defineField({
// name: 'type',
// title: 'Validation Type',
// type: 'string',

// hidden: ({parent}) => !parent?.type,
// options: {
// // TODO: I think this needs to be a custom input component?
// // list: ({parent}) => (parent?.type ? validationTypesByFieldType[parent.type] : []),
// list: [],
// },
// }),
// defineField({
// name: 'value',
// title: 'Value',
// type: 'string',
// }),
// defineField({
// name: 'message',
// title: 'Error Message',
// type: 'string',
// }),
// ],
// },
// ],
// }),
defineField({
name: 'validation',
title: 'Validation Rules',
type: 'array',
hidden: ({parent}) => {
if (!parent?.type) return true
const validationTypes = validationTypesByFieldType[parent.type]
return !validationTypes || validationTypes.length === 0
},
of: [
{
type: 'object',
fields: [
defineField({
name: 'type',
title: 'Validation Type',
type: 'string',
options: {
// TODO: I think this needs to be a custom input component?
// list: ({parent}) => (parent?.type ? validationTypesByFieldType[parent.type] : []),
list: [],
},
components: {
input: ValidationType,
},
}),
defineField({
name: 'value',
title: 'Value',
type: 'string',
}),
defineField({
name: 'message',
title: 'Error Message',
type: 'string',
}),
],
preview: {
select: {
title: 'type',
subtitle: 'value',
},
},
},
],
}),
defineField({
name: 'choices',
title: 'Choices',
Expand Down
Loading