@react-fabric/form 1.0.0-beta.31 → 1.0.0-beta.33

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 (61) hide show
  1. package/dist/cjs/builder/FileField.js +4 -4
  2. package/dist/cjs/builder/FormField.js +41 -31
  3. package/dist/cjs/builder/SchemaEditor.js +428 -0
  4. package/dist/cjs/form/Controller.js +1 -1
  5. package/dist/cjs/form/Form.js +3 -4
  6. package/dist/cjs/hooks/useFormBuilder.js +27 -15
  7. package/dist/cjs/index.js +8 -2
  8. package/dist/cjs/input/Checkbox.js +4 -3
  9. package/dist/cjs/input/DateInput.js +183 -0
  10. package/dist/cjs/input/Field.js +1 -1
  11. package/dist/cjs/input/{Range.js → RangeSlider.js} +2 -2
  12. package/dist/cjs/input/Switch.js +4 -3
  13. package/dist/cjs/internal/Field.module.css +1 -1
  14. package/dist/cjs/internal/InputWrapper.js +6 -4
  15. package/dist/cjs/types/schema.js +40 -0
  16. package/dist/esm/builder/FileField.js +4 -4
  17. package/dist/esm/builder/FormField.js +41 -31
  18. package/dist/esm/builder/SchemaEditor.js +428 -0
  19. package/dist/esm/form/Controller.js +1 -1
  20. package/dist/esm/form/Form.js +4 -5
  21. package/dist/esm/hooks/useFormBuilder.js +25 -13
  22. package/dist/esm/index.js +4 -1
  23. package/dist/esm/input/Checkbox.js +4 -3
  24. package/dist/esm/input/DateInput.js +183 -0
  25. package/dist/esm/input/Field.js +1 -1
  26. package/dist/esm/input/{Range.js → RangeSlider.js} +2 -2
  27. package/dist/esm/input/Switch.js +4 -3
  28. package/dist/esm/internal/Field.module.css +1 -1
  29. package/dist/esm/internal/InputWrapper.js +6 -4
  30. package/dist/esm/types/schema.js +40 -0
  31. package/dist/types/builder/FileField.d.ts +2 -2
  32. package/dist/types/builder/FormField.d.ts +3 -1
  33. package/dist/types/builder/SchemaEditor.d.ts +10 -0
  34. package/dist/types/form/Controller.d.ts +1 -1
  35. package/dist/types/form/Form.d.ts +5 -1
  36. package/dist/types/hooks/useFormBuilder.d.ts +3 -1
  37. package/dist/types/index.d.ts +4 -2
  38. package/dist/types/input/Checkbox.d.ts +4 -3
  39. package/dist/types/input/DateInput.d.ts +19 -0
  40. package/dist/types/input/{Range.d.ts → RangeSlider.d.ts} +1 -1
  41. package/dist/types/input/Switch.d.ts +1 -1
  42. package/dist/types/internal/InputWrapper.d.ts +1 -1
  43. package/dist/types/types/index.d.ts +6 -2
  44. package/dist/types/types/schema.d.ts +52 -33
  45. package/package.json +5 -4
  46. package/src/builder/FileField.tsx +5 -4
  47. package/src/builder/FormField.tsx +42 -29
  48. package/src/builder/SchemaEditor.tsx +441 -0
  49. package/src/form/Controller.tsx +2 -2
  50. package/src/form/Form.tsx +6 -4
  51. package/src/hooks/useFormBuilder.tsx +29 -15
  52. package/src/index.ts +5 -2
  53. package/src/input/Checkbox.tsx +4 -3
  54. package/src/input/DateInput.tsx +248 -0
  55. package/src/input/Field.tsx +2 -1
  56. package/src/input/{Range.tsx → RangeSlider.tsx} +1 -1
  57. package/src/input/Switch.tsx +2 -0
  58. package/src/internal/Field.module.css +12 -1
  59. package/src/internal/InputWrapper.tsx +7 -2
  60. package/src/types/index.ts +8 -4
  61. package/src/types/schema.ts +65 -41
@@ -45,6 +45,8 @@ export interface InputProps<V = string> extends TestProps {
45
45
  *
46
46
  */
47
47
  decorateEnd?: JSX.Element | string | number | boolean;
48
+ decorateEndShowWhenEmpty?: boolean;
49
+ decorateEndHideWhenEmpty?: boolean;
48
50
  /**
49
51
  * info label at bottom
50
52
  */
@@ -90,7 +92,7 @@ export interface InputProps<V = string> extends TestProps {
90
92
  */
91
93
  labelWidth?: string;
92
94
  }
93
- export interface BaseSelectProps<T> extends RefProp<HTMLInputElement>, Omit<InputProps, "value" | "onChange"> {
95
+ export interface BaseSelectProps<T extends AnyObject = string> {
94
96
  /**
95
97
  * list options
96
98
  */
@@ -132,7 +134,8 @@ export interface BaseSelectProps<T> extends RefProp<HTMLInputElement>, Omit<Inpu
132
134
  */
133
135
  matcher?: (option: T, query: string) => boolean;
134
136
  }
135
- export type SelectProps<T> = BaseSelectProps<T> & ({
137
+ type BaseSelectType<T> = BaseSelectProps<T> & RefProp<HTMLInputElement> & Omit<InputProps, "value" | "onChange">;
138
+ export type SelectProps<T> = BaseSelectType<T> & ({
136
139
  /**
137
140
  * is multiple selection
138
141
  */
@@ -167,3 +170,4 @@ export type SelectProps<T> = BaseSelectProps<T> & ({
167
170
  */
168
171
  onChange?: (value?: string[]) => void;
169
172
  });
173
+ export {};
@@ -1,59 +1,78 @@
1
- interface BaseSchemaDef {
1
+ import { type DateLike } from "@react-fabric/date";
2
+ export declare enum DATA_TYPES {
3
+ TEXT = "text",
4
+ COLOR = "color",
5
+ BOOL = "bool",
6
+ DATE = "date",
7
+ STRING = "string",
8
+ NUMBER = "number",
9
+ RANGE = "range",
10
+ FILE = "file",
11
+ AVATAR = "avatar",
12
+ SCHEMA = "schema"
13
+ }
14
+ interface SingleValue {
15
+ multiple?: false;
16
+ minItems?: never;
17
+ maxItems?: never;
18
+ }
19
+ interface MultipleValue {
20
+ multiple: true;
21
+ minItems?: number;
22
+ maxItems?: number;
23
+ }
24
+ export type ValueType = SingleValue | MultipleValue;
25
+ interface BaseSchema {
2
26
  id: string;
3
27
  label: string;
4
28
  datatype: string;
5
29
  required?: boolean;
6
- multiple?: boolean;
7
- defaultValue?: AnyObject;
8
30
  }
9
- interface SchemaDef extends BaseSchemaDef {
10
- datatype: "text" | "color";
31
+ export interface DefaultSchema extends BaseSchema {
32
+ datatype: DATA_TYPES.TEXT | DATA_TYPES.COLOR;
11
33
  }
12
- interface BooleanSchema extends BaseSchemaDef {
13
- datatype: "boolean";
14
- defaultValue?: boolean;
34
+ export interface BooleanSchema extends BaseSchema {
35
+ datatype: DATA_TYPES.BOOL;
15
36
  }
16
- interface StringSchema extends BaseSchemaDef {
17
- datatype: "string";
18
- options?: string[];
37
+ export interface StringSchema extends BaseSchema {
38
+ datatype: DATA_TYPES.STRING;
19
39
  regex?: string;
20
- identifierType?: string;
40
+ options?: string[];
41
+ optionList?: string;
42
+ allowCustom?: boolean;
21
43
  }
22
- interface RangeSchema extends BaseSchemaDef {
23
- datatype: "range";
44
+ export interface RangeSchema extends BaseSchema {
45
+ datatype: DATA_TYPES.RANGE;
24
46
  min?: number;
25
47
  max?: number;
26
48
  step?: number;
27
- defaultValue?: [number, number];
28
49
  }
29
- interface NumberSchema extends BaseSchemaDef {
30
- datatype: "number";
31
- min?: number;
32
- max?: number;
33
- step?: number;
34
- defaultValue?: number;
50
+ export interface DateSchema extends BaseSchema {
51
+ datatype: DATA_TYPES.DATE;
52
+ type?: "date" | "datetime";
53
+ min?: DateLike;
54
+ max?: DateLike;
35
55
  }
36
- interface DecimalSchema extends BaseSchemaDef {
37
- datatype: "decimal";
56
+ export interface NumberSchema extends BaseSchema {
57
+ datatype: DATA_TYPES.NUMBER;
38
58
  min?: number;
39
59
  max?: number;
40
60
  step?: number;
41
- defaultValue?: number;
42
61
  }
43
- export interface FileSchema extends BaseSchemaDef {
44
- datatype: "file";
62
+ export interface FileSchema extends BaseSchema {
63
+ datatype: DATA_TYPES.FILE;
45
64
  accept?: string;
46
65
  }
47
- export interface AvatarSchema extends BaseSchemaDef {
48
- datatype: "avatar";
66
+ export interface AvatarSchema extends BaseSchema {
67
+ datatype: DATA_TYPES.AVATAR;
49
68
  multiple?: false;
50
69
  }
51
- export type SchemaType = SchemaDef | BooleanSchema | StringSchema | RangeSchema | NumberSchema | DecimalSchema | FileSchema | AvatarSchema;
52
- export interface ChildSchema extends BaseSchemaDef {
53
- datatype: "schema";
70
+ export type SchemaType = (DefaultSchema & ValueType) | (BooleanSchema & ValueType) | (StringSchema & ValueType) | (RangeSchema & ValueType) | (DateSchema & ValueType) | (NumberSchema & ValueType) | (FileSchema & ValueType) | (AvatarSchema & ValueType);
71
+ export interface ChildSchema extends BaseSchema {
72
+ datatype: DATA_TYPES.SCHEMA;
54
73
  inline?: boolean;
55
74
  schema: SchemaType[];
56
- defaultValue?: never;
57
75
  }
58
- export type FormSchema = SchemaType | ChildSchema;
76
+ export type SchemaDef = SchemaType | (ChildSchema & ValueType);
77
+ export type FormSchema = SchemaDef[];
59
78
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-fabric/form",
3
- "version": "1.0.0-beta.31",
3
+ "version": "1.0.0-beta.33",
4
4
  "description": "RUF: Form package",
5
5
  "author": "Adarsh Pastakia <adarshpastakia@outlook.com>",
6
6
  "license": "MIT",
@@ -39,8 +39,9 @@
39
39
  "@dnd-kit/utilities": "^3.2.2",
40
40
  "@floating-ui/react": "^0.26.24",
41
41
  "@hookform/resolvers": "^3.9.0",
42
- "@react-fabric/core": "^1.0.0-beta.31",
43
- "@react-fabric/utilities": "^1.0.0-beta.31",
42
+ "@react-fabric/core": "^1.0.0-beta.33",
43
+ "@react-fabric/date": "^1.0.0-beta.33",
44
+ "@react-fabric/utilities": "^1.0.0-beta.33",
44
45
  "classnames": "^2.5.1",
45
46
  "color": "^4.2.3",
46
47
  "react-color": "^2.19.3",
@@ -60,5 +61,5 @@
60
61
  "node": ">=18.12.0",
61
62
  "npm": ">=8.19.0"
62
63
  },
63
- "gitHead": "de71c6875264d385666d0fa441785b9884951561"
64
+ "gitHead": "3b7ffc99aac7b8fdc72c7122c39db55d1104297f"
64
65
  }
@@ -24,20 +24,21 @@
24
24
  import { Button, Icon, ProgressBar } from "@react-fabric/core";
25
25
  import { FileUtil, Format } from "@react-fabric/utilities";
26
26
  import { Fragment } from "react";
27
- import { type UploadHandler, useFileUploader } from "../hooks/useFileUploader";
27
+ import { useFileUploader, type UploadHandler } from "../hooks/useFileUploader";
28
28
  import { Field } from "../input/Field";
29
29
  import { HiddenInput } from "../input/Hidden";
30
- import { type FileSchema } from "../types/schema";
30
+ import { type FileSchema, type ValueType } from "../types/schema";
31
31
 
32
32
  export const FileField = ({
33
33
  uploadHandler,
34
34
  inline,
35
35
  label,
36
36
  accept,
37
- multiple = false,
38
37
  value,
38
+ fileUrl,
39
+ multiple,
39
40
  ...rest
40
- }: Partial<FileSchema> & {
41
+ }: Partial<FileSchema & ValueType> & {
41
42
  value?: AnyObject;
42
43
  inline?: boolean;
43
44
  fileUrl?: (path: string) => string;
@@ -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
154
  label={t(label, label)}
142
155
  required={required}
143
- onAdd={() => ""}
156
+ onAdd={() => String("")}
144
157
  >
145
158
  <Controller>
146
159
  <E {...props} />