@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.
Files changed (60) hide show
  1. package/.turbo/turbo-build.log +6 -6
  2. package/CHANGELOG.md +12 -0
  3. package/README.md +121 -62
  4. package/dev/main.tsx +453 -209
  5. package/dist/common/button.d.ts +3 -2
  6. package/dist/common/calendar.d.ts +4 -4
  7. package/dist/common/form.d.ts +11 -11
  8. package/dist/common/icon.d.ts +2 -1
  9. package/dist/common/input.d.ts +3 -2
  10. package/dist/common/label.d.ts +3 -2
  11. package/dist/common/slider.d.ts +4 -0
  12. package/dist/common/textarea.d.ts +3 -2
  13. package/dist/components/config.d.ts +4 -2
  14. package/dist/components/containers/accordion.d.ts +5 -6
  15. package/dist/components/containers/category.d.ts +3 -2
  16. package/dist/components/fields/checkbox.d.ts +5 -3
  17. package/dist/components/fields/date-picker.d.ts +4 -2
  18. package/dist/components/fields/file.d.ts +5 -3
  19. package/dist/components/fields/input.d.ts +5 -3
  20. package/dist/components/fields/radiogroup.d.ts +7 -3
  21. package/dist/components/fields/submit.d.ts +3 -2
  22. package/dist/components/fields/textarea.d.ts +5 -3
  23. package/dist/components/fields/toggle.d.ts +5 -3
  24. package/dist/components/fields/tri-toggle.d.ts +5 -0
  25. package/dist/components/form.d.ts +2 -2
  26. package/dist/components/provider.d.ts +21 -0
  27. package/dist/components/renderer.d.ts +1 -0
  28. package/dist/index.d.ts +4 -3
  29. package/dist/twintag-form.react.js +12880 -12304
  30. package/dist/types/index.d.ts +43 -20
  31. package/dist/utils/data.d.ts +7 -2
  32. package/dist/utils/style.d.ts +2 -1
  33. package/index.html +6 -3
  34. package/lib/common/slider.tsx +38 -0
  35. package/lib/components/config.tsx +46 -17
  36. package/lib/components/containers/accordion.tsx +33 -18
  37. package/lib/components/containers/category.tsx +1 -1
  38. package/lib/components/fields/checkbox.tsx +5 -3
  39. package/lib/components/fields/date-picker.tsx +9 -2
  40. package/lib/components/fields/file.tsx +5 -4
  41. package/lib/components/fields/input.tsx +5 -3
  42. package/lib/components/fields/radiogroup.tsx +9 -3
  43. package/lib/components/fields/submit.tsx +1 -1
  44. package/lib/components/fields/textarea.tsx +5 -3
  45. package/lib/components/fields/toggle.tsx +5 -3
  46. package/lib/components/fields/tri-toggle.tsx +141 -0
  47. package/lib/components/form.tsx +5 -7
  48. package/lib/components/provider.tsx +163 -0
  49. package/lib/components/{Render/index.tsx → renderer.tsx} +19 -2
  50. package/lib/index.tsx +41 -41
  51. package/lib/types/index.ts +69 -29
  52. package/lib/utils/data.ts +5 -2
  53. package/package.json +6 -8
  54. package/dist/components/Render/index.d.ts +0 -7
  55. package/dist/components/Render/useJson.d.ts +0 -2
  56. package/dist/components/index.d.ts +0 -7
  57. package/dist/provider.d.ts +0 -8
  58. package/lib/components/Render/useJson.ts +0 -76
  59. package/lib/components/index.tsx +0 -24
  60. package/lib/provider.tsx +0 -39
@@ -1,76 +0,0 @@
1
- import type {
2
- ParsedJsonComponent,
3
- RawJsonComponent,
4
- TwintagFormContext,
5
- } from '@twintag/twintag-form/types';
6
-
7
- import { useMemo } from 'react';
8
- import { LabelToString } from 'twintag-types/dom';
9
- import { v4 as uuid } from 'uuid';
10
-
11
- import { useTwintagForm } from '@twintag/twintag-form/provider';
12
-
13
- type Components = TwintagFormContext['components'];
14
-
15
- const format = (component: RawJsonComponent, components: Components) => {
16
- const copy = { ...component };
17
- let formattedConfig: ParsedJsonComponent;
18
-
19
- if (copy.props) {
20
- formattedConfig = { ...copy.props } as ParsedJsonComponent;
21
- } else {
22
- formattedConfig = { ...copy } as unknown as ParsedJsonComponent;
23
- }
24
-
25
- if (!copy.type) return Object.create(null) as ParsedJsonComponent;
26
-
27
- const [type, subtype = undefined] = copy.type.split(':');
28
- formattedConfig.id = `${type}-${uuid()}`;
29
-
30
- const comp = components.get(type);
31
-
32
- if (comp) {
33
- formattedConfig.comp = comp;
34
- }
35
-
36
- formattedConfig.type = type;
37
-
38
- if (subtype) {
39
- formattedConfig.subtype = subtype;
40
- }
41
-
42
- if (copy.props) {
43
- if (copy.props.subtype) {
44
- formattedConfig.subtype = copy.props.subtype;
45
- }
46
-
47
- if (copy.props.label) {
48
- const labelParsed = LabelToString.safeParse(copy.props.label);
49
- formattedConfig.label = labelParsed.success
50
- ? labelParsed.data
51
- : undefined;
52
- }
53
- }
54
-
55
- if (copy.children && Array.isArray(copy.children)) {
56
- formattedConfig.items = copy.children.map((item) =>
57
- format(item, components),
58
- );
59
- }
60
-
61
- return formattedConfig;
62
- };
63
-
64
- export const useJson = (config: RawJsonComponent | RawJsonComponent[]) => {
65
- const { components } = useTwintagForm();
66
-
67
- const __config = useMemo(() => {
68
- if (Array.isArray(config)) {
69
- return config.map((item) => format(item, components));
70
- }
71
-
72
- return format(config, components);
73
- }, [config, components]);
74
-
75
- return __config;
76
- };
@@ -1,24 +0,0 @@
1
- import type { RawJsonComponent } from '@twintag/twintag-form/types';
2
-
3
- import { Renderer } from '@twintag/twintag-form/components/Render';
4
- import { useJson } from '@twintag/twintag-form/components/Render/useJson';
5
-
6
- type Props = {
7
- config: RawJsonComponent | RawJsonComponent[];
8
- };
9
-
10
- export const TwintagJsonRenderer = ({ config }: Props) => {
11
- const __config = useJson(config);
12
-
13
- if (Array.isArray(__config)) {
14
- return (
15
- <>
16
- {__config.map((item, idx) => (
17
- <Renderer key={idx} config={item} />
18
- ))}
19
- </>
20
- );
21
- }
22
-
23
- return <Renderer config={__config} />;
24
- };
package/lib/provider.tsx DELETED
@@ -1,39 +0,0 @@
1
- import type { TwintagFormContext } from '@twintag/twintag-form/types';
2
- import type { WithChildren } from 'twintag-types/dom';
3
-
4
- import { createContext, useContext } from 'react';
5
-
6
- export const twintagFormContext = createContext<TwintagFormContext>({
7
- variant: 'default',
8
- size: 'base',
9
- components: new Map(),
10
- });
11
-
12
- export const useTwintagForm = () => {
13
- const context = useContext(twintagFormContext);
14
-
15
- if (!context) {
16
- throw new Error('useTwintagForm must be used within a TwintagFormProvider');
17
- }
18
-
19
- return context;
20
- };
21
-
22
- export const TwintagFormProvider = ({
23
- children,
24
- variant,
25
- size,
26
- components,
27
- }: TwintagFormContext & WithChildren) => {
28
- return (
29
- <twintagFormContext.Provider
30
- value={{
31
- variant,
32
- size,
33
- components,
34
- }}
35
- >
36
- {children}
37
- </twintagFormContext.Provider>
38
- );
39
- };