@twintag/twintag-form 1.2.0 → 1.3.0

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.
@@ -4746,6 +4746,7 @@ const kr = {
4746
4746
  },
4747
4747
  presets: [Bt(), yr()]
4748
4748
  }), Cr = Mt(Sr), zr = St(pt, {
4749
+ // @ts-expect-error: ts limitation
4749
4750
  props: {
4750
4751
  config: "json",
4751
4752
  options: "json"
@@ -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
- resetOnSubmit,
17
- children,
18
- }: Props) => {
19
- const ref = useRef<HTMLFormElement>(null);
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?.dispatchEvent(eventWithValidData);
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
- const onInvalid = (__errors: FieldValues) => {
50
- const eventWithInvalidData = new CustomEvent('twintag-form-invalid', {
51
- detail: __errors,
52
- bubbles: true,
53
- cancelable: false,
54
- composed: true,
55
- });
45
+ ref.current?.dispatchEvent(eventWithValidData);
46
+ };
56
47
 
57
- ref.current?.dispatchEvent(eventWithInvalidData);
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
- const onValidate = () => {
61
- void form.handleSubmit(onValid, onInvalid)();
62
- };
56
+ ref.current?.dispatchEvent(eventWithInvalidData);
57
+ };
63
58
 
64
- const onGetValues = () => {
65
- const values = form.getValues();
59
+ const onValidate = () => {
60
+ void form.handleSubmit(onValid, onInvalid)();
61
+ };
66
62
 
67
- const eventWithValues = new CustomEvent('twintag-form-values', {
68
- detail: values,
69
- bubbles: true,
70
- cancelable: false,
71
- composed: true,
72
- });
63
+ const onGetValues = () => {
64
+ const values = form.getValues();
73
65
 
74
- ref.current?.dispatchEvent(eventWithValues);
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
- useEffect(() => {
78
- const form = ref.current;
73
+ ref.current?.dispatchEvent(eventWithValues);
74
+ };
79
75
 
80
- if (form) {
81
- form.addEventListener('twintag-form-validate', onValidate);
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.removeEventListener('twintag-form-validate', onValidate);
88
- form.removeEventListener('twintag-form-get_values', onGetValues);
80
+ form.addEventListener('twintag-form-validate', onValidate);
81
+ form.addEventListener('twintag-form-get_values', onGetValues);
89
82
  }
90
- };
91
- }, []);
92
-
93
- return (
94
- <Form {...form}>
95
- <form
96
- ref={ref}
97
- className='relative space-y-4 font-tf-font'
98
- onSubmit={form.handleSubmit(onValid, onInvalid)}
99
- >
100
- {children}
101
- </form>
102
- </Form>
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,20 @@ type Props = {
15
17
  }>;
16
18
  };
17
19
 
18
- export const Form = ({ config, options }: Props) => {
19
- return (
20
- <TwintagFormProvider variant={options?.variant} components={components}>
21
- <TwintagForm
22
- defaultValues={options?.defaultValues}
23
- resetOnSubmit={options?.resetOnSubmit}
24
- >
25
- {config.map((item, idx) => (
26
- <TwintagJsonRenderer key={idx} config={item} />
27
- ))}
28
- </TwintagForm>
29
- </TwintagFormProvider>
30
- );
31
- };
20
+ export const Form = forwardRef<HTMLFormElement, Props>(
21
+ ({ config, options }, ref) => {
22
+ return (
23
+ <TwintagFormProvider variant={options?.variant} components={components}>
24
+ <TwintagForm
25
+ ref={ref}
26
+ defaultValues={options?.defaultValues}
27
+ resetOnSubmit={options?.resetOnSubmit}
28
+ >
29
+ {config.map((item, idx) => (
30
+ <TwintagJsonRenderer key={idx} config={item} />
31
+ ))}
32
+ </TwintagForm>
33
+ </TwintagFormProvider>
34
+ );
35
+ },
36
+ );
@@ -8,6 +8,7 @@ import config from '../twind.config';
8
8
  const withTwind = install(config);
9
9
 
10
10
  const TwintagFormElement = r2wc(Form, {
11
+ // @ts-expect-error: ts limitation
11
12
  props: {
12
13
  config: 'json',
13
14
  options: 'json',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twintag/twintag-form",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "author": "Twintag",
5
5
  "type": "module",
6
6
  "exports": {