@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,160 @@
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 { Button, CoreIcons, Icon, useDebounce } from "@react-fabric/core";
25
+ import { type RefProp } from "@react-fabric/core/dist/types/types";
26
+ import {
27
+ Fragment,
28
+ useCallback,
29
+ useDeferredValue,
30
+ useEffect,
31
+ useMemo,
32
+ useRef,
33
+ useState,
34
+ type ChangeEvent,
35
+ } from "react";
36
+ import { InputWrapper } from "../internal/InputWrapper";
37
+ import { type InputProps } from "../types";
38
+ import { handleEnter } from "../utils";
39
+
40
+ export interface SearchProps extends InputProps, RefProp<HTMLInputElement> {
41
+ /**
42
+ * is searching
43
+ */
44
+ searching?: boolean;
45
+ /**
46
+ * search on value change
47
+ */
48
+ searchOnChange?: boolean;
49
+ /**
50
+ * search handler
51
+ */
52
+ onSearch?: (query?: string) => void;
53
+ }
54
+
55
+ export const Search = ({
56
+ ref,
57
+ value,
58
+ name,
59
+ invalid,
60
+ readOnly,
61
+ disabled,
62
+ required,
63
+ searching,
64
+ placeholder,
65
+ autoFocus,
66
+ allowClear = true,
67
+ searchOnChange = true,
68
+ decorateEnd,
69
+ decorateStart,
70
+ onSearch,
71
+ onChange,
72
+ onEnterPressed,
73
+ ...rest
74
+ }: SearchProps) => {
75
+ const [actualValue, setActualValue] = useState("");
76
+ const deferred = useDeferredValue(value);
77
+
78
+ const refValue = useRef("");
79
+ const handleSearch = useDebounce(
80
+ () => onSearch?.(refValue.current),
81
+ [onSearch],
82
+ 250,
83
+ );
84
+
85
+ const handleEnterPressed = useMemo(
86
+ () => handleEnter(handleSearch),
87
+ [handleSearch],
88
+ );
89
+
90
+ useEffect(() => {
91
+ setActualValue(deferred ?? "");
92
+ }, [deferred]);
93
+
94
+ const handleChange = useCallback(
95
+ (e?: ChangeEvent<HTMLInputElement>) => {
96
+ const value = e?.target.value;
97
+ refValue.current = value ?? "";
98
+ setActualValue(value ?? "");
99
+ onChange?.(value ?? undefined);
100
+
101
+ // value was cleared so force fire onSearch
102
+ if (searchOnChange) {
103
+ handleSearch();
104
+ }
105
+ },
106
+ [onChange, searchOnChange, handleSearch],
107
+ );
108
+
109
+ return (
110
+ <InputWrapper
111
+ showClear={!!actualValue}
112
+ onClear={handleChange}
113
+ allowClear={allowClear}
114
+ invalid={invalid}
115
+ readOnly={readOnly}
116
+ disabled={disabled}
117
+ required={required}
118
+ decorateStart={decorateStart}
119
+ decorateEnd={
120
+ <Fragment>
121
+ {decorateEnd}
122
+ {searching && (
123
+ <Icon className="p-2" icon={CoreIcons.spinner} animate="spin" />
124
+ )}
125
+ {!searching && (
126
+ <Button
127
+ tabIndex={-1}
128
+ variant="link"
129
+ aria-label="search"
130
+ disabled={!actualValue}
131
+ icon={CoreIcons.search}
132
+ onClick={handleSearch}
133
+ />
134
+ )}
135
+ </Fragment>
136
+ }
137
+ {...rest}
138
+ >
139
+ <input
140
+ className="appearance-none bg-transparent py-1 px-2 flex-1 border-none outline-none ring-0"
141
+ aria-invalid={invalid}
142
+ aria-disabled={disabled}
143
+ aria-readonly={readOnly}
144
+ placeholder={placeholder}
145
+ disabled={disabled}
146
+ readOnly={readOnly}
147
+ size={1}
148
+ data-testid={name}
149
+ name={name}
150
+ type="search"
151
+ ref={ref}
152
+ autoComplete="off"
153
+ {...{ autoFocus }}
154
+ value={actualValue}
155
+ onChange={handleChange}
156
+ onKeyDown={handleEnterPressed}
157
+ />
158
+ </InputWrapper>
159
+ );
160
+ };
@@ -0,0 +1,282 @@
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 { Format } from "@react-fabric/utilities";
25
+ import classNames from "classnames";
26
+ import {
27
+ Fragment,
28
+ useCallback,
29
+ useDeferredValue,
30
+ useEffect,
31
+ useMemo,
32
+ useState,
33
+ } from "react";
34
+ import { InputWrapper } from "../internal/InputWrapper";
35
+ import { useRanger } from "../ranger";
36
+ import { type InputProps } from "../types";
37
+
38
+ export interface SliderProps
39
+ extends Omit<InputProps<number>, "placeholder" | "allowClear" | "children"> {
40
+ /**
41
+ * min value
42
+ */
43
+ min?: number;
44
+ /**
45
+ * max value
46
+ */
47
+ max?: number;
48
+ /**
49
+ * increment step
50
+ */
51
+ step?: number;
52
+ /**
53
+ * slider color
54
+ */
55
+ color?: string;
56
+ /**
57
+ * slider height (only vertical)
58
+ */
59
+ height?: number;
60
+ /**
61
+ * vertical orientation
62
+ */
63
+ vertical?: boolean;
64
+ /**
65
+ * display value tooltip
66
+ */
67
+ showValue?: boolean;
68
+ /**
69
+ * display min/max labels
70
+ */
71
+ showLabels?: boolean;
72
+ /**
73
+ * custom min label
74
+ */
75
+ minLabel?: string;
76
+ /**
77
+ * custom max label
78
+ */
79
+ maxLabel?: string;
80
+ /**
81
+ * step ranges
82
+ */
83
+ ranges?: number[];
84
+ /**
85
+ * infinity max value
86
+ */
87
+ useInfinity?: true;
88
+ /**
89
+ * format value
90
+ */
91
+ format?: string;
92
+ /**
93
+ * slide event handler
94
+ */
95
+ onSlide?: (val: number) => void;
96
+ }
97
+
98
+ /**
99
+ * Number slider input
100
+ */
101
+ export const Slider = ({
102
+ error,
103
+ name,
104
+ value,
105
+ step = 1,
106
+ min = 0,
107
+ max = 100,
108
+ height = 192,
109
+ color,
110
+ vertical,
111
+ showLabels,
112
+ showValue,
113
+ minLabel,
114
+ maxLabel,
115
+ ranges,
116
+ useInfinity,
117
+ format,
118
+ invalid,
119
+ autoFocus,
120
+ disabled,
121
+ readOnly,
122
+ onBlur,
123
+ onChange,
124
+ onEnterPressed,
125
+ onFocus,
126
+ onSlide,
127
+ ...rest
128
+ }: SliderProps) => {
129
+ const [actualValue, setActualValue] = useState<number>(0);
130
+ const [displayValue, setDisplayValue] = useState(false);
131
+ const deferred = useDeferredValue(value);
132
+
133
+ const minValue = useMemo(() => ranges?.[0] ?? min, [min, ranges]);
134
+ const maxValue = useMemo(() => ranges?.slice(-1)[0] ?? max, [max, ranges]);
135
+
136
+ useEffect(() => {
137
+ setActualValue(
138
+ useInfinity && deferred === Infinity ? max : deferred ?? min,
139
+ );
140
+ }, [deferred, min, max, useInfinity]);
141
+ useEffect(() => {
142
+ setDisplayValue(!!showValue);
143
+ }, [showValue]);
144
+
145
+ const handleChange = useCallback(
146
+ (value: number) => {
147
+ const val = useInfinity && value === max ? Infinity : value;
148
+ setActualValue(val ?? 0);
149
+ onChange?.(val ?? undefined);
150
+ },
151
+ [onChange, max, useInfinity],
152
+ );
153
+
154
+ const minDisplay = useMemo(
155
+ () => (
156
+ <span className="text-sm text-dimmed px-2.5 py-px whitespace-nowrap overflow-hidden text-center">
157
+ {minLabel ?? Format.number(min, format)}
158
+ </span>
159
+ ),
160
+ [minLabel, min, format],
161
+ );
162
+ const maxDisplay = useMemo(
163
+ () => (
164
+ <span className="text-sm text-dimmed px-2.5 py-px whitespace-nowrap overflow-hidden text-center">
165
+ {maxLabel ?? (useInfinity ? "∞" : Format.number(max, format))}
166
+ </span>
167
+ ),
168
+ [maxLabel, max, format, useInfinity],
169
+ );
170
+
171
+ const rangerInstance = useRanger({
172
+ values: [actualValue],
173
+ min: minValue,
174
+ max: maxValue,
175
+ stepSize: step,
176
+ steps: ranges,
177
+ vertical,
178
+ tickSize: Math.max((max - min) / 10, step),
179
+ onDrag: (val) => onSlide?.(val[0]),
180
+ onDragStart: () => setDisplayValue(true),
181
+ onDragEnd: () => setDisplayValue(!!showValue),
182
+ onChange: (values: readonly number[]) => handleChange(values[0]),
183
+ });
184
+
185
+ return (
186
+ <InputWrapper
187
+ {...rest}
188
+ noOutline
189
+ error={error}
190
+ invalid={invalid}
191
+ isDisabled={disabled}
192
+ isReadOnly={readOnly}
193
+ canClear={!!actualValue}
194
+ >
195
+ <div
196
+ className={classNames(
197
+ "flex-auto order-3 flex items-center select-none relative z-0",
198
+ vertical && "flex-col w-8",
199
+ invalid ? "text-danger-500" : "text-primary-600",
200
+ )}
201
+ data-vertical={!!vertical}
202
+ style={invalid ? undefined : { color }}
203
+ >
204
+ {showLabels && (vertical ? maxDisplay : minDisplay)}
205
+ <div
206
+ ref={rangerInstance.trackElRef}
207
+ className={classNames(
208
+ "flex-auto relative flex items-center",
209
+ vertical ? "my-2 w-8" : "mx-2 h-8",
210
+ )}
211
+ style={{ height: vertical ? height : undefined }}
212
+ >
213
+ {rangerInstance.ticks.map(({ value, key, styles }) => (
214
+ <div
215
+ key={key}
216
+ className={classNames(
217
+ "absolute inline-block h-1 w-1 rounded-sm bg-invert opacity-30 leading-[0] z-1",
218
+ vertical && "left-1/2 -ml-0.5",
219
+ )}
220
+ style={styles}
221
+ />
222
+ ))}
223
+ {rangerInstance.segments.map(({ key, styles, active }) => (
224
+ <div
225
+ key={key}
226
+ className={classNames(
227
+ "absolute",
228
+ active
229
+ ? "bg-current z-1 opacity-90"
230
+ : "bg-invert opacity-5 z-0",
231
+ vertical
232
+ ? "-bottom-1 left-1/2 -translate-x-1/2"
233
+ : "top-1/2 -translate-y-1/2",
234
+ !vertical && (active ? "h-1" : "h-0.5"),
235
+ vertical && (active ? "w-1" : "w-0.5"),
236
+ )}
237
+ style={styles}
238
+ />
239
+ ))}
240
+ {rangerInstance.handles.map(
241
+ ({ key, value, props, styles, percentage, valueStyles }) => (
242
+ <Fragment key={key}>
243
+ <button
244
+ type="button"
245
+ className={classNames(
246
+ "bg-current cursor-pointer appearance-none h-3 w-3 rounded-full z-3 absolute",
247
+ "focus:ring-2 focus:ring-offset-2",
248
+ vertical && "left-1/2 -ml-1.5",
249
+ )}
250
+ {...props}
251
+ style={styles}
252
+ />
253
+ {displayValue && (
254
+ <div
255
+ className={classNames(
256
+ "bg-invert text-invert py-px px-2 text-xs absolute rounded z-[5] whitespace-nowrap",
257
+ "before:absolute before:h-1.5 before:w-1.5 before:z-[-1] before:bg-invert before:top-1/2 before:-translate-y-1/2 before:rotate-45",
258
+ vertical
259
+ ? "end-0 ltr:translate-x-full rtl:-translate-x-full translate-y-2"
260
+ : "top-1/2 -translate-y-1/2",
261
+ !vertical &&
262
+ (percentage > 50
263
+ ? "-ms-3 ltr:-translate-x-full rtl:translate-x-full ltr:before:translate-x-[0.125rem] rtl:before:-translate-x-[0.125rem] before:end-0"
264
+ : "ms-3 ltr:before:-translate-x-[0.125rem] rtl:before:translate-x-[0.125rem] before:start-0"),
265
+ )}
266
+ data-align={percentage > 50 ? "start" : "end"}
267
+ style={valueStyles}
268
+ >
269
+ {value === Infinity || (useInfinity && value === max)
270
+ ? maxLabel ?? "∞"
271
+ : Format.number(value, format)}
272
+ </div>
273
+ )}
274
+ </Fragment>
275
+ ),
276
+ )}
277
+ </div>
278
+ {showLabels && (vertical ? minDisplay : maxDisplay)}
279
+ </div>
280
+ </InputWrapper>
281
+ );
282
+ };
@@ -0,0 +1,168 @@
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 {
25
+ type ColorType,
26
+ type RefProp,
27
+ } from "@react-fabric/core/dist/types/types";
28
+ import classNames from "classnames";
29
+ import {
30
+ useCallback,
31
+ useDeferredValue,
32
+ useEffect,
33
+ useState,
34
+ type ChangeEvent,
35
+ } from "react";
36
+ import { ErrorIcon } from "../internal/ErrorIcon";
37
+ import { getBgClass, getColor } from "../utils";
38
+
39
+ export interface SwitchProps extends RefProp<HTMLInputElement> {
40
+ /**
41
+ * Switch label
42
+ */
43
+ label?: JSX.Element | string;
44
+ /**
45
+ * checked
46
+ */
47
+ checked?: boolean;
48
+ /**
49
+ * on checked change
50
+ */
51
+ onChange?: (checked: boolean) => void;
52
+
53
+ /**
54
+ * disabled input
55
+ */
56
+ disabled?: boolean;
57
+ /**
58
+ * invalid value input
59
+ */
60
+ invalid?: boolean;
61
+ /**
62
+ * error message
63
+ */
64
+ error?: string;
65
+
66
+ /**
67
+ * field width
68
+ */
69
+ width?: number | string;
70
+
71
+ /**
72
+ * cutom Switch icon
73
+ */
74
+ color?: ColorType;
75
+ /**
76
+ * cutom Switch icon
77
+ */
78
+ defaultColor?: ColorType;
79
+
80
+ name?: string;
81
+ }
82
+
83
+ export const Switch = ({
84
+ ref,
85
+ label,
86
+ checked,
87
+ invalid,
88
+ disabled,
89
+ error,
90
+ width,
91
+ color,
92
+ name,
93
+ defaultColor,
94
+ onChange,
95
+ ...rest
96
+ }: SwitchProps) => {
97
+ const [actualValue, setActualValue] = useState(false);
98
+ const deferred = useDeferredValue(!!checked);
99
+
100
+ useEffect(() => {
101
+ setActualValue(deferred);
102
+ }, [deferred]);
103
+
104
+ const handleChange = useCallback(
105
+ (e?: ChangeEvent<HTMLInputElement>) => {
106
+ setActualValue(e?.target.checked ?? false);
107
+ onChange?.(e?.target.checked ?? false);
108
+ },
109
+ [onChange],
110
+ );
111
+
112
+ return (
113
+ <label
114
+ className={classNames(
115
+ invalid ? "ring-danger-500" : "ring-primary-500",
116
+ disabled
117
+ ? "opacity-50 cursor-not-allowed"
118
+ : "hover:opacity-100 cursor-pointer",
119
+ "inline-flex gap-1 py-1 items-center relative cursor-pointer opacity-85 rounded ring-offset-2 has-[:focus-visible]:ring-1",
120
+ )}
121
+ style={{ width }}
122
+ >
123
+ <div
124
+ className={classNames(
125
+ actualValue && getBgClass(color ? `${color}-500` : "primary-500"),
126
+ !actualValue &&
127
+ (defaultColor ? getBgClass(`${defaultColor}-500`) : "bg-tint-200"),
128
+ "relative flex h-6 w-10 cursor-pointer rounded-full p-1 transition-colors duration-200 ease-in-out",
129
+ )}
130
+ style={{
131
+ backgroundColor: actualValue
132
+ ? getColor(color)
133
+ : getColor(defaultColor),
134
+ }}
135
+ >
136
+ <input
137
+ className={classNames(
138
+ "appearance-none absolute opacity-0 inset-0 bg-transparent border-none outline-none ring-0",
139
+ )}
140
+ aria-invalid={invalid}
141
+ aria-disabled={disabled}
142
+ disabled={disabled}
143
+ aria-errormessage={error}
144
+ type="checkbox"
145
+ ref={ref}
146
+ size={1}
147
+ tabIndex={0}
148
+ autoComplete="off"
149
+ checked={actualValue}
150
+ onChange={handleChange}
151
+ {...rest}
152
+ />
153
+ <span
154
+ aria-hidden="true"
155
+ className={classNames(
156
+ !disabled && "shadow-sm",
157
+ actualValue ? "translate-x-4" : "translate-x-0",
158
+ "pointer-events-none inline-block size-4 rounded-full bg-white ring-0 transition duration-200 ease-in-out",
159
+ )}
160
+ />
161
+ </div>
162
+ <ErrorIcon invalid={invalid} error={error} />
163
+ {label && (
164
+ <span className="flex-initial leading-5 truncate">{label}</span>
165
+ )}
166
+ </label>
167
+ );
168
+ };