@twintag/twintag-form 1.2.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/.turbo/turbo-build.log +13 -13
- package/CHANGELOG.md +6 -0
- package/dev/index.css +32 -32
- package/dist/components/form.d.ts +8 -8
- package/dist/index.d.ts +2 -3
- package/dist/twintag-form.react.js +2293 -2293
- package/dist/twintag-form.webcomponent.js +1 -0
- package/lib/components/form.tsx +79 -79
- package/lib/index.tsx +18 -14
- package/lib/webcomponent.ts +1 -0
- package/package.json +1 -1
package/lib/components/form.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { FieldValues } from 'react-hook-form';
|
|
2
2
|
import type { WithChildren } from 'twintag-types/dom';
|
|
3
3
|
|
|
4
|
-
import { useEffect, useRef } from 'react';
|
|
4
|
+
import { forwardRef, useEffect, useImperativeHandle, useRef } from 'react';
|
|
5
5
|
import { useForm } from 'react-hook-form';
|
|
6
6
|
|
|
7
7
|
import { Form } from '@twintag/twintag-form/common/form';
|
|
@@ -11,94 +11,94 @@ type Props = {
|
|
|
11
11
|
resetOnSubmit?: boolean;
|
|
12
12
|
} & WithChildren;
|
|
13
13
|
|
|
14
|
-
export const TwintagForm = (
|
|
15
|
-
defaultValues,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
const form = useForm({
|
|
22
|
-
defaultValues,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
useEffect(() => {
|
|
26
|
-
if (form.formState.isSubmitSuccessful && resetOnSubmit) {
|
|
27
|
-
if (defaultValues) {
|
|
28
|
-
form.reset(
|
|
29
|
-
Object.keys(defaultValues).reduce(
|
|
30
|
-
(acc, key) => ({ ...acc, [key]: '' }),
|
|
31
|
-
{},
|
|
32
|
-
),
|
|
33
|
-
);
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}, [form.formState]);
|
|
37
|
-
|
|
38
|
-
const onValid = (data: FieldValues) => {
|
|
39
|
-
const eventWithValidData = new CustomEvent('twintag-form-valid', {
|
|
40
|
-
detail: data,
|
|
41
|
-
bubbles: true,
|
|
42
|
-
cancelable: false,
|
|
43
|
-
composed: true,
|
|
14
|
+
export const TwintagForm = forwardRef<HTMLFormElement, Props>(
|
|
15
|
+
({ defaultValues, resetOnSubmit, children }, fowardedRef) => {
|
|
16
|
+
const ref = useRef<HTMLFormElement>(null);
|
|
17
|
+
|
|
18
|
+
const form = useForm({
|
|
19
|
+
defaultValues,
|
|
44
20
|
});
|
|
45
21
|
|
|
46
|
-
ref.current
|
|
47
|
-
|
|
22
|
+
useImperativeHandle(fowardedRef, () => ref.current!);
|
|
23
|
+
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
if (form.formState.isSubmitSuccessful && resetOnSubmit) {
|
|
26
|
+
if (defaultValues) {
|
|
27
|
+
form.reset(
|
|
28
|
+
Object.keys(defaultValues).reduce(
|
|
29
|
+
(acc, key) => ({ ...acc, [key]: '' }),
|
|
30
|
+
{},
|
|
31
|
+
),
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}, [form.formState]);
|
|
36
|
+
|
|
37
|
+
const onValid = (data: FieldValues) => {
|
|
38
|
+
const eventWithValidData = new CustomEvent('twintag-form-valid', {
|
|
39
|
+
detail: data,
|
|
40
|
+
bubbles: true,
|
|
41
|
+
cancelable: false,
|
|
42
|
+
composed: true,
|
|
43
|
+
});
|
|
48
44
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
detail: __errors,
|
|
52
|
-
bubbles: true,
|
|
53
|
-
cancelable: false,
|
|
54
|
-
composed: true,
|
|
55
|
-
});
|
|
45
|
+
ref.current?.dispatchEvent(eventWithValidData);
|
|
46
|
+
};
|
|
56
47
|
|
|
57
|
-
|
|
58
|
-
|
|
48
|
+
const onInvalid = (__errors: FieldValues) => {
|
|
49
|
+
const eventWithInvalidData = new CustomEvent('twintag-form-invalid', {
|
|
50
|
+
detail: __errors,
|
|
51
|
+
bubbles: true,
|
|
52
|
+
cancelable: false,
|
|
53
|
+
composed: true,
|
|
54
|
+
});
|
|
59
55
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
};
|
|
56
|
+
ref.current?.dispatchEvent(eventWithInvalidData);
|
|
57
|
+
};
|
|
63
58
|
|
|
64
|
-
|
|
65
|
-
|
|
59
|
+
const onValidate = () => {
|
|
60
|
+
void form.handleSubmit(onValid, onInvalid)();
|
|
61
|
+
};
|
|
66
62
|
|
|
67
|
-
const
|
|
68
|
-
|
|
69
|
-
bubbles: true,
|
|
70
|
-
cancelable: false,
|
|
71
|
-
composed: true,
|
|
72
|
-
});
|
|
63
|
+
const onGetValues = () => {
|
|
64
|
+
const values = form.getValues();
|
|
73
65
|
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
const eventWithValues = new CustomEvent('twintag-form-values', {
|
|
67
|
+
detail: values,
|
|
68
|
+
bubbles: true,
|
|
69
|
+
cancelable: false,
|
|
70
|
+
composed: true,
|
|
71
|
+
});
|
|
76
72
|
|
|
77
|
-
|
|
78
|
-
|
|
73
|
+
ref.current?.dispatchEvent(eventWithValues);
|
|
74
|
+
};
|
|
79
75
|
|
|
80
|
-
|
|
81
|
-
form.
|
|
82
|
-
form.addEventListener('twintag-form-get_values', onGetValues);
|
|
83
|
-
}
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
const form = ref.current;
|
|
84
78
|
|
|
85
|
-
return () => {
|
|
86
79
|
if (form) {
|
|
87
|
-
form.
|
|
88
|
-
form.
|
|
80
|
+
form.addEventListener('twintag-form-validate', onValidate);
|
|
81
|
+
form.addEventListener('twintag-form-get_values', onGetValues);
|
|
89
82
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
83
|
+
|
|
84
|
+
return () => {
|
|
85
|
+
if (form) {
|
|
86
|
+
form.removeEventListener('twintag-form-validate', onValidate);
|
|
87
|
+
form.removeEventListener('twintag-form-get_values', onGetValues);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
}, []);
|
|
91
|
+
|
|
92
|
+
return (
|
|
93
|
+
<Form {...form}>
|
|
94
|
+
<form
|
|
95
|
+
ref={ref}
|
|
96
|
+
className='relative space-y-4 font-tf-font'
|
|
97
|
+
onSubmit={form.handleSubmit(onValid, onInvalid)}
|
|
98
|
+
>
|
|
99
|
+
{children}
|
|
100
|
+
</form>
|
|
101
|
+
</Form>
|
|
102
|
+
);
|
|
103
|
+
},
|
|
104
|
+
);
|
package/lib/index.tsx
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { ComponentProps } from 'react';
|
|
2
2
|
import type { RawJsonComponent } from '@twintag/twintag-form/types';
|
|
3
3
|
|
|
4
|
+
import { forwardRef } from 'react';
|
|
5
|
+
|
|
4
6
|
import { TwintagJsonRenderer } from '@twintag/twintag-form/components';
|
|
5
7
|
import { components } from '@twintag/twintag-form/components/config';
|
|
6
8
|
import { TwintagForm } from '@twintag/twintag-form/components/form';
|
|
@@ -15,17 +17,19 @@ type Props = {
|
|
|
15
17
|
}>;
|
|
16
18
|
};
|
|
17
19
|
|
|
18
|
-
export const Form =
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
<
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
20
|
+
export const Form = forwardRef<HTMLFormElement, Props>(
|
|
21
|
+
({ config, options }) => {
|
|
22
|
+
return (
|
|
23
|
+
<TwintagFormProvider variant={options?.variant} components={components}>
|
|
24
|
+
<TwintagForm
|
|
25
|
+
defaultValues={options?.defaultValues}
|
|
26
|
+
resetOnSubmit={options?.resetOnSubmit}
|
|
27
|
+
>
|
|
28
|
+
{config.map((item, idx) => (
|
|
29
|
+
<TwintagJsonRenderer key={idx} config={item} />
|
|
30
|
+
))}
|
|
31
|
+
</TwintagForm>
|
|
32
|
+
</TwintagFormProvider>
|
|
33
|
+
);
|
|
34
|
+
},
|
|
35
|
+
);
|
package/lib/webcomponent.ts
CHANGED