@twintag/twintag-form 1.4.0-beta.0 → 1.4.0-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 (59) hide show
  1. package/.turbo/turbo-build.log +6 -6
  2. package/CHANGELOG.md +6 -0
  3. package/README.md +121 -62
  4. package/dev/main.tsx +233 -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 +3 -2
  15. package/dist/components/containers/category.d.ts +3 -2
  16. package/dist/components/fields/checkbox.d.ts +4 -2
  17. package/dist/components/fields/date-picker.d.ts +3 -1
  18. package/dist/components/fields/file.d.ts +4 -2
  19. package/dist/components/fields/input.d.ts +4 -2
  20. package/dist/components/fields/radiogroup.d.ts +4 -2
  21. package/dist/components/fields/submit.d.ts +3 -2
  22. package/dist/components/fields/textarea.d.ts +4 -2
  23. package/dist/components/fields/toggle.d.ts +4 -2
  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 +12828 -12261
  30. package/dist/types/index.d.ts +25 -17
  31. package/dist/utils/data.d.ts +2 -1
  32. package/dist/utils/style.d.ts +2 -1
  33. package/index.html +5 -3
  34. package/lib/common/slider.tsx +38 -0
  35. package/lib/components/config.tsx +46 -17
  36. package/lib/components/containers/accordion.tsx +1 -1
  37. package/lib/components/containers/category.tsx +1 -1
  38. package/lib/components/fields/checkbox.tsx +4 -2
  39. package/lib/components/fields/date-picker.tsx +4 -1
  40. package/lib/components/fields/file.tsx +83 -82
  41. package/lib/components/fields/input.tsx +4 -2
  42. package/lib/components/fields/radiogroup.tsx +4 -2
  43. package/lib/components/fields/submit.tsx +1 -1
  44. package/lib/components/fields/textarea.tsx +4 -2
  45. package/lib/components/fields/toggle.tsx +4 -2
  46. package/lib/components/fields/tri-toggle.tsx +141 -0
  47. package/lib/components/form.tsx +5 -7
  48. package/lib/components/provider.tsx +159 -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 +49 -40
  52. package/package.json +7 -9
  53. package/dist/components/Render/index.d.ts +0 -7
  54. package/dist/components/Render/useJson.d.ts +0 -2
  55. package/dist/components/index.d.ts +0 -7
  56. package/dist/provider.d.ts +0 -8
  57. package/lib/components/Render/useJson.ts +0 -76
  58. package/lib/components/index.tsx +0 -24
  59. package/lib/provider.tsx +0 -39
@@ -1,8 +1,20 @@
1
- import type { ElementType } from 'react';
2
- export type TwintagFormContext = {
3
- variant?: 'default' | 'beta';
4
- size?: 'base' | 'sm';
5
- components: Map<string, ElementType>;
1
+ import { ElementType } from 'react';
2
+
3
+ export type ComponentConfig = {
4
+ defaultValue?: unknown;
5
+ component: ElementType;
6
+ };
7
+ export type ParsedJsonComponent = Props & {
8
+ id: string;
9
+ type: string;
10
+ subtype?: string;
11
+ comp?: ElementType;
12
+ };
13
+ export type RawJsonComponent = RawProps & Record<string, unknown> & {
14
+ type: string;
15
+ children?: RawJsonComponent[];
16
+ } & {
17
+ props?: RawProps;
6
18
  };
7
19
  type RawProps = Partial<{
8
20
  bind: string;
@@ -16,20 +28,16 @@ type RawProps = Partial<{
16
28
  optionalField: boolean;
17
29
  }>;
18
30
  }>;
19
- export type RawJsonComponent = RawProps & Record<string, unknown> & {
20
- type: string;
21
- children?: RawJsonComponent[];
22
- } & {
23
- props?: RawProps;
24
- };
25
- export type ParsedJsonComponent = Props & {
26
- id: string;
27
- type: string;
28
- subtype?: string;
29
- comp?: ElementType;
30
- };
31
31
  export type Props = Omit<RawProps, 'subtype'> & {
32
32
  bind: string;
33
33
  items?: ParsedJsonComponent[];
34
34
  };
35
+ export type TwintagFormContext = {
36
+ variant?: 'default' | 'beta';
37
+ size?: 'base' | 'sm';
38
+ inputs: Map<string, ComponentConfig>;
39
+ components: Map<string, ElementType>;
40
+ config: ParsedJsonComponent[] | ParsedJsonComponent;
41
+ resetValues: Record<string, unknown>;
42
+ };
35
43
  export {};
@@ -1,2 +1,3 @@
1
- import type { Props } from '../types';
1
+ import { Props } from '../types';
2
+
2
3
  export declare const getBindSetFromItems: (items: Props[]) => Set<string>;
@@ -1,2 +1,3 @@
1
- import type { ClassValue } from 'clsx';
1
+ import { ClassValue } from 'clsx';
2
+
2
3
  export declare function cn(...inputs: ClassValue[]): string;
package/index.html CHANGED
@@ -8,9 +8,11 @@
8
8
  <body>
9
9
  <div
10
10
  style="
11
- display: grid;
12
- grid-template-columns: repeat(2, 1fr);
13
- padding: 2rem;
11
+ display: flex;
12
+ flex-direction: column;
13
+ align-items: center;
14
+ gap: 2rem;
15
+ padding: 1rem;
14
16
  "
15
17
  >
16
18
  <div>
@@ -0,0 +1,38 @@
1
+ import * as React from 'react';
2
+ import * as SliderPrimitive from '@radix-ui/react-slider';
3
+ import { tv } from 'tailwind-variants';
4
+
5
+ const sliderVariants = tv({
6
+ slots: {
7
+ root: 'relative flex w-full touch-none select-none items-center',
8
+ track: 'relative h-2 w-full grow overflow-hidden rounded-full bg-muted',
9
+ range: 'absolute h-full bg-accent',
10
+ thumb:
11
+ 'block h-5 w-5 rounded-full border-2 border-accent bg-background ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50',
12
+ },
13
+ });
14
+
15
+ const Slider = React.forwardRef<
16
+ React.ElementRef<typeof SliderPrimitive.Root>,
17
+ React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root>
18
+ >(({ className, ...props }, ref) => {
19
+ const { root, track, range, thumb } = sliderVariants();
20
+
21
+ return (
22
+ <SliderPrimitive.Root
23
+ ref={ref}
24
+ className={root({
25
+ className,
26
+ })}
27
+ {...props}
28
+ >
29
+ <SliderPrimitive.Track className={track()}>
30
+ <SliderPrimitive.Range className={range()} />
31
+ </SliderPrimitive.Track>
32
+ <SliderPrimitive.Thumb className={thumb()} />
33
+ </SliderPrimitive.Root>
34
+ );
35
+ });
36
+ Slider.displayName = SliderPrimitive.Root.displayName;
37
+
38
+ export { Slider };
@@ -1,25 +1,54 @@
1
1
  import type { ElementType, FC } from 'react';
2
+ import type { ComponentConfig } from '../types';
2
3
 
3
4
  import { Accordion } from './containers/accordion';
4
5
  import { Category } from './containers/category';
5
- import { Checkbox } from './fields/checkbox';
6
- import { DatePicker } from './fields/date-picker';
7
- import { File } from './fields/file';
8
- import { Input as InputField } from './fields/input';
9
- import { RadioGroup } from './fields/radiogroup';
6
+ import { Checkbox, DEFAULT_CHECKBOX_VALUE } from './fields/checkbox';
7
+ import { DatePicker, DEFAULT_DATE_PICKER_VALUE } from './fields/date-picker';
8
+ import { DEFAULT_FILE_VALUE, File } from './fields/file';
9
+ import { DEFAULT_INPUT_VALUE, Input as InputField } from './fields/input';
10
+ import { DEFAULT_RADIOGROUP_VALUE, RadioGroup } from './fields/radiogroup';
10
11
  import { Submit } from './fields/submit';
11
- import { Textarea } from './fields/textarea';
12
- import { Toggle } from './fields/toggle';
12
+ import { DEFAULT_TEXTAREA_VALUE, Textarea } from './fields/textarea';
13
+ import { DEFAULT_TOGGLE_VALUE, Toggle } from './fields/toggle';
14
+ import { DEFAULT_TRI_TOGGLE_VALUE, TriToggle } from './fields/tri-toggle';
13
15
 
14
- export const Inputs = new Map<string, ElementType>()
15
- .set('simple', InputField)
16
- .set('textarea', Textarea)
17
- .set('toggle', Toggle)
18
- .set('file', File)
19
- .set('submit', Submit)
20
- .set('checkbox', Checkbox)
21
- .set('radiogroup', RadioGroup)
22
- .set('datepicker', DatePicker);
16
+ export const Inputs = new Map<string, ComponentConfig>()
17
+ .set('simple', {
18
+ defaultValue: DEFAULT_INPUT_VALUE,
19
+ component: InputField,
20
+ })
21
+ .set('textarea', {
22
+ defaultValue: DEFAULT_TEXTAREA_VALUE,
23
+ component: Textarea,
24
+ })
25
+ .set('toggle', {
26
+ defaultValue: DEFAULT_TOGGLE_VALUE,
27
+ component: Toggle,
28
+ })
29
+ .set('file', {
30
+ defaultValue: DEFAULT_FILE_VALUE,
31
+ component: File,
32
+ })
33
+ .set('submit', {
34
+ component: Submit,
35
+ })
36
+ .set('checkbox', {
37
+ defaultValue: DEFAULT_CHECKBOX_VALUE,
38
+ component: Checkbox,
39
+ })
40
+ .set('radiogroup', {
41
+ defaultValue: DEFAULT_RADIOGROUP_VALUE,
42
+ component: RadioGroup,
43
+ })
44
+ .set('datepicker', {
45
+ defaultValue: DEFAULT_DATE_PICKER_VALUE,
46
+ component: DatePicker,
47
+ })
48
+ .set('tri-toggle', {
49
+ defaultValue: DEFAULT_TRI_TOGGLE_VALUE,
50
+ component: TriToggle,
51
+ });
23
52
 
24
53
  type InputProps = {
25
54
  subtype?: string;
@@ -27,7 +56,7 @@ type InputProps = {
27
56
  bind?: string;
28
57
  };
29
58
  const Input: FC<InputProps> = ({ subtype = 'simple', ...rest }) => {
30
- const Comp = Inputs.get(subtype);
59
+ const Comp = Inputs.get(subtype)?.component;
31
60
 
32
61
  if (!Comp) return <></>;
33
62
 
@@ -11,7 +11,7 @@ import {
11
11
  AccordionTrigger,
12
12
  } from '@twintag/twintag-form/common/accordion';
13
13
  import { Icon } from '@twintag/twintag-form/common/icon';
14
- import { useTwintagForm } from '@twintag/twintag-form/provider';
14
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
15
15
  import { camelize } from '@twintag/twintag-form/utils/string';
16
16
 
17
17
  type AccordionRootProps = {
@@ -3,7 +3,7 @@ import type { WithChildren } from 'twintag-types/dom';
3
3
 
4
4
  import { tv } from 'tailwind-variants';
5
5
 
6
- import { useTwintagForm } from '@twintag/twintag-form/provider';
6
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
7
7
 
8
8
  const categoryStyle = tv({
9
9
  slots: {
@@ -13,7 +13,9 @@ import {
13
13
  FormLabel,
14
14
  FormMessage,
15
15
  } from '@twintag/twintag-form/common/form';
16
- import { useTwintagForm } from '@twintag/twintag-form/provider';
16
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
17
+
18
+ export const DEFAULT_CHECKBOX_VALUE = false;
17
19
 
18
20
  export const Checkbox: FC<Props> = ({ label, bind, options }) => {
19
21
  const { control } = useFormContext();
@@ -23,9 +25,9 @@ export const Checkbox: FC<Props> = ({ label, bind, options }) => {
23
25
 
24
26
  return (
25
27
  <FormField
28
+ defaultValue={DEFAULT_CHECKBOX_VALUE}
26
29
  control={control}
27
30
  name={bind}
28
- defaultValue={false}
29
31
  rules={{
30
32
  required: isOptional ? false : options?.error ?? true,
31
33
  }}
@@ -21,7 +21,7 @@ import {
21
21
  PopoverContent,
22
22
  PopoverTrigger,
23
23
  } from '@twintag/twintag-form/common/popover';
24
- import { useTwintagForm } from '@twintag/twintag-form/provider';
24
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
25
25
 
26
26
  const datePickerTriggerVariants = tv({
27
27
  slots: {
@@ -63,6 +63,8 @@ const datePickerTriggerVariants = tv({
63
63
  },
64
64
  });
65
65
 
66
+ export const DEFAULT_DATE_PICKER_VALUE = null;
67
+
66
68
  export const DatePicker: React.FC<Props> = ({ label, bind, options }) => {
67
69
  const { variant, size } = useTwintagForm();
68
70
  const { control } = useFormContext();
@@ -73,6 +75,7 @@ export const DatePicker: React.FC<Props> = ({ label, bind, options }) => {
73
75
 
74
76
  return (
75
77
  <FormField
78
+ defaultValue={DEFAULT_DATE_PICKER_VALUE}
76
79
  control={control}
77
80
  name={bind}
78
81
  rules={{
@@ -1,82 +1,83 @@
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 { Input } from '@twintag/twintag-form/common/input';
16
- import { useTwintagForm } from '@twintag/twintag-form/provider';
17
-
18
- export const File: FC<Props> = ({ label, bind, options }) => {
19
- const { control } = useFormContext();
20
- const { variant, size } = useTwintagForm();
21
-
22
- const isOptional = options?.optionalField;
23
-
24
- return (
25
- <FormField
26
- defaultValue=''
27
- control={control}
28
- name={bind}
29
- rules={{
30
- required: isOptional ? false : options?.error ?? true,
31
- }}
32
- render={({ field, formState }) => (
33
- <FormItem variant={variant}>
34
- <div className='space-y-1'>
35
- <FormLabel htmlFor={bind} size={size}>
36
- {label}
37
- {!isOptional ? (
38
- <span className='ml-0.5 inline-block'>
39
- <AsteriskIcon className='h-3 w-3 text-destructive' />
40
- </span>
41
- ) : (
42
- <></>
43
- )}
44
- </FormLabel>
45
- {options?.description ? (
46
- <FormDescription size={size}>
47
- {options.description}
48
- </FormDescription>
49
- ) : (
50
- <></>
51
- )}
52
- </div>
53
- <FormControl>
54
- <Input
55
- ref={(el) => {
56
- field.ref(el);
57
-
58
- if (el && formState.isSubmitSuccessful) {
59
- el.value = '';
60
- }
61
- }}
62
- id={bind}
63
- type='file'
64
- name={field.name}
65
- size={size}
66
- variant={variant}
67
- capture='environment'
68
- multiple
69
- onBlur={field.onBlur}
70
- onChange={(e) => {
71
- if (e.target.files) {
72
- field.onChange(e.target.files);
73
- }
74
- }}
75
- />
76
- </FormControl>
77
- <FormMessage size={size} className='pt-1.5' />
78
- </FormItem>
79
- )}
80
- />
81
- );
82
- };
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 { Input } from '@twintag/twintag-form/common/input';
16
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
17
+
18
+ export const DEFAULT_FILE_VALUE = '';
19
+
20
+ export const File: FC<Props> = ({ label, bind, options }) => {
21
+ const { control } = useFormContext();
22
+ const { variant, size } = useTwintagForm();
23
+
24
+ const isOptional = options?.optionalField;
25
+
26
+ return (
27
+ <FormField
28
+ defaultValue={DEFAULT_FILE_VALUE}
29
+ control={control}
30
+ name={bind}
31
+ rules={{
32
+ required: isOptional ? false : options?.error ?? true,
33
+ }}
34
+ render={({ field, formState }) => (
35
+ <FormItem variant={variant}>
36
+ <div className='space-y-1'>
37
+ <FormLabel htmlFor={bind} size={size}>
38
+ {label}
39
+ {!isOptional ? (
40
+ <span className='ml-0.5 inline-block'>
41
+ <AsteriskIcon className='h-3 w-3 text-destructive' />
42
+ </span>
43
+ ) : (
44
+ <></>
45
+ )}
46
+ </FormLabel>
47
+ {options?.description ? (
48
+ <FormDescription size={size}>
49
+ {options.description}
50
+ </FormDescription>
51
+ ) : (
52
+ <></>
53
+ )}
54
+ </div>
55
+ <FormControl>
56
+ <Input
57
+ ref={(el) => {
58
+ field.ref(el);
59
+
60
+ if (el && formState.isSubmitSuccessful) {
61
+ el.value = '';
62
+ }
63
+ }}
64
+ id={bind}
65
+ type='file'
66
+ name={field.name}
67
+ size={size}
68
+ variant={variant}
69
+ multiple
70
+ onBlur={field.onBlur}
71
+ onChange={(e) => {
72
+ if (e.target.files) {
73
+ field.onChange(e.target.files);
74
+ }
75
+ }}
76
+ />
77
+ </FormControl>
78
+ <FormMessage size={size} className='pt-1.5' />
79
+ </FormItem>
80
+ )}
81
+ />
82
+ );
83
+ };
@@ -13,7 +13,9 @@ import {
13
13
  FormMessage,
14
14
  } from '@twintag/twintag-form/common/form';
15
15
  import { Input as InputPrimitive } from '@twintag/twintag-form/common/input';
16
- import { useTwintagForm } from '@twintag/twintag-form/provider';
16
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
17
+
18
+ export const DEFAULT_INPUT_VALUE = '';
17
19
 
18
20
  export const Input: FC<Props> = ({ label, bind, options }) => {
19
21
  const { control } = useFormContext();
@@ -24,7 +26,7 @@ export const Input: FC<Props> = ({ label, bind, options }) => {
24
26
 
25
27
  return (
26
28
  <FormField
27
- defaultValue=''
29
+ defaultValue={DEFAULT_INPUT_VALUE}
28
30
  control={control}
29
31
  name={bind}
30
32
  rules={{
@@ -16,7 +16,9 @@ import {
16
16
  RadioGroup as CommonRadioGroup,
17
17
  RadioGroupItem,
18
18
  } from '@twintag/twintag-form/common/radiogroup';
19
- import { useTwintagForm } from '@twintag/twintag-form/provider';
19
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
20
+
21
+ export const DEFAULT_RADIOGROUP_VALUE = '';
20
22
 
21
23
  export const RadioGroup: FC<Props> = ({ label, bind, items, options }) => {
22
24
  const { control } = useFormContext();
@@ -26,9 +28,9 @@ export const RadioGroup: FC<Props> = ({ label, bind, items, options }) => {
26
28
 
27
29
  return (
28
30
  <FormField
31
+ defaultValue={DEFAULT_RADIOGROUP_VALUE}
29
32
  control={control}
30
33
  name={bind}
31
- defaultValue=''
32
34
  rules={{
33
35
  required: isOptional ? false : options?.error ?? true,
34
36
  }}
@@ -2,7 +2,7 @@ import type { FC } from 'react';
2
2
  import type { Props } from '@twintag/twintag-form/types';
3
3
 
4
4
  import { Button } from '@twintag/twintag-form/common/button';
5
- import { useTwintagForm } from '@twintag/twintag-form/provider';
5
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
6
6
 
7
7
  export const Submit: FC<Props> = ({ label }) => {
8
8
  const { size } = useTwintagForm();
@@ -13,7 +13,9 @@ import {
13
13
  FormMessage,
14
14
  } from '@twintag/twintag-form/common/form';
15
15
  import { Textarea as CommonTextarea } from '@twintag/twintag-form/common/textarea';
16
- import { useTwintagForm } from '@twintag/twintag-form/provider';
16
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
17
+
18
+ export const DEFAULT_TEXTAREA_VALUE = '';
17
19
 
18
20
  export const Textarea: FC<Props> = ({ label, bind, options }) => {
19
21
  const { control } = useFormContext();
@@ -23,7 +25,7 @@ export const Textarea: FC<Props> = ({ label, bind, options }) => {
23
25
 
24
26
  return (
25
27
  <FormField
26
- defaultValue=''
28
+ defaultValue={DEFAULT_TEXTAREA_VALUE}
27
29
  control={control}
28
30
  name={bind}
29
31
  rules={{
@@ -13,7 +13,9 @@ import {
13
13
  FormMessage,
14
14
  } from '@twintag/twintag-form/common/form';
15
15
  import { Switch } from '@twintag/twintag-form/common/switch';
16
- import { useTwintagForm } from '@twintag/twintag-form/provider';
16
+ import { useTwintagForm } from '@twintag/twintag-form/components/provider';
17
+
18
+ export const DEFAULT_TOGGLE_VALUE = false;
17
19
 
18
20
  export const Toggle: FC<Props> = ({ label, bind, options }) => {
19
21
  const { control } = useFormContext();
@@ -23,9 +25,9 @@ export const Toggle: FC<Props> = ({ label, bind, options }) => {
23
25
 
24
26
  return (
25
27
  <FormField
28
+ defaultValue={DEFAULT_TOGGLE_VALUE}
26
29
  control={control}
27
30
  name={bind}
28
- defaultValue={false}
29
31
  rules={{
30
32
  required: isOptional ? false : options?.error ?? true,
31
33
  }}