@react-fabric/form 1.0.0-beta.9 → 1.0.2

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 (215) hide show
  1. package/{src/input/ArrayInput.module.css → css/_arrayInput.css} +8 -8
  2. package/css/_colorPicker.css +95 -0
  3. package/{src/internal/Field.module.css → css/_field.css} +94 -33
  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 +107 -78
  7. package/dist/cjs/builder/FormField.js +60 -39
  8. package/dist/cjs/builder/SchemaEditor.js +72 -0
  9. package/dist/cjs/builder/components/AddSchemaFields.js +64 -0
  10. package/dist/cjs/builder/components/SchemaArrayForm.js +146 -0
  11. package/dist/cjs/builder/components/SchemaField.js +423 -0
  12. package/dist/cjs/form/Controller.js +66 -19
  13. package/dist/cjs/form/Form.js +97 -39
  14. package/dist/cjs/form/useForm.js +32 -0
  15. package/dist/cjs/hooks/useFileUploader.js +74 -45
  16. package/dist/cjs/hooks/useFormBuilder.js +85 -31
  17. package/dist/cjs/i18n/{ar.json.js → ar/form.json.js} +5 -4
  18. package/dist/cjs/i18n/{en.json.js → en/form.json.js} +5 -5
  19. package/dist/cjs/i18n/index.js +4 -4
  20. package/dist/cjs/index.js +41 -2
  21. package/dist/cjs/input/ArrayInput.js +259 -135
  22. package/dist/cjs/input/AvatarInput.js +99 -63
  23. package/dist/cjs/input/Checkbox.js +24 -25
  24. package/dist/cjs/input/ColorInput.js +138 -81
  25. package/dist/cjs/input/ColorPicker.js +78 -95
  26. package/dist/cjs/input/DateInput.js +192 -0
  27. package/dist/cjs/input/Field.js +21 -20
  28. package/dist/cjs/input/File.js +85 -80
  29. package/dist/cjs/input/Hidden.js +27 -13
  30. package/dist/cjs/input/Input.js +49 -48
  31. package/dist/cjs/input/Number.js +46 -44
  32. package/dist/cjs/input/Password.js +51 -49
  33. package/dist/cjs/input/Radio.js +26 -16
  34. package/dist/cjs/input/RadioGroup.js +26 -11
  35. package/dist/cjs/input/{Range.js → RangeSlider.js} +61 -66
  36. package/dist/cjs/input/Search.js +58 -64
  37. package/dist/cjs/input/Slider.js +57 -66
  38. package/dist/cjs/input/Switch.js +29 -27
  39. package/dist/cjs/input/Textarea.js +65 -53
  40. package/dist/cjs/internal/ErrorIcon.js +9 -9
  41. package/dist/cjs/internal/FieldWrapper.js +23 -13
  42. package/dist/cjs/internal/InputWrapper.js +85 -87
  43. package/dist/cjs/ranger/index.js +7 -7
  44. package/dist/cjs/ranger/utils.js +9 -4
  45. package/dist/cjs/select/AutoComplete.js +93 -76
  46. package/dist/cjs/select/DualList.js +235 -154
  47. package/dist/cjs/select/List.js +122 -89
  48. package/dist/cjs/select/Options.js +58 -54
  49. package/dist/cjs/select/Select.js +119 -89
  50. package/dist/cjs/select/helpers.js +5 -5
  51. package/dist/cjs/select/useSelect.js +83 -58
  52. package/dist/cjs/types/schema.js +43 -0
  53. package/dist/cjs/utils/eventHandlers.js +16 -6
  54. package/dist/cjs/utils/getColor.js +6 -6
  55. package/dist/cjs/utils/getProps.js +49 -0
  56. package/dist/esm/builder/FileField.js +109 -80
  57. package/dist/esm/builder/FormField.js +60 -39
  58. package/dist/esm/builder/SchemaEditor.js +72 -0
  59. package/dist/esm/builder/components/AddSchemaFields.js +64 -0
  60. package/dist/esm/builder/components/SchemaArrayForm.js +146 -0
  61. package/dist/esm/builder/components/SchemaField.js +423 -0
  62. package/dist/esm/form/Controller.js +66 -19
  63. package/dist/esm/form/Form.js +97 -39
  64. package/dist/esm/form/useForm.js +32 -0
  65. package/dist/esm/hooks/useFileUploader.js +75 -46
  66. package/dist/esm/hooks/useFormBuilder.js +84 -30
  67. package/dist/esm/i18n/{ar.json.js → ar/form.json.js} +5 -4
  68. package/dist/esm/i18n/{en.json.js → en/form.json.js} +5 -5
  69. package/dist/esm/i18n/index.js +2 -2
  70. package/dist/esm/index.js +36 -1
  71. package/dist/esm/input/ArrayInput.js +260 -136
  72. package/dist/esm/input/AvatarInput.js +99 -63
  73. package/dist/esm/input/Checkbox.js +26 -27
  74. package/dist/esm/input/ColorInput.js +141 -84
  75. package/dist/esm/input/ColorPicker.js +80 -97
  76. package/dist/esm/input/DateInput.js +192 -0
  77. package/dist/esm/input/Field.js +21 -20
  78. package/dist/esm/input/File.js +89 -84
  79. package/dist/esm/input/Hidden.js +28 -14
  80. package/dist/esm/input/Input.js +51 -50
  81. package/dist/esm/input/Number.js +48 -46
  82. package/dist/esm/input/Password.js +54 -52
  83. package/dist/esm/input/Radio.js +27 -17
  84. package/dist/esm/input/RadioGroup.js +27 -12
  85. package/dist/esm/input/{Range.js → RangeSlider.js} +62 -67
  86. package/dist/esm/input/Search.js +61 -67
  87. package/dist/esm/input/Slider.js +58 -67
  88. package/dist/esm/input/Switch.js +31 -29
  89. package/dist/esm/input/Textarea.js +67 -55
  90. package/dist/esm/internal/ErrorIcon.js +11 -11
  91. package/dist/esm/internal/FieldWrapper.js +23 -13
  92. package/dist/esm/internal/InputWrapper.js +88 -90
  93. package/dist/esm/ranger/index.js +7 -7
  94. package/dist/esm/ranger/utils.js +9 -4
  95. package/dist/esm/select/AutoComplete.js +95 -78
  96. package/dist/esm/select/DualList.js +233 -152
  97. package/dist/esm/select/List.js +125 -92
  98. package/dist/esm/select/Options.js +58 -54
  99. package/dist/esm/select/Select.js +122 -92
  100. package/dist/esm/select/helpers.js +5 -5
  101. package/dist/esm/select/useSelect.js +84 -59
  102. package/{src/input/ColorPicker.module.css → dist/esm/types/schema.js} +18 -27
  103. package/dist/esm/utils/eventHandlers.js +16 -6
  104. package/dist/esm/utils/getColor.js +6 -6
  105. package/dist/esm/utils/getProps.js +49 -0
  106. package/dist/types/builder/FileField.d.ts +7 -4
  107. package/dist/types/builder/FormField.d.ts +4 -2
  108. package/dist/types/builder/SchemaEditor.d.ts +6 -0
  109. package/dist/types/builder/components/AddSchemaFields.d.ts +2 -0
  110. package/dist/types/builder/components/SchemaArrayForm.d.ts +2 -0
  111. package/dist/types/builder/components/SchemaField.d.ts +12 -0
  112. package/dist/types/form/Controller.d.ts +45 -3
  113. package/dist/types/form/Form.d.ts +38 -7
  114. package/dist/types/form/useForm.d.ts +6 -0
  115. package/dist/types/hooks/useFileUploader.d.ts +24 -3
  116. package/dist/types/hooks/useFormBuilder.d.ts +41 -3
  117. package/dist/types/index.d.ts +32 -2
  118. package/dist/types/input/ArrayInput.d.ts +68 -13
  119. package/dist/types/input/AvatarInput.d.ts +23 -6
  120. package/dist/types/input/Checkbox.d.ts +12 -10
  121. package/dist/types/input/ColorInput.d.ts +15 -5
  122. package/dist/types/input/ColorPicker.d.ts +10 -2
  123. package/dist/types/input/DateInput.d.ts +21 -0
  124. package/dist/types/input/Field.d.ts +9 -36
  125. package/dist/types/input/File.d.ts +16 -4
  126. package/dist/types/input/Hidden.d.ts +7 -6
  127. package/dist/types/input/Input.d.ts +11 -4
  128. package/dist/types/input/Number.d.ts +9 -4
  129. package/dist/types/input/Password.d.ts +9 -4
  130. package/dist/types/input/Radio.d.ts +9 -4
  131. package/dist/types/input/RadioGroup.d.ts +14 -6
  132. package/dist/types/input/{Range.d.ts → RangeSlider.d.ts} +11 -6
  133. package/dist/types/input/Search.d.ts +14 -4
  134. package/dist/types/input/Slider.d.ts +9 -6
  135. package/dist/types/input/Switch.d.ts +13 -8
  136. package/dist/types/input/Textarea.d.ts +15 -4
  137. package/dist/types/internal/ErrorIcon.d.ts +4 -1
  138. package/dist/types/internal/FieldWrapper.d.ts +10 -1
  139. package/dist/types/internal/InputWrapper.d.ts +19 -1
  140. package/dist/types/ranger/index.d.ts +7 -3
  141. package/dist/types/ranger/utils.d.ts +2 -2
  142. package/dist/types/select/AutoComplete.d.ts +7 -2
  143. package/dist/types/select/DualList.d.ts +11 -37
  144. package/dist/types/select/List.d.ts +7 -2
  145. package/dist/types/select/Options.d.ts +8 -7
  146. package/dist/types/select/Select.d.ts +7 -2
  147. package/dist/types/select/helpers.d.ts +2 -2
  148. package/dist/types/select/useSelect.d.ts +10 -28
  149. package/dist/types/types/index.d.ts +73 -39
  150. package/dist/types/types/schema.d.ts +78 -32
  151. package/dist/types/utils/eventHandlers.d.ts +4 -4
  152. package/dist/types/utils/getColor.d.ts +4 -4
  153. package/dist/types/utils/getProps.d.ts +26 -0
  154. package/dist/types/utils/index.d.ts +1 -0
  155. package/package.json +25 -25
  156. package/src/builder/FileField.tsx +73 -56
  157. package/src/builder/FormField.tsx +66 -58
  158. package/src/builder/SchemaEditor.tsx +78 -0
  159. package/src/builder/components/AddSchemaFields.tsx +64 -0
  160. package/src/builder/components/SchemaArrayForm.tsx +146 -0
  161. package/src/builder/components/SchemaField.tsx +405 -0
  162. package/src/form/Controller.tsx +61 -24
  163. package/src/form/Form.tsx +74 -50
  164. package/src/form/useForm.ts +53 -0
  165. package/src/hooks/useFileUploader.ts +115 -84
  166. package/src/hooks/useFormBuilder.tsx +80 -41
  167. package/src/i18n/{ar.json → ar/form.json} +5 -4
  168. package/src/i18n/{en.json → en/form.json} +5 -5
  169. package/src/i18n/index.ts +2 -2
  170. package/src/index.ts +34 -20
  171. package/src/input/ArrayInput.tsx +255 -173
  172. package/src/input/AvatarInput.tsx +82 -39
  173. package/src/input/Checkbox.tsx +30 -54
  174. package/src/input/ColorInput.tsx +146 -105
  175. package/src/input/ColorPicker.tsx +76 -102
  176. package/src/input/DateInput.tsx +225 -0
  177. package/src/input/Field.tsx +23 -65
  178. package/src/input/File.tsx +76 -98
  179. package/src/input/Hidden.tsx +36 -24
  180. package/src/input/Input.tsx +55 -53
  181. package/src/input/Number.tsx +49 -54
  182. package/src/input/Password.tsx +61 -66
  183. package/src/input/Radio.tsx +28 -37
  184. package/src/input/RadioGroup.tsx +30 -25
  185. package/src/input/{Range.tsx → RangeSlider.tsx} +81 -109
  186. package/src/input/Search.tsx +64 -77
  187. package/src/input/Slider.tsx +80 -109
  188. package/src/input/Switch.tsx +39 -53
  189. package/src/input/Textarea.tsx +72 -59
  190. package/src/internal/ErrorIcon.tsx +22 -21
  191. package/src/internal/FieldWrapper.tsx +33 -23
  192. package/src/internal/InputWrapper.tsx +132 -143
  193. package/src/ranger/index.tsx +37 -117
  194. package/src/ranger/utils.ts +12 -11
  195. package/src/select/AutoComplete.tsx +113 -123
  196. package/src/select/DualList.tsx +218 -204
  197. package/src/select/List.tsx +130 -97
  198. package/src/select/Options.tsx +59 -57
  199. package/src/select/Select.tsx +148 -122
  200. package/src/select/helpers.ts +13 -8
  201. package/src/select/useSelect.tsx +110 -127
  202. package/src/types/index.ts +76 -45
  203. package/src/types/schema.ts +97 -40
  204. package/src/utils/eventHandlers.ts +24 -26
  205. package/src/utils/getColor.ts +9 -8
  206. package/src/utils/getProps.ts +47 -0
  207. package/src/utils/index.ts +1 -0
  208. package/dist/cjs/input/ArrayInput.module.css +0 -1
  209. package/dist/cjs/input/ColorPicker.module.css +0 -1
  210. package/dist/cjs/input/File.module.css +0 -1
  211. package/dist/cjs/internal/Field.module.css +0 -1
  212. package/dist/esm/input/ArrayInput.module.css +0 -1
  213. package/dist/esm/input/ColorPicker.module.css +0 -1
  214. package/dist/esm/input/File.module.css +0 -1
  215. package/dist/esm/internal/Field.module.css +0 -1
@@ -21,18 +21,10 @@
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 Color from "color";
25
- import {
26
- memo,
27
- startTransition,
28
- useCallback,
29
- useDeferredValue,
30
- useEffect,
31
- useState,
32
- type FC,
33
- } from "react";
34
- import { SketchPicker } from "react-color";
35
- import classes from "./ColorPicker.module.css";
24
+ import { useControlledValue } from "@react-fabric/core";
25
+ import chroma from "chroma-js";
26
+ import { memo, startTransition, useCallback } from "react";
27
+ import { BlockPicker, SketchPicker } from "react-color";
36
28
 
37
29
  export interface ColorProps {
38
30
  /**
@@ -47,6 +39,10 @@ export interface ColorProps {
47
39
  * hide alpha value
48
40
  */
49
41
  hideAlpha?: boolean;
42
+ /**
43
+ * show only swatches, hide the color picker
44
+ */
45
+ picker?: "default" | "swatches";
50
46
  /**
51
47
  * color value
52
48
  */
@@ -58,16 +54,16 @@ export interface ColorProps {
58
54
  }
59
55
 
60
56
  const DEFAULT_SWATCHES = [
61
- Color("#d7323a").mix(Color("#171a1c"), 0.5).hex(),
62
- Color("#ff781c").mix(Color("#171a1c"), 0.5).hex(),
63
- Color("#f7b731").mix(Color("#171a1c"), 0.5).hex(),
64
- Color("#88ab1b").mix(Color("#171a1c"), 0.5).hex(),
65
- Color("#22aa85").mix(Color("#171a1c"), 0.5).hex(),
66
- Color("#399bec").mix(Color("#171a1c"), 0.5).hex(),
67
- Color("#4862c8").mix(Color("#171a1c"), 0.5).hex(),
68
- Color("#aa66cf").mix(Color("#171a1c"), 0.5).hex(),
69
- Color("#f43f7b").mix(Color("#171a1c"), 0.5).hex(),
70
- Color("#a37f4f").mix(Color("#171a1c"), 0.5).hex(),
57
+ chroma.mix("#d7323a", "#171a1c", 0.5, "lab").hex(),
58
+ chroma.mix("#ff781c", "#171a1c", 0.5, "lab").hex(),
59
+ chroma.mix("#f7b731", "#171a1c", 0.5, "lab").hex(),
60
+ chroma.mix("#88ab1b", "#171a1c", 0.5, "lab").hex(),
61
+ chroma.mix("#22aa85", "#171a1c", 0.5, "lab").hex(),
62
+ chroma.mix("#399bec", "#171a1c", 0.5, "lab").hex(),
63
+ chroma.mix("#4862c8", "#171a1c", 0.5, "lab").hex(),
64
+ chroma.mix("#aa66cf", "#171a1c", 0.5, "lab").hex(),
65
+ chroma.mix("#f43f7b", "#171a1c", 0.5, "lab").hex(),
66
+ chroma.mix("#a37f4f", "#171a1c", 0.5, "lab").hex(),
71
67
 
72
68
  "#d7323a",
73
69
  "#ff781c",
@@ -80,16 +76,16 @@ const DEFAULT_SWATCHES = [
80
76
  "#f43f7b",
81
77
  "#a37f4f",
82
78
 
83
- Color("#d7323a").mix(Color("#f1f3f4"), 0.5).hex(),
84
- Color("#ff781c").mix(Color("#f1f3f4"), 0.5).hex(),
85
- Color("#f7b731").mix(Color("#f1f3f4"), 0.5).hex(),
86
- Color("#88ab1b").mix(Color("#f1f3f4"), 0.5).hex(),
87
- Color("#22aa85").mix(Color("#f1f3f4"), 0.5).hex(),
88
- Color("#399bec").mix(Color("#f1f3f4"), 0.5).hex(),
89
- Color("#4862c8").mix(Color("#f1f3f4"), 0.5).hex(),
90
- Color("#aa66cf").mix(Color("#f1f3f4"), 0.5).hex(),
91
- Color("#f43f7b").mix(Color("#f1f3f4"), 0.5).hex(),
92
- Color("#a37f4f").mix(Color("#f1f3f4"), 0.5).hex(),
79
+ chroma.mix("#d7323a", "#f1f3f4", 0.5, "lab").hex(),
80
+ chroma.mix("#ff781c", "#f1f3f4", 0.5, "lab").hex(),
81
+ chroma.mix("#f7b731", "#f1f3f4", 0.5, "lab").hex(),
82
+ chroma.mix("#88ab1b", "#f1f3f4", 0.5, "lab").hex(),
83
+ chroma.mix("#22aa85", "#f1f3f4", 0.5, "lab").hex(),
84
+ chroma.mix("#399bec", "#f1f3f4", 0.5, "lab").hex(),
85
+ chroma.mix("#4862c8", "#f1f3f4", 0.5, "lab").hex(),
86
+ chroma.mix("#aa66cf", "#f1f3f4", 0.5, "lab").hex(),
87
+ chroma.mix("#f43f7b", "#f1f3f4", 0.5, "lab").hex(),
88
+ chroma.mix("#a37f4f", "#f1f3f4", 0.5, "lab").hex(),
93
89
 
94
90
  "#171a1c",
95
91
  "#2d3439",
@@ -103,44 +99,48 @@ const DEFAULT_SWATCHES = [
103
99
  "#f1f3f4",
104
100
  ];
105
101
 
106
- // eslint-disable-next-line react/display-name
107
- export const ColorPicker: FC<ColorProps> = memo(
108
- ({
109
- value,
110
- onChange,
111
- hideAlpha,
112
- defaultColor = "#fff",
113
- swatches = DEFAULT_SWATCHES,
114
- }: ColorProps) => {
115
- const [actualValue, setActualValue] = useState("");
116
- const deferred = useDeferredValue(value ?? defaultColor);
102
+ const ColorPickerComponent = ({
103
+ value,
104
+ onChange,
105
+ hideAlpha,
106
+ defaultColor = "#fff",
107
+ swatches = DEFAULT_SWATCHES,
108
+ picker = "default",
109
+ }: ColorProps) => {
110
+ const { currentValue, updateValue } = useControlledValue(value, defaultColor);
117
111
 
118
- useEffect(() => {
119
- setActualValue(deferred ?? "");
120
- }, [deferred]);
112
+ const handleChange = useCallback(
113
+ (e: string) => {
114
+ updateValue(e ?? defaultColor);
115
+ if (onChange) startTransition(() => onChange(e));
116
+ },
117
+ [defaultColor, onChange, updateValue],
118
+ );
121
119
 
122
- const handleChange = useCallback(
123
- (e: string) => {
124
- setActualValue(e ?? defaultColor);
125
- onChange != null && startTransition(() => onChange(e));
126
- },
127
- [onChange],
128
- );
120
+ const E = (picker === "swatches" ? BlockPicker : SketchPicker) as unknown as React.FC<AnyObject>;
129
121
 
130
- return (
131
- <SketchPicker
122
+ return (
123
+ <div
124
+ role="none"
125
+ className="fabric-colorPicker-container"
126
+ // prevent default on mouse down to avoid selection issues
127
+ onMouseDown={(e) => e.preventDefault()}
128
+ style={
129
+ {
130
+ "--color-selected": value,
131
+ } as React.CSSProperties
132
+ }
133
+ >
134
+ <E
132
135
  width="16rem"
133
- className={classes.colorPicker}
136
+ className={"fabric-colorPicker"}
134
137
  styles={
135
138
  {
136
139
  default: {
137
140
  hue: {
138
- borderRadius: "99px",
139
141
  boxShadow: "0 0 1px 1px var(--color-shadow)",
140
142
  },
141
143
  alpha: {
142
- height: "1rem",
143
- borderRadius: "99px",
144
144
  backgroundColor: "var(--fabric-bg)",
145
145
  boxShadow: "0 0 1px 1px var(--color-shadow)",
146
146
  },
@@ -155,56 +155,30 @@ export const ColorPicker: FC<ColorProps> = memo(
155
155
  },
156
156
  picker: {
157
157
  backgroundColor: "var(--fabric-bg)",
158
- boxShadow:
159
- "0 0 0 1px var(--color-shadow), 0 0 8px var(--fabric-shadow)",
160
- },
161
- Alpha: {
162
- alpha: {
163
- paddingRight: "calc(1rem - 2px)",
164
- },
165
- slider: {
166
- width: "calc(1rem - 2px)",
167
- borderRadius: "99px",
168
- height: "calc(1rem - 2px)",
169
- transform: "none",
170
- },
171
- },
172
- Hue: {
173
- hue: {
174
- paddingRight: "calc(1rem - 2px)",
175
- },
176
- slider: {
177
- width: "calc(1rem - 2px)",
178
- borderRadius: "99px",
179
- height: "calc(1rem - 2px)",
180
- transform: "none",
181
- },
182
- },
183
- Saturation: {
184
- circle: {
185
- width: "0.5rem",
186
- borderRadius: "99px",
187
- height: "0.5rem",
188
- transform: "translate(-0.25rem, -0.25rem)",
189
- },
158
+ boxShadow: "0 0 0 1px var(--color-shadow), 0 0 8px var(--fabric-shadow)",
190
159
  },
191
160
  checkboard: {
192
161
  white: "transparent",
193
162
  black: "red",
194
163
  },
195
164
  },
196
- } as AnyObject
165
+ } as React.CSSProperties
197
166
  }
198
- color={actualValue}
167
+ color={currentValue}
168
+ colors={swatches}
199
169
  presetColors={swatches}
200
170
  disableAlpha={hideAlpha}
201
- onChange={({ rgb: { a = 1, ...rgb } }) =>
202
- setActualValue(Color(rgb).alpha(a)[a < 1 ? "hexa" : "hex"]())
203
- }
204
- onChangeComplete={({ rgb: { a = 1, ...rgb } }) =>
205
- handleChange(Color(rgb).alpha(a)[a < 1 ? "hexa" : "hex"]())
206
- }
171
+ onChange={({ rgb: { a = 1, ...rgb } }) => updateValue(chroma(rgb).alpha(a).hex())}
172
+ onChangeComplete={({ rgb: { a = 1, ...rgb } }) => handleChange(chroma(rgb).alpha(a).hex())}
207
173
  />
208
- );
209
- },
210
- );
174
+ </div>
175
+ );
176
+ };
177
+
178
+ /**
179
+ * ColorPicker component allows users to select a color using a color picker UI.
180
+ * It supports swatches, alpha transparency, and custom styles.
181
+ * The component uses the `react-color` library for the color picker functionality.
182
+ */
183
+ export const ColorPicker = memo(ColorPickerComponent);
184
+ ColorPicker.displayName = "ColorPicker";
@@ -0,0 +1,225 @@
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 { useControlledValue } from "@react-fabric/core";
38
+ import type { RefProp } from "@react-fabric/core/dist/types/types";
39
+ import type { DateLike } from "@react-fabric/date";
40
+ import { DatePanel, DateUtil } from "@react-fabric/date";
41
+ import { cn, mergeRefs } from "@react-fabric/utilities";
42
+ import { startTransition, useCallback, useEffect, useId, useMemo, useRef, useState } from "react";
43
+ import { InputWrapper } from "../internal/InputWrapper";
44
+ import type { InputProps, WrapperProps } from "../types";
45
+ import { getWrapperProps, useHandleEnter } from "../utils";
46
+
47
+ export interface DateProps extends InputProps<DateLike>, WrapperProps, RefProp<HTMLInputElement> {
48
+ type?: "date" | "datetime";
49
+ /**
50
+ * minimum value
51
+ */
52
+ min?: DateLike;
53
+ /**
54
+ * maximum value
55
+ */
56
+ max?: DateLike;
57
+ }
58
+
59
+ /**
60
+ * Date input component that allows users to select a date or datetime.
61
+ * It uses the Floating UI library for positioning the date picker.
62
+ * It also provides a button to clear the selected date.
63
+ * The component supports both date and datetime types, with appropriate formatting.
64
+ */
65
+ export function DateInput(props: DateProps) {
66
+ const {
67
+ ref,
68
+ name,
69
+ value,
70
+ invalid,
71
+ readOnly,
72
+ disabled,
73
+ placeholder,
74
+ defaultValue,
75
+ autoFocus,
76
+ error,
77
+ type = "date",
78
+ onBlur,
79
+ onFocus,
80
+ onChange,
81
+ onEnterPressed,
82
+ allowClear,
83
+ min,
84
+ max,
85
+ } = props;
86
+ const id = useId();
87
+ const { currentValue, updateValue } = useControlledValue(value, defaultValue);
88
+ const [inputValue, setInputValue] = useState("");
89
+
90
+ const inputFormat = useMemo(() => (type === "datetime" ? "yyyy-MM-dd HH:mm" : "yyyy-MM-dd"), [type]);
91
+
92
+ useEffect(() => {
93
+ // eslint-disable-next-line @eslint-react/set-state-in-effect
94
+ setInputValue(currentValue ? DateUtil.format(currentValue, inputFormat) : "");
95
+ }, [currentValue, inputFormat]);
96
+
97
+ const handleEnterPressed = useHandleEnter(onEnterPressed);
98
+
99
+ const handleChange = useCallback(
100
+ (e?: React.ChangeEvent<HTMLInputElement>) => {
101
+ const value = e?.currentTarget.value ?? defaultValue;
102
+ if (value) {
103
+ const parsed = DateUtil.parseDate(value);
104
+ updateValue(parsed?.toISOString?.());
105
+ setInputValue(DateUtil.format(parsed, inputFormat));
106
+ if (onChange) startTransition(() => onChange(parsed?.toISOString?.()));
107
+ } else {
108
+ updateValue("");
109
+ setInputValue("");
110
+ if (onChange) startTransition(() => onChange());
111
+ }
112
+ },
113
+ [defaultValue, updateValue, inputFormat, onChange],
114
+ );
115
+
116
+ const handleDateChange = useCallback(
117
+ (value?: string) => {
118
+ if (value) {
119
+ const parsed = DateUtil.parseDate(value);
120
+ updateValue(parsed?.toISOString?.());
121
+ setInputValue(DateUtil.format(parsed, inputFormat));
122
+ if (onChange) startTransition(() => onChange(parsed?.toISOString?.()));
123
+ } else {
124
+ updateValue("");
125
+ setInputValue("");
126
+ if (onChange) startTransition(() => onChange());
127
+ }
128
+ },
129
+ [updateValue, inputFormat, onChange],
130
+ );
131
+
132
+ const arrowRef = useRef(null);
133
+ const [isOpen, setIsOpen] = useState(false);
134
+ const { refs, floatingStyles, context } = useFloating({
135
+ open: isOpen,
136
+ onOpenChange: (open, _, reason) => {
137
+ if (reason === "reference-press") return;
138
+ setIsOpen(open);
139
+ },
140
+ strategy: "fixed",
141
+ placement: "bottom-start",
142
+ whileElementsMounted: autoUpdate,
143
+ middleware: [
144
+ shift({ padding: 8 }),
145
+ flip(),
146
+ offset(9),
147
+ arrow({
148
+ element: arrowRef,
149
+ }),
150
+ ],
151
+ });
152
+
153
+ const focus = useFocus(context, {
154
+ enabled: !disabled,
155
+ });
156
+ const click = useClick(context, {
157
+ enabled: !disabled,
158
+ toggle: false,
159
+ });
160
+ const dismiss = useDismiss(context, {
161
+ referencePress: true,
162
+ });
163
+
164
+ const { getReferenceProps, getFloatingProps } = useInteractions([dismiss, click, focus]);
165
+
166
+ return (
167
+ <InputWrapper
168
+ id={id}
169
+ showClear={allowClear && !!currentValue && !disabled && !readOnly}
170
+ onClear={handleChange}
171
+ {...getWrapperProps(props)}
172
+ >
173
+ <input
174
+ id={id}
175
+ className={cn(
176
+ "appearance-none bg-transparent py-1 px-4 not-first:ps-2 not-last:pe-2 flex-1 border-none outline-none ring-0 peer",
177
+ !currentValue && "empty",
178
+ )}
179
+ aria-invalid={invalid}
180
+ aria-disabled={disabled}
181
+ aria-readonly={readOnly}
182
+ aria-errormessage={error}
183
+ placeholder={placeholder}
184
+ disabled={disabled}
185
+ readOnly={readOnly}
186
+ data-testid={name}
187
+ name={name}
188
+ size={1}
189
+ type={type === "datetime" ? "datetime-local" : "date"}
190
+ autoComplete="off"
191
+ // eslint-disable-next-line jsx-a11y-x/no-autofocus
192
+ autoFocus={autoFocus}
193
+ value={inputValue}
194
+ ref={mergeRefs(ref, (el: HTMLInputElement) => refs.setReference(el))}
195
+ {...getReferenceProps({
196
+ onBlur,
197
+ onFocus,
198
+ })}
199
+ onChange={handleChange}
200
+ onKeyDown={handleEnterPressed}
201
+ />
202
+ {isOpen && (
203
+ <div
204
+ ref={(el) => refs.setFloating(el)}
205
+ style={{
206
+ zIndex: "var(--z-popover)",
207
+ ...floatingStyles,
208
+ }}
209
+ className="shadow-lg bg-default ring-1 ring-tint-100"
210
+ data-ref="dropdownBody"
211
+ {...getFloatingProps()}
212
+ >
213
+ <DatePanel
214
+ withTime={type === "datetime"}
215
+ value={currentValue ?? ""}
216
+ max={max}
217
+ min={min}
218
+ onChange={handleDateChange}
219
+ />
220
+ <FloatingArrow ref={arrowRef} context={context} strokeWidth={0.5} className="fill-default stroke-muted" />
221
+ </div>
222
+ )}
223
+ </InputWrapper>
224
+ );
225
+ }
@@ -21,50 +21,13 @@
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 {
25
- type ChildrenProp,
26
- type CssProp,
27
- } from "@react-fabric/core/dist/types/types";
28
- import classNames from "classnames";
29
- import { Children, cloneElement } from "react";
30
- import classes from "../internal/Field.module.css";
24
+ import type { ChildrenProp, CssProp } from "@react-fabric/core/dist/types/types";
25
+ import { cn } from "@react-fabric/utilities";
31
26
  import { FieldWrapper } from "../internal/FieldWrapper";
27
+ import type { WrapperProps } from "../types";
28
+ import { getWrapperProps } from "../utils";
32
29
 
33
- export interface FieldProps extends ChildrenProp, CssProp {
34
- /**
35
- * input label
36
- */
37
- label?: string;
38
- /**
39
- * append to label end
40
- */
41
- appendLabel?: JSX.Element | string | number | boolean;
42
- /**
43
- * info label at bottom
44
- */
45
- info?: string;
46
-
47
- /**
48
- * required
49
- */
50
- required?: boolean;
51
- /**
52
- * hide outline
53
- */
54
- noOutline?: boolean;
55
-
56
- /**
57
- * field width
58
- */
59
- width?: number | string;
60
- /**
61
- * inline label and input
62
- */
63
- inline?: boolean;
64
- /**
65
- * label width for inline
66
- */
67
- labelWidth?: string;
30
+ export interface FieldProps extends ChildrenProp, WrapperProps, CssProp {
68
31
  /**
69
32
  * dont apply bg and outline
70
33
  */
@@ -72,33 +35,28 @@ export interface FieldProps extends ChildrenProp, CssProp {
72
35
  vertical?: boolean;
73
36
  }
74
37
 
75
- export const Field = ({
76
- children,
77
- vertical,
78
- noOutline,
79
- plain,
80
- ...rest
81
- }: FieldProps) => {
38
+ /**
39
+ * Field component for rendering multiple form fields under a single label.
40
+ * It supports various properties for customization, including label, info, required state,
41
+ * and styling options.
42
+ */
43
+ export function Field(props: FieldProps) {
44
+ const { children, vertical, borderless, plain, inline } = props;
82
45
  return (
83
- <FieldWrapper {...rest}>
46
+ <FieldWrapper {...getWrapperProps(props)}>
84
47
  <div
85
- className={classNames(
86
- classes.fieldGroup,
87
- !plain && !noOutline && "outline",
88
- !plain && "bg-base",
89
- "flex-1 rounded",
90
- !vertical && "flex flex-nowrap",
48
+ className={cn(
49
+ "fabric-fieldGroup",
50
+ !plain && !borderless && "outline focus-within:outline-primary-500 has-aria-invalid:focus-within:outline-danger-500",
51
+ !plain && "bg-default",
52
+ plain && "flex-wrap",
53
+ inline && "flex-1",
54
+ vertical ? "rounded-capped" : "rounded flex",
91
55
  )}
56
+ data-vertical={!!vertical}
92
57
  >
93
- {Children.map(
94
- children,
95
- (child) =>
96
- child &&
97
- cloneElement(child as AnyObject, {
98
- "data-inner": true,
99
- }),
100
- )}
58
+ {children}
101
59
  </div>
102
60
  </FieldWrapper>
103
61
  );
104
- };
62
+ }