@twintag/twintag-form 1.4.0-beta.0 → 1.4.0-beta.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.
- package/.turbo/turbo-build.log +6 -6
- package/CHANGELOG.md +12 -0
- package/README.md +121 -62
- package/dev/main.tsx +453 -209
- package/dist/common/button.d.ts +3 -2
- package/dist/common/calendar.d.ts +4 -4
- package/dist/common/form.d.ts +11 -11
- package/dist/common/icon.d.ts +2 -1
- package/dist/common/input.d.ts +3 -2
- package/dist/common/label.d.ts +3 -2
- package/dist/common/slider.d.ts +4 -0
- package/dist/common/textarea.d.ts +3 -2
- package/dist/components/config.d.ts +4 -2
- package/dist/components/containers/accordion.d.ts +5 -6
- package/dist/components/containers/category.d.ts +3 -2
- package/dist/components/fields/checkbox.d.ts +5 -3
- package/dist/components/fields/date-picker.d.ts +4 -2
- package/dist/components/fields/file.d.ts +5 -3
- package/dist/components/fields/input.d.ts +5 -3
- package/dist/components/fields/radiogroup.d.ts +7 -3
- package/dist/components/fields/submit.d.ts +3 -2
- package/dist/components/fields/textarea.d.ts +5 -3
- package/dist/components/fields/toggle.d.ts +5 -3
- package/dist/components/fields/tri-toggle.d.ts +5 -0
- package/dist/components/form.d.ts +2 -2
- package/dist/components/provider.d.ts +21 -0
- package/dist/components/renderer.d.ts +1 -0
- package/dist/index.d.ts +4 -3
- package/dist/twintag-form.react.js +12880 -12304
- package/dist/types/index.d.ts +43 -20
- package/dist/utils/data.d.ts +7 -2
- package/dist/utils/style.d.ts +2 -1
- package/index.html +6 -3
- package/lib/common/slider.tsx +38 -0
- package/lib/components/config.tsx +46 -17
- package/lib/components/containers/accordion.tsx +33 -18
- package/lib/components/containers/category.tsx +1 -1
- package/lib/components/fields/checkbox.tsx +5 -3
- package/lib/components/fields/date-picker.tsx +9 -2
- package/lib/components/fields/file.tsx +5 -4
- package/lib/components/fields/input.tsx +5 -3
- package/lib/components/fields/radiogroup.tsx +9 -3
- package/lib/components/fields/submit.tsx +1 -1
- package/lib/components/fields/textarea.tsx +5 -3
- package/lib/components/fields/toggle.tsx +5 -3
- package/lib/components/fields/tri-toggle.tsx +141 -0
- package/lib/components/form.tsx +5 -7
- package/lib/components/provider.tsx +163 -0
- package/lib/components/{Render/index.tsx → renderer.tsx} +19 -2
- package/lib/index.tsx +41 -41
- package/lib/types/index.ts +69 -29
- package/lib/utils/data.ts +5 -2
- package/package.json +6 -8
- package/dist/components/Render/index.d.ts +0 -7
- package/dist/components/Render/useJson.d.ts +0 -2
- package/dist/components/index.d.ts +0 -7
- package/dist/provider.d.ts +0 -8
- package/lib/components/Render/useJson.ts +0 -76
- package/lib/components/index.tsx +0 -24
- package/lib/provider.tsx +0 -39
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { FC } from 'react';
|
|
2
|
+
import type { Props } from '@twintag/twintag-form/types';
|
|
3
|
+
import type { VariantProps } from 'tailwind-variants';
|
|
4
|
+
|
|
5
|
+
import * as React from 'react';
|
|
6
|
+
import * as SliderPrimitive from '@radix-ui/react-slider';
|
|
7
|
+
import { useFormContext } from 'react-hook-form';
|
|
8
|
+
import { tv } from 'tailwind-variants';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
FormControl,
|
|
12
|
+
FormDescription,
|
|
13
|
+
FormField,
|
|
14
|
+
FormItem,
|
|
15
|
+
FormLabel,
|
|
16
|
+
FormMessage,
|
|
17
|
+
} from '@twintag/twintag-form/common/form';
|
|
18
|
+
import { useTwintagForm } from '@twintag/twintag-form/components/provider';
|
|
19
|
+
|
|
20
|
+
const sliderVariants = tv({
|
|
21
|
+
slots: {
|
|
22
|
+
root: 'relative flex w-11 touch-none select-none items-center',
|
|
23
|
+
track: 'relative h-5 w-full grow overflow-hidden rounded-full bg-muted',
|
|
24
|
+
range: 'absolute h-full bg-accent',
|
|
25
|
+
thumb:
|
|
26
|
+
'relative block h-4 w-4 rounded-full border-2 border-transparent bg-background shadow-lg focus-visible:outline-none focus-visible:ring-0 disabled:pointer-events-none disabled:opacity-50',
|
|
27
|
+
},
|
|
28
|
+
variants: {
|
|
29
|
+
state: {
|
|
30
|
+
// false
|
|
31
|
+
0: {
|
|
32
|
+
thumb: 'left-0.5',
|
|
33
|
+
},
|
|
34
|
+
// null
|
|
35
|
+
1: {
|
|
36
|
+
thumb: 'h-5 w-5 border-accent',
|
|
37
|
+
},
|
|
38
|
+
// true
|
|
39
|
+
2: {
|
|
40
|
+
thumb: 'right-0.5',
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
defaultVariants: {
|
|
45
|
+
state: 1,
|
|
46
|
+
},
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const Slider = React.forwardRef<
|
|
50
|
+
React.ElementRef<typeof SliderPrimitive.Root>,
|
|
51
|
+
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root> & {
|
|
52
|
+
classNames?: Record<keyof (typeof sliderVariants)['slots'], string>;
|
|
53
|
+
}
|
|
54
|
+
>(({ className, ...props }, ref) => {
|
|
55
|
+
const val = props.value?.[0] as VariantProps<typeof sliderVariants>['state'];
|
|
56
|
+
|
|
57
|
+
const { root, track, range, thumb } = sliderVariants({
|
|
58
|
+
state: val,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<SliderPrimitive.Root
|
|
63
|
+
disabled
|
|
64
|
+
ref={ref}
|
|
65
|
+
className={root({
|
|
66
|
+
className,
|
|
67
|
+
})}
|
|
68
|
+
onClick={(e) => {
|
|
69
|
+
e.preventDefault();
|
|
70
|
+
|
|
71
|
+
if (val !== undefined) {
|
|
72
|
+
const states = Object.keys(sliderVariants.variants.state);
|
|
73
|
+
const value = states.indexOf(val.toString());
|
|
74
|
+
const nextValue = value === states.length - 1 ? 0 : value + 1;
|
|
75
|
+
props.onValueChange?.([nextValue]);
|
|
76
|
+
}
|
|
77
|
+
}}
|
|
78
|
+
{...props}
|
|
79
|
+
>
|
|
80
|
+
<SliderPrimitive.Track className={track()}>
|
|
81
|
+
<SliderPrimitive.Range className={range()} />
|
|
82
|
+
</SliderPrimitive.Track>
|
|
83
|
+
<SliderPrimitive.Thumb className={thumb()} />
|
|
84
|
+
</SliderPrimitive.Root>
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
Slider.displayName = SliderPrimitive.Root.displayName;
|
|
88
|
+
|
|
89
|
+
export const DEFAULT_TRI_TOGGLE_VALUE = null;
|
|
90
|
+
|
|
91
|
+
export const TriToggle: FC<Props<'input'>> = ({ label, bind, options }) => {
|
|
92
|
+
const { control } = useFormContext();
|
|
93
|
+
const { variant, size } = useTwintagForm();
|
|
94
|
+
|
|
95
|
+
return (
|
|
96
|
+
<FormField
|
|
97
|
+
defaultValue={DEFAULT_TRI_TOGGLE_VALUE}
|
|
98
|
+
control={control}
|
|
99
|
+
name={bind}
|
|
100
|
+
rules={{
|
|
101
|
+
required: false,
|
|
102
|
+
}}
|
|
103
|
+
render={({ field }) => (
|
|
104
|
+
<FormItem variant={variant}>
|
|
105
|
+
<div className='flex flex-row items-center justify-between'>
|
|
106
|
+
<div className='space-y-1'>
|
|
107
|
+
<FormLabel htmlFor={bind} size={size}>
|
|
108
|
+
{label}
|
|
109
|
+
</FormLabel>
|
|
110
|
+
{options?.description ? (
|
|
111
|
+
<FormDescription size={size}>
|
|
112
|
+
{options.description}
|
|
113
|
+
</FormDescription>
|
|
114
|
+
) : (
|
|
115
|
+
<></>
|
|
116
|
+
)}
|
|
117
|
+
</div>
|
|
118
|
+
<FormControl>
|
|
119
|
+
<Slider
|
|
120
|
+
id={bind}
|
|
121
|
+
ref={field.ref}
|
|
122
|
+
min={0}
|
|
123
|
+
max={2}
|
|
124
|
+
step={1}
|
|
125
|
+
value={[
|
|
126
|
+
field.value === true ? 2 : field.value === null ? 1 : 0,
|
|
127
|
+
]}
|
|
128
|
+
onValueChange={(value) => {
|
|
129
|
+
field.onChange(
|
|
130
|
+
value[0] === 2 ? true : value[0] === 1 ? null : false,
|
|
131
|
+
);
|
|
132
|
+
}}
|
|
133
|
+
/>
|
|
134
|
+
</FormControl>
|
|
135
|
+
</div>
|
|
136
|
+
<FormMessage size={size} />
|
|
137
|
+
</FormItem>
|
|
138
|
+
)}
|
|
139
|
+
/>
|
|
140
|
+
);
|
|
141
|
+
};
|
package/lib/components/form.tsx
CHANGED
|
@@ -5,6 +5,7 @@ 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';
|
|
8
|
+
import { useTwintagForm } from '@twintag/twintag-form/components/provider';
|
|
8
9
|
|
|
9
10
|
type Props = {
|
|
10
11
|
defaultValues?: FieldValues;
|
|
@@ -13,6 +14,8 @@ type Props = {
|
|
|
13
14
|
|
|
14
15
|
export const TwintagForm = forwardRef<HTMLFormElement, Props>(
|
|
15
16
|
({ defaultValues, resetOnSubmit, children }, fowardedRef) => {
|
|
17
|
+
const { resetValues } = useTwintagForm();
|
|
18
|
+
|
|
16
19
|
const ref = useRef<HTMLFormElement>(null);
|
|
17
20
|
|
|
18
21
|
const form = useForm({
|
|
@@ -24,15 +27,10 @@ export const TwintagForm = forwardRef<HTMLFormElement, Props>(
|
|
|
24
27
|
useEffect(() => {
|
|
25
28
|
if (form.formState.isSubmitSuccessful && resetOnSubmit) {
|
|
26
29
|
if (defaultValues) {
|
|
27
|
-
form.reset(
|
|
28
|
-
Object.keys(defaultValues).reduce(
|
|
29
|
-
(acc, key) => ({ ...acc, [key]: '' }),
|
|
30
|
-
{},
|
|
31
|
-
),
|
|
32
|
-
);
|
|
30
|
+
form.reset(resetValues || defaultValues);
|
|
33
31
|
}
|
|
34
32
|
}
|
|
35
|
-
}, [form.formState]);
|
|
33
|
+
}, [form.formState, defaultValues]);
|
|
36
34
|
|
|
37
35
|
const onValid = (data: FieldValues) => {
|
|
38
36
|
const eventWithValidData = new CustomEvent('twintag-form-valid', {
|
|
@@ -0,0 +1,163 @@
|
|
|
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
|
+
});
|
|
20
|
+
|
|
21
|
+
export const useTwintagForm = () => {
|
|
22
|
+
const context = useContext(twintagFormContext);
|
|
23
|
+
|
|
24
|
+
if (!context) {
|
|
25
|
+
throw new Error('useTwintagForm must be used within a TwintagFormProvider');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return context;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type Components = TwintagFormContext['components'];
|
|
32
|
+
type Inputs = TwintagFormContext['inputs'];
|
|
33
|
+
|
|
34
|
+
const format = (component: RawJsonComponent, components: Components) => {
|
|
35
|
+
const copy = { ...component };
|
|
36
|
+
let formattedConfig: ParsedJsonComponent;
|
|
37
|
+
|
|
38
|
+
if (copy.props) {
|
|
39
|
+
formattedConfig = { ...copy.props } as ParsedJsonComponent;
|
|
40
|
+
} else {
|
|
41
|
+
formattedConfig = { ...copy } as unknown as ParsedJsonComponent;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!copy.type) return Object.create(null) as ParsedJsonComponent;
|
|
45
|
+
|
|
46
|
+
const [type, subtype = undefined] = copy.type.split(':');
|
|
47
|
+
formattedConfig.id = `${type}-${uuid()}`;
|
|
48
|
+
|
|
49
|
+
const comp = components.get(type);
|
|
50
|
+
|
|
51
|
+
if (comp) {
|
|
52
|
+
formattedConfig.comp = comp;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
formattedConfig.type = type;
|
|
56
|
+
|
|
57
|
+
if (subtype) {
|
|
58
|
+
formattedConfig.subtype = subtype;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (copy.props) {
|
|
62
|
+
if (copy.props.subtype) {
|
|
63
|
+
formattedConfig.subtype = copy.props.subtype;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (copy.props.label) {
|
|
67
|
+
const labelParsed = LabelToString.safeParse(copy.props.label);
|
|
68
|
+
formattedConfig.label = labelParsed.success
|
|
69
|
+
? labelParsed.data
|
|
70
|
+
: undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (copy.children && Array.isArray(copy.children)) {
|
|
75
|
+
formattedConfig.items = copy.children.map((item) =>
|
|
76
|
+
format(item, components),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return formattedConfig;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const getResetValues = (
|
|
84
|
+
config: ParsedJsonComponent | ParsedJsonComponent[],
|
|
85
|
+
inputs: Inputs,
|
|
86
|
+
) => {
|
|
87
|
+
const stack = Array.isArray(config) ? [...config] : [config];
|
|
88
|
+
|
|
89
|
+
const resetValues = new Map<string, unknown>();
|
|
90
|
+
|
|
91
|
+
while (stack.length > 0) {
|
|
92
|
+
const currentObj = stack.pop();
|
|
93
|
+
|
|
94
|
+
if (!currentObj) continue;
|
|
95
|
+
|
|
96
|
+
for (const [key, value] of Object.entries(currentObj)) {
|
|
97
|
+
if (Array.isArray(value)) {
|
|
98
|
+
for (const item of value) {
|
|
99
|
+
stack.push(item);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (typeof value === 'object' && value !== null) {
|
|
104
|
+
stack.push(value as unknown as ParsedJsonComponent);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (
|
|
108
|
+
key === 'subtype' &&
|
|
109
|
+
'bind' in currentObj &&
|
|
110
|
+
typeof currentObj.bind === 'string'
|
|
111
|
+
) {
|
|
112
|
+
resetValues.set(
|
|
113
|
+
currentObj.bind,
|
|
114
|
+
inputs.get(value as string)?.defaultValue,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return Object.fromEntries(resetValues);
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const TwintagFormProvider = ({
|
|
124
|
+
variant,
|
|
125
|
+
size,
|
|
126
|
+
inputs,
|
|
127
|
+
components,
|
|
128
|
+
config,
|
|
129
|
+
children,
|
|
130
|
+
}: {
|
|
131
|
+
config: RawJsonComponent | RawJsonComponent[];
|
|
132
|
+
variant: TwintagFormContext['variant'];
|
|
133
|
+
size: TwintagFormContext['size'];
|
|
134
|
+
inputs: Inputs;
|
|
135
|
+
components: Components;
|
|
136
|
+
} & WithChildren) => {
|
|
137
|
+
const __config = useMemo(() => {
|
|
138
|
+
if (Array.isArray(config)) {
|
|
139
|
+
return config.map((item) => format(item, components));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return format(config, components);
|
|
143
|
+
}, [config, components]);
|
|
144
|
+
|
|
145
|
+
const resetValues = useMemo(() => {
|
|
146
|
+
return getResetValues(__config, inputs);
|
|
147
|
+
}, [__config, inputs]);
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<twintagFormContext.Provider
|
|
151
|
+
value={{
|
|
152
|
+
config: __config,
|
|
153
|
+
variant,
|
|
154
|
+
size,
|
|
155
|
+
inputs,
|
|
156
|
+
components,
|
|
157
|
+
resetValues,
|
|
158
|
+
}}
|
|
159
|
+
>
|
|
160
|
+
{children}
|
|
161
|
+
</twintagFormContext.Provider>
|
|
162
|
+
);
|
|
163
|
+
};
|
|
@@ -2,12 +2,13 @@ import type { FC } from 'react';
|
|
|
2
2
|
import type { ParsedJsonComponent } from '@twintag/twintag-form/types';
|
|
3
3
|
|
|
4
4
|
import { Containers } from '@twintag/twintag-form/components/config';
|
|
5
|
+
import { useTwintagForm } from '@twintag/twintag-form/components/provider';
|
|
5
6
|
|
|
6
7
|
type Props = {
|
|
7
8
|
config: ParsedJsonComponent;
|
|
8
9
|
};
|
|
9
10
|
|
|
10
|
-
|
|
11
|
+
const RecursiveRenderer: FC<Props> = ({ config }) => {
|
|
11
12
|
const { id, type, items, comp, ...rest } = config;
|
|
12
13
|
|
|
13
14
|
if (!type || !comp) return <></>;
|
|
@@ -19,7 +20,7 @@ export const Renderer: FC<Props> = ({ config }) => {
|
|
|
19
20
|
return (
|
|
20
21
|
<Component key={id} items={items} {...rest}>
|
|
21
22
|
{items.map((item) => (
|
|
22
|
-
<
|
|
23
|
+
<RecursiveRenderer key={item.id} config={{ ...item }} />
|
|
23
24
|
))}
|
|
24
25
|
</Component>
|
|
25
26
|
);
|
|
@@ -30,3 +31,19 @@ export const Renderer: FC<Props> = ({ config }) => {
|
|
|
30
31
|
|
|
31
32
|
return <Component key={id} {...rest} />;
|
|
32
33
|
};
|
|
34
|
+
|
|
35
|
+
export const JsonRenderer = () => {
|
|
36
|
+
const { config } = useTwintagForm();
|
|
37
|
+
|
|
38
|
+
if (Array.isArray(config)) {
|
|
39
|
+
return (
|
|
40
|
+
<>
|
|
41
|
+
{config.map((item) => (
|
|
42
|
+
<RecursiveRenderer key={item.id} config={{ ...item }} />
|
|
43
|
+
))}
|
|
44
|
+
</>
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return <RecursiveRenderer config={config} />;
|
|
49
|
+
};
|
package/lib/index.tsx
CHANGED
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import type { ComponentProps } from 'react';
|
|
2
|
-
import type { RawJsonComponent } from '@twintag/twintag-form/types';
|
|
3
|
-
|
|
4
|
-
import { forwardRef } from 'react';
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
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
|
-
}>;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export const Form = forwardRef<HTMLFormElement, Props>(
|
|
22
|
-
({ config, options }, ref) => {
|
|
23
|
-
return (
|
|
24
|
-
<TwintagFormProvider
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
{
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
</TwintagForm>
|
|
38
|
-
</TwintagFormProvider>
|
|
39
|
-
);
|
|
40
|
-
},
|
|
41
|
-
);
|
|
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
|
+
}>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export const Form = forwardRef<HTMLFormElement, Props>(
|
|
22
|
+
({ config, options }, ref) => {
|
|
23
|
+
return (
|
|
24
|
+
<TwintagFormProvider
|
|
25
|
+
inputs={Inputs}
|
|
26
|
+
components={components}
|
|
27
|
+
variant={options?.variant}
|
|
28
|
+
size={options?.size}
|
|
29
|
+
config={config}
|
|
30
|
+
>
|
|
31
|
+
<TwintagForm
|
|
32
|
+
ref={ref}
|
|
33
|
+
defaultValues={options?.defaultValues}
|
|
34
|
+
resetOnSubmit={options?.resetOnSubmit}
|
|
35
|
+
>
|
|
36
|
+
<JsonRenderer />
|
|
37
|
+
</TwintagForm>
|
|
38
|
+
</TwintagFormProvider>
|
|
39
|
+
);
|
|
40
|
+
},
|
|
41
|
+
);
|
package/lib/types/index.ts
CHANGED
|
@@ -1,40 +1,80 @@
|
|
|
1
1
|
import type { ElementType } from 'react';
|
|
2
2
|
|
|
3
|
-
export type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
components: Map<string, ElementType>;
|
|
3
|
+
export type InputCompConfig = {
|
|
4
|
+
defaultValue?: unknown;
|
|
5
|
+
component: ElementType;
|
|
7
6
|
};
|
|
8
7
|
|
|
9
|
-
type
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
description: string;
|
|
16
|
-
placeholder: string;
|
|
17
|
-
error: string;
|
|
18
|
-
optionalField: boolean;
|
|
19
|
-
}>;
|
|
20
|
-
}>;
|
|
8
|
+
type CompType = 'input' | 'item' | 'accordion' | 'category';
|
|
9
|
+
type StringLiteral<T> = T extends string
|
|
10
|
+
? string extends T
|
|
11
|
+
? never
|
|
12
|
+
: T
|
|
13
|
+
: never;
|
|
21
14
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
15
|
+
type RawProps<
|
|
16
|
+
TType extends string = string,
|
|
17
|
+
TSubtype extends string = string,
|
|
18
|
+
> = TType extends 'input' | 'item'
|
|
19
|
+
? {
|
|
20
|
+
type: TType;
|
|
21
|
+
bind: string;
|
|
22
|
+
label?: string;
|
|
23
|
+
subtype?: string;
|
|
24
|
+
options?: Partial<{
|
|
25
|
+
description: string;
|
|
26
|
+
placeholder: string;
|
|
27
|
+
error: string;
|
|
28
|
+
optionalField: boolean;
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
31
|
+
: TType extends 'accordion'
|
|
32
|
+
?
|
|
33
|
+
TSubtype extends 'item'
|
|
34
|
+
? {
|
|
35
|
+
type: TType;
|
|
36
|
+
subtype: TSubtype;
|
|
37
|
+
value?: string;
|
|
38
|
+
label: string;
|
|
39
|
+
icon: string;
|
|
40
|
+
}
|
|
41
|
+
:TSubtype extends 'multiple' | 'single'
|
|
42
|
+
? { type: TType; subtype: TSubtype; defaultValue?: string }
|
|
43
|
+
: never
|
|
44
|
+
: {
|
|
45
|
+
type: TType;
|
|
46
|
+
label?: string;
|
|
47
|
+
subtype?: string;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type RawJsonComponent =
|
|
51
|
+
CompType extends infer T ? T extends CompType ? {
|
|
52
|
+
type: StringLiteral<T>;
|
|
53
|
+
props?: Omit<RawProps<StringLiteral<T>>, 'type'>;
|
|
25
54
|
children?: RawJsonComponent[];
|
|
26
|
-
}
|
|
27
|
-
|
|
55
|
+
} : never : never
|
|
56
|
+
|
|
57
|
+
export type ParsedJsonComponent<TType extends string = string> =
|
|
58
|
+
RawProps<TType> & {
|
|
59
|
+
id: string;
|
|
60
|
+
type: string;
|
|
61
|
+
subtype?: string;
|
|
62
|
+
comp?: ElementType;
|
|
63
|
+
items?: ParsedJsonComponent[];
|
|
28
64
|
};
|
|
29
65
|
|
|
30
|
-
export type
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
66
|
+
export type Props<
|
|
67
|
+
TType extends string = string,
|
|
68
|
+
TSubtype extends string = string,
|
|
69
|
+
> = Omit<RawProps<TType, TSubtype>, 'type'> & {
|
|
70
|
+
items?: (Props<'input'> & ParsedJsonComponent)[];
|
|
35
71
|
};
|
|
36
72
|
|
|
37
|
-
export type
|
|
38
|
-
|
|
39
|
-
|
|
73
|
+
export type TwintagFormContext = {
|
|
74
|
+
variant?: 'default' | 'beta';
|
|
75
|
+
size?: 'base' | 'sm';
|
|
76
|
+
inputs: Map<string, InputCompConfig>;
|
|
77
|
+
components: Map<string, ElementType>;
|
|
78
|
+
config: ParsedJsonComponent[] | ParsedJsonComponent;
|
|
79
|
+
resetValues: Record<string, unknown>;
|
|
40
80
|
};
|
package/lib/utils/data.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { Props } from '@twintag/twintag-form/types';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
type Items = (Props<'input'> & {
|
|
4
|
+
items?: Items[];
|
|
5
|
+
})[];
|
|
6
|
+
export const getBindSetFromItems = (items: Items): Set<string> => {
|
|
4
7
|
const binds = new Set<string>();
|
|
5
8
|
|
|
6
9
|
items.forEach((item) => {
|
|
@@ -9,7 +12,7 @@ export const getBindSetFromItems = (items: Props[]): Set<string> => {
|
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
if (item.items) {
|
|
12
|
-
const subBinds = getBindSetFromItems(item.items);
|
|
15
|
+
const subBinds = getBindSetFromItems(item.items as unknown as Items);
|
|
13
16
|
subBinds.forEach((bind) => binds.add(bind));
|
|
14
17
|
}
|
|
15
18
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twintag/twintag-form",
|
|
3
|
-
"version": "1.4.0-beta.
|
|
3
|
+
"version": "1.4.0-beta.2",
|
|
4
4
|
"author": "Twintag",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"@radix-ui/react-radio-group": "^1.1.3",
|
|
25
25
|
"@radix-ui/react-scroll-area": "^1.0.5",
|
|
26
26
|
"@radix-ui/react-select": "^2.0.0",
|
|
27
|
+
"@radix-ui/react-slider": "^1.1.2",
|
|
27
28
|
"@radix-ui/react-slot": "^1.0.2",
|
|
28
29
|
"@radix-ui/react-switch": "^1.0.2",
|
|
29
30
|
"@storybook/react": "7.0.2",
|
|
@@ -42,17 +43,14 @@
|
|
|
42
43
|
"uuid": "^9.0.1"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
|
-
"@rollup/plugin-typescript": "^11.1.1",
|
|
46
|
-
"@storybook/addon-actions": "^7.0.20",
|
|
47
46
|
"@twind/with-react": "^1.1.3",
|
|
48
47
|
"@types/uuid": "^9.0.7",
|
|
49
|
-
"@vitejs/plugin-react-swc": "^3.
|
|
50
|
-
"rollup": "^3.18.0",
|
|
48
|
+
"@vitejs/plugin-react-swc": "^3.6.0",
|
|
51
49
|
"tailwindcss": "^3.3.6",
|
|
52
|
-
"vite": "^
|
|
53
|
-
"vite-plugin-dts": "^3.
|
|
54
|
-
"twintag-types": "0.2.0",
|
|
50
|
+
"vite": "^5.2.11",
|
|
51
|
+
"vite-plugin-dts": "^3.9.1",
|
|
55
52
|
"tsconfig": "0.0.0",
|
|
53
|
+
"twintag-types": "0.2.0",
|
|
56
54
|
"eslint-config-custom": "1.0.0"
|
|
57
55
|
},
|
|
58
56
|
"publishConfig": {
|
package/dist/provider.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { JSX } from 'react/jsx-runtime';
|
|
2
|
-
import { Context } from 'react';
|
|
3
|
-
/// <reference types="react" />
|
|
4
|
-
import type { TwintagFormContext } from './types';
|
|
5
|
-
import type { WithChildren } from 'twintag-types/dom';
|
|
6
|
-
export declare const twintagFormContext: Context<TwintagFormContext>;
|
|
7
|
-
export declare const useTwintagForm: () => TwintagFormContext;
|
|
8
|
-
export declare const TwintagFormProvider: ({ children, variant, size, components, }: TwintagFormContext & WithChildren) => JSX.Element;
|