@react-fabric/form 1.0.0-beta.5 → 1.0.0-beta.52

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 (187) 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} +64 -29
  4. package/{src/input/File.module.css → css/_file.css} +21 -6
  5. package/css/styles.css +27 -0
  6. package/dist/cjs/builder/FileField.js +37 -25
  7. package/dist/cjs/builder/FormField.js +50 -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 +414 -0
  12. package/dist/cjs/form/Controller.js +46 -1
  13. package/dist/cjs/form/Form.js +43 -6
  14. package/dist/cjs/hooks/useFileUploader.js +16 -0
  15. package/dist/cjs/hooks/useFormBuilder.js +57 -15
  16. package/dist/cjs/i18n/ar.json.js +5 -4
  17. package/dist/cjs/i18n/en.json.js +5 -5
  18. package/dist/cjs/index.js +11 -2
  19. package/dist/cjs/input/ArrayInput.js +100 -33
  20. package/dist/cjs/input/AvatarInput.js +17 -8
  21. package/dist/cjs/input/Checkbox.js +15 -5
  22. package/dist/cjs/input/ColorInput.js +108 -56
  23. package/dist/cjs/input/ColorPicker.js +11 -35
  24. package/dist/cjs/input/DateInput.js +195 -0
  25. package/dist/cjs/input/Field.js +10 -4
  26. package/dist/cjs/input/File.js +14 -7
  27. package/dist/cjs/input/Hidden.js +11 -1
  28. package/dist/cjs/input/Input.js +19 -7
  29. package/dist/cjs/input/Number.js +17 -7
  30. package/dist/cjs/input/Password.js +16 -8
  31. package/dist/cjs/input/Radio.js +11 -2
  32. package/dist/cjs/input/RadioGroup.js +14 -3
  33. package/dist/cjs/input/{Range.js → RangeSlider.js} +14 -6
  34. package/dist/cjs/input/Search.js +25 -16
  35. package/dist/cjs/input/Slider.js +10 -4
  36. package/dist/cjs/input/Switch.js +39 -7
  37. package/dist/cjs/input/Textarea.js +23 -4
  38. package/dist/cjs/internal/ErrorIcon.js +1 -1
  39. package/dist/cjs/internal/FieldWrapper.js +11 -4
  40. package/dist/cjs/internal/InputWrapper.js +19 -16
  41. package/dist/cjs/select/AutoComplete.js +19 -8
  42. package/dist/cjs/select/DualList.js +15 -1
  43. package/dist/cjs/select/List.js +45 -20
  44. package/dist/cjs/select/Options.js +2 -2
  45. package/dist/cjs/select/Select.js +14 -4
  46. package/dist/cjs/select/useSelect.js +10 -6
  47. package/dist/cjs/types/schema.js +43 -0
  48. package/dist/cjs/utils/eventHandlers.js +11 -5
  49. package/dist/cjs/utils/getColor.js +6 -0
  50. package/dist/esm/builder/FileField.js +39 -27
  51. package/dist/esm/builder/FormField.js +50 -34
  52. package/dist/esm/builder/SchemaEditor.js +72 -0
  53. package/dist/esm/builder/components/AddSchemaFields.js +62 -0
  54. package/dist/esm/builder/components/SchemaArrayForm.js +143 -0
  55. package/dist/esm/builder/components/SchemaField.js +414 -0
  56. package/dist/esm/form/Controller.js +46 -1
  57. package/dist/esm/form/Form.js +43 -6
  58. package/dist/esm/hooks/useFileUploader.js +16 -0
  59. package/dist/esm/hooks/useFormBuilder.js +55 -13
  60. package/dist/esm/i18n/ar.json.js +5 -4
  61. package/dist/esm/i18n/en.json.js +5 -5
  62. package/dist/esm/index.js +5 -1
  63. package/dist/esm/input/ArrayInput.js +100 -33
  64. package/dist/esm/input/AvatarInput.js +17 -8
  65. package/dist/esm/input/Checkbox.js +15 -5
  66. package/dist/esm/input/ColorInput.js +110 -58
  67. package/dist/esm/input/ColorPicker.js +11 -35
  68. package/dist/esm/input/DateInput.js +195 -0
  69. package/dist/esm/input/Field.js +11 -5
  70. package/dist/esm/input/File.js +16 -9
  71. package/dist/esm/input/Hidden.js +11 -1
  72. package/dist/esm/input/Input.js +21 -9
  73. package/dist/esm/input/Number.js +19 -9
  74. package/dist/esm/input/Password.js +18 -10
  75. package/dist/esm/input/Radio.js +11 -2
  76. package/dist/esm/input/RadioGroup.js +15 -4
  77. package/dist/esm/input/{Range.js → RangeSlider.js} +14 -6
  78. package/dist/esm/input/Search.js +27 -18
  79. package/dist/esm/input/Slider.js +10 -4
  80. package/dist/esm/input/Switch.js +41 -9
  81. package/dist/esm/input/Textarea.js +24 -5
  82. package/dist/esm/internal/ErrorIcon.js +1 -1
  83. package/dist/esm/internal/FieldWrapper.js +11 -4
  84. package/dist/esm/internal/InputWrapper.js +19 -16
  85. package/dist/esm/select/AutoComplete.js +19 -8
  86. package/dist/esm/select/DualList.js +15 -1
  87. package/dist/esm/select/List.js +46 -21
  88. package/dist/esm/select/Options.js +2 -2
  89. package/dist/esm/select/Select.js +14 -4
  90. package/dist/esm/select/useSelect.js +11 -7
  91. package/{src/input/ColorPicker.module.css → dist/esm/types/schema.js} +18 -27
  92. package/dist/esm/utils/eventHandlers.js +11 -5
  93. package/dist/esm/utils/getColor.js +6 -1
  94. package/dist/types/builder/FileField.d.ts +4 -2
  95. package/dist/types/builder/FormField.d.ts +3 -1
  96. package/dist/types/builder/SchemaEditor.d.ts +9 -0
  97. package/dist/types/builder/components/AddSchemaFields.d.ts +2 -0
  98. package/dist/types/builder/components/SchemaArrayForm.d.ts +2 -0
  99. package/dist/types/builder/components/SchemaField.d.ts +10 -0
  100. package/dist/types/form/Controller.d.ts +47 -2
  101. package/dist/types/form/Form.d.ts +37 -2
  102. package/dist/types/hooks/useFileUploader.d.ts +16 -0
  103. package/dist/types/hooks/useFormBuilder.d.ts +32 -1
  104. package/dist/types/index.d.ts +5 -2
  105. package/dist/types/input/ArrayInput.d.ts +78 -6
  106. package/dist/types/input/AvatarInput.d.ts +14 -3
  107. package/dist/types/input/Checkbox.d.ts +13 -4
  108. package/dist/types/input/ColorInput.d.ts +15 -2
  109. package/dist/types/input/ColorPicker.d.ts +11 -1
  110. package/dist/types/input/DateInput.d.ts +28 -0
  111. package/dist/types/input/Field.d.ts +5 -1
  112. package/dist/types/input/File.d.ts +9 -0
  113. package/dist/types/input/Hidden.d.ts +8 -0
  114. package/dist/types/input/Input.d.ts +9 -1
  115. package/dist/types/input/Number.d.ts +9 -1
  116. package/dist/types/input/Password.d.ts +7 -1
  117. package/dist/types/input/Radio.d.ts +9 -1
  118. package/dist/types/input/RadioGroup.d.ts +26 -3
  119. package/dist/types/input/{Range.d.ts → RangeSlider.d.ts} +10 -2
  120. package/dist/types/input/Search.d.ts +14 -1
  121. package/dist/types/input/Slider.d.ts +7 -1
  122. package/dist/types/input/Switch.d.ts +16 -4
  123. package/dist/types/input/Textarea.d.ts +15 -1
  124. package/dist/types/internal/FieldWrapper.d.ts +1 -1
  125. package/dist/types/internal/InputWrapper.d.ts +1 -1
  126. package/dist/types/ranger/index.d.ts +2 -2
  127. package/dist/types/select/AutoComplete.d.ts +8 -1
  128. package/dist/types/select/DualList.d.ts +17 -2
  129. package/dist/types/select/List.d.ts +9 -1
  130. package/dist/types/select/Select.d.ts +9 -1
  131. package/dist/types/select/useSelect.d.ts +7 -6
  132. package/dist/types/types/index.d.ts +28 -7
  133. package/dist/types/types/schema.d.ts +78 -33
  134. package/dist/types/utils/eventHandlers.d.ts +1 -1
  135. package/package.json +15 -12
  136. package/src/builder/FileField.tsx +53 -27
  137. package/src/builder/FormField.tsx +52 -32
  138. package/src/builder/SchemaEditor.tsx +79 -0
  139. package/src/builder/components/AddSchemaFields.tsx +74 -0
  140. package/src/builder/components/SchemaArrayForm.tsx +158 -0
  141. package/src/builder/components/SchemaField.tsx +436 -0
  142. package/src/form/Controller.tsx +48 -3
  143. package/src/form/Form.tsx +45 -5
  144. package/src/hooks/useFileUploader.ts +16 -0
  145. package/src/hooks/useFormBuilder.tsx +58 -15
  146. package/src/i18n/ar.json +5 -4
  147. package/src/i18n/en.json +5 -5
  148. package/src/index.ts +11 -2
  149. package/src/input/ArrayInput.tsx +169 -54
  150. package/src/input/AvatarInput.tsx +20 -8
  151. package/src/input/Checkbox.tsx +15 -5
  152. package/src/input/ColorInput.tsx +126 -69
  153. package/src/input/ColorPicker.tsx +12 -36
  154. package/src/input/DateInput.tsx +259 -0
  155. package/src/input/Field.tsx +15 -11
  156. package/src/input/File.tsx +16 -14
  157. package/src/input/Hidden.tsx +12 -1
  158. package/src/input/Input.tsx +23 -10
  159. package/src/input/Number.tsx +18 -10
  160. package/src/input/Password.tsx +19 -12
  161. package/src/input/Radio.tsx +11 -2
  162. package/src/input/RadioGroup.tsx +35 -4
  163. package/src/input/{Range.tsx → RangeSlider.tsx} +13 -5
  164. package/src/input/Search.tsx +29 -19
  165. package/src/input/Slider.tsx +10 -4
  166. package/src/input/Switch.tsx +52 -7
  167. package/src/input/Textarea.tsx +29 -5
  168. package/src/internal/ErrorIcon.tsx +1 -1
  169. package/src/internal/FieldWrapper.tsx +8 -4
  170. package/src/internal/InputWrapper.tsx +36 -20
  171. package/src/select/AutoComplete.tsx +12 -1
  172. package/src/select/DualList.tsx +19 -0
  173. package/src/select/List.tsx +49 -8
  174. package/src/select/Options.tsx +1 -1
  175. package/src/select/Select.tsx +21 -3
  176. package/src/select/useSelect.tsx +13 -5
  177. package/src/types/index.ts +34 -8
  178. package/src/types/schema.ts +96 -41
  179. package/src/utils/eventHandlers.ts +9 -3
  180. package/dist/cjs/input/ArrayInput.module.css +0 -1
  181. package/dist/cjs/input/ColorPicker.module.css +0 -1
  182. package/dist/cjs/input/File.module.css +0 -1
  183. package/dist/cjs/internal/Field.module.css +0 -1
  184. package/dist/esm/input/ArrayInput.module.css +0 -1
  185. package/dist/esm/input/ColorPicker.module.css +0 -1
  186. package/dist/esm/input/File.module.css +0 -1
  187. package/dist/esm/internal/Field.module.css +0 -1
@@ -1,59 +1,104 @@
1
- interface BaseSchemaDef {
1
+ import { type Elements } from "@react-fabric/core/dist/types/types";
2
+ import { type DateLike } from "@react-fabric/date";
3
+ export declare enum DATA_TYPES {
4
+ TEXT = "text",
5
+ COLOR = "color",
6
+ BOOL = "bool",
7
+ DATE = "date",
8
+ DATETIME = "datetime",
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 | DATA_TYPES.DATETIME;
55
+ min?: DateLike;
56
+ max?: DateLike;
35
57
  }
36
- interface DecimalSchema extends BaseSchemaDef {
37
- datatype: "decimal";
58
+ export interface NumberSchema extends BaseSchema {
59
+ datatype: DATA_TYPES.NUMBER;
38
60
  min?: number;
39
61
  max?: number;
40
62
  step?: number;
41
- defaultValue?: number;
42
63
  }
43
- export interface FileSchema extends BaseSchemaDef {
44
- datatype: "file";
64
+ export interface FileSchema extends BaseSchema {
65
+ datatype: DATA_TYPES.FILE;
45
66
  accept?: string;
46
67
  }
47
- export interface AvatarSchema extends BaseSchemaDef {
48
- datatype: "avatar";
68
+ export interface AvatarSchema extends BaseSchema {
69
+ datatype: DATA_TYPES.AVATAR;
49
70
  multiple?: false;
50
71
  }
51
- export type SchemaType = SchemaDef | BooleanSchema | StringSchema | RangeSchema | NumberSchema | DecimalSchema | FileSchema | AvatarSchema;
52
- export interface ChildSchema extends BaseSchemaDef {
53
- datatype: "schema";
72
+ export type SchemaType = (DefaultSchema & ValueType) | (BooleanSchema & ValueType) | (StringSchema & ValueType) | (RangeSchema & ValueType) | (DateSchema & ValueType) | (NumberSchema & ValueType) | (FileSchema & ValueType) | (AvatarSchema & ValueType);
73
+ export interface ChildSchema extends BaseSchema {
74
+ datatype: DATA_TYPES.SCHEMA;
54
75
  inline?: boolean;
55
76
  schema: SchemaType[];
56
- defaultValue?: never;
57
77
  }
58
- export type FormSchema = SchemaType | ChildSchema;
78
+ export type SchemaDef = SchemaType | (ChildSchema & ValueType);
79
+ export type FormSchema = SchemaDef[];
80
+ export interface SchemaEditorProps {
81
+ /**
82
+ * default form schema
83
+ */
84
+ schemaDef: FormSchema;
85
+ /**
86
+ * is dynamic schema (allow add/remove)
87
+ */
88
+ dynamic?: boolean;
89
+ /**
90
+ * acceptable list of data types
91
+ */
92
+ acceptableTypes?: DATA_TYPES[];
93
+ /**
94
+ * available option lists in application
95
+ */
96
+ optionLists?: string[];
97
+ /**
98
+ * footer actions
99
+ */
100
+ children?: Elements<JSX.Element> | Array<Elements<JSX.Element>>;
101
+ onChange?: (schema: FormSchema) => void;
102
+ onSubmit?: (schema: FormSchema) => void;
103
+ }
59
104
  export {};
@@ -1,6 +1,6 @@
1
1
  import { type KeyboardEvent, type MouseEvent } from "react";
2
2
  /** ***************** common handler for enter press *******************/
3
- export declare const handleEnter: (callback?: (e: KeyboardEvent) => void, { stopPropagation, preventDefault, }?: {
3
+ export declare const useHandleEnter: (callback?: (e: KeyboardEvent) => void, { stopPropagation, preventDefault, }?: {
4
4
  stopPropagation?: boolean;
5
5
  preventDefault?: boolean;
6
6
  }) => (e: KeyboardEvent) => false | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-fabric/form",
3
- "version": "1.0.0-beta.5",
3
+ "version": "1.0.0-beta.52",
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
  ],
@@ -33,24 +35,25 @@
33
35
  "watch": "npm-run-all -p watch:rollup watch:dts"
34
36
  },
35
37
  "dependencies": {
36
- "@dnd-kit/core": "^6.1.0",
37
- "@dnd-kit/modifiers": "^7.0.0",
38
- "@dnd-kit/sortable": "^8.0.0",
38
+ "@dnd-kit/core": "^6.3.1",
39
+ "@dnd-kit/modifiers": "^9.0.0",
40
+ "@dnd-kit/sortable": "^10.0.0",
39
41
  "@dnd-kit/utilities": "^3.2.2",
40
- "@floating-ui/react": "0.26.24",
42
+ "@floating-ui/react": "^0.27.9",
41
43
  "@hookform/resolvers": "^3.9.0",
42
- "@react-fabric/core": "^1.0.0-beta.5",
43
- "@react-fabric/utilities": "^1.0.0-beta.5",
44
+ "@react-fabric/core": "^1.0.0-beta.52",
45
+ "@react-fabric/date": "^1.0.0-beta.52",
46
+ "@react-fabric/utilities": "^1.0.0-beta.52",
44
47
  "classnames": "^2.5.1",
45
48
  "color": "^4.2.3",
46
49
  "react-color": "^2.19.3",
47
50
  "react-hook-form": "^7.52.1"
48
51
  },
49
52
  "peerDependencies": {
50
- "i18next": "^23.15.1",
51
- "react": ">=18.0.0 <20.0.0",
52
- "react-dom": ">=18.0.0 <20.0.0",
53
- "react-i18next": "^15.0.2"
53
+ "i18next": "^25.2.1",
54
+ "react": "^19.0.0",
55
+ "react-dom": "^19.0.0",
56
+ "react-i18next": "^15.5.2"
54
57
  },
55
58
  "devDependencies": {
56
59
  "@types/axios": "^0.14.0",
@@ -60,5 +63,5 @@
60
63
  "node": ">=18.12.0",
61
64
  "npm": ">=8.19.0"
62
65
  },
63
- "gitHead": "7d4a7ed1ea9890652826bef58d01c31280d761c8"
66
+ "gitHead": "0eabb9d88d91af93d657bcc209b226fd576e8260"
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,35 @@ 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.DATETIME) return DateInput;
83
+ if (rest.datatype === DATA_TYPES.COLOR) return ColorInput;
84
+ if (hasOptions) return Select;
72
85
 
73
86
  return Input;
74
- }, [rest.datatype]);
87
+ }, [rest.datatype, hasOptions]);
75
88
 
76
89
  const props = useMemo(() => {
77
- if (rest.datatype === "number")
90
+ if (rest.datatype === DATA_TYPES.NUMBER)
78
91
  return {
79
92
  min: rest.min,
80
93
  max: rest.max,
81
94
  step: rest.step ?? 1,
82
95
  };
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")
96
+ if (rest.datatype === DATA_TYPES.RANGE)
90
97
  return {
91
98
  min: rest.min ?? 1,
92
99
  max: rest.max ?? 99,
@@ -94,20 +101,33 @@ export const FormField = ({
94
101
  showValue: true,
95
102
  showLabels: true,
96
103
  };
97
- if (rest.datatype === "text") return { rows: 3 };
98
- if (rest.datatype === "color") return { showPicker: true };
104
+ if (rest.datatype === DATA_TYPES.DATE)
105
+ return {
106
+ min: rest.min,
107
+ max: rest.max,
108
+ type: "date",
109
+ };
110
+ if (rest.datatype === DATA_TYPES.DATETIME)
111
+ return {
112
+ min: rest.min,
113
+ max: rest.max,
114
+ type: "datetime",
115
+ };
116
+ if (rest.datatype === DATA_TYPES.TEXT) return { rows: 3 };
117
+ if (rest.datatype === DATA_TYPES.COLOR) return { showPicker: true };
99
118
 
100
- if (rest.datatype === "string" && rest.options)
119
+ if (rest.datatype === DATA_TYPES.STRING && hasOptions)
101
120
  return {
102
- options: rest.options,
103
121
  multiple: rest.multiple,
122
+ allowCreate: rest.allowCustom,
104
123
  searchable: true,
124
+ ...(optionLists?.[rest.optionList ?? ""] ?? { options: rest.options }),
105
125
  };
106
126
 
107
127
  return {};
108
- }, [rest]);
128
+ }, [rest, hasOptions, optionLists]);
109
129
 
110
- if (rest.datatype === "avatar")
130
+ if (rest.datatype === DATA_TYPES.AVATAR)
111
131
  return (
112
132
  uploadHandler && (
113
133
  <Controller name={fieldName}>
@@ -120,7 +140,7 @@ export const FormField = ({
120
140
  )
121
141
  );
122
142
 
123
- if (rest.datatype === "file")
143
+ if (rest.datatype === DATA_TYPES.FILE)
124
144
  return (
125
145
  uploadHandler && (
126
146
  <Controller name={fieldName}>
@@ -135,12 +155,12 @@ export const FormField = ({
135
155
  )
136
156
  );
137
157
 
138
- return rest.multiple && !("options" in rest) ? (
158
+ return rest.multiple && !hasOptions ? (
139
159
  <ArrayInput
140
160
  name={fieldName}
141
- label={t(label, label)}
161
+ label={t(`fb.${id}`, label || id)}
142
162
  required={required}
143
- onAdd={() => ""}
163
+ onAdd={() => String("")}
144
164
  >
145
165
  <Controller>
146
166
  <E {...props} />
@@ -151,8 +171,8 @@ export const FormField = ({
151
171
  <E
152
172
  {...props}
153
173
  autoFocus={autoFocus}
154
- label={t(label, label)}
155
- placeholder={t(label, label)}
174
+ label={t(`fb.${id}`, label || id)}
175
+ placeholder={t(`fb.${id}`, label || id)}
156
176
  required={required}
157
177
  inline={inline}
158
178
  />
@@ -0,0 +1,79 @@
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
+ /**
36
+ * SchemaEditor component allows users to edit a schema definition.
37
+ * It provides a form interface to modify schema properties dynamically.
38
+ *
39
+ * @param {SchemaEditorProps} props - The properties for the SchemaEditor component.
40
+ * @returns {JSX.Element} The rendered SchemaEditor component.
41
+ */
42
+ export const SchemaEditor = ({
43
+ schemaDef,
44
+ dynamic,
45
+ acceptableTypes,
46
+ optionLists = EMPTY_ARRAY,
47
+ children,
48
+ onSubmit,
49
+ onChange,
50
+ }: SchemaEditorProps) => {
51
+ const formRef = useRef<FormRef<{ schema: FormSchema }>>(null);
52
+
53
+ const handleChange = useCallback((model: { schema: FormSchema }) => {
54
+ onChange?.(model.schema);
55
+ }, []);
56
+
57
+ const handleSubmit = useCallback((model: { schema: FormSchema }) => {
58
+ onSubmit?.(model.schema);
59
+ }, []);
60
+
61
+ useEffect(() => {
62
+ formRef.current?.setValues({ schema: schemaDef });
63
+ }, [schemaDef]);
64
+
65
+ return (
66
+ <Form formRef={formRef} onChange={handleChange} onSubmit={handleSubmit}>
67
+ <SchemaArrayForm
68
+ dynamic={dynamic}
69
+ optionLists={optionLists}
70
+ acceptableTypes={acceptableTypes ?? Object.values(DATA_TYPES)}
71
+ />
72
+ {children && (
73
+ <Footer flex className="border-t bg-base px-6 py-2">
74
+ {children}
75
+ </Footer>
76
+ )}
77
+ </Form>
78
+ );
79
+ };
@@ -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
+ };