@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.
- package/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +12 -0
- package/dev/main.tsx +24 -0
- package/dist/components/form.d.ts +8 -8
- package/dist/index.d.ts +2 -3
- package/dist/twintag-form.react.js +2293 -2286
- package/dist/twintag-form.webcomponent.js +1 -0
- package/index.html +26 -15
- package/lib/components/form.tsx +82 -69
- package/lib/index.tsx +18 -14
- package/lib/webcomponent.ts +1 -0
- package/package.json +3 -3
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
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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>
|
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,81 +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
|
-
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
|
|
48
|
-
};
|
|
22
|
+
useImperativeHandle(fowardedRef, () => ref.current!);
|
|
49
23
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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
|
-
|
|
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
|
-
|
|
62
|
-
|
|
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
|
-
|
|
66
|
-
|
|
73
|
+
ref.current?.dispatchEvent(eventWithValues);
|
|
74
|
+
};
|
|
67
75
|
|
|
68
|
-
|
|
69
|
-
form.
|
|
70
|
-
}
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
const form = ref.current;
|
|
71
78
|
|
|
72
|
-
return () => {
|
|
73
79
|
if (form) {
|
|
74
|
-
form.
|
|
80
|
+
form.addEventListener('twintag-form-validate', onValidate);
|
|
81
|
+
form.addEventListener('twintag-form-get_values', onGetValues);
|
|
75
82
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
>
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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 =
|
|
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
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twintag/twintag-form",
|
|
3
|
-
"version": "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
|
}
|