@twintag/twintag-form 1.6.1 → 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.
@@ -1,163 +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
- });
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
- };
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,41 +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
- }>;
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
- );
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,82 +1,102 @@
1
- import type { ElementType } from 'react';
2
-
3
- export type InputCompConfig = {
4
- defaultValue?: unknown;
5
- component: ElementType;
6
- };
7
-
8
- type CompType = 'input' | 'item' | 'accordion' | 'category';
9
- type StringLiteral<T> = T extends string
10
- ? string extends T
11
- ? never
12
- : T
13
- : never;
14
-
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
- ? TSubtype extends 'item'
33
- ? {
34
- type: TType;
35
- subtype: TSubtype;
36
- value?: string;
37
- label: string;
38
- icon: string;
39
- }
40
- : TSubtype extends 'multiple' | 'single'
41
- ? { type: TType; subtype: TSubtype; defaultValue?: string }
42
- : never
43
- : {
44
- type: TType;
45
- label?: string;
46
- subtype?: string;
47
- };
48
-
49
- export type RawJsonComponent = CompType extends infer T
50
- ? T extends CompType
51
- ? {
52
- type: StringLiteral<T>;
53
- props?: Omit<RawProps<StringLiteral<T>>, 'type'>;
54
- children?: RawJsonComponent[];
55
- }
56
- : never
57
- : never;
58
-
59
- export type ParsedJsonComponent<TType extends string = string> =
60
- RawProps<TType> & {
61
- id: string;
62
- type: string;
63
- subtype?: string;
64
- comp?: ElementType;
65
- items?: ParsedJsonComponent[];
66
- };
67
-
68
- export type Props<
69
- TType extends string = string,
70
- TSubtype extends string = string,
71
- > = Omit<RawProps<TType, TSubtype>, 'type'> & {
72
- items?: (Props<'input'> & ParsedJsonComponent)[];
73
- };
74
-
75
- export type TwintagFormContext = {
76
- variant?: 'default' | 'outline';
77
- size?: 'base' | 'sm';
78
- inputs: Map<string, InputCompConfig>;
79
- components: Map<string, ElementType>;
80
- config: ParsedJsonComponent[] | ParsedJsonComponent;
81
- resetValues: Record<string, unknown>;
82
- };
1
+ import type { ElementType } from 'react';
2
+
3
+ export type InputCompConfig = {
4
+ defaultValue?: unknown;
5
+ component: ElementType;
6
+ };
7
+
8
+ type BaseInputOptions = Partial<{
9
+ description: string;
10
+ error: string;
11
+ optionalField: boolean;
12
+ }>;
13
+
14
+ type TextInputOptions = BaseInputOptions & {
15
+ placeholder?: string;
16
+ };
17
+
18
+ type DatePickerInputOptions = BaseInputOptions & {
19
+ placeholder?: string;
20
+ };
21
+
22
+ type FileInputOptions = BaseInputOptions & {
23
+ accept?: string;
24
+ };
25
+
26
+ type CompType = 'input' | 'item' | 'accordion' | 'category';
27
+ type StringLiteral<T> = T extends string
28
+ ? string extends T
29
+ ? never
30
+ : T
31
+ : never;
32
+
33
+ type RawProps<
34
+ TType extends string = string,
35
+ TSubtype extends string = string,
36
+ > = TType extends 'input' | 'item'
37
+ ? {
38
+ type: TType;
39
+ bind: string;
40
+ label?: string;
41
+ subtype?: string;
42
+ options?: TSubtype extends 'text' | 'textarea'
43
+ ? TextInputOptions
44
+ : TSubtype extends 'datepicker'
45
+ ? DatePickerInputOptions
46
+ : TSubtype extends 'file'
47
+ ? FileInputOptions
48
+ : BaseInputOptions;
49
+ }
50
+ : TType extends 'accordion'
51
+ ? TSubtype extends 'item'
52
+ ? {
53
+ type: TType;
54
+ subtype: TSubtype;
55
+ value?: string;
56
+ label: string;
57
+ icon: string;
58
+ }
59
+ : TSubtype extends 'multiple' | 'single'
60
+ ? { type: TType; subtype: TSubtype; defaultValue?: string }
61
+ : never
62
+ : {
63
+ type: TType;
64
+ label?: string;
65
+ subtype?: string;
66
+ };
67
+
68
+ export type RawJsonComponent = CompType extends infer T
69
+ ? T extends CompType
70
+ ? {
71
+ type: StringLiteral<T>;
72
+ props?: Omit<RawProps<StringLiteral<T>>, 'type'>;
73
+ children?: RawJsonComponent[];
74
+ }
75
+ : never
76
+ : never;
77
+
78
+ export type ParsedJsonComponent<TType extends string = string> =
79
+ RawProps<TType> & {
80
+ id: string;
81
+ type: string;
82
+ subtype?: string;
83
+ comp?: ElementType;
84
+ items?: ParsedJsonComponent[];
85
+ };
86
+
87
+ export type Props<
88
+ TType extends string = string,
89
+ TSubtype extends string = string,
90
+ > = Omit<RawProps<TType, TSubtype>, 'type'> & {
91
+ items?: (Props<'input'> & ParsedJsonComponent)[];
92
+ };
93
+
94
+ export type TwintagFormContext = {
95
+ variant?: 'default' | 'outline';
96
+ size?: 'base' | 'sm';
97
+ inputs: Map<string, InputCompConfig>;
98
+ components: Map<string, ElementType>;
99
+ config: ParsedJsonComponent[] | ParsedJsonComponent;
100
+ resetValues: Record<string, unknown>;
101
+ acceptFileType?: string;
102
+ };
@@ -1,13 +1,13 @@
1
1
  import r2wc from '@r2wc/react-to-web-component';
2
2
  import install from '@twind/with-web-components';
3
3
 
4
- import { Form } from '@twintag/twintag-form/index';
4
+ import { Form } from './index';
5
5
 
6
6
  import config from '../twind.config';
7
7
 
8
8
  const withTwind = install(config);
9
9
 
10
- const TwintagFormElement = r2wc(Form, {
10
+ export const TwintagFormElement = r2wc(Form, {
11
11
  props: {
12
12
  config: 'json',
13
13
  options: 'json',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twintag/twintag-form",
3
- "version": "1.6.1",
3
+ "version": "1.6.2-beta.1",
4
4
  "author": "Twintag",
5
5
  "type": "module",
6
6
  "exports": {
package/vite.config.ts CHANGED
@@ -1,11 +1,12 @@
1
1
  import react from '@vitejs/plugin-react-swc';
2
2
  import { dirname, resolve } from 'pathe';
3
+ import { fileURLToPath } from 'node:url'
3
4
  import { defineConfig } from 'vite';
4
5
  import dts from 'vite-plugin-dts';
5
6
 
6
7
  import { dependencies, devDependencies } from './package.json';
7
8
 
8
- export const __dirname = dirname(new URL(import.meta.url).pathname);
9
+ export const __dirname = dirname(fileURLToPath(import.meta.url));
9
10
 
10
11
  const externalPackages = [
11
12
  ...Object.keys(dependencies || {}),