@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.
@@ -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
- // import {formSchema} from './form-schema'
2
- // import {formiumInput} from './formium'
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,
@@ -1,4 +1,4 @@
1
- import {defineEventHandler, H3Event} from 'h3' // For Nuxt.js/Nitro
1
+ import {defineEventHandler, type H3Event} from 'h3' // For Nuxt.js/Nitro
2
2
  import type {IncomingMessage, ServerResponse} from 'http'
3
3
 
4
4
  // Utility function to detect the framework at runtime