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