@twintag/twintag-form 1.4.0-beta.2 → 1.4.0

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.
@@ -14262,7 +14262,11 @@ const pc = gt({
14262
14262
  defaultVariants: {
14263
14263
  variant: "default"
14264
14264
  }
14265
- }), wc = ({ type: e, defaultValue: t, children: r }) => {
14265
+ }), wc = ({
14266
+ type: e,
14267
+ defaultValue: t,
14268
+ children: r
14269
+ }) => {
14266
14270
  const { variant: n } = it();
14267
14271
  return e === "single" ? /* @__PURE__ */ x.jsx(Us, { type: "single", defaultValue: t, collapsible: !0, children: /* @__PURE__ */ x.jsx("div", { className: qs({ variant: n }), children: r }) }) : /* @__PURE__ */ x.jsx(Us, { type: "multiple", children: /* @__PURE__ */ x.jsx("div", { className: qs({ variant: n }), children: r }) });
14268
14272
  };
@@ -14314,7 +14318,7 @@ const Xy = De({
14314
14318
  return /* @__PURE__ */ x.jsxs(
14315
14319
  ac,
14316
14320
  {
14317
- value: t ?? D8(e),
14321
+ value: t || D8(e),
14318
14322
  className: c(),
14319
14323
  ...a,
14320
14324
  children: [
@@ -31,7 +31,11 @@ const accordionRootVariants = tv({
31
31
  variant: 'default',
32
32
  },
33
33
  });
34
- const AccordionRoot: FC<AccordionRootProps> = ({ type, defaultValue, children }) => {
34
+ const AccordionRoot: FC<AccordionRootProps> = ({
35
+ type,
36
+ defaultValue,
37
+ children,
38
+ }) => {
35
39
  const { variant } = useTwintagForm();
36
40
 
37
41
  if (type === 'single') {
@@ -104,7 +108,7 @@ const AccordionItem: FC<AccordionItemProps> = ({
104
108
 
105
109
  return (
106
110
  <AccordionItemPrimitive
107
- value={value ?? camelize(label)}
111
+ value={value ? value : camelize(label)}
108
112
  className={item()}
109
113
  {...rest}
110
114
  >
@@ -1,163 +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
- };
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
+ };
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 { 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
+ }>;
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
+ );
@@ -29,17 +29,16 @@ type RawProps<
29
29
  }>;
30
30
  }
31
31
  : TType extends 'accordion'
32
- ?
33
- TSubtype extends 'item'
34
- ? {
32
+ ? TSubtype extends 'item'
33
+ ? {
35
34
  type: TType;
36
35
  subtype: TSubtype;
37
36
  value?: string;
38
37
  label: string;
39
38
  icon: string;
40
39
  }
41
- :TSubtype extends 'multiple' | 'single'
42
- ? { type: TType; subtype: TSubtype; defaultValue?: string }
40
+ : TSubtype extends 'multiple' | 'single'
41
+ ? { type: TType; subtype: TSubtype; defaultValue?: string }
43
42
  : never
44
43
  : {
45
44
  type: TType;
@@ -47,12 +46,15 @@ type RawProps<
47
46
  subtype?: string;
48
47
  };
49
48
 
50
- export type RawJsonComponent =
51
- CompType extends infer T ? T extends CompType ? {
52
- type: StringLiteral<T>;
53
- props?: Omit<RawProps<StringLiteral<T>>, 'type'>;
54
- children?: RawJsonComponent[];
55
- } : never : never
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;
56
58
 
57
59
  export type ParsedJsonComponent<TType extends string = string> =
58
60
  RawProps<TType> & {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@twintag/twintag-form",
3
- "version": "1.4.0-beta.2",
3
+ "version": "1.4.0",
4
4
  "author": "Twintag",
5
5
  "type": "module",
6
6
  "exports": {
@@ -49,9 +49,9 @@
49
49
  "tailwindcss": "^3.3.6",
50
50
  "vite": "^5.2.11",
51
51
  "vite-plugin-dts": "^3.9.1",
52
+ "eslint-config-custom": "1.0.0",
52
53
  "tsconfig": "0.0.0",
53
- "twintag-types": "0.2.0",
54
- "eslint-config-custom": "1.0.0"
54
+ "twintag-types": "0.2.0"
55
55
  },
56
56
  "publishConfig": {
57
57
  "access": "public"