@react-fabric/form 1.0.0-beta.4 → 1.0.0-beta.41

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 (155) hide show
  1. package/{src/input/ArrayInput.module.css → css/_arrayInput.css} +8 -8
  2. package/css/_colorPicker.css +65 -0
  3. package/{src/internal/Field.module.css → css/_field.css} +26 -15
  4. package/{src/input/File.module.css → css/_file.css} +20 -6
  5. package/css/styles.css +27 -0
  6. package/dist/cjs/builder/FileField.js +37 -25
  7. package/dist/cjs/builder/FormField.js +44 -34
  8. package/dist/cjs/builder/SchemaEditor.js +72 -0
  9. package/dist/cjs/builder/components/AddSchemaFields.js +62 -0
  10. package/dist/cjs/builder/components/SchemaArrayForm.js +143 -0
  11. package/dist/cjs/builder/components/SchemaField.js +413 -0
  12. package/dist/cjs/form/Controller.js +1 -1
  13. package/dist/cjs/form/Form.js +19 -10
  14. package/dist/cjs/hooks/useFormBuilder.js +27 -15
  15. package/dist/cjs/index.js +9 -2
  16. package/dist/cjs/input/ArrayInput.js +36 -25
  17. package/dist/cjs/input/AvatarInput.js +1 -1
  18. package/dist/cjs/input/Checkbox.js +5 -4
  19. package/dist/cjs/input/ColorInput.js +70 -28
  20. package/dist/cjs/input/ColorPicker.js +1 -35
  21. package/dist/cjs/input/DateInput.js +183 -0
  22. package/dist/cjs/input/Field.js +4 -4
  23. package/dist/cjs/input/File.js +2 -3
  24. package/dist/cjs/input/Hidden.js +3 -1
  25. package/dist/cjs/input/Input.js +1 -1
  26. package/dist/cjs/input/Number.js +1 -1
  27. package/dist/cjs/input/Password.js +1 -1
  28. package/dist/cjs/input/Radio.js +1 -1
  29. package/dist/cjs/input/RadioGroup.js +6 -3
  30. package/dist/cjs/input/{Range.js → RangeSlider.js} +3 -3
  31. package/dist/cjs/input/Search.js +1 -1
  32. package/dist/cjs/input/Slider.js +1 -1
  33. package/dist/cjs/input/Switch.js +27 -5
  34. package/dist/cjs/input/Textarea.js +7 -3
  35. package/dist/cjs/internal/ErrorIcon.js +1 -1
  36. package/dist/cjs/internal/FieldWrapper.js +4 -3
  37. package/dist/cjs/internal/InputWrapper.js +14 -16
  38. package/dist/cjs/select/AutoComplete.js +9 -6
  39. package/dist/cjs/select/List.js +41 -20
  40. package/dist/cjs/select/Options.js +2 -2
  41. package/dist/cjs/select/Select.js +15 -9
  42. package/dist/cjs/select/useSelect.js +15 -8
  43. package/{src/input/ColorPicker.module.css → dist/cjs/types/schema.js} +18 -27
  44. package/dist/cjs/utils/getColor.js +6 -0
  45. package/dist/esm/builder/FileField.js +39 -27
  46. package/dist/esm/builder/FormField.js +44 -34
  47. package/dist/esm/builder/SchemaEditor.js +72 -0
  48. package/dist/esm/builder/components/AddSchemaFields.js +62 -0
  49. package/dist/esm/builder/components/SchemaArrayForm.js +143 -0
  50. package/dist/esm/builder/components/SchemaField.js +413 -0
  51. package/dist/esm/form/Controller.js +1 -1
  52. package/dist/esm/form/Form.js +19 -10
  53. package/dist/esm/hooks/useFormBuilder.js +25 -13
  54. package/dist/esm/index.js +4 -1
  55. package/dist/esm/input/ArrayInput.js +38 -27
  56. package/dist/esm/input/AvatarInput.js +1 -1
  57. package/dist/esm/input/Checkbox.js +5 -4
  58. package/dist/esm/input/ColorInput.js +72 -30
  59. package/dist/esm/input/ColorPicker.js +1 -35
  60. package/dist/esm/input/DateInput.js +183 -0
  61. package/dist/esm/input/Field.js +5 -5
  62. package/dist/esm/input/File.js +2 -3
  63. package/dist/esm/input/Hidden.js +3 -1
  64. package/dist/esm/input/Input.js +1 -1
  65. package/dist/esm/input/Number.js +1 -1
  66. package/dist/esm/input/Password.js +1 -1
  67. package/dist/esm/input/Radio.js +1 -1
  68. package/dist/esm/input/RadioGroup.js +7 -4
  69. package/dist/esm/input/{Range.js → RangeSlider.js} +3 -3
  70. package/dist/esm/input/Search.js +1 -1
  71. package/dist/esm/input/Slider.js +1 -1
  72. package/dist/esm/input/Switch.js +29 -7
  73. package/dist/esm/input/Textarea.js +7 -3
  74. package/dist/esm/internal/ErrorIcon.js +1 -1
  75. package/dist/esm/internal/FieldWrapper.js +4 -3
  76. package/dist/esm/internal/InputWrapper.js +14 -16
  77. package/dist/esm/select/AutoComplete.js +9 -6
  78. package/dist/esm/select/List.js +42 -21
  79. package/dist/esm/select/Options.js +2 -2
  80. package/dist/esm/select/Select.js +15 -9
  81. package/dist/esm/select/useSelect.js +16 -9
  82. package/dist/esm/types/schema.js +41 -0
  83. package/dist/esm/utils/getColor.js +6 -1
  84. package/dist/types/builder/FileField.d.ts +4 -2
  85. package/dist/types/builder/FormField.d.ts +3 -1
  86. package/dist/types/builder/SchemaEditor.d.ts +2 -0
  87. package/dist/types/builder/components/AddSchemaFields.d.ts +2 -0
  88. package/dist/types/builder/components/SchemaArrayForm.d.ts +2 -0
  89. package/dist/types/builder/components/SchemaField.d.ts +10 -0
  90. package/dist/types/form/Controller.d.ts +1 -1
  91. package/dist/types/hooks/useFormBuilder.d.ts +3 -1
  92. package/dist/types/index.d.ts +4 -2
  93. package/dist/types/input/ArrayInput.d.ts +20 -3
  94. package/dist/types/input/Checkbox.d.ts +4 -3
  95. package/dist/types/input/ColorInput.d.ts +1 -1
  96. package/dist/types/input/DateInput.d.ts +19 -0
  97. package/dist/types/input/RadioGroup.d.ts +18 -2
  98. package/dist/types/input/{Range.d.ts → RangeSlider.d.ts} +1 -1
  99. package/dist/types/input/Switch.d.ts +7 -3
  100. package/dist/types/input/Textarea.d.ts +5 -1
  101. package/dist/types/internal/InputWrapper.d.ts +1 -1
  102. package/dist/types/select/AutoComplete.d.ts +1 -1
  103. package/dist/types/select/List.d.ts +1 -1
  104. package/dist/types/select/Select.d.ts +1 -1
  105. package/dist/types/select/useSelect.d.ts +2 -1
  106. package/dist/types/types/index.d.ts +17 -5
  107. package/dist/types/types/schema.d.ts +79 -33
  108. package/package.json +10 -7
  109. package/src/builder/FileField.tsx +53 -27
  110. package/src/builder/FormField.tsx +45 -32
  111. package/src/builder/SchemaEditor.tsx +72 -0
  112. package/src/builder/components/AddSchemaFields.tsx +74 -0
  113. package/src/builder/components/SchemaArrayForm.tsx +158 -0
  114. package/src/builder/components/SchemaField.tsx +435 -0
  115. package/src/form/Controller.tsx +2 -2
  116. package/src/form/Form.tsx +15 -8
  117. package/src/hooks/useFormBuilder.tsx +29 -15
  118. package/src/index.ts +10 -2
  119. package/src/input/ArrayInput.tsx +80 -42
  120. package/src/input/AvatarInput.tsx +1 -1
  121. package/src/input/Checkbox.tsx +5 -4
  122. package/src/input/ColorInput.tsx +102 -34
  123. package/src/input/ColorPicker.tsx +1 -35
  124. package/src/input/DateInput.tsx +248 -0
  125. package/src/input/Field.tsx +9 -11
  126. package/src/input/File.tsx +3 -6
  127. package/src/input/Hidden.tsx +4 -1
  128. package/src/input/Input.tsx +2 -2
  129. package/src/input/Number.tsx +2 -2
  130. package/src/input/Password.tsx +2 -2
  131. package/src/input/Radio.tsx +1 -1
  132. package/src/input/RadioGroup.tsx +27 -4
  133. package/src/input/{Range.tsx → RangeSlider.tsx} +2 -2
  134. package/src/input/Search.tsx +2 -2
  135. package/src/input/Slider.tsx +1 -1
  136. package/src/input/Switch.tsx +42 -6
  137. package/src/input/Textarea.tsx +11 -3
  138. package/src/internal/ErrorIcon.tsx +1 -1
  139. package/src/internal/FieldWrapper.tsx +3 -3
  140. package/src/internal/InputWrapper.tsx +21 -20
  141. package/src/select/AutoComplete.tsx +4 -1
  142. package/src/select/List.tsx +37 -8
  143. package/src/select/Options.tsx +1 -1
  144. package/src/select/Select.tsx +9 -3
  145. package/src/select/useSelect.tsx +30 -19
  146. package/src/types/index.ts +22 -7
  147. package/src/types/schema.ts +96 -41
  148. package/dist/cjs/input/ArrayInput.module.css +0 -1
  149. package/dist/cjs/input/ColorPicker.module.css +0 -1
  150. package/dist/cjs/input/File.module.css +0 -1
  151. package/dist/cjs/internal/Field.module.css +0 -1
  152. package/dist/esm/input/ArrayInput.module.css +0 -1
  153. package/dist/esm/input/ColorPicker.module.css +0 -1
  154. package/dist/esm/input/File.module.css +0 -1
  155. package/dist/esm/internal/Field.module.css +0 -1
@@ -1,59 +1,105 @@
1
- interface BaseSchemaDef {
1
+ /// <reference types="react" />
2
+ import { type Elements } from "@react-fabric/core/dist/types/types";
3
+ import { type DateLike } from "@react-fabric/date";
4
+ export declare enum DATA_TYPES {
5
+ TEXT = "text",
6
+ COLOR = "color",
7
+ BOOL = "bool",
8
+ DATE = "date",
9
+ STRING = "string",
10
+ NUMBER = "number",
11
+ RANGE = "range",
12
+ FILE = "file",
13
+ AVATAR = "avatar",
14
+ SCHEMA = "schema"
15
+ }
16
+ export declare const SCHEMA_CUSTOM_LIST = "--CUSTOM--";
17
+ interface SingleValue {
18
+ multiple?: false;
19
+ minItems?: never;
20
+ maxItems?: never;
21
+ }
22
+ interface MultipleValue {
23
+ multiple: true;
24
+ minItems?: number;
25
+ maxItems?: number;
26
+ }
27
+ export type ValueType = SingleValue | MultipleValue;
28
+ interface BaseSchema {
2
29
  id: string;
3
30
  label: string;
4
31
  datatype: string;
5
32
  required?: boolean;
6
- multiple?: boolean;
7
- defaultValue?: AnyObject;
8
33
  }
9
- interface SchemaDef extends BaseSchemaDef {
10
- datatype: "text" | "color";
34
+ export interface DefaultSchema extends BaseSchema {
35
+ datatype: DATA_TYPES.TEXT | DATA_TYPES.COLOR;
11
36
  }
12
- interface BooleanSchema extends BaseSchemaDef {
13
- datatype: "boolean";
14
- defaultValue?: boolean;
37
+ export interface BooleanSchema extends BaseSchema {
38
+ datatype: DATA_TYPES.BOOL;
15
39
  }
16
- interface StringSchema extends BaseSchemaDef {
17
- datatype: "string";
18
- options?: string[];
40
+ export interface StringSchema extends BaseSchema {
41
+ datatype: DATA_TYPES.STRING;
19
42
  regex?: string;
20
- identifierType?: string;
43
+ options?: string[];
44
+ optionList?: string;
45
+ allowCustom?: boolean;
21
46
  }
22
- interface RangeSchema extends BaseSchemaDef {
23
- datatype: "range";
47
+ export interface RangeSchema extends BaseSchema {
48
+ datatype: DATA_TYPES.RANGE;
24
49
  min?: number;
25
50
  max?: number;
26
51
  step?: number;
27
- defaultValue?: [number, number];
28
52
  }
29
- interface NumberSchema extends BaseSchemaDef {
30
- datatype: "number";
31
- min?: number;
32
- max?: number;
33
- step?: number;
34
- defaultValue?: number;
53
+ export interface DateSchema extends BaseSchema {
54
+ datatype: DATA_TYPES.DATE;
55
+ type?: "date" | "datetime";
56
+ min?: DateLike;
57
+ max?: DateLike;
35
58
  }
36
- interface DecimalSchema extends BaseSchemaDef {
37
- datatype: "decimal";
59
+ export interface NumberSchema extends BaseSchema {
60
+ datatype: DATA_TYPES.NUMBER;
38
61
  min?: number;
39
62
  max?: number;
40
63
  step?: number;
41
- defaultValue?: number;
42
64
  }
43
- export interface FileSchema extends BaseSchemaDef {
44
- datatype: "file";
65
+ export interface FileSchema extends BaseSchema {
66
+ datatype: DATA_TYPES.FILE;
45
67
  accept?: string;
46
68
  }
47
- export interface AvatarSchema extends BaseSchemaDef {
48
- datatype: "avatar";
69
+ export interface AvatarSchema extends BaseSchema {
70
+ datatype: DATA_TYPES.AVATAR;
49
71
  multiple?: false;
50
72
  }
51
- export type SchemaType = SchemaDef | BooleanSchema | StringSchema | RangeSchema | NumberSchema | DecimalSchema | FileSchema | AvatarSchema;
52
- export interface ChildSchema extends BaseSchemaDef {
53
- datatype: "schema";
73
+ export type SchemaType = (DefaultSchema & ValueType) | (BooleanSchema & ValueType) | (StringSchema & ValueType) | (RangeSchema & ValueType) | (DateSchema & ValueType) | (NumberSchema & ValueType) | (FileSchema & ValueType) | (AvatarSchema & ValueType);
74
+ export interface ChildSchema extends BaseSchema {
75
+ datatype: DATA_TYPES.SCHEMA;
54
76
  inline?: boolean;
55
77
  schema: SchemaType[];
56
- defaultValue?: never;
57
78
  }
58
- export type FormSchema = SchemaType | ChildSchema;
79
+ export type SchemaDef = SchemaType | (ChildSchema & ValueType);
80
+ export type FormSchema = SchemaDef[];
81
+ export interface SchemaEditorProps {
82
+ /**
83
+ * default form schema
84
+ */
85
+ schemaDef: FormSchema;
86
+ /**
87
+ * is dynamic schema (allow add/remove)
88
+ */
89
+ dynamic?: boolean;
90
+ /**
91
+ * acceptable list of data types
92
+ */
93
+ acceptableTypes?: DATA_TYPES[];
94
+ /**
95
+ * available option lists in application
96
+ */
97
+ optionLists?: string[];
98
+ /**
99
+ * footer actions
100
+ */
101
+ children?: Elements<JSX.Element> | Array<Elements<JSX.Element>>;
102
+ onChange?: (schema: FormSchema) => void;
103
+ onSubmit?: (schema: FormSchema) => void;
104
+ }
59
105
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-fabric/form",
3
- "version": "1.0.0-beta.4",
3
+ "version": "1.0.0-beta.41",
4
4
  "description": "RUF: Form package",
5
5
  "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
6
  "license": "MIT",
@@ -11,9 +11,11 @@
11
11
  "types": "dist/types/index.d.ts",
12
12
  "directories": {
13
13
  "src": "src",
14
+ "styles": "css",
14
15
  "test": "tests"
15
16
  },
16
17
  "files": [
18
+ "css",
17
19
  "src",
18
20
  "dist"
19
21
  ],
@@ -37,10 +39,11 @@
37
39
  "@dnd-kit/modifiers": "^7.0.0",
38
40
  "@dnd-kit/sortable": "^8.0.0",
39
41
  "@dnd-kit/utilities": "^3.2.2",
40
- "@floating-ui/react": "0.26.24",
42
+ "@floating-ui/react": "^0.26.24",
41
43
  "@hookform/resolvers": "^3.9.0",
42
- "@react-fabric/core": "^1.0.0-beta.4",
43
- "@react-fabric/utilities": "^1.0.0-beta.4",
44
+ "@react-fabric/core": "^1.0.0-beta.41",
45
+ "@react-fabric/date": "^1.0.0-beta.41",
46
+ "@react-fabric/utilities": "^1.0.0-beta.41",
44
47
  "classnames": "^2.5.1",
45
48
  "color": "^4.2.3",
46
49
  "react-color": "^2.19.3",
@@ -48,8 +51,8 @@
48
51
  },
49
52
  "peerDependencies": {
50
53
  "i18next": "^23.15.1",
51
- "react": ">=18.0.0 <20.0.0",
52
- "react-dom": ">=18.0.0 <20.0.0",
54
+ "react": "^19.0.0",
55
+ "react-dom": "^19.0.0",
53
56
  "react-i18next": "^15.0.2"
54
57
  },
55
58
  "devDependencies": {
@@ -60,5 +63,5 @@
60
63
  "node": ">=18.12.0",
61
64
  "npm": ">=8.19.0"
62
65
  },
63
- "gitHead": "47b0aede19252a3a3ef8df75c08a21caabde8100"
66
+ "gitHead": "bc737416d3c6a217a6b8c47291c3975a22722d80"
64
67
  }
@@ -21,28 +21,35 @@
21
21
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
- import { Button, Icon, ProgressBar } from "@react-fabric/core";
25
- import { FileUtil, Format } from "@react-fabric/utilities";
24
+ import { Button, Icon, ProgressBar, Tooltip } from "@react-fabric/core";
25
+ import { FileUtil, Format, isString } from "@react-fabric/utilities";
26
26
  import { Fragment } from "react";
27
- import { type UploadHandler, useFileUploader } from "../hooks/useFileUploader";
27
+ import { useTranslation } from "react-i18next";
28
+ import { useFileUploader, type UploadHandler } from "../hooks/useFileUploader";
28
29
  import { Field } from "../input/Field";
29
30
  import { HiddenInput } from "../input/Hidden";
30
- import { type FileSchema } from "../types/schema";
31
+ import { type FileSchema, type ValueType } from "../types/schema";
31
32
 
32
33
  export const FileField = ({
33
34
  uploadHandler,
34
35
  inline,
35
36
  label,
36
37
  accept,
37
- multiple = false,
38
38
  value,
39
+ fileUrl,
40
+ multiple,
41
+ invalid,
42
+ error,
39
43
  ...rest
40
- }: Partial<FileSchema> & {
44
+ }: Partial<FileSchema & ValueType> & {
41
45
  value?: AnyObject;
42
46
  inline?: boolean;
47
+ invalid?: boolean;
48
+ error?: AnyObject;
43
49
  fileUrl?: (path: string) => string;
44
50
  uploadHandler: UploadHandler;
45
51
  }) => {
52
+ const { t } = useTranslation();
46
53
  const { pending, files, list, upload, remove } = useFileUploader(
47
54
  async (data, config) => await uploadHandler?.(data, config),
48
55
  value,
@@ -51,29 +58,48 @@ export const FileField = ({
51
58
 
52
59
  return (
53
60
  <Fragment>
54
- <Field plain inline={inline} label={label} className="inline-block">
61
+ <Field plain inline={inline} label={label}>
55
62
  <HiddenInput hiddenValue={multiple ? list : list?.[0]} {...rest} />
56
- <span>
57
- <Button
58
- badge={pending > 0 ? `${pending}` : undefined}
59
- data-ref="filebtn"
60
- >
61
- <Fragment>
62
- <span>Add file</span>
63
- <input
64
- type="file"
65
- accept={accept}
66
- multiple={multiple}
67
- className="absolute inset-0 opacity-0 z-5"
68
- onChange={(e) => [
69
- upload(e.target.files),
70
- (e.target.value = ""),
71
- ]}
72
- />
73
- </Fragment>
74
- </Button>
75
- </span>
76
63
  </Field>
64
+ <Tooltip
65
+ color="danger-200"
66
+ content={
67
+ isString(error)
68
+ ? `${error}`
69
+ : error &&
70
+ (t(
71
+ error?.key,
72
+ error.values
73
+ ? {
74
+ ...error?.values,
75
+ label: t(
76
+ error.values?.label ?? "form:badkey",
77
+ error.values?.path,
78
+ ),
79
+ }
80
+ : {},
81
+ ) as string)
82
+ }
83
+ disabled={!error}
84
+ >
85
+ <Button
86
+ badge={pending > 0 ? `${pending}` : undefined}
87
+ color={error ? "danger" : undefined}
88
+ variant={error ? "soft" : undefined}
89
+ data-ref="filebtn"
90
+ >
91
+ <Fragment>
92
+ <span>Add file</span>
93
+ <input
94
+ type="file"
95
+ accept={accept}
96
+ multiple={multiple}
97
+ className="absolute inset-0 opacity-0 z-5"
98
+ onChange={(e) => [upload(e.target.files), (e.target.value = "")]}
99
+ />
100
+ </Fragment>
101
+ </Button>
102
+ </Tooltip>
77
103
  <br />
78
104
  <div className="bg-alternate">
79
105
  {files.map((file, idx) => (
@@ -21,6 +21,7 @@
21
21
  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
22
  */
23
23
 
24
+ import { isEmpty } from "@react-fabric/utilities";
24
25
  import { useMemo } from "react";
25
26
  import { useTranslation } from "react-i18next";
26
27
  import { Controller } from "../form/Controller";
@@ -28,13 +29,15 @@ import { type UploadHandler } from "../hooks/useFileUploader";
28
29
  import { ArrayInput } from "../input/ArrayInput";
29
30
  import { AvatarInput } from "../input/AvatarInput";
30
31
  import { ColorInput } from "../input/ColorInput";
32
+ import { DateInput } from "../input/DateInput";
31
33
  import { Input } from "../input/Input";
32
34
  import { Number } from "../input/Number";
33
- import { Range } from "../input/Range";
35
+ import { RangeSlider } from "../input/RangeSlider";
34
36
  import { Switch } from "../input/Switch";
35
37
  import { Textarea } from "../input/Textarea";
36
38
  import { Select } from "../select/Select";
37
- import { type SchemaType } from "../types/schema";
39
+ import { type BaseSelectProps } from "../types";
40
+ import { DATA_TYPES, type SchemaType } from "../types/schema";
38
41
  import { FileField } from "./FileField";
39
42
 
40
43
  export const FormField = ({
@@ -44,7 +47,7 @@ export const FormField = ({
44
47
  inline,
45
48
  prefix,
46
49
  autoFocus,
47
- defaultValue,
50
+ optionLists,
48
51
  fileUrl,
49
52
  uploadHandler,
50
53
  ...rest
@@ -52,6 +55,7 @@ export const FormField = ({
52
55
  inline?: boolean;
53
56
  prefix?: string;
54
57
  autoFocus?: boolean;
58
+ optionLists?: KeyValue<BaseSelectProps<any>>;
55
59
  fileUrl?: (path: string) => string;
56
60
  uploadHandler?: UploadHandler;
57
61
  }) => {
@@ -61,32 +65,34 @@ export const FormField = ({
61
65
  [id, prefix],
62
66
  );
63
67
 
64
- const E = useMemo(() => {
65
- if (rest.datatype === "number" || rest.datatype === "decimal")
66
- return Number;
67
- if (rest.datatype === "range") return Range;
68
- if (rest.datatype === "boolean") return Switch;
69
- if (rest.datatype === "text") return Textarea;
70
- if (rest.datatype === "color") return ColorInput;
71
- if (rest.datatype === "string" && rest.options) return Select;
68
+ const hasOptions = useMemo(
69
+ () =>
70
+ rest.datatype === DATA_TYPES.STRING &&
71
+ ((rest.optionList === "--CUSTOM--" && !isEmpty(rest.options)) ||
72
+ !isEmpty(optionLists?.[rest.optionList ?? ""])),
73
+ [rest],
74
+ );
75
+
76
+ const E = useMemo<AnyObject>(() => {
77
+ if (rest.datatype === DATA_TYPES.NUMBER) return Number;
78
+ if (rest.datatype === DATA_TYPES.RANGE) return RangeSlider;
79
+ if (rest.datatype === DATA_TYPES.BOOL) return Switch;
80
+ if (rest.datatype === DATA_TYPES.TEXT) return Textarea;
81
+ if (rest.datatype === DATA_TYPES.DATE) return DateInput;
82
+ if (rest.datatype === DATA_TYPES.COLOR) return ColorInput;
83
+ if (hasOptions) return Select;
72
84
 
73
85
  return Input;
74
- }, [rest.datatype]);
86
+ }, [rest.datatype, hasOptions]);
75
87
 
76
88
  const props = useMemo(() => {
77
- if (rest.datatype === "number")
89
+ if (rest.datatype === DATA_TYPES.NUMBER)
78
90
  return {
79
91
  min: rest.min,
80
92
  max: rest.max,
81
93
  step: rest.step ?? 1,
82
94
  };
83
- if (rest.datatype === "decimal")
84
- return {
85
- min: rest.min,
86
- max: rest.max,
87
- step: rest.step ?? 0.1,
88
- };
89
- if (rest.datatype === "range")
95
+ if (rest.datatype === DATA_TYPES.RANGE)
90
96
  return {
91
97
  min: rest.min ?? 1,
92
98
  max: rest.max ?? 99,
@@ -94,20 +100,27 @@ export const FormField = ({
94
100
  showValue: true,
95
101
  showLabels: true,
96
102
  };
97
- if (rest.datatype === "text") return { rows: 3 };
98
- if (rest.datatype === "color") return { showPicker: true };
103
+ if (rest.datatype === DATA_TYPES.DATE)
104
+ return {
105
+ min: rest.min,
106
+ max: rest.max,
107
+ type: rest.type,
108
+ };
109
+ if (rest.datatype === DATA_TYPES.TEXT) return { rows: 3 };
110
+ if (rest.datatype === DATA_TYPES.COLOR) return { showPicker: true };
99
111
 
100
- if (rest.datatype === "string" && rest.options)
112
+ if (rest.datatype === DATA_TYPES.STRING && hasOptions)
101
113
  return {
102
- options: rest.options,
103
114
  multiple: rest.multiple,
115
+ allowCreate: rest.allowCustom,
104
116
  searchable: true,
117
+ ...(optionLists?.[rest.optionList ?? ""] ?? { options: rest.options }),
105
118
  };
106
119
 
107
120
  return {};
108
- }, [rest]);
121
+ }, [rest, hasOptions, optionLists]);
109
122
 
110
- if (rest.datatype === "avatar")
123
+ if (rest.datatype === DATA_TYPES.AVATAR)
111
124
  return (
112
125
  uploadHandler && (
113
126
  <Controller name={fieldName}>
@@ -120,7 +133,7 @@ export const FormField = ({
120
133
  )
121
134
  );
122
135
 
123
- if (rest.datatype === "file")
136
+ if (rest.datatype === DATA_TYPES.FILE)
124
137
  return (
125
138
  uploadHandler && (
126
139
  <Controller name={fieldName}>
@@ -135,12 +148,12 @@ export const FormField = ({
135
148
  )
136
149
  );
137
150
 
138
- return rest.multiple && !("options" in rest) ? (
151
+ return rest.multiple && !hasOptions ? (
139
152
  <ArrayInput
140
153
  name={fieldName}
141
- label={t(label, label)}
154
+ label={t(`fb.${id}`, label || id)}
142
155
  required={required}
143
- onAdd={() => ""}
156
+ onAdd={() => String("")}
144
157
  >
145
158
  <Controller>
146
159
  <E {...props} />
@@ -151,8 +164,8 @@ export const FormField = ({
151
164
  <E
152
165
  {...props}
153
166
  autoFocus={autoFocus}
154
- label={t(label, label)}
155
- placeholder={t(label, label)}
167
+ label={t(`fb.${id}`, label || id)}
168
+ placeholder={t(`fb.${id}`, label || id)}
156
169
  required={required}
157
170
  inline={inline}
158
171
  />
@@ -0,0 +1,72 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2025 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import { Footer } from "@react-fabric/core";
25
+ import { EMPTY_ARRAY } from "@react-fabric/utilities";
26
+ import { useCallback, useEffect, useRef } from "react";
27
+ import { Form, type FormRef } from "../form/Form";
28
+ import {
29
+ DATA_TYPES,
30
+ type FormSchema,
31
+ type SchemaEditorProps,
32
+ } from "../types/schema";
33
+ import { SchemaArrayForm } from "./components/SchemaArrayForm";
34
+
35
+ export const SchemaEditor = ({
36
+ schemaDef,
37
+ dynamic,
38
+ acceptableTypes,
39
+ optionLists = EMPTY_ARRAY,
40
+ children,
41
+ onSubmit,
42
+ onChange,
43
+ }: SchemaEditorProps) => {
44
+ const formRef = useRef<FormRef<{ schema: FormSchema }>>(null);
45
+
46
+ const handleChange = useCallback((model: { schema: FormSchema }) => {
47
+ onChange?.(model.schema);
48
+ }, []);
49
+
50
+ const handleSubmit = useCallback((model: { schema: FormSchema }) => {
51
+ onSubmit?.(model.schema);
52
+ }, []);
53
+
54
+ useEffect(() => {
55
+ formRef.current?.setValues({ schema: schemaDef });
56
+ }, [schemaDef]);
57
+
58
+ return (
59
+ <Form formRef={formRef} onChange={handleChange} onSubmit={handleSubmit}>
60
+ <SchemaArrayForm
61
+ dynamic={dynamic}
62
+ optionLists={optionLists}
63
+ acceptableTypes={acceptableTypes ?? Object.values(DATA_TYPES)}
64
+ />
65
+ {children && (
66
+ <Footer flex className="border-t bg-base px-6 py-2">
67
+ {children}
68
+ </Footer>
69
+ )}
70
+ </Form>
71
+ );
72
+ };
@@ -0,0 +1,74 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2025 Adarsh Pastakia
8
+ *
9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
10
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
11
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12
+ * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13
+ * subject to the following conditions:
14
+ *
15
+ * The above copyright notice and this permission notice shall be included in all copies or substantial
16
+ * portions of the Software.
17
+ *
18
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
19
+ * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ */
23
+
24
+ import {
25
+ Button,
26
+ Content,
27
+ Footer,
28
+ Modal,
29
+ type ModalProps,
30
+ } from "@react-fabric/core";
31
+ import { yup } from "@react-fabric/utilities";
32
+ import { useCallback } from "react";
33
+ import { Controller } from "../../form/Controller";
34
+ import { Form } from "../../form/Form";
35
+ import { ArrayInput } from "../../input/ArrayInput";
36
+ import { Input } from "../../input/Input";
37
+
38
+ const fieldSchema = yup.object({
39
+ fields: yup.array(yup.string().trim().required()).required().min(1),
40
+ });
41
+
42
+ export const AddSchemaFields = (props: ModalProps) => {
43
+ const handleSubmit = useCallback((model: KeyValue) => {
44
+ props.onClose(
45
+ model.fields
46
+ .map((f: string) => f.split(",").map((ff) => ff.trim()))
47
+ .flat(2),
48
+ );
49
+ }, []);
50
+
51
+ return (
52
+ <Modal {...props}>
53
+ <Form
54
+ schema={fieldSchema}
55
+ defaultValues={{ fields: [" "] }}
56
+ onSubmit={handleSubmit}
57
+ >
58
+ <Content>
59
+ <ArrayInput minItems={1} name="fields" onAdd={() => String("")}>
60
+ <Controller>
61
+ <Input autoFocus />
62
+ </Controller>
63
+ </ArrayInput>
64
+ <p className="text-sm text-muted">
65
+ Provide list of fields or comma-separated value
66
+ </p>
67
+ </Content>
68
+ <Footer flex justify="end" className="px-2 py-1">
69
+ <Button type="submit">Add</Button>
70
+ </Footer>
71
+ </Form>
72
+ </Modal>
73
+ );
74
+ };