@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
@@ -0,0 +1,248 @@
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
+ arrow,
26
+ autoUpdate,
27
+ flip,
28
+ FloatingArrow,
29
+ offset,
30
+ shift,
31
+ useClick,
32
+ useDismiss,
33
+ useFloating,
34
+ useFocus,
35
+ useInteractions,
36
+ } from "@floating-ui/react";
37
+ import { type RefProp } from "@react-fabric/core/dist/types/types";
38
+ import { type DateLike, DatePanel, DateUtil } from "@react-fabric/date";
39
+ import { mergeRefs } from "@react-fabric/utilities";
40
+ import {
41
+ type ChangeEvent,
42
+ startTransition,
43
+ useCallback,
44
+ useDeferredValue,
45
+ useEffect,
46
+ useMemo,
47
+ useRef,
48
+ useState,
49
+ } from "react";
50
+ import { InputWrapper } from "../internal/InputWrapper";
51
+ import { type InputProps } from "../types";
52
+ import { handleEnter } from "../utils";
53
+ import classNames from "classnames";
54
+
55
+ export interface DateProps
56
+ extends InputProps<string>,
57
+ RefProp<HTMLInputElement> {
58
+ type?: "date" | "datetime";
59
+ /**
60
+ * date format
61
+ */
62
+ format?: string;
63
+ /**
64
+ * minimum value
65
+ */
66
+ min?: DateLike;
67
+ /**
68
+ * maximum value
69
+ */
70
+ max?: DateLike;
71
+ }
72
+
73
+ export const DateInput = ({
74
+ ref,
75
+ name,
76
+ value,
77
+ invalid,
78
+ readOnly,
79
+ disabled,
80
+ required,
81
+ placeholder,
82
+ autoFocus,
83
+ error,
84
+ type = "date",
85
+ onChange,
86
+ onEnterPressed,
87
+ format = "dd/MM/yyyy",
88
+ min,
89
+ max,
90
+ ...rest
91
+ }: DateProps) => {
92
+ const [actualValue, setActualValue] = useState("");
93
+ const [inputValue, setInputValue] = useState("");
94
+ const deferred = useDeferredValue(value);
95
+
96
+ const inputFormat = useMemo(
97
+ () => (type === "datetime" ? "yyyy-MM-dd HH:mm" : "yyyy-MM-dd"),
98
+ [type],
99
+ );
100
+
101
+ useEffect(() => {
102
+ setActualValue(deferred ?? "");
103
+ setInputValue(deferred ? DateUtil.format(deferred, inputFormat) : "");
104
+ }, [deferred, inputFormat]);
105
+
106
+ const handleEnterPressed = useMemo(
107
+ () => handleEnter(onEnterPressed),
108
+ [onEnterPressed],
109
+ );
110
+
111
+ const handleChange = useCallback(
112
+ (e?: ChangeEvent<HTMLInputElement>) => {
113
+ const value = e?.currentTarget.value;
114
+ if (value) {
115
+ const parsed = DateUtil.parseDate(value);
116
+ setActualValue(parsed?.toISOString?.());
117
+ setInputValue(DateUtil.format(parsed, inputFormat));
118
+ onChange != null &&
119
+ startTransition(() => onChange(parsed?.toISOString?.()));
120
+ } else {
121
+ setActualValue("");
122
+ setInputValue("");
123
+ onChange != null && startTransition(() => onChange(""));
124
+ }
125
+ },
126
+ [onChange, inputFormat],
127
+ );
128
+
129
+ const handleDateChange = useCallback(
130
+ (value?: string) => {
131
+ if (value) {
132
+ const parsed = DateUtil.parseDate(value);
133
+ setActualValue(parsed?.toISOString?.());
134
+ setInputValue(DateUtil.format(parsed, inputFormat));
135
+ onChange != null &&
136
+ startTransition(() => onChange(parsed?.toISOString?.()));
137
+ } else {
138
+ setActualValue("");
139
+ setInputValue("");
140
+ onChange != null && startTransition(() => onChange(""));
141
+ }
142
+ },
143
+ [onChange, inputFormat],
144
+ );
145
+
146
+ const arrowRef = useRef(null);
147
+ const [isOpen, setIsOpen] = useState(false);
148
+ const { refs, floatingStyles, context } = useFloating({
149
+ open: isOpen,
150
+ onOpenChange: (open, _, reason) => {
151
+ console.log(reason);
152
+ if (reason === "reference-press") return;
153
+ setIsOpen(open);
154
+ },
155
+ strategy: "fixed",
156
+ placement: "bottom-start",
157
+ whileElementsMounted: autoUpdate,
158
+ middleware: [
159
+ shift({ padding: 8 }),
160
+ flip(),
161
+ offset(9),
162
+ arrow({
163
+ element: arrowRef,
164
+ }),
165
+ ],
166
+ });
167
+
168
+ const focus = useFocus(context, {
169
+ enabled: !disabled,
170
+ });
171
+ const click = useClick(context, {
172
+ enabled: !disabled,
173
+ toggle: false,
174
+ });
175
+ const dismiss = useDismiss(context, {
176
+ referencePress: true,
177
+ });
178
+
179
+ const { getReferenceProps, getFloatingProps } = useInteractions([
180
+ dismiss,
181
+ click,
182
+ focus,
183
+ ]);
184
+
185
+ return (
186
+ <InputWrapper
187
+ showClear={!!actualValue}
188
+ onClear={handleChange}
189
+ invalid={invalid}
190
+ readOnly={readOnly}
191
+ disabled={disabled}
192
+ required={required}
193
+ error={error}
194
+ {...rest}
195
+ >
196
+ <input
197
+ className={classNames(
198
+ "appearance-none bg-transparent py-1 px-2 flex-1 border-none outline-none ring-0 peer",
199
+ !value && "empty",
200
+ )}
201
+ aria-invalid={invalid}
202
+ aria-disabled={disabled}
203
+ aria-readonly={readOnly}
204
+ aria-errormessage={error}
205
+ placeholder={placeholder}
206
+ disabled={disabled}
207
+ readOnly={readOnly}
208
+ data-testid={name}
209
+ name={name}
210
+ size={1}
211
+ type={type === "datetime" ? "datetime-local" : "date"}
212
+ autoComplete="off"
213
+ autoFocus={autoFocus}
214
+ value={inputValue}
215
+ ref={mergeRefs(ref, refs.setReference)}
216
+ {...getReferenceProps()}
217
+ onChange={handleChange}
218
+ onKeyDown={handleEnterPressed}
219
+ />
220
+ {isOpen && (
221
+ <div
222
+ ref={refs.setFloating}
223
+ style={{
224
+ zIndex: "var(--z-popover)",
225
+ ...floatingStyles,
226
+ }}
227
+ className="shadow-lg bg-base ring-1 ring-tint-100"
228
+ data-ref="dropdownBody"
229
+ {...getFloatingProps()}
230
+ >
231
+ <DatePanel
232
+ withTime={type === "datetime"}
233
+ value={actualValue}
234
+ max={max}
235
+ min={min}
236
+ onChange={handleDateChange}
237
+ />
238
+ <FloatingArrow
239
+ ref={arrowRef}
240
+ context={context}
241
+ strokeWidth={0.5}
242
+ className="fill-base stroke-muted"
243
+ />
244
+ </div>
245
+ )}
246
+ </InputWrapper>
247
+ );
248
+ };
@@ -87,8 +87,9 @@ export const Field = ({
87
87
  classes.fieldGroup,
88
88
  !plain && !noOutline && "outline",
89
89
  !plain && "bg-base",
90
+ plain && "flex-wrap",
90
91
  "flex-1 rounded",
91
- !vertical && "flex flex-nowrap",
92
+ !vertical && "flex",
92
93
  )}
93
94
  >
94
95
  {cloneChildren(children, (child: AnyObject) =>
@@ -97,7 +97,7 @@ export interface RangeProps
97
97
  /**
98
98
  * Number Range input
99
99
  */
100
- export const Range = ({
100
+ export const RangeSlider = ({
101
101
  error,
102
102
  name,
103
103
  value,
@@ -102,6 +102,8 @@ export const Switch = ({
102
102
  onLabelColor,
103
103
  offLabelColor,
104
104
  defaultColor,
105
+ // @ts-expect-error ignore
106
+ required,
105
107
  onChange,
106
108
  ...rest
107
109
  }: SwitchProps) => {
@@ -71,7 +71,7 @@
71
71
  }
72
72
 
73
73
  & > input[type="number"] + .inputClear {
74
- margin-inline: -3rem 2rem;
74
+ margin-inline: -3rem 0;
75
75
  }
76
76
 
77
77
  & input::-ms-clear,
@@ -90,6 +90,17 @@
90
90
  letter-spacing: 2px;
91
91
  }
92
92
 
93
+ & input[type="date"]::-webkit-calendar-picker-indicator,
94
+ & input[type="datetime-local"]::-webkit-calendar-picker-indicator {
95
+ display: none;
96
+ appearance: none;
97
+ }
98
+
99
+ & input[type="date"]:global(.empty)::-webkit-datetime-edit,
100
+ & input[type="datetime-local"]:global(.empty)::-webkit-datetime-edit {
101
+ color: theme(colors.tint.400);
102
+ }
103
+
93
104
  & input:-webkit-autofill {
94
105
  box-shadow: 0 0 0 999em var(--fabric-input) inset;
95
106
 
@@ -44,6 +44,8 @@ export const InputWrapper = ({
44
44
  showClear,
45
45
  decorateStart,
46
46
  decorateEnd,
47
+ decorateEndShowWhenEmpty,
48
+ decorateEndHideWhenEmpty,
47
49
  children,
48
50
  onClear,
49
51
  noOutline,
@@ -107,7 +109,7 @@ export const InputWrapper = ({
107
109
  !noOutline &&
108
110
  !noBorder &&
109
111
  "bg-[var(--fabric-input)] outline overflow-hidden",
110
- "flex flex-nowrap items-center",
112
+ "flex flex-nowrap items-center flex-1",
111
113
  textarea ? "rounded-capped group" : "rounded",
112
114
  isExpanded
113
115
  ? "fixed inset-4 z-[99]"
@@ -155,6 +157,9 @@ export const InputWrapper = ({
155
157
  classes.decorate,
156
158
  "text-muted leading-none flex-content",
157
159
  isString(decorateEnd) && "px-1",
160
+ decorateEndHideWhenEmpty && "peer-placeholder-shown:hidden",
161
+ decorateEndShowWhenEmpty &&
162
+ "hidden peer-placeholder-shown:inline-block",
158
163
  )}
159
164
  >
160
165
  {decorateEnd}
@@ -174,7 +179,7 @@ export const InputWrapper = ({
174
179
  <Wrapper {...wrapperProps}>
175
180
  <div
176
181
  ref={elRef}
177
- className="flex-1 relative min-h-8"
182
+ className="flex-1 relative min-h-8 flex flex-col flex-nowrap"
178
183
  style={{ width, flexBasis: width }}
179
184
  >
180
185
  {el}
@@ -72,6 +72,8 @@ export interface InputProps<V = string> extends TestProps {
72
72
  *
73
73
  */
74
74
  decorateEnd?: JSX.Element | string | number | boolean;
75
+ decorateEndShowWhenEmpty?: boolean;
76
+ decorateEndHideWhenEmpty?: boolean;
75
77
  /**
76
78
  * info label at bottom
77
79
  */
@@ -120,9 +122,7 @@ export interface InputProps<V = string> extends TestProps {
120
122
  labelWidth?: string;
121
123
  }
122
124
 
123
- export interface BaseSelectProps<T>
124
- extends RefProp<HTMLInputElement>,
125
- Omit<InputProps, "value" | "onChange"> {
125
+ export interface BaseSelectProps<T extends AnyObject = string> {
126
126
  /**
127
127
  * list options
128
128
  */
@@ -165,7 +165,11 @@ export interface BaseSelectProps<T>
165
165
  matcher?: (option: T, query: string) => boolean;
166
166
  }
167
167
 
168
- export type SelectProps<T> = BaseSelectProps<T> &
168
+ type BaseSelectType<T> = BaseSelectProps<T> &
169
+ RefProp<HTMLInputElement> &
170
+ Omit<InputProps, "value" | "onChange">;
171
+
172
+ export type SelectProps<T> = BaseSelectType<T> &
169
173
  (
170
174
  | {
171
175
  /**
@@ -21,80 +21,104 @@
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
- interface BaseSchemaDef {
24
+ import { type DateLike } from "@react-fabric/date";
25
+
26
+ export enum DATA_TYPES {
27
+ TEXT = "text",
28
+ COLOR = "color",
29
+ BOOL = "bool",
30
+ DATE = "date",
31
+ STRING = "string",
32
+ NUMBER = "number",
33
+ RANGE = "range",
34
+ FILE = "file",
35
+ AVATAR = "avatar",
36
+ SCHEMA = "schema",
37
+ }
38
+
39
+ interface SingleValue {
40
+ multiple?: false;
41
+ minItems?: never;
42
+ maxItems?: never;
43
+ }
44
+
45
+ interface MultipleValue {
46
+ multiple: true;
47
+ minItems?: number;
48
+ maxItems?: number;
49
+ }
50
+
51
+ export type ValueType = SingleValue | MultipleValue;
52
+
53
+ interface BaseSchema {
25
54
  id: string;
26
55
  label: string;
27
56
  datatype: string;
28
57
  required?: boolean;
29
- multiple?: boolean;
30
- defaultValue?: AnyObject;
31
58
  }
32
59
 
33
- interface SchemaDef extends BaseSchemaDef {
34
- datatype: "text" | "color";
60
+ export interface DefaultSchema extends BaseSchema {
61
+ datatype: DATA_TYPES.TEXT | DATA_TYPES.COLOR;
35
62
  }
36
63
 
37
- interface BooleanSchema extends BaseSchemaDef {
38
- datatype: "boolean";
39
- defaultValue?: boolean;
64
+ export interface BooleanSchema extends BaseSchema {
65
+ datatype: DATA_TYPES.BOOL;
40
66
  }
41
67
 
42
- interface StringSchema extends BaseSchemaDef {
43
- datatype: "string";
44
- options?: string[];
68
+ export interface StringSchema extends BaseSchema {
69
+ datatype: DATA_TYPES.STRING;
45
70
  regex?: string;
46
- identifierType?: string;
71
+ options?: string[];
72
+ optionList?: string;
73
+ allowCustom?: boolean;
47
74
  }
48
75
 
49
- interface RangeSchema extends BaseSchemaDef {
50
- datatype: "range";
76
+ export interface RangeSchema extends BaseSchema {
77
+ datatype: DATA_TYPES.RANGE;
51
78
  min?: number;
52
79
  max?: number;
53
80
  step?: number;
54
- defaultValue?: [number, number];
55
81
  }
56
82
 
57
- interface NumberSchema extends BaseSchemaDef {
58
- datatype: "number";
59
- min?: number;
60
- max?: number;
61
- step?: number;
62
- defaultValue?: number;
83
+ export interface DateSchema extends BaseSchema {
84
+ datatype: DATA_TYPES.DATE;
85
+ type?: "date" | "datetime";
86
+ min?: DateLike;
87
+ max?: DateLike;
63
88
  }
64
89
 
65
- interface DecimalSchema extends BaseSchemaDef {
66
- datatype: "decimal";
90
+ export interface NumberSchema extends BaseSchema {
91
+ datatype: DATA_TYPES.NUMBER;
67
92
  min?: number;
68
93
  max?: number;
69
94
  step?: number;
70
- defaultValue?: number;
71
95
  }
72
96
 
73
- export interface FileSchema extends BaseSchemaDef {
74
- datatype: "file";
97
+ export interface FileSchema extends BaseSchema {
98
+ datatype: DATA_TYPES.FILE;
75
99
  accept?: string;
76
100
  }
77
101
 
78
- export interface AvatarSchema extends BaseSchemaDef {
79
- datatype: "avatar";
102
+ export interface AvatarSchema extends BaseSchema {
103
+ datatype: DATA_TYPES.AVATAR;
80
104
  multiple?: false;
81
105
  }
82
106
 
83
107
  export type SchemaType =
84
- | SchemaDef
85
- | BooleanSchema
86
- | StringSchema
87
- | RangeSchema
88
- | NumberSchema
89
- | DecimalSchema
90
- | FileSchema
91
- | AvatarSchema;
92
-
93
- export interface ChildSchema extends BaseSchemaDef {
94
- datatype: "schema";
108
+ | (DefaultSchema & ValueType)
109
+ | (BooleanSchema & ValueType)
110
+ | (StringSchema & ValueType)
111
+ | (RangeSchema & ValueType)
112
+ | (DateSchema & ValueType)
113
+ | (NumberSchema & ValueType)
114
+ | (FileSchema & ValueType)
115
+ | (AvatarSchema & ValueType);
116
+
117
+ export interface ChildSchema extends BaseSchema {
118
+ datatype: DATA_TYPES.SCHEMA;
95
119
  inline?: boolean;
96
120
  schema: SchemaType[];
97
- defaultValue?: never;
98
121
  }
99
122
 
100
- export type FormSchema = SchemaType | ChildSchema;
123
+ export type SchemaDef = SchemaType | (ChildSchema & ValueType);
124
+ export type FormSchema = SchemaDef[];