@sanity/form-toolkit 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -1
- package/dist/index.d.mts +66 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +332 -2
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +335 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -3
- package/src/form-schema/components/default-field.tsx +113 -0
- package/src/form-schema/components/form-renderer.tsx +58 -0
- package/src/form-schema/components/types.ts +53 -0
- package/src/form-schema/index.ts +9 -8
- package/src/form-schema/schema-types/form-field.ts +173 -109
- package/src/form-schema/schema-types/form.ts +43 -1
- package/src/form-schema/schema-types/index.ts +1 -1
- package/src/form-schema/structure/document-view.tsx +78 -0
- package/src/form-schema/structure/index.ts +11 -0
- package/src/index.ts +5 -2
- package/src/shared/create-handler.ts +1 -1
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import {type FC, type FormEvent, useState} from 'react'
|
|
2
|
+
|
|
3
|
+
import {FormRenderer} from '../components/form-renderer'
|
|
4
|
+
import type {FieldState, FormDataProps} from '../components/types'
|
|
5
|
+
|
|
6
|
+
interface UseStateExampleProps {
|
|
7
|
+
formData: FormDataProps
|
|
8
|
+
onSubmit?: (data: Record<string, unknown>) => void
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const UseStateExample: FC<UseStateExampleProps> = ({formData, onSubmit = () => null}) => {
|
|
12
|
+
const [values, setValues] = useState<Record<string, FieldState['value']>>({})
|
|
13
|
+
const [errors, setErrors] = useState<Record<string, string | undefined>>({})
|
|
14
|
+
|
|
15
|
+
const getFieldState = (fieldName: string) => ({
|
|
16
|
+
value: values[fieldName],
|
|
17
|
+
onChange: (value: unknown) => {
|
|
18
|
+
// @ts-expect-error todo: fix this
|
|
19
|
+
setValues((prev) => ({
|
|
20
|
+
...prev,
|
|
21
|
+
[fieldName]: value,
|
|
22
|
+
}))
|
|
23
|
+
// Clear error when value changes
|
|
24
|
+
if (errors[fieldName]) {
|
|
25
|
+
setErrors((prev) => ({
|
|
26
|
+
...prev,
|
|
27
|
+
[fieldName]: undefined,
|
|
28
|
+
}))
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
onBlur: () => {
|
|
32
|
+
// Example validation on blur
|
|
33
|
+
const field = formData.fields
|
|
34
|
+
?.flatMap((formField) => formField)
|
|
35
|
+
.find((formField) => formField?.name === fieldName)
|
|
36
|
+
|
|
37
|
+
if (field?.required && !values[fieldName]) {
|
|
38
|
+
setErrors((prev) => ({
|
|
39
|
+
...prev,
|
|
40
|
+
[fieldName]: 'This field is required',
|
|
41
|
+
}))
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const getFieldError = (fieldName: string) => errors[fieldName]
|
|
47
|
+
|
|
48
|
+
const handleSubmit = (e: FormEvent) => {
|
|
49
|
+
e.preventDefault()
|
|
50
|
+
onSubmit(values)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return (
|
|
54
|
+
<>
|
|
55
|
+
<style>
|
|
56
|
+
{`
|
|
57
|
+
.form-field > label {
|
|
58
|
+
display: block;
|
|
59
|
+
}
|
|
60
|
+
.form-field {
|
|
61
|
+
margin-bottom: 1rem;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
`}
|
|
65
|
+
</style>
|
|
66
|
+
<FormRenderer
|
|
67
|
+
formData={formData}
|
|
68
|
+
onSubmit={handleSubmit}
|
|
69
|
+
getFieldState={getFieldState}
|
|
70
|
+
getFieldError={getFieldError}
|
|
71
|
+
/>
|
|
72
|
+
</>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
export const DocumentView = (props: {document: {displayed: FormDataProps}}) => {
|
|
76
|
+
// console.log('props', props)
|
|
77
|
+
return <UseStateExample formData={props.document.displayed} />
|
|
78
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type {DefaultDocumentNodeResolver} from 'sanity/structure'
|
|
2
|
+
|
|
3
|
+
import {DocumentView} from './document-view'
|
|
4
|
+
|
|
5
|
+
export const defaultDocumentNode: DefaultDocumentNodeResolver = (S, {schemaType}) => {
|
|
6
|
+
// Conditionally return a different configuration based on the schema type
|
|
7
|
+
if (schemaType === 'form') {
|
|
8
|
+
return S.document().views([S.view.form(), S.view.component(DocumentView).title('Web')])
|
|
9
|
+
}
|
|
10
|
+
return S.document().views([S.view.form()])
|
|
11
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import {FormRenderer, formSchema} from './form-schema'
|
|
2
|
+
import type {FormDataProps} from './form-schema/components/types'
|
|
3
3
|
import {hubSpotInput} from './hubspot'
|
|
4
4
|
import {hubSpotHandler} from './hubspot/create-handler'
|
|
5
5
|
import {fetchHubSpotData} from './hubspot/fetch-hubspot-data'
|
|
@@ -9,6 +9,9 @@ import {fetchMailchimpData, mailchimpHandler} from './mailchimp/create-handler'
|
|
|
9
9
|
export {
|
|
10
10
|
fetchHubSpotData,
|
|
11
11
|
fetchMailchimpData,
|
|
12
|
+
type FormDataProps,
|
|
13
|
+
FormRenderer,
|
|
14
|
+
formSchema,
|
|
12
15
|
hubSpotHandler,
|
|
13
16
|
hubSpotInput,
|
|
14
17
|
mailchimpHandler,
|