@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,42 @@
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
+ @layer comp {
25
+ .fileInput {
26
+ &::-webkit-file-upload-button {
27
+ @apply bg-primary-500 opacity-80 hover:opacity-90 text-white rounded border-none py-0 cursor-pointer;
28
+ }
29
+
30
+ &[aria-invalid="true"]::-webkit-file-upload-button {
31
+ @apply bg-danger-500;
32
+ }
33
+
34
+ &:disabled::-webkit-file-upload-button {
35
+ @apply bg-muted pointer-events-none;
36
+ }
37
+
38
+ &[readOnly] {
39
+ @apply pointer-events-none;
40
+ }
41
+ }
42
+ }
@@ -0,0 +1,224 @@
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 { CoreIcons, Icon } from "@react-fabric/core";
25
+ import { type RefProp } from "@react-fabric/core/dist/types/types";
26
+ import {
27
+ FileUtil,
28
+ Format,
29
+ isArray,
30
+ isEmpty,
31
+ mergeRefs,
32
+ } from "@react-fabric/utilities";
33
+ import classNames from "classnames";
34
+ import {
35
+ Fragment,
36
+ useCallback,
37
+ useDeferredValue,
38
+ useEffect,
39
+ useMemo,
40
+ useRef,
41
+ useState,
42
+ type ChangeEvent,
43
+ } from "react";
44
+ import { InputWrapper } from "../internal/InputWrapper";
45
+ import { type InputProps } from "../types";
46
+ import { handleEnter } from "../utils";
47
+ import classes from "./File.module.css";
48
+
49
+ export interface FileInputProps
50
+ extends InputProps<AnyObject>,
51
+ RefProp<HTMLInputElement> {
52
+ /**
53
+ * allow selecting mutiple files
54
+ */
55
+ multiple?: boolean;
56
+ /**
57
+ * show selected file list
58
+ */
59
+ showList?: boolean;
60
+ /**
61
+ * acceptable file types
62
+ */
63
+ accept?: string;
64
+ }
65
+
66
+ const FileExists = (prop: AnyObject) => {
67
+ const [kill, setKill] = useState(false);
68
+ useEffect(() => {
69
+ setTimeout(() => {
70
+ setKill(true);
71
+ }, 2000);
72
+ });
73
+ return kill ? null : (
74
+ <div className="bg-danger-500 text-white px-2 rounded text-xs">
75
+ File [{prop.name}] already added to list
76
+ </div>
77
+ );
78
+ };
79
+
80
+ export const FileInput = ({
81
+ ref,
82
+ value,
83
+ invalid,
84
+ readOnly,
85
+ disabled,
86
+ required,
87
+ placeholder,
88
+ autoFocus,
89
+ error,
90
+ name,
91
+ accept,
92
+ showList = true,
93
+ multiple,
94
+ onChange,
95
+ onEnterPressed,
96
+ ...rest
97
+ }: FileInputProps) => {
98
+ const listRef = useRef<File[]>([]);
99
+ const inputRef = useRef<HTMLInputElement>(null);
100
+ const [exists, setExists] = useState<string[]>([]);
101
+ const [actualValue, setActualValue] = useState<File | File[] | null>();
102
+ const deferred = useDeferredValue(value);
103
+
104
+ const handleEnterPressed = useMemo(
105
+ () => handleEnter(onEnterPressed),
106
+ [onEnterPressed],
107
+ );
108
+
109
+ useEffect(() => {
110
+ setActualValue(deferred);
111
+ }, [deferred]);
112
+
113
+ const handleChange = useCallback(
114
+ (e?: ChangeEvent<HTMLInputElement>) => {
115
+ setExists([]);
116
+ if (multiple) {
117
+ if (e?.target.files) {
118
+ if (showList) {
119
+ const exists: string[] = [];
120
+ Array.from(e.target.files).forEach((file) => {
121
+ listRef.current.find((ef) => ef.name === file.name)
122
+ ? exists.push(file.name)
123
+ : listRef.current.push(file);
124
+ });
125
+ setActualValue(listRef.current.slice());
126
+ setExists(exists);
127
+ }
128
+ e.target.value = "";
129
+ onChange?.(listRef.current);
130
+ } else {
131
+ listRef.current = [];
132
+ setActualValue(listRef.current);
133
+ onChange?.(listRef.current);
134
+ }
135
+ } else {
136
+ setActualValue(e?.target.files?.item(0));
137
+ onChange?.(e?.target.files?.item(0) ?? undefined);
138
+ if (!e && inputRef.current) inputRef.current.value = "";
139
+ }
140
+ },
141
+ [onChange, multiple, showList],
142
+ );
143
+
144
+ const removeFile = useCallback(
145
+ (idx: number) => {
146
+ listRef.current.splice(idx, 1);
147
+ setActualValue(listRef.current);
148
+ onChange?.(listRef.current);
149
+ },
150
+ [onChange],
151
+ );
152
+
153
+ return (
154
+ <InputWrapper
155
+ showClear={!isEmpty(actualValue)}
156
+ onClear={handleChange}
157
+ invalid={invalid}
158
+ readOnly={readOnly}
159
+ disabled={disabled}
160
+ required={required}
161
+ error={error}
162
+ listing={
163
+ showList && (
164
+ <Fragment>
165
+ {isArray(actualValue) &&
166
+ actualValue?.map?.((file: AnyObject, idx: number) => (
167
+ <div
168
+ key={idx}
169
+ className="flex gap-1 items-center text-sm text-dimmed py-1 px-2"
170
+ >
171
+ <div className="flex-1 truncate">
172
+ <div className="font-medium">{file.name}</div>
173
+ <span className="text-xs">{FileUtil.mime(file.type)}</span>
174
+ </div>
175
+ <span className="flex-content text-end w-24">
176
+ {Format.bytes(file.size)}
177
+ </span>
178
+ <Icon
179
+ className="cursor-pointer opacity-30 hover:opacity-80"
180
+ icon={CoreIcons.close}
181
+ onClick={() => {
182
+ removeFile(idx);
183
+ }}
184
+ />
185
+ </div>
186
+ ))}
187
+ {exists.map((name) => (
188
+ <FileExists key={new Date().getTime()} name={name} />
189
+ ))}
190
+ </Fragment>
191
+ )
192
+ }
193
+ {...rest}
194
+ >
195
+ <input
196
+ className={classNames(
197
+ "appearance-none bg-transparent py-1 px-2 flex-1 border-none outline-none ring-0",
198
+ classes.fileInput,
199
+ isArray(actualValue) &&
200
+ actualValue?.length &&
201
+ "text-[var(--fabric-bg)]",
202
+ )}
203
+ aria-invalid={invalid}
204
+ aria-disabled={disabled}
205
+ aria-readonly={readOnly}
206
+ aria-errormessage={error}
207
+ placeholder={placeholder}
208
+ disabled={disabled}
209
+ readOnly={readOnly}
210
+ size={1}
211
+ type="file"
212
+ multiple={multiple}
213
+ data-testid={name}
214
+ name={name}
215
+ accept={accept}
216
+ ref={mergeRefs(ref, inputRef)}
217
+ autoComplete="off"
218
+ {...{ autoFocus }}
219
+ onChange={handleChange}
220
+ onKeyDown={handleEnterPressed}
221
+ />
222
+ </InputWrapper>
223
+ );
224
+ };
@@ -0,0 +1,65 @@
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 { Fragment, useEffect } from "react";
25
+ import { ErrorIcon } from "../internal/ErrorIcon";
26
+
27
+ interface Props {
28
+ /**
29
+ * field name
30
+ */
31
+ name?: string;
32
+ /**
33
+ * hidden value will fire change for form update
34
+ */
35
+ hiddenValue?: AnyObject;
36
+ /**
37
+ * form onChange handler
38
+ */
39
+ onChange?: (value: AnyObject) => void;
40
+ }
41
+
42
+ export const HiddenInput = ({
43
+ hiddenValue,
44
+ onChange,
45
+ // @ts-expect-error ignore
46
+ // eslint-disable-next-line react/prop-types
47
+ invalid,
48
+ // @ts-expect-error ignore
49
+ // eslint-disable-next-line react/prop-types
50
+ error,
51
+ // @ts-expect-error ignore
52
+ // eslint-disable-next-line react/prop-types
53
+ value = "",
54
+ name,
55
+ }: Props) => {
56
+ useEffect(() => {
57
+ onChange?.(hiddenValue);
58
+ }, [hiddenValue]);
59
+ return (
60
+ <Fragment>
61
+ <input type="hidden" onChange={onChange} value={value} name={name} />
62
+ <ErrorIcon invalid={invalid} error={error} />
63
+ </Fragment>
64
+ );
65
+ };
@@ -0,0 +1,114 @@
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 { type RefProp } from "@react-fabric/core/dist/types/types";
25
+ import {
26
+ useCallback,
27
+ useDeferredValue,
28
+ useEffect,
29
+ useMemo,
30
+ useState,
31
+ type ChangeEvent,
32
+ } from "react";
33
+ import { InputWrapper } from "../internal/InputWrapper";
34
+ import { type InputProps } from "../types";
35
+ import { handleEnter } from "../utils";
36
+
37
+ export interface TextInputProps extends InputProps, RefProp<HTMLInputElement> {
38
+ /**
39
+ * input type
40
+ */
41
+ type?: "email" | "file" | "search" | "tel" | "text" | "url";
42
+ }
43
+
44
+ export const Input = ({
45
+ ref,
46
+ name,
47
+ value,
48
+ invalid,
49
+ readOnly,
50
+ disabled,
51
+ required,
52
+ placeholder,
53
+ autoFocus,
54
+ error,
55
+ type = "text",
56
+ onChange,
57
+ onEnterPressed,
58
+ ...rest
59
+ }: TextInputProps) => {
60
+ const [actualValue, setActualValue] = useState("");
61
+ const deferred = useDeferredValue(value);
62
+
63
+ const handleEnterPressed = useMemo(
64
+ () => handleEnter(onEnterPressed),
65
+ [onEnterPressed],
66
+ );
67
+
68
+ useEffect(() => {
69
+ setActualValue(deferred ?? "");
70
+ }, [deferred]);
71
+
72
+ const handleChange = useCallback(
73
+ (e?: ChangeEvent<HTMLInputElement>) => {
74
+ const value = e?.target.value;
75
+ setActualValue(value ?? "");
76
+ onChange?.(value ?? undefined);
77
+ },
78
+ [onchange],
79
+ );
80
+
81
+ return (
82
+ <InputWrapper
83
+ showClear={!!actualValue}
84
+ onClear={handleChange}
85
+ invalid={invalid}
86
+ readOnly={readOnly}
87
+ disabled={disabled}
88
+ required={required}
89
+ error={error}
90
+ {...rest}
91
+ >
92
+ <input
93
+ className="appearance-none bg-transparent py-1 px-2 flex-1 border-none outline-none ring-0 peer"
94
+ aria-invalid={invalid}
95
+ aria-disabled={disabled}
96
+ aria-readonly={readOnly}
97
+ aria-errormessage={error}
98
+ placeholder={placeholder}
99
+ disabled={disabled}
100
+ readOnly={readOnly}
101
+ data-testid={name}
102
+ name={name}
103
+ size={1}
104
+ type={type}
105
+ ref={ref}
106
+ autoComplete="off"
107
+ {...{ autoFocus }}
108
+ value={actualValue}
109
+ onChange={handleChange}
110
+ onKeyDown={handleEnterPressed}
111
+ />
112
+ </InputWrapper>
113
+ );
114
+ };
@@ -0,0 +1,126 @@
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 { type RefProp } from "@react-fabric/core/dist/types/types";
25
+ import {
26
+ useCallback,
27
+ useDeferredValue,
28
+ useEffect,
29
+ useMemo,
30
+ useState,
31
+ type ChangeEvent,
32
+ } from "react";
33
+ import { InputWrapper } from "../internal/InputWrapper";
34
+ import { type InputProps } from "../types";
35
+ import { handleEnter } from "../utils";
36
+
37
+ export interface NumberProps
38
+ extends InputProps<number>,
39
+ RefProp<HTMLInputElement> {
40
+ /**
41
+ * minimum value
42
+ */
43
+ min?: number;
44
+ /**
45
+ * maximum value
46
+ */
47
+ max?: number;
48
+ /**
49
+ * increment step
50
+ */
51
+ step?: number;
52
+ }
53
+
54
+ export const Number = ({
55
+ ref,
56
+ value,
57
+ invalid,
58
+ readOnly,
59
+ disabled,
60
+ required,
61
+ placeholder,
62
+ autoFocus,
63
+ name,
64
+ min,
65
+ max,
66
+ step,
67
+ onChange,
68
+ onEnterPressed,
69
+ ...rest
70
+ }: NumberProps) => {
71
+ const [actualValue, setActualValue] = useState<"" | number>("");
72
+ const deferred = useDeferredValue(value);
73
+
74
+ const handleEnterPressed = useMemo(
75
+ () => handleEnter(onEnterPressed),
76
+ [onEnterPressed],
77
+ );
78
+
79
+ useEffect(() => {
80
+ setActualValue(deferred ?? "");
81
+ }, [deferred]);
82
+
83
+ const handleChange = useCallback(
84
+ (e?: ChangeEvent<HTMLInputElement>) => {
85
+ const number = e?.target.valueAsNumber;
86
+ setActualValue(number ?? "");
87
+ onChange?.(number ?? undefined);
88
+ },
89
+ [onChange],
90
+ );
91
+
92
+ return (
93
+ <InputWrapper
94
+ showClear={!!actualValue}
95
+ onClear={handleChange}
96
+ invalid={invalid}
97
+ readOnly={readOnly}
98
+ disabled={disabled}
99
+ required={required}
100
+ {...rest}
101
+ >
102
+ <input
103
+ className="appearance-none bg-transparent py-1 px-2 flex-1 border-none outline-none ring-0"
104
+ aria-invalid={invalid}
105
+ aria-disabled={disabled}
106
+ aria-readonly={readOnly}
107
+ placeholder={placeholder}
108
+ disabled={disabled}
109
+ readOnly={readOnly}
110
+ data-testid={name}
111
+ name={name}
112
+ type="number"
113
+ size={1}
114
+ min={min}
115
+ max={max}
116
+ step={step}
117
+ ref={ref}
118
+ autoComplete="off"
119
+ {...{ autoFocus }}
120
+ value={actualValue}
121
+ onChange={handleChange}
122
+ onKeyDown={handleEnterPressed}
123
+ />
124
+ </InputWrapper>
125
+ );
126
+ };