@twintag/twintag-form 1.1.1 → 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.
@@ -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"
package/index.html CHANGED
@@ -1,15 +1,26 @@
1
- <!doctype html>
2
- <html lang="en">
3
- <head>
4
- <meta charset="UTF-8" />
5
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
- <title>Twintag Form</title>
7
- </head>
8
- <body>
9
- <div style="margin-inline: auto; max-width: 32rem; background: hsl(var(--tf-background)); padding: 2rem">
10
- <twintag-form></twintag-form>
11
- </div>
12
- <script type="module" src="./dev/main.tsx"></script>
13
- </body>
14
- </html>
15
-
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Twintag Form</title>
7
+ </head>
8
+ <body>
9
+ <div
10
+ style="
11
+ display: grid;
12
+ grid-template-columns: repeat(2, 1fr);
13
+ padding: 2rem;
14
+ "
15
+ >
16
+ <div>
17
+ <button id="get-values">Get Values</button>
18
+ <button id="submit">Submit</button>
19
+ </div>
20
+ <div style="max-width: 32rem; background: hsl(var(--tf-background))">
21
+ <twintag-form></twintag-form>
22
+ </div>
23
+ </div>
24
+ <script type="module" src="./dev/main.tsx"></script>
25
+ </body>
26
+ </html>
@@ -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,81 +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
- console.log('form', data);
40
- const customSubmit = new CustomEvent('twintag-form-valid', {
41
- detail: data,
42
- bubbles: true,
43
- cancelable: false,
44
- 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,
45
20
  });
46
21
 
47
- ref.current?.dispatchEvent(customSubmit);
48
- };
22
+ useImperativeHandle(fowardedRef, () => ref.current!);
49
23
 
50
- const onInvalid = (__errors: FieldValues) => {
51
- const customSubmit = new CustomEvent('twintag-form-invalid', {
52
- detail: __errors,
53
- bubbles: true,
54
- cancelable: false,
55
- composed: true,
56
- });
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
+ });
44
+
45
+ ref.current?.dispatchEvent(eventWithValidData);
46
+ };
47
+
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
+ });
57
55
 
58
- ref.current?.dispatchEvent(customSubmit);
59
- };
56
+ ref.current?.dispatchEvent(eventWithInvalidData);
57
+ };
58
+
59
+ const onValidate = () => {
60
+ void form.handleSubmit(onValid, onInvalid)();
61
+ };
62
+
63
+ const onGetValues = () => {
64
+ const values = form.getValues();
60
65
 
61
- const onValidate = () => {
62
- void form.handleSubmit(onValid, onInvalid)();
63
- };
66
+ const eventWithValues = new CustomEvent('twintag-form-values', {
67
+ detail: values,
68
+ bubbles: true,
69
+ cancelable: false,
70
+ composed: true,
71
+ });
64
72
 
65
- useEffect(() => {
66
- const form = ref.current;
73
+ ref.current?.dispatchEvent(eventWithValues);
74
+ };
67
75
 
68
- if (form) {
69
- form.addEventListener('twintag-form-validate', onValidate);
70
- }
76
+ useEffect(() => {
77
+ const form = ref.current;
71
78
 
72
- return () => {
73
79
  if (form) {
74
- form.removeEventListener('twintag-form-validate', onValidate);
80
+ form.addEventListener('twintag-form-validate', onValidate);
81
+ form.addEventListener('twintag-form-get_values', onGetValues);
75
82
  }
76
- };
77
- }, []);
78
-
79
- return (
80
- <Form {...form}>
81
- <form
82
- id='twintag-form'
83
- ref={ref}
84
- className='relative space-y-4 font-tf-font'
85
- onSubmit={form.handleSubmit(onValid, onInvalid)}
86
- >
87
- {children}
88
- </form>
89
- </Form>
90
- );
91
- };
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 = ({ 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 }) => {
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
+ );
@@ -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.1.1",
3
+ "version": "1.2.1",
4
4
  "author": "Twintag",
5
5
  "type": "module",
6
6
  "exports": {
@@ -62,7 +62,7 @@
62
62
  "dev": "vite",
63
63
  "build": "tsc && vite build",
64
64
  "lint": "eslint . --ext .ts,.tsx",
65
- "format": "prettier --write \"**/*.{ts,md,tsx,cjs,mjs,json}\" --ignore-path ../../.gitignore",
66
- "format:check": "prettier --check \"**/*.{ts,tsx,cjs,mjs,json}\" --ignore-path ../../.gitignore"
65
+ "format": "prettier --write \"**/*.{ts,md,tsx,cjs,mjs,json,html}\" --ignore-path ../../.gitignore",
66
+ "format:check": "prettier --check \"**/*.{ts,md,tsx,cjs,mjs,json,html}\" --ignore-path ../../.gitignore"
67
67
  }
68
68
  }