@twintag/twintag-form 1.6.2-beta.0 → 1.6.2-beta.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.
Files changed (54) hide show
  1. package/.turbo/turbo-build.log +22 -0
  2. package/CHANGELOG.md +221 -215
  3. package/README.md +407 -407
  4. package/dev/main.tsx +460 -460
  5. package/dist/common/accordion.d.ts +7 -0
  6. package/dist/common/button.d.ts +71 -0
  7. package/dist/common/calendar.d.ts +139 -0
  8. package/dist/common/checkbox.d.ts +4 -0
  9. package/dist/common/form.d.ts +86 -0
  10. package/dist/common/icon.d.ts +8 -0
  11. package/dist/common/input.d.ts +133 -0
  12. package/dist/common/label.d.ts +21 -0
  13. package/dist/common/popover.d.ts +6 -0
  14. package/dist/common/radiogroup.d.ts +5 -0
  15. package/dist/common/slider.d.ts +4 -0
  16. package/dist/common/switch.d.ts +4 -0
  17. package/dist/common/textarea.d.ts +68 -0
  18. package/dist/components/config.d.ts +6 -0
  19. package/dist/components/containers/accordion.d.ts +7 -0
  20. package/dist/components/containers/category.d.ts +8 -0
  21. package/dist/components/fields/checkbox.d.ts +5 -0
  22. package/dist/components/fields/date-picker.d.ts +4 -0
  23. package/dist/components/fields/file.d.ts +5 -0
  24. package/dist/components/fields/input.d.ts +5 -0
  25. package/dist/components/fields/radiogroup.d.ts +7 -0
  26. package/dist/components/fields/submit.d.ts +4 -0
  27. package/dist/components/fields/textarea.d.ts +5 -0
  28. package/dist/components/fields/toggle.d.ts +5 -0
  29. package/dist/components/fields/tri-toggle.d.ts +5 -0
  30. package/dist/components/form.d.ts +9 -0
  31. package/dist/components/input.d.ts +4 -0
  32. package/dist/components/label.d.ts +4 -0
  33. package/dist/components/provider.d.ts +14 -0
  34. package/dist/components/renderer.d.ts +1 -0
  35. package/dist/index.cjs +1 -0
  36. package/dist/index.d.ts +17 -0
  37. package/dist/index.js +4465 -0
  38. package/dist/types/index.d.ts +68 -0
  39. package/dist/utils/data.d.ts +7 -0
  40. package/dist/utils/string.d.ts +3 -0
  41. package/dist/utils/style.d.ts +3 -0
  42. package/dist/webcomponent.cjs +1 -0
  43. package/dist/webcomponent.d.ts +1 -0
  44. package/dist/webcomponent.js +1165 -0
  45. package/lib/components/fields/date-picker.tsx +154 -154
  46. package/lib/components/fields/file.tsx +84 -84
  47. package/lib/components/fields/input.tsx +70 -70
  48. package/lib/components/fields/textarea.tsx +72 -72
  49. package/lib/components/provider.tsx +162 -162
  50. package/lib/index.tsx +45 -45
  51. package/lib/types/index.ts +102 -102
  52. package/lib/webcomponent.ts +2 -2
  53. package/package.json +1 -1
  54. package/vite.config.ts +2 -1
@@ -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
+ };
package/lib/index.tsx CHANGED
@@ -1,45 +1,45 @@
1
- import type { ComponentProps } from 'react';
2
- import type { RawJsonComponent } from '@twintag/twintag-form/types';
3
-
4
- import { forwardRef } from 'react';
5
-
6
- import { components, Inputs } from '@twintag/twintag-form/components/config';
7
- import { TwintagForm } from '@twintag/twintag-form/components/form';
8
- import { TwintagFormProvider } from '@twintag/twintag-form/components/provider';
9
- import { JsonRenderer } from '@twintag/twintag-form/components/renderer';
10
-
11
- type Props = {
12
- config: RawJsonComponent[];
13
- options?: Partial<{
14
- defaultValues: ComponentProps<typeof TwintagForm>['defaultValues'];
15
- variant: ComponentProps<typeof TwintagFormProvider>['variant'];
16
- size: ComponentProps<typeof TwintagFormProvider>['size'];
17
- resetOnSubmit?: boolean;
18
- acceptFileType?: ComponentProps<
19
- typeof TwintagFormProvider
20
- >['acceptFileType'];
21
- }>;
22
- };
23
-
24
- export const Form = forwardRef<HTMLFormElement, Props>(
25
- ({ config, options }, ref) => {
26
- return (
27
- <TwintagFormProvider
28
- inputs={Inputs}
29
- components={components}
30
- variant={options?.variant}
31
- size={options?.size}
32
- config={config}
33
- acceptFileType={options?.acceptFileType}
34
- >
35
- <TwintagForm
36
- ref={ref}
37
- defaultValues={options?.defaultValues}
38
- resetOnSubmit={options?.resetOnSubmit}
39
- >
40
- <JsonRenderer />
41
- </TwintagForm>
42
- </TwintagFormProvider>
43
- );
44
- },
45
- );
1
+ import type { ComponentProps } from 'react';
2
+ import type { RawJsonComponent } from '@twintag/twintag-form/types';
3
+
4
+ import { forwardRef } from 'react';
5
+
6
+ import { components, Inputs } from '@twintag/twintag-form/components/config';
7
+ import { TwintagForm } from '@twintag/twintag-form/components/form';
8
+ import { TwintagFormProvider } from '@twintag/twintag-form/components/provider';
9
+ import { JsonRenderer } from '@twintag/twintag-form/components/renderer';
10
+
11
+ type Props = {
12
+ config: RawJsonComponent[];
13
+ options?: Partial<{
14
+ defaultValues: ComponentProps<typeof TwintagForm>['defaultValues'];
15
+ variant: ComponentProps<typeof TwintagFormProvider>['variant'];
16
+ size: ComponentProps<typeof TwintagFormProvider>['size'];
17
+ resetOnSubmit?: boolean;
18
+ acceptFileType?: ComponentProps<
19
+ typeof TwintagFormProvider
20
+ >['acceptFileType'];
21
+ }>;
22
+ };
23
+
24
+ export const Form = forwardRef<HTMLFormElement, Props>(
25
+ ({ config, options }, ref) => {
26
+ return (
27
+ <TwintagFormProvider
28
+ inputs={Inputs}
29
+ components={components}
30
+ variant={options?.variant}
31
+ size={options?.size}
32
+ config={config}
33
+ acceptFileType={options?.acceptFileType}
34
+ >
35
+ <TwintagForm
36
+ ref={ref}
37
+ defaultValues={options?.defaultValues}
38
+ resetOnSubmit={options?.resetOnSubmit}
39
+ >
40
+ <JsonRenderer />
41
+ </TwintagForm>
42
+ </TwintagFormProvider>
43
+ );
44
+ },
45
+ );