@react-fabric/form 1.0.0-beta.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.
Files changed (187) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +10 -0
  3. package/dist/cjs/builder/FileField.js +131 -0
  4. package/dist/cjs/builder/FormField.js +119 -0
  5. package/dist/cjs/form/Controller.js +64 -0
  6. package/dist/cjs/form/Form.js +128 -0
  7. package/dist/cjs/hooks/useFileUploader.js +204 -0
  8. package/dist/cjs/hooks/useFormBuilder.js +87 -0
  9. package/dist/cjs/i18n/ar.json.js +37 -0
  10. package/dist/cjs/i18n/en.json.js +38 -0
  11. package/dist/cjs/i18n/index.js +33 -0
  12. package/dist/cjs/index.js +56 -0
  13. package/dist/cjs/input/ArrayInput.js +189 -0
  14. package/dist/cjs/input/ArrayInput.module.css +1 -0
  15. package/dist/cjs/input/AvatarInput.js +141 -0
  16. package/dist/cjs/input/Checkbox.js +83 -0
  17. package/dist/cjs/input/ColorInput.js +100 -0
  18. package/dist/cjs/input/ColorPicker.js +122 -0
  19. package/dist/cjs/input/ColorPicker.module.css +1 -0
  20. package/dist/cjs/input/Field.js +31 -0
  21. package/dist/cjs/input/File.js +165 -0
  22. package/dist/cjs/input/File.module.css +1 -0
  23. package/dist/cjs/input/Hidden.js +54 -0
  24. package/dist/cjs/input/Input.js +79 -0
  25. package/dist/cjs/input/Number.js +80 -0
  26. package/dist/cjs/input/Password.js +104 -0
  27. package/dist/cjs/input/Radio.js +70 -0
  28. package/dist/cjs/input/RadioGroup.js +31 -0
  29. package/dist/cjs/input/Range.js +169 -0
  30. package/dist/cjs/input/Search.js +109 -0
  31. package/dist/cjs/input/Slider.js +172 -0
  32. package/dist/cjs/input/Switch.js +79 -0
  33. package/dist/cjs/input/Textarea.js +87 -0
  34. package/dist/cjs/internal/ErrorIcon.js +32 -0
  35. package/dist/cjs/internal/Field.module.css +1 -0
  36. package/dist/cjs/internal/FieldWrapper.js +78 -0
  37. package/dist/cjs/internal/InputWrapper.js +113 -0
  38. package/dist/cjs/ranger/index.js +352 -0
  39. package/dist/cjs/ranger/utils.js +49 -0
  40. package/dist/cjs/select/AutoComplete.js +268 -0
  41. package/dist/cjs/select/DualList.js +238 -0
  42. package/dist/cjs/select/List.js +241 -0
  43. package/dist/cjs/select/Options.js +76 -0
  44. package/dist/cjs/select/Select.js +283 -0
  45. package/dist/cjs/select/helpers.js +41 -0
  46. package/dist/cjs/select/useSelect.js +337 -0
  47. package/dist/cjs/utils/eventHandlers.js +50 -0
  48. package/dist/cjs/utils/getColor.js +40 -0
  49. package/dist/esm/builder/FileField.js +131 -0
  50. package/dist/esm/builder/FormField.js +119 -0
  51. package/dist/esm/form/Controller.js +64 -0
  52. package/dist/esm/form/Form.js +128 -0
  53. package/dist/esm/hooks/useFileUploader.js +204 -0
  54. package/dist/esm/hooks/useFormBuilder.js +87 -0
  55. package/dist/esm/i18n/ar.json.js +31 -0
  56. package/dist/esm/i18n/en.json.js +32 -0
  57. package/dist/esm/i18n/index.js +33 -0
  58. package/dist/esm/index.js +28 -0
  59. package/dist/esm/input/ArrayInput.js +189 -0
  60. package/dist/esm/input/ArrayInput.module.css +1 -0
  61. package/dist/esm/input/AvatarInput.js +141 -0
  62. package/dist/esm/input/Checkbox.js +83 -0
  63. package/dist/esm/input/ColorInput.js +100 -0
  64. package/dist/esm/input/ColorPicker.js +122 -0
  65. package/dist/esm/input/ColorPicker.module.css +1 -0
  66. package/dist/esm/input/Field.js +31 -0
  67. package/dist/esm/input/File.js +165 -0
  68. package/dist/esm/input/File.module.css +1 -0
  69. package/dist/esm/input/Hidden.js +54 -0
  70. package/dist/esm/input/Input.js +79 -0
  71. package/dist/esm/input/Number.js +80 -0
  72. package/dist/esm/input/Password.js +104 -0
  73. package/dist/esm/input/Radio.js +70 -0
  74. package/dist/esm/input/RadioGroup.js +31 -0
  75. package/dist/esm/input/Range.js +169 -0
  76. package/dist/esm/input/Search.js +109 -0
  77. package/dist/esm/input/Slider.js +172 -0
  78. package/dist/esm/input/Switch.js +79 -0
  79. package/dist/esm/input/Textarea.js +87 -0
  80. package/dist/esm/internal/ErrorIcon.js +32 -0
  81. package/dist/esm/internal/Field.module.css +1 -0
  82. package/dist/esm/internal/FieldWrapper.js +78 -0
  83. package/dist/esm/internal/InputWrapper.js +113 -0
  84. package/dist/esm/ranger/index.js +352 -0
  85. package/dist/esm/ranger/utils.js +47 -0
  86. package/dist/esm/select/AutoComplete.js +268 -0
  87. package/dist/esm/select/DualList.js +238 -0
  88. package/dist/esm/select/List.js +241 -0
  89. package/dist/esm/select/Options.js +76 -0
  90. package/dist/esm/select/Select.js +283 -0
  91. package/dist/esm/select/helpers.js +40 -0
  92. package/dist/esm/select/useSelect.js +337 -0
  93. package/dist/esm/utils/eventHandlers.js +50 -0
  94. package/dist/esm/utils/getColor.js +39 -0
  95. package/dist/types/builder/FileField.d.ts +8 -0
  96. package/dist/types/builder/FormField.d.ts +9 -0
  97. package/dist/types/form/Controller.d.ts +20 -0
  98. package/dist/types/form/Form.d.ts +33 -0
  99. package/dist/types/hooks/useFileUploader.d.ts +23 -0
  100. package/dist/types/hooks/useFormBuilder.d.ts +16 -0
  101. package/dist/types/i18n/index.d.ts +1 -0
  102. package/dist/types/index.d.ts +27 -0
  103. package/dist/types/input/ArrayInput.d.ts +64 -0
  104. package/dist/types/input/AvatarInput.d.ts +31 -0
  105. package/dist/types/input/Checkbox.d.ts +46 -0
  106. package/dist/types/input/ColorInput.d.ts +10 -0
  107. package/dist/types/input/ColorPicker.d.ts +24 -0
  108. package/dist/types/input/Field.d.ts +41 -0
  109. package/dist/types/input/File.d.ts +17 -0
  110. package/dist/types/input/Hidden.d.ts +16 -0
  111. package/dist/types/input/Input.d.ts +9 -0
  112. package/dist/types/input/Number.d.ts +17 -0
  113. package/dist/types/input/Password.d.ts +13 -0
  114. package/dist/types/input/Radio.d.ts +42 -0
  115. package/dist/types/input/RadioGroup.d.ts +30 -0
  116. package/dist/types/input/Range.d.ts +59 -0
  117. package/dist/types/input/Search.d.ts +17 -0
  118. package/dist/types/input/Slider.d.ts +63 -0
  119. package/dist/types/input/Switch.d.ts +42 -0
  120. package/dist/types/input/Textarea.d.ts +17 -0
  121. package/dist/types/internal/ErrorIcon.d.ts +1 -0
  122. package/dist/types/internal/FieldWrapper.d.ts +1 -0
  123. package/dist/types/internal/InputWrapper.d.ts +1 -0
  124. package/dist/types/ranger/index.d.ts +70 -0
  125. package/dist/types/ranger/utils.d.ts +25 -0
  126. package/dist/types/select/AutoComplete.d.ts +17 -0
  127. package/dist/types/select/DualList.d.ts +38 -0
  128. package/dist/types/select/List.d.ts +4 -0
  129. package/dist/types/select/Options.d.ts +14 -0
  130. package/dist/types/select/Select.d.ts +2 -0
  131. package/dist/types/select/helpers.d.ts +2 -0
  132. package/dist/types/select/useSelect.d.ts +50 -0
  133. package/dist/types/types/index.d.ts +169 -0
  134. package/dist/types/types/schema.d.ts +59 -0
  135. package/dist/types/utils/eventHandlers.d.ts +11 -0
  136. package/dist/types/utils/getColor.d.ts +4 -0
  137. package/dist/types/utils/index.d.ts +2 -0
  138. package/package.json +64 -0
  139. package/src/builder/FileField.tsx +122 -0
  140. package/src/builder/FormField.tsx +161 -0
  141. package/src/form/Controller.tsx +103 -0
  142. package/src/form/Form.tsx +160 -0
  143. package/src/hooks/useFileUploader.ts +234 -0
  144. package/src/hooks/useFormBuilder.tsx +128 -0
  145. package/src/i18n/ar.json +23 -0
  146. package/src/i18n/en.json +24 -0
  147. package/src/i18n/index.ts +28 -0
  148. package/src/index.ts +54 -0
  149. package/src/input/ArrayInput.module.css +41 -0
  150. package/src/input/ArrayInput.tsx +318 -0
  151. package/src/input/AvatarInput.tsx +150 -0
  152. package/src/input/Checkbox.tsx +165 -0
  153. package/src/input/ColorInput.tsx +157 -0
  154. package/src/input/ColorPicker.module.css +51 -0
  155. package/src/input/ColorPicker.tsx +210 -0
  156. package/src/input/Field.tsx +96 -0
  157. package/src/input/File.module.css +42 -0
  158. package/src/input/File.tsx +224 -0
  159. package/src/input/Hidden.tsx +65 -0
  160. package/src/input/Input.tsx +114 -0
  161. package/src/input/Number.tsx +126 -0
  162. package/src/input/Password.tsx +150 -0
  163. package/src/input/Radio.tsx +146 -0
  164. package/src/input/RadioGroup.tsx +75 -0
  165. package/src/input/Range.tsx +276 -0
  166. package/src/input/Search.tsx +160 -0
  167. package/src/input/Slider.tsx +282 -0
  168. package/src/input/Switch.tsx +168 -0
  169. package/src/input/Textarea.tsx +127 -0
  170. package/src/internal/ErrorIcon.tsx +61 -0
  171. package/src/internal/Field.module.css +139 -0
  172. package/src/internal/FieldWrapper.tsx +84 -0
  173. package/src/internal/InputWrapper.tsx +187 -0
  174. package/src/ranger/index.tsx +514 -0
  175. package/src/ranger/utils.ts +48 -0
  176. package/src/select/AutoComplete.tsx +354 -0
  177. package/src/select/DualList.tsx +286 -0
  178. package/src/select/List.tsx +288 -0
  179. package/src/select/Options.tsx +121 -0
  180. package/src/select/Select.tsx +350 -0
  181. package/src/select/helpers.ts +36 -0
  182. package/src/select/useSelect.tsx +413 -0
  183. package/src/types/index.ts +203 -0
  184. package/src/types/schema.ts +100 -0
  185. package/src/utils/eventHandlers.ts +72 -0
  186. package/src/utils/getColor.ts +47 -0
  187. package/src/utils/index.ts +25 -0
@@ -0,0 +1,50 @@
1
+ /// <reference types="react" />
2
+ interface State {
3
+ multiple: boolean;
4
+ emptyValue: AnyObject;
5
+ open: boolean;
6
+ loading: boolean;
7
+ activeIndex: number | null;
8
+ selectedIndex: number | null;
9
+ value: AnyObject;
10
+ query?: string;
11
+ items: AnyObject[];
12
+ }
13
+ export declare const useSelect: ({ options, value, autoComplete, multiple, allowCreate, groupProperty, valueProperty, labelProperty, createOption, matcher, onQuery, onChange, onSelect, }: {
14
+ value?: AnyObject;
15
+ options?: AnyObject[];
16
+ multiple?: boolean;
17
+ allowCreate?: boolean;
18
+ groupProperty?: AnyObject;
19
+ valueProperty?: AnyObject;
20
+ labelProperty?: AnyObject;
21
+ autoComplete?: boolean;
22
+ matcher?: (item: AnyObject, query: string) => boolean;
23
+ createOption?: (query: string) => AnyObject;
24
+ onQuery?: (query: string) => AnyObject;
25
+ onSelect?: (item: AnyObject) => void;
26
+ onChange?: (value: AnyObject) => void;
27
+ }) => {
28
+ state: State | {
29
+ value: any;
30
+ activeIndex: null;
31
+ selectedIndex: any;
32
+ multiple: boolean;
33
+ emptyValue: AnyObject;
34
+ open: boolean;
35
+ loading: boolean;
36
+ query?: string | undefined;
37
+ items: AnyObject[];
38
+ };
39
+ listRef: import("react").MutableRefObject<HTMLElement[]>;
40
+ focusableRef: import("react").RefObject<HTMLElement>;
41
+ listContentRef: import("react").MutableRefObject<any[]>;
42
+ handleChange: (value?: AnyObject) => void;
43
+ handleRemove: (index?: number) => void;
44
+ handleQuery: (query: string) => void;
45
+ macthOption: (item: AnyObject, query: string) => boolean;
46
+ setActiveIndex: (index: number | null) => void;
47
+ setOpen: (open: boolean) => void;
48
+ setItemRef: (index: number, node: HTMLElement) => void;
49
+ };
50
+ export {};
@@ -0,0 +1,169 @@
1
+ /// <reference types="react" />
2
+ import { type RefProp } from "@react-fabric/core/dist/types/types";
3
+ export interface InputProps<V = string> {
4
+ /**
5
+ * value
6
+ */
7
+ value?: V;
8
+ /**
9
+ * change handler
10
+ */
11
+ onChange?: (value?: V) => void;
12
+ /**
13
+ * on focus event handler
14
+ */
15
+ onFocus?: React.FocusEventHandler;
16
+ /**
17
+ * on blur event handler
18
+ */
19
+ onBlur?: React.FocusEventHandler;
20
+ /**
21
+ * on enter press callback
22
+ */
23
+ onEnterPressed?: React.KeyboardEventHandler;
24
+ /**
25
+ * input name
26
+ */
27
+ name?: string;
28
+ /**
29
+ * input label
30
+ */
31
+ label?: string;
32
+ /**
33
+ * input placeholder
34
+ */
35
+ placeholder?: string;
36
+ /**
37
+ * append to label end
38
+ */
39
+ appendLabel?: JSX.Element | string | number | boolean;
40
+ /**
41
+ *
42
+ */
43
+ decorateStart?: JSX.Element | string | number | boolean;
44
+ /**
45
+ *
46
+ */
47
+ decorateEnd?: JSX.Element | string | number | boolean;
48
+ /**
49
+ * info label at bottom
50
+ */
51
+ info?: string;
52
+ /**
53
+ * required
54
+ */
55
+ required?: boolean;
56
+ /**
57
+ * disabled input
58
+ */
59
+ disabled?: boolean;
60
+ /**
61
+ * read-only input
62
+ */
63
+ readOnly?: boolean;
64
+ /**
65
+ * auto focus when rendered
66
+ */
67
+ autoFocus?: boolean;
68
+ /**
69
+ * show clear button
70
+ */
71
+ allowClear?: boolean;
72
+ /**
73
+ * invalid value input
74
+ */
75
+ invalid?: boolean;
76
+ /**
77
+ * error message
78
+ */
79
+ error?: string;
80
+ /**
81
+ * field width
82
+ */
83
+ width?: number | string;
84
+ /**
85
+ * inline label and input
86
+ */
87
+ inline?: boolean;
88
+ /**
89
+ * label width for inline
90
+ */
91
+ labelWidth?: string;
92
+ }
93
+ export interface BaseSelectProps<T> extends RefProp<HTMLInputElement>, Omit<InputProps, "value" | "onChange"> {
94
+ /**
95
+ * list options
96
+ */
97
+ options?: T[];
98
+ /**
99
+ * group by property name
100
+ */
101
+ groupProperty?: keyof T;
102
+ /**
103
+ * label property name
104
+ */
105
+ labelProperty?: keyof T;
106
+ /**
107
+ * value property name
108
+ */
109
+ valueProperty?: keyof T;
110
+ /**
111
+ * searchable list
112
+ */
113
+ searchable?: boolean;
114
+ /**
115
+ * allow create item
116
+ */
117
+ allowCreate?: boolean;
118
+ /**
119
+ * create new item object
120
+ */
121
+ createOption?: (query: string) => T;
122
+ /**
123
+ * query handler
124
+ */
125
+ onQuery?: (query: string) => T[] | Promise<T[]>;
126
+ /**
127
+ * item renderer
128
+ */
129
+ renderer?: (option: T) => React.ReactNode;
130
+ /**
131
+ * item matcher
132
+ */
133
+ matcher?: (option: T, query: string) => boolean;
134
+ }
135
+ export type SelectProps<T> = BaseSelectProps<T> & ({
136
+ /**
137
+ * is multiple selection
138
+ */
139
+ multiple?: never | false;
140
+ /**
141
+ * selected value
142
+ */
143
+ value?: string;
144
+ /**
145
+ * select handler
146
+ */
147
+ onSelect?: (option: T) => void;
148
+ /**
149
+ * change handler
150
+ */
151
+ onChange?: (value?: string) => void;
152
+ } | {
153
+ /**
154
+ * is multiple selection
155
+ */
156
+ multiple: true;
157
+ /**
158
+ * selected value
159
+ */
160
+ value?: string[];
161
+ /**
162
+ * select handler
163
+ */
164
+ onSelect?: (option: T[]) => void;
165
+ /**
166
+ * change handler
167
+ */
168
+ onChange?: (value?: string[]) => void;
169
+ });
@@ -0,0 +1,59 @@
1
+ interface BaseSchemaDef {
2
+ id: string;
3
+ label: string;
4
+ datatype: string;
5
+ required?: boolean;
6
+ multiple?: boolean;
7
+ defaultValue?: AnyObject;
8
+ }
9
+ interface SchemaDef extends BaseSchemaDef {
10
+ datatype: "text" | "color";
11
+ }
12
+ interface BooleanSchema extends BaseSchemaDef {
13
+ datatype: "boolean";
14
+ defaultValue?: boolean;
15
+ }
16
+ interface StringSchema extends BaseSchemaDef {
17
+ datatype: "string";
18
+ options?: string[];
19
+ regex?: string;
20
+ identifierType?: string;
21
+ }
22
+ interface RangeSchema extends BaseSchemaDef {
23
+ datatype: "range";
24
+ min?: number;
25
+ max?: number;
26
+ step?: number;
27
+ defaultValue?: [number, number];
28
+ }
29
+ interface NumberSchema extends BaseSchemaDef {
30
+ datatype: "number";
31
+ min?: number;
32
+ max?: number;
33
+ step?: number;
34
+ defaultValue?: number;
35
+ }
36
+ interface DecimalSchema extends BaseSchemaDef {
37
+ datatype: "decimal";
38
+ min?: number;
39
+ max?: number;
40
+ step?: number;
41
+ defaultValue?: number;
42
+ }
43
+ export interface FileSchema extends BaseSchemaDef {
44
+ datatype: "file";
45
+ accept?: string;
46
+ }
47
+ export interface AvatarSchema extends BaseSchemaDef {
48
+ datatype: "avatar";
49
+ multiple?: false;
50
+ }
51
+ export type SchemaType = SchemaDef | BooleanSchema | StringSchema | RangeSchema | NumberSchema | DecimalSchema | FileSchema | AvatarSchema;
52
+ export interface ChildSchema extends BaseSchemaDef {
53
+ datatype: "schema";
54
+ inline?: boolean;
55
+ schema: SchemaType[];
56
+ defaultValue?: never;
57
+ }
58
+ export type FormSchema = SchemaType | ChildSchema;
59
+ export {};
@@ -0,0 +1,11 @@
1
+ import { type KeyboardEvent, type MouseEvent } from "react";
2
+ /** ***************** common handler for enter press *******************/
3
+ export declare const handleEnter: (callback?: (e: KeyboardEvent) => void, { stopPropagation, preventDefault, }?: {
4
+ stopPropagation?: boolean;
5
+ preventDefault?: boolean;
6
+ }) => (e: KeyboardEvent) => false | undefined;
7
+ /** ***************** common handler for mouse primary click *******************/
8
+ export declare const handleClick: (callback?: (e: MouseEvent) => void, { stopPropagation, preventDefault, }?: {
9
+ stopPropagation?: boolean;
10
+ preventDefault?: boolean;
11
+ }) => (e: MouseEvent) => false | undefined;
@@ -0,0 +1,4 @@
1
+ export declare const getColor: (color?: string) => string | undefined;
2
+ export declare const getBgClass: (bg: string) => string | undefined;
3
+ export declare const getColorClass: (color: string) => string | undefined;
4
+ export declare const getBorderClass: (color: string) => string | undefined;
@@ -0,0 +1,2 @@
1
+ export * from "./eventHandlers";
2
+ export * from "./getColor";
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@react-fabric/form",
3
+ "version": "1.0.0-beta.0",
4
+ "description": "RUF: Form package",
5
+ "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
+ "license": "MIT",
7
+ "repository": "https://github.com/adarshpastakia/react-ui-framework/tree/main/packages/form",
8
+ "source": "src/index.ts",
9
+ "main": "dist/cjs/index.js",
10
+ "module": "dist/esm/index.js",
11
+ "types": "dist/types/index.d.ts",
12
+ "directories": {
13
+ "src": "src",
14
+ "test": "tests"
15
+ },
16
+ "files": [
17
+ "src",
18
+ "dist"
19
+ ],
20
+ "publishConfig": {
21
+ "access": "public",
22
+ "registry": "https://registry.npmjs.org/"
23
+ },
24
+ "scripts": {
25
+ "makelink": "yarn link",
26
+ "removelink": "yarn unlink",
27
+ "prebuild": "rm -rf ./dist",
28
+ "build:dts": "tsc -p ./tsconfig.build.json",
29
+ "build:rollup": "rollup -c ../../rollup.config.mjs",
30
+ "build": "npm-run-all -p build:rollup build:dts",
31
+ "watch:dts": "tsc -w -p ./tsconfig.build.json",
32
+ "watch:rollup": "rollup -w -c ../../rollup.config.mjs",
33
+ "watch": "npm-run-all -p watch:rollup watch:dts"
34
+ },
35
+ "dependencies": {
36
+ "@dnd-kit/core": "^6.1.0",
37
+ "@dnd-kit/modifiers": "^7.0.0",
38
+ "@dnd-kit/sortable": "^8.0.0",
39
+ "@dnd-kit/utilities": "^3.2.2",
40
+ "@floating-ui/react": "0.26.24",
41
+ "@hookform/resolvers": "^3.9.0",
42
+ "@react-fabric/core": "^1.0.0-beta.0",
43
+ "@react-fabric/utilities": "^1.0.0-beta.0",
44
+ "classnames": "^2.5.1",
45
+ "color": "^4.2.3",
46
+ "react-color": "^2.19.3",
47
+ "react-hook-form": "^7.52.1"
48
+ },
49
+ "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"
54
+ },
55
+ "devDependencies": {
56
+ "@types/axios": "^0.14.0",
57
+ "@types/react-color": "^3.0.12"
58
+ },
59
+ "engines": {
60
+ "node": ">=18.12.0",
61
+ "npm": ">=8.19.0"
62
+ },
63
+ "gitHead": "83fa79064b1d57e71149005432192d506810bbda"
64
+ }
@@ -0,0 +1,122 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 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 { Button, Icon, ProgressBar } from "@react-fabric/core";
25
+ import { FileUtil, Format } from "@react-fabric/utilities";
26
+ import { Fragment } from "react";
27
+ import { type UploadHandler, useFileUploader } from "../hooks/useFileUploader";
28
+ import { Field } from "../input/Field";
29
+ import { HiddenInput } from "../input/Hidden";
30
+ import { type FileSchema } from "../types/schema";
31
+
32
+ export const FileField = ({
33
+ uploadHandler,
34
+ inline,
35
+ label,
36
+ accept,
37
+ multiple = false,
38
+ value,
39
+ ...rest
40
+ }: Partial<FileSchema> & {
41
+ value?: AnyObject;
42
+ inline?: boolean;
43
+ fileUrl?: (path: string) => string;
44
+ uploadHandler: UploadHandler;
45
+ }) => {
46
+ const { pending, files, list, upload, remove } = useFileUploader(
47
+ async (data, config) => await uploadHandler?.(data, config),
48
+ value,
49
+ multiple,
50
+ );
51
+
52
+ return (
53
+ <Fragment>
54
+ <Field plain inline={inline} label={label} className="inline-block">
55
+ <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
+ </Field>
77
+ <br />
78
+ <div className="bg-alternate">
79
+ {files.map((file, idx) => (
80
+ <div key={idx} className="px-2 py-1">
81
+ <div className="flex flex-nowrap gap-1 items-start">
82
+ <div className="flex-1 truncate pe-2">
83
+ {file.filename}
84
+ <br />
85
+ <span className="text-xs">{FileUtil.mime(file.mime)}</span>
86
+ <span className="text-xs px-1">{Format.bytes(file.size)}</span>
87
+ </div>
88
+ <div className="flex-initial truncate px-2">
89
+ {file.error && (
90
+ <span className="text-danger-600">{file.error}</span>
91
+ )}
92
+ </div>
93
+ {file.progress && (
94
+ <Icon
95
+ icon="mdi mdi-stop-circle"
96
+ bg="danger"
97
+ color="white"
98
+ size="sm"
99
+ className="p-[2px]"
100
+ aria-label="abort"
101
+ onClick={() => file.abort?.()}
102
+ />
103
+ )}
104
+ {!file.progress && (
105
+ <Icon
106
+ icon="mdi mdi-trash-can"
107
+ color="danger"
108
+ size="sm"
109
+ aria-label="remove"
110
+ onClick={() => remove(file.key)}
111
+ />
112
+ )}
113
+ </div>
114
+ {file.progress && (
115
+ <ProgressBar animate value={file.progress * 100} size="xxs" />
116
+ )}
117
+ </div>
118
+ ))}
119
+ </div>
120
+ </Fragment>
121
+ );
122
+ };
@@ -0,0 +1,161 @@
1
+ /*
2
+ * React Fabric
3
+ * @version: 1.0.0
4
+ *
5
+ *
6
+ * The MIT License (MIT)
7
+ * Copyright (c) 2024 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 { useMemo } from "react";
25
+ import { useTranslation } from "react-i18next";
26
+ import { Controller } from "../form/Controller";
27
+ import { type UploadHandler } from "../hooks/useFileUploader";
28
+ import { ArrayInput } from "../input/ArrayInput";
29
+ import { AvatarInput } from "../input/AvatarInput";
30
+ import { ColorInput } from "../input/ColorInput";
31
+ import { Input } from "../input/Input";
32
+ import { Number } from "../input/Number";
33
+ import { Range } from "../input/Range";
34
+ import { Switch } from "../input/Switch";
35
+ import { Textarea } from "../input/Textarea";
36
+ import { Select } from "../select/Select";
37
+ import { type SchemaType } from "../types/schema";
38
+ import { FileField } from "./FileField";
39
+
40
+ export const FormField = ({
41
+ id,
42
+ label,
43
+ required,
44
+ inline,
45
+ prefix,
46
+ autoFocus,
47
+ defaultValue,
48
+ fileUrl,
49
+ uploadHandler,
50
+ ...rest
51
+ }: SchemaType & {
52
+ inline?: boolean;
53
+ prefix?: string;
54
+ autoFocus?: boolean;
55
+ fileUrl?: (path: string) => string;
56
+ uploadHandler?: UploadHandler;
57
+ }) => {
58
+ const { t } = useTranslation();
59
+ const fieldName = useMemo(
60
+ () => [prefix, id].filter(Boolean).join("."),
61
+ [id, prefix],
62
+ );
63
+
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;
72
+
73
+ return Input;
74
+ }, [rest.datatype]);
75
+
76
+ const props = useMemo(() => {
77
+ if (rest.datatype === "number")
78
+ return {
79
+ min: rest.min,
80
+ max: rest.max,
81
+ step: rest.step ?? 1,
82
+ };
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")
90
+ return {
91
+ min: rest.min ?? 1,
92
+ max: rest.max ?? 99,
93
+ step: rest.step ?? 1,
94
+ showValue: true,
95
+ showLabels: true,
96
+ };
97
+ if (rest.datatype === "text") return { rows: 3 };
98
+ if (rest.datatype === "color") return { showPicker: true };
99
+
100
+ if (rest.datatype === "string" && rest.options)
101
+ return {
102
+ options: rest.options,
103
+ multiple: rest.multiple,
104
+ searchable: true,
105
+ };
106
+
107
+ return {};
108
+ }, [rest]);
109
+
110
+ if (rest.datatype === "avatar")
111
+ return (
112
+ uploadHandler && (
113
+ <Controller name={fieldName}>
114
+ <AvatarInput
115
+ uploadHandler={uploadHandler}
116
+ fileUrl={fileUrl}
117
+ name={fieldName}
118
+ />
119
+ </Controller>
120
+ )
121
+ );
122
+
123
+ if (rest.datatype === "file")
124
+ return (
125
+ uploadHandler && (
126
+ <Controller name={fieldName}>
127
+ <FileField
128
+ uploadHandler={uploadHandler}
129
+ inline={inline}
130
+ label={label}
131
+ id={id}
132
+ {...rest}
133
+ />
134
+ </Controller>
135
+ )
136
+ );
137
+
138
+ return rest.multiple && !("options" in rest) ? (
139
+ <ArrayInput
140
+ name={fieldName}
141
+ label={t(label, label)}
142
+ required={required}
143
+ onAdd={() => ""}
144
+ >
145
+ <Controller>
146
+ <E {...props} />
147
+ </Controller>
148
+ </ArrayInput>
149
+ ) : (
150
+ <Controller name={fieldName}>
151
+ <E
152
+ {...props}
153
+ autoFocus={autoFocus}
154
+ label={t(label, label)}
155
+ placeholder={t(label, label)}
156
+ required={required}
157
+ inline={inline}
158
+ />
159
+ </Controller>
160
+ );
161
+ };