@twintag/twintag-form 1.6.2-beta.1 → 1.6.2

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.
@@ -1,70 +1,70 @@
1
- import type { FC } from 'react';
2
- import type { Props } from '@twintag/twintag-form/types';
3
-
4
- import { AsteriskIcon } from 'lucide-react';
5
- import { useFormContext } from 'react-hook-form';
6
-
7
- import {
8
- FormControl,
9
- FormDescription,
10
- FormField,
11
- FormItem,
12
- FormLabel,
13
- FormMessage,
14
- } from '@twintag/twintag-form/common/form';
15
- import { Input as InputPrimitive } from '@twintag/twintag-form/common/input';
16
- import { useTwintagForm } from '@twintag/twintag-form/components/provider';
17
-
18
- export const DEFAULT_INPUT_VALUE = '';
19
-
20
- export const Input: FC<Props<'input', 'text'>> = ({ label, bind, options }) => {
21
- const { control } = useFormContext();
22
-
23
- const { variant, size } = useTwintagForm();
24
-
25
- const isOptional = options?.optionalField;
26
-
27
- return (
28
- <FormField
29
- defaultValue={DEFAULT_INPUT_VALUE}
30
- control={control}
31
- name={bind}
32
- rules={{
33
- required: isOptional ? false : (options?.error ?? true),
34
- }}
35
- render={({ field }) => (
36
- <FormItem variant={variant}>
37
- <div className='space-y-1'>
38
- <FormLabel size={size}>
39
- {label}
40
- {!isOptional ? (
41
- <span className='ml-0.5 inline-block'>
42
- <AsteriskIcon className='h-3 w-3 text-tf-destructive' />
43
- </span>
44
- ) : (
45
- <></>
46
- )}
47
- </FormLabel>
48
- {options?.description ? (
49
- <FormDescription size={size}>
50
- {options.description}
51
- </FormDescription>
52
- ) : (
53
- <></>
54
- )}
55
- </div>
56
- <FormControl>
57
- <InputPrimitive
58
- type='text'
59
- size={size}
60
- variant={variant}
61
- placeholder={options?.placeholder ?? 'Write here...'}
62
- {...field}
63
- />
64
- </FormControl>
65
- <FormMessage size={size} className='pt-1' />
66
- </FormItem>
67
- )}
68
- />
69
- );
70
- };
1
+ import type { FC } from 'react';
2
+ import type { Props } from '@twintag/twintag-form/types';
3
+
4
+ import { AsteriskIcon } from 'lucide-react';
5
+ import { useFormContext } from 'react-hook-form';
6
+
7
+ import {
8
+ FormControl,
9
+ FormDescription,
10
+ FormField,
11
+ FormItem,
12
+ FormLabel,
13
+ FormMessage,
14
+ } from '@twintag/twintag-form/common/form';
15
+ import { Input as InputPrimitive } from '@twintag/twintag-form/common/input';
16
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
17
+
18
+ export const DEFAULT_INPUT_VALUE = '';
19
+
20
+ export const Input: FC<Props<'input', 'text'>> = ({ label, bind, options }) => {
21
+ const { control } = useFormContext();
22
+
23
+ const { variant, size } = useTwintagForm();
24
+
25
+ const isOptional = options?.optionalField;
26
+
27
+ return (
28
+ <FormField
29
+ defaultValue={DEFAULT_INPUT_VALUE}
30
+ control={control}
31
+ name={bind}
32
+ rules={{
33
+ required: isOptional ? false : (options?.error ?? true),
34
+ }}
35
+ render={({ field }) => (
36
+ <FormItem variant={variant}>
37
+ <div className='space-y-1'>
38
+ <FormLabel size={size}>
39
+ {label}
40
+ {!isOptional ? (
41
+ <span className='ml-0.5 inline-block'>
42
+ <AsteriskIcon className='h-3 w-3 text-tf-destructive' />
43
+ </span>
44
+ ) : (
45
+ <></>
46
+ )}
47
+ </FormLabel>
48
+ {options?.description ? (
49
+ <FormDescription size={size}>
50
+ {options.description}
51
+ </FormDescription>
52
+ ) : (
53
+ <></>
54
+ )}
55
+ </div>
56
+ <FormControl>
57
+ <InputPrimitive
58
+ type='text'
59
+ size={size}
60
+ variant={variant}
61
+ placeholder={options?.placeholder ?? 'Write here...'}
62
+ {...field}
63
+ />
64
+ </FormControl>
65
+ <FormMessage size={size} className='pt-1' />
66
+ </FormItem>
67
+ )}
68
+ />
69
+ );
70
+ };
@@ -1,72 +1,72 @@
1
- import type { FC } from 'react';
2
- import type { Props } from '@twintag/twintag-form/types';
3
-
4
- import { AsteriskIcon } from 'lucide-react';
5
- import { useFormContext } from 'react-hook-form';
6
-
7
- import {
8
- FormControl,
9
- FormDescription,
10
- FormField,
11
- FormItem,
12
- FormLabel,
13
- FormMessage,
14
- } from '@twintag/twintag-form/common/form';
15
- import { Textarea as CommonTextarea } from '@twintag/twintag-form/common/textarea';
16
- import { useTwintagForm } from '@twintag/twintag-form/components/provider';
17
-
18
- export const DEFAULT_TEXTAREA_VALUE = '';
19
-
20
- export const Textarea: FC<Props<'input', 'text'>> = ({
21
- label,
22
- bind,
23
- options,
24
- }) => {
25
- const { control } = useFormContext();
26
- const { variant, size } = useTwintagForm();
27
-
28
- const isOptional = options?.optionalField;
29
-
30
- return (
31
- <FormField
32
- defaultValue={DEFAULT_TEXTAREA_VALUE}
33
- control={control}
34
- name={bind}
35
- rules={{
36
- required: isOptional ? false : (options?.error ?? true),
37
- }}
38
- render={({ field }) => (
39
- <FormItem variant={variant}>
40
- <div className='space-y-1'>
41
- <FormLabel size={size}>
42
- {label}
43
- {!isOptional ? (
44
- <span className='ml-0.5 inline-block'>
45
- <AsteriskIcon className='h-3 w-3 text-tf-destructive' />
46
- </span>
47
- ) : (
48
- <></>
49
- )}
50
- </FormLabel>
51
- {options?.description ? (
52
- <FormDescription size={size}>
53
- {options.description}
54
- </FormDescription>
55
- ) : (
56
- <></>
57
- )}
58
- </div>
59
- <FormControl>
60
- <CommonTextarea
61
- {...field}
62
- size={size}
63
- variant={variant}
64
- placeholder={options?.placeholder ?? 'Write here...'}
65
- />
66
- </FormControl>
67
- <FormMessage size={size} className='pt-1' />
68
- </FormItem>
69
- )}
70
- />
71
- );
72
- };
1
+ import type { FC } from 'react';
2
+ import type { Props } from '@twintag/twintag-form/types';
3
+
4
+ import { AsteriskIcon } from 'lucide-react';
5
+ import { useFormContext } from 'react-hook-form';
6
+
7
+ import {
8
+ FormControl,
9
+ FormDescription,
10
+ FormField,
11
+ FormItem,
12
+ FormLabel,
13
+ FormMessage,
14
+ } from '@twintag/twintag-form/common/form';
15
+ import { Textarea as CommonTextarea } from '@twintag/twintag-form/common/textarea';
16
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
17
+
18
+ export const DEFAULT_TEXTAREA_VALUE = '';
19
+
20
+ export const Textarea: FC<Props<'input', 'text'>> = ({
21
+ label,
22
+ bind,
23
+ options,
24
+ }) => {
25
+ const { control } = useFormContext();
26
+ const { variant, size } = useTwintagForm();
27
+
28
+ const isOptional = options?.optionalField;
29
+
30
+ return (
31
+ <FormField
32
+ defaultValue={DEFAULT_TEXTAREA_VALUE}
33
+ control={control}
34
+ name={bind}
35
+ rules={{
36
+ required: isOptional ? false : (options?.error ?? true),
37
+ }}
38
+ render={({ field }) => (
39
+ <FormItem variant={variant}>
40
+ <div className='space-y-1'>
41
+ <FormLabel size={size}>
42
+ {label}
43
+ {!isOptional ? (
44
+ <span className='ml-0.5 inline-block'>
45
+ <AsteriskIcon className='h-3 w-3 text-tf-destructive' />
46
+ </span>
47
+ ) : (
48
+ <></>
49
+ )}
50
+ </FormLabel>
51
+ {options?.description ? (
52
+ <FormDescription size={size}>
53
+ {options.description}
54
+ </FormDescription>
55
+ ) : (
56
+ <></>
57
+ )}
58
+ </div>
59
+ <FormControl>
60
+ <CommonTextarea
61
+ {...field}
62
+ size={size}
63
+ variant={variant}
64
+ placeholder={options?.placeholder ?? 'Write here...'}
65
+ />
66
+ </FormControl>
67
+ <FormMessage size={size} className='pt-1' />
68
+ </FormItem>
69
+ )}
70
+ />
71
+ );
72
+ };
@@ -1,162 +1,162 @@
1
- import type {
2
- ParsedJsonComponent,
3
- RawJsonComponent,
4
- TwintagFormContext,
5
- } from '@twintag/twintag-form/types';
6
- import type { WithChildren } from 'twintag-types/dom';
7
-
8
- import { createContext, useContext, useMemo } from 'react';
9
- import { LabelToString } from 'twintag-types/dom';
10
- import { v4 as uuid } from 'uuid';
11
-
12
- export const twintagFormContext = createContext<TwintagFormContext>({
13
- variant: 'default',
14
- size: 'base',
15
- inputs: new Map(),
16
- components: new Map(),
17
- config: [],
18
- resetValues: {},
19
- acceptFileType: undefined,
20
- });
21
-
22
- export const useTwintagForm = () => {
23
- const context = useContext(twintagFormContext);
24
-
25
- if (!context) {
26
- throw new Error('useTwintagForm must be used within a TwintagFormProvider');
27
- }
28
-
29
- return context;
30
- };
31
-
32
- type Components = TwintagFormContext['components'];
33
- type Inputs = TwintagFormContext['inputs'];
34
-
35
- const format = (component: RawJsonComponent, components: Components) => {
36
- const copy = { ...component };
37
- let formattedConfig: ParsedJsonComponent;
38
-
39
- if (copy.props) {
40
- formattedConfig = { ...copy.props } as ParsedJsonComponent;
41
- } else {
42
- formattedConfig = { ...copy } as unknown as ParsedJsonComponent;
43
- }
44
-
45
- if (!copy.type) return Object.create(null) as ParsedJsonComponent;
46
-
47
- const [type, subtype = undefined] = copy.type.split(':');
48
- formattedConfig.id = `${type}-${uuid()}`;
49
-
50
- const comp = components.get(type);
51
-
52
- if (comp) {
53
- formattedConfig.comp = comp;
54
- }
55
-
56
- formattedConfig.type = type;
57
-
58
- if (subtype) {
59
- formattedConfig.subtype = subtype;
60
- }
61
-
62
- if (copy.props) {
63
- if (copy.props.subtype) {
64
- formattedConfig.subtype = copy.props.subtype;
65
- }
66
-
67
- if (copy.props.label) {
68
- const labelParsed = LabelToString.safeParse(copy.props.label);
69
- formattedConfig.label = labelParsed.success
70
- ? labelParsed.data
71
- : undefined;
72
- }
73
- }
74
-
75
- if (copy.children && Array.isArray(copy.children)) {
76
- formattedConfig.items = copy.children.map((item) =>
77
- format(item, components),
78
- );
79
- }
80
-
81
- return formattedConfig;
82
- };
83
-
84
- export const getResetValues = (
85
- config: ParsedJsonComponent | ParsedJsonComponent[],
86
- inputs: Inputs,
87
- ) => {
88
- const stack = Array.isArray(config) ? [...config] : [config];
89
-
90
- const resetValues = new Map<string, unknown>();
91
-
92
- while (stack.length > 0) {
93
- const currentObj = stack.pop();
94
-
95
- if (!currentObj) continue;
96
-
97
- for (const [key, value] of Object.entries(currentObj)) {
98
- if (Array.isArray(value)) {
99
- for (const item of value) {
100
- stack.push(item);
101
- }
102
- }
103
-
104
- if (typeof value === 'object' && value !== null) {
105
- stack.push(value as unknown as ParsedJsonComponent);
106
- }
107
-
108
- if (
109
- key === 'subtype' &&
110
- 'bind' in currentObj &&
111
- typeof currentObj.bind === 'string'
112
- ) {
113
- resetValues.set(
114
- currentObj.bind,
115
- inputs.get(value as string)?.defaultValue,
116
- );
117
- }
118
- }
119
- }
120
-
121
- return Object.fromEntries(resetValues);
122
- };
123
-
124
- export const TwintagFormProvider = ({
125
- variant,
126
- size,
127
- inputs,
128
- components,
129
- config,
130
- acceptFileType,
131
- children,
132
- }: Omit<TwintagFormContext, 'config' | 'resetValues'> & {
133
- config: RawJsonComponent | RawJsonComponent[];
134
- } & WithChildren) => {
135
- const __config = useMemo(() => {
136
- if (Array.isArray(config)) {
137
- return config.map((item) => format(item, components));
138
- }
139
-
140
- return format(config, components);
141
- }, [config, components]);
142
-
143
- const resetValues = useMemo(() => {
144
- return getResetValues(__config, inputs);
145
- }, [__config, inputs]);
146
-
147
- return (
148
- <twintagFormContext.Provider
149
- value={{
150
- config: __config,
151
- variant,
152
- size,
153
- inputs,
154
- components,
155
- resetValues,
156
- acceptFileType,
157
- }}
158
- >
159
- {children}
160
- </twintagFormContext.Provider>
161
- );
162
- };
1
+ import type {
2
+ ParsedJsonComponent,
3
+ RawJsonComponent,
4
+ TwintagFormContext,
5
+ } from '@twintag/twintag-form/types';
6
+ import type { WithChildren } from 'twintag-types/dom';
7
+
8
+ import { createContext, useContext, useMemo } from 'react';
9
+ import { LabelToString } from 'twintag-types/dom';
10
+ import { v4 as uuid } from 'uuid';
11
+
12
+ export const twintagFormContext = createContext<TwintagFormContext>({
13
+ variant: 'default',
14
+ size: 'base',
15
+ inputs: new Map(),
16
+ components: new Map(),
17
+ config: [],
18
+ resetValues: {},
19
+ acceptFileType: undefined,
20
+ });
21
+
22
+ export const useTwintagForm = () => {
23
+ const context = useContext(twintagFormContext);
24
+
25
+ if (!context) {
26
+ throw new Error('useTwintagForm must be used within a TwintagFormProvider');
27
+ }
28
+
29
+ return context;
30
+ };
31
+
32
+ type Components = TwintagFormContext['components'];
33
+ type Inputs = TwintagFormContext['inputs'];
34
+
35
+ const format = (component: RawJsonComponent, components: Components) => {
36
+ const copy = { ...component };
37
+ let formattedConfig: ParsedJsonComponent;
38
+
39
+ if (copy.props) {
40
+ formattedConfig = { ...copy.props } as ParsedJsonComponent;
41
+ } else {
42
+ formattedConfig = { ...copy } as unknown as ParsedJsonComponent;
43
+ }
44
+
45
+ if (!copy.type) return Object.create(null) as ParsedJsonComponent;
46
+
47
+ const [type, subtype = undefined] = copy.type.split(':');
48
+ formattedConfig.id = `${type}-${uuid()}`;
49
+
50
+ const comp = components.get(type);
51
+
52
+ if (comp) {
53
+ formattedConfig.comp = comp;
54
+ }
55
+
56
+ formattedConfig.type = type;
57
+
58
+ if (subtype) {
59
+ formattedConfig.subtype = subtype;
60
+ }
61
+
62
+ if (copy.props) {
63
+ if (copy.props.subtype) {
64
+ formattedConfig.subtype = copy.props.subtype;
65
+ }
66
+
67
+ if (copy.props.label) {
68
+ const labelParsed = LabelToString.safeParse(copy.props.label);
69
+ formattedConfig.label = labelParsed.success
70
+ ? labelParsed.data
71
+ : undefined;
72
+ }
73
+ }
74
+
75
+ if (copy.children && Array.isArray(copy.children)) {
76
+ formattedConfig.items = copy.children.map((item) =>
77
+ format(item, components),
78
+ );
79
+ }
80
+
81
+ return formattedConfig;
82
+ };
83
+
84
+ export const getResetValues = (
85
+ config: ParsedJsonComponent | ParsedJsonComponent[],
86
+ inputs: Inputs,
87
+ ) => {
88
+ const stack = Array.isArray(config) ? [...config] : [config];
89
+
90
+ const resetValues = new Map<string, unknown>();
91
+
92
+ while (stack.length > 0) {
93
+ const currentObj = stack.pop();
94
+
95
+ if (!currentObj) continue;
96
+
97
+ for (const [key, value] of Object.entries(currentObj)) {
98
+ if (Array.isArray(value)) {
99
+ for (const item of value) {
100
+ stack.push(item);
101
+ }
102
+ }
103
+
104
+ if (typeof value === 'object' && value !== null) {
105
+ stack.push(value as unknown as ParsedJsonComponent);
106
+ }
107
+
108
+ if (
109
+ key === 'subtype' &&
110
+ 'bind' in currentObj &&
111
+ typeof currentObj.bind === 'string'
112
+ ) {
113
+ resetValues.set(
114
+ currentObj.bind,
115
+ inputs.get(value as string)?.defaultValue,
116
+ );
117
+ }
118
+ }
119
+ }
120
+
121
+ return Object.fromEntries(resetValues);
122
+ };
123
+
124
+ export const TwintagFormProvider = ({
125
+ variant,
126
+ size,
127
+ inputs,
128
+ components,
129
+ config,
130
+ acceptFileType,
131
+ children,
132
+ }: Omit<TwintagFormContext, 'config' | 'resetValues'> & {
133
+ config: RawJsonComponent | RawJsonComponent[];
134
+ } & WithChildren) => {
135
+ const __config = useMemo(() => {
136
+ if (Array.isArray(config)) {
137
+ return config.map((item) => format(item, components));
138
+ }
139
+
140
+ return format(config, components);
141
+ }, [config, components]);
142
+
143
+ const resetValues = useMemo(() => {
144
+ return getResetValues(__config, inputs);
145
+ }, [__config, inputs]);
146
+
147
+ return (
148
+ <twintagFormContext.Provider
149
+ value={{
150
+ config: __config,
151
+ variant,
152
+ size,
153
+ inputs,
154
+ components,
155
+ resetValues,
156
+ acceptFileType,
157
+ }}
158
+ >
159
+ {children}
160
+ </twintagFormContext.Provider>
161
+ );
162
+ };