@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,127 @@
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
+ useRef,
30
+ useState,
31
+ type ChangeEvent,
32
+ } from "react";
33
+ import { InputWrapper } from "../internal/InputWrapper";
34
+ import { type InputProps } from "../types";
35
+
36
+ export interface TextareaProps
37
+ extends Omit<InputProps, "onEnterPressed">,
38
+ RefProp<HTMLTextAreaElement> {
39
+ /**
40
+ * textarae rows
41
+ */
42
+ rows?: number;
43
+ /**
44
+ * maximum rows
45
+ */
46
+ maxRows?: number;
47
+ /**
48
+ * increment rows on value change
49
+ */
50
+ variableRows?: boolean;
51
+ }
52
+
53
+ export const Textarea = ({
54
+ ref,
55
+ value,
56
+ name,
57
+ invalid,
58
+ readOnly,
59
+ disabled,
60
+ required,
61
+ placeholder,
62
+ autoFocus,
63
+ rows = 5,
64
+ maxRows = 12,
65
+ variableRows,
66
+ onChange,
67
+ ...rest
68
+ }: TextareaProps) => {
69
+ const [actualValue, setActualValue] = useState("");
70
+ const [fixedRows, setFixedRows] = useState(rows);
71
+ const deferred = useDeferredValue(value);
72
+ const wrapperRef = useRef(null);
73
+
74
+ useEffect(() => {
75
+ setActualValue(deferred ?? "");
76
+ }, [deferred]);
77
+
78
+ const handleChange = useCallback(
79
+ (e?: ChangeEvent<HTMLTextAreaElement>) => {
80
+ const value = e?.target.value;
81
+ setActualValue(value ?? "");
82
+ onChange?.(value ?? undefined);
83
+ },
84
+ [onChange],
85
+ );
86
+
87
+ useEffect(() => {
88
+ if (variableRows) {
89
+ const lines = actualValue?.split("\n").length ?? 1;
90
+ setFixedRows(Math.max(rows, Math.min(maxRows, lines)));
91
+ } else {
92
+ setFixedRows(rows);
93
+ }
94
+ }, [actualValue, variableRows, rows, maxRows]);
95
+
96
+ return (
97
+ <InputWrapper
98
+ showClear={!!actualValue}
99
+ onClear={handleChange}
100
+ invalid={invalid}
101
+ readOnly={readOnly}
102
+ disabled={disabled}
103
+ required={required}
104
+ wrapperRef={wrapperRef}
105
+ textarea
106
+ {...rest}
107
+ >
108
+ <textarea
109
+ className="appearance-none self-stretch bg-transparent py-1 px-2 flex-1 border-none outline-none ring-0 min-h-20 resize-none"
110
+ aria-invalid={invalid}
111
+ aria-disabled={disabled}
112
+ aria-readonly={readOnly}
113
+ placeholder={placeholder}
114
+ disabled={disabled}
115
+ readOnly={readOnly}
116
+ rows={fixedRows}
117
+ data-testid={name}
118
+ name={name}
119
+ ref={ref}
120
+ autoComplete="off"
121
+ {...{ autoFocus }}
122
+ value={actualValue}
123
+ onChange={handleChange}
124
+ />
125
+ </InputWrapper>
126
+ );
127
+ };
@@ -0,0 +1,61 @@
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, Tooltip } from "@react-fabric/core";
25
+ import { isString } from "@react-fabric/utilities";
26
+ import { useTranslation } from "react-i18next";
27
+
28
+ export const ErrorIcon = ({ error = "", invalid }: KeyValue) => {
29
+ const { t } = useTranslation();
30
+
31
+ return (
32
+ invalid && (
33
+ <Tooltip
34
+ content={
35
+ isString(error)
36
+ ? `${error}`
37
+ : (t(
38
+ error?.key,
39
+ error.values
40
+ ? {
41
+ ...error?.values,
42
+ label: t(
43
+ error.values?.label ?? "form:badkey",
44
+ error.values?.path,
45
+ ),
46
+ }
47
+ : {},
48
+ ) as string)
49
+ }
50
+ placement="bottom"
51
+ color="danger"
52
+ >
53
+ <Icon
54
+ className="flex-content first:ms-2 self-center"
55
+ color="danger"
56
+ icon={CoreIcons.alert}
57
+ />
58
+ </Tooltip>
59
+ )
60
+ );
61
+ };
@@ -0,0 +1,139 @@
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
+ .fieldWrapper {
26
+ /* later */
27
+ }
28
+ }
29
+
30
+ @layer var {
31
+ .inputWrapper {
32
+ &::after {
33
+ inset-inline: 50%;
34
+ transition: all 300ms;
35
+ transition-delay: 100ms;
36
+ }
37
+
38
+ & .decorate:has([data-ref="button"]) {
39
+ display: flex;
40
+ align-self: stretch;
41
+ }
42
+
43
+ &:has(input[disabled], textarea[disabled]) {
44
+ cursor: not-allowed;
45
+ background-color: theme(colors.tint.50);
46
+
47
+ & input,
48
+ & textarea {
49
+ cursor: not-allowed;
50
+ }
51
+ }
52
+
53
+ &:focus-within,
54
+ &:has([data-open="true"]) {
55
+ z-index: 1;
56
+ background-color: var(--fabric-input-focus);
57
+ outline-color: var(--primary);
58
+
59
+ &:not(:has([aria-readonly="true"]:not([hidden])))::after {
60
+ inset-inline: 0;
61
+ }
62
+ }
63
+
64
+ &:global(.fixed) {
65
+ box-shadow: 0 0 2rem 0.5rem var(--fabric-shadow);
66
+ }
67
+
68
+ & input,
69
+ & textarea {
70
+ max-width: 100%;
71
+ }
72
+
73
+ & > input[type="number"] + .inputClear {
74
+ margin-inline: -3rem 2rem;
75
+ }
76
+
77
+ & input::-ms-clear,
78
+ & select::-ms-clear,
79
+ & textarea::-ms-clear {
80
+ display: none;
81
+ }
82
+
83
+ & input[type="search"]::-webkit-search-cancel-button {
84
+ display: none;
85
+ }
86
+
87
+ & input[type="password"]:not(:placeholder-shown) {
88
+ /* stylelint-disable-next-line font-family-no-missing-generic-family-keyword */
89
+ font-family: caption;
90
+ letter-spacing: 2px;
91
+ }
92
+
93
+ & input:-webkit-autofill {
94
+ box-shadow: 0 0 0 999em var(--fabric-input) inset;
95
+
96
+ -webkit-text-fill-color: theme(colors.accent.700);
97
+ }
98
+
99
+ & input:-webkit-autofill:focus {
100
+ box-shadow: 0 0 0 999em var(--fabric-input-focus) inset;
101
+
102
+ -webkit-text-fill-color: theme(colors.accent.700);
103
+ }
104
+
105
+ &.invalidWrapper {
106
+ outline-color: theme(colors.danger.500);
107
+
108
+ &::after {
109
+ background-color: theme(colors.danger.500);
110
+ }
111
+ }
112
+ }
113
+
114
+ .fieldGroup {
115
+ > * {
116
+ line-height: 1;
117
+ }
118
+
119
+ > :not(:first-child) {
120
+ &,
121
+ & > div,
122
+ & > [role="button"],
123
+ & > [data-ref="button"] > [role="button"] {
124
+ border-start-start-radius: 0;
125
+ border-end-start-radius: 0;
126
+ }
127
+ }
128
+
129
+ > :not(:last-child) {
130
+ &,
131
+ & > div,
132
+ & > [role="button"],
133
+ & > [data-ref="button"] > [role="button"] {
134
+ border-start-end-radius: 0;
135
+ border-end-end-radius: 0;
136
+ }
137
+ }
138
+ }
139
+ }
@@ -0,0 +1,84 @@
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 classNames from "classnames";
25
+ import classes from "./Field.module.css";
26
+
27
+ export const FieldWrapper = ({
28
+ label,
29
+ appendLabel,
30
+ width,
31
+ labelWidth = 192,
32
+ inline,
33
+ info,
34
+ disabled,
35
+ required,
36
+ children,
37
+ listing,
38
+ className,
39
+ "data-inner": isInner,
40
+ }: KeyValue) => {
41
+ return (
42
+ <div
43
+ className={classNames(
44
+ classes.fieldWrapper,
45
+ className,
46
+ inline ? "flex flex-nowrap gap-1" : "block",
47
+ )}
48
+ data-ref="fieldWrapper"
49
+ style={{ width, flexBasis: width }}
50
+ >
51
+ {!isInner && (
52
+ <div
53
+ className="flex flex-nowrap flex-content gap-1 py-0.5 px-2"
54
+ style={{ width: inline ? labelWidth : undefined }}
55
+ >
56
+ <label
57
+ role="none"
58
+ className={classNames(
59
+ "flex-initial truncate text-sm",
60
+ disabled && "text-muted",
61
+ )}
62
+ onClick={(e) =>
63
+ e.currentTarget
64
+ .closest<HTMLElement>('[data-ref="fieldWrapper"]')
65
+ ?.querySelector<HTMLElement>("input,textarea")
66
+ ?.focus()
67
+ }
68
+ >
69
+ {label}
70
+ </label>
71
+ {required && (
72
+ <span className="text-danger-500 select-none flex-content text-[10px]">
73
+ ✽
74
+ </span>
75
+ )}
76
+ {appendLabel && <span className="flex-content">{appendLabel}</span>}
77
+ </div>
78
+ )}
79
+ {children}
80
+ {listing}
81
+ {info && <div className="text-sm text-muted flex-content">{info}</div>}
82
+ </div>
83
+ );
84
+ };
@@ -0,0 +1,187 @@
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 { FloatingPortal } from "@floating-ui/react";
25
+ import { CoreIcons, Icon } from "@react-fabric/core";
26
+ import { isString } from "@react-fabric/utilities";
27
+ import classNames from "classnames";
28
+ import {
29
+ useCallback,
30
+ useEffect,
31
+ useMemo,
32
+ useRef,
33
+ useState,
34
+ type MouseEvent,
35
+ } from "react";
36
+ import { Fragment } from "react/jsx-runtime";
37
+ import { ErrorIcon } from "./ErrorIcon";
38
+ import classes from "./Field.module.css";
39
+ import { FieldWrapper } from "./FieldWrapper";
40
+
41
+ export const InputWrapper = ({
42
+ invalid,
43
+ error,
44
+ allowClear,
45
+ showClear,
46
+ decorateStart,
47
+ decorateEnd,
48
+ children,
49
+ onClear,
50
+ noOutline,
51
+ noBorder,
52
+ textarea = false,
53
+ "data-inner": isInner,
54
+ "data-open": isOpen,
55
+ // expand autocomplete
56
+ floatingExpand,
57
+ wrapperRef,
58
+ width,
59
+ ...rest
60
+ }: KeyValue) => {
61
+ const elRef = useRef<HTMLDivElement>(null);
62
+ const [isExpanded, setExpanded] = useState(false);
63
+
64
+ const Wrapper = useMemo(
65
+ () => (isInner || !rest.label ? Fragment : FieldWrapper),
66
+ [isInner, rest.label],
67
+ );
68
+ const wrapperProps = useMemo(
69
+ () => (isInner || !rest.label ? {} : { ...rest, width }),
70
+ [isInner, rest, width],
71
+ );
72
+
73
+ const handleClear = useCallback(
74
+ (e: MouseEvent<HTMLDivElement>) => {
75
+ e.currentTarget.parentElement
76
+ ?.querySelector<HTMLElement>("input,textarea,button")
77
+ ?.focus();
78
+ onClear?.();
79
+ },
80
+ [onClear],
81
+ );
82
+
83
+ const doFocus = useCallback(
84
+ (e: MouseEvent<HTMLDivElement>) => {
85
+ e.currentTarget.parentElement
86
+ ?.querySelector<HTMLElement>("input,textarea,button")
87
+ ?.focus();
88
+ },
89
+ [onClear],
90
+ );
91
+
92
+ useEffect(() => {
93
+ isExpanded &&
94
+ setTimeout(
95
+ () => wrapperRef?.current?.querySelector("textarea")?.focus(),
96
+ 50,
97
+ );
98
+ }, [isExpanded]);
99
+
100
+ const el = (
101
+ <div
102
+ role="none"
103
+ ref={wrapperRef}
104
+ className={classNames(
105
+ classes.inputWrapper,
106
+ invalid && classes.invalidWrapper,
107
+ !noOutline &&
108
+ !noBorder &&
109
+ "bg-[var(--fabric-input)] outline overflow-hidden",
110
+ "flex flex-nowrap items-center",
111
+ textarea ? "rounded-capped group" : "rounded",
112
+ isExpanded
113
+ ? "fixed inset-4 z-[99]"
114
+ : floatingExpand
115
+ ? "absolute inset-x-0"
116
+ : "relative",
117
+ !noOutline &&
118
+ "after:absolute after:bottom-0 after:h-px after:bg-primary-500 after:mx-auto",
119
+ )}
120
+ onKeyDown={(e) => e.key === "Escape" && setExpanded(false)}
121
+ >
122
+ {decorateStart && (
123
+ <div
124
+ onClickCapture={doFocus}
125
+ className={classNames(
126
+ classes.decorate,
127
+ "text-muted leading-none flex-content",
128
+ isString(decorateStart) && "px-1",
129
+ )}
130
+ >
131
+ {decorateStart}
132
+ </div>
133
+ )}
134
+ <ErrorIcon invalid={invalid} error={error} />
135
+ {children}
136
+ {allowClear && showClear && (
137
+ <Icon
138
+ icon={CoreIcons.close}
139
+ onClick={handleClear}
140
+ className={classNames(
141
+ "text-tint-500 hover:text-tint-700 p-1 z-1 flex-content",
142
+ classes.inputClear,
143
+ textarea && "self-start mt-1 me-1",
144
+ )}
145
+ />
146
+ )}
147
+ {decorateEnd && (
148
+ <div
149
+ onClickCapture={doFocus}
150
+ className={classNames(
151
+ classes.decorate,
152
+ "text-muted leading-none flex-content",
153
+ isString(decorateEnd) && "px-1",
154
+ )}
155
+ >
156
+ {decorateEnd}
157
+ </div>
158
+ )}
159
+ {textarea && (
160
+ <Icon
161
+ onClick={() => setExpanded(!isExpanded)}
162
+ icon={isExpanded ? CoreIcons.arrowCollapse : CoreIcons.arrowExpand}
163
+ className="hidden group-hover:block absolute top-0 end-0 bg-base hover:bg-tint-200 p-1 opacity-65 rounded-capped"
164
+ />
165
+ )}
166
+ </div>
167
+ );
168
+
169
+ return (
170
+ <Wrapper {...wrapperProps}>
171
+ <div
172
+ ref={elRef}
173
+ className="flex-1 relative min-h-8"
174
+ style={{ width, flexBasis: width }}
175
+ >
176
+ {!isExpanded && el}
177
+ {isExpanded && (
178
+ <FloatingPortal
179
+ root={elRef.current?.closest<HTMLElement>(".theme-base")}
180
+ >
181
+ {el}
182
+ </FloatingPortal>
183
+ )}
184
+ </div>
185
+ </Wrapper>
186
+ );
187
+ };