@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,113 @@
1
+ 'use client';
2
+
3
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
4
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
5
+ import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
6
+ import { FloatingPortal } from '@floating-ui/react';
7
+ import { Icon, CoreIcons } from '@react-fabric/core';
8
+ import { isString } from '@react-fabric/utilities';
9
+ import classNames from 'classnames';
10
+ import { useRef, useState, useMemo, useCallback, useEffect } from 'react';
11
+ import { Fragment, jsxs, jsx } from 'react/jsx-runtime';
12
+ import { ErrorIcon } from './ErrorIcon.js';
13
+ import classes from './Field.module.css';
14
+ import { FieldWrapper } from './FieldWrapper.js';
15
+
16
+ var _excluded = ["invalid", "error", "allowClear", "showClear", "decorateStart", "decorateEnd", "children", "onClear", "noOutline", "noBorder", "textarea", "data-inner", "data-open", "floatingExpand", "wrapperRef", "width"];
17
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
18
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
19
+ var InputWrapper = function InputWrapper(_ref) {
20
+ var _elRef$current;
21
+ var invalid = _ref.invalid,
22
+ error = _ref.error,
23
+ allowClear = _ref.allowClear,
24
+ showClear = _ref.showClear,
25
+ decorateStart = _ref.decorateStart,
26
+ decorateEnd = _ref.decorateEnd,
27
+ children = _ref.children,
28
+ onClear = _ref.onClear,
29
+ noOutline = _ref.noOutline,
30
+ noBorder = _ref.noBorder,
31
+ _ref$textarea = _ref.textarea,
32
+ textarea = _ref$textarea === void 0 ? false : _ref$textarea,
33
+ isInner = _ref["data-inner"];
34
+ _ref["data-open"];
35
+ var floatingExpand = _ref.floatingExpand,
36
+ wrapperRef = _ref.wrapperRef,
37
+ width = _ref.width,
38
+ rest = _objectWithoutProperties(_ref, _excluded);
39
+ var elRef = useRef(null);
40
+ var _useState = useState(false),
41
+ _useState2 = _slicedToArray(_useState, 2),
42
+ isExpanded = _useState2[0],
43
+ setExpanded = _useState2[1];
44
+ var Wrapper = useMemo(function () {
45
+ return isInner || !rest.label ? Fragment : FieldWrapper;
46
+ }, [isInner, rest.label]);
47
+ var wrapperProps = useMemo(function () {
48
+ return isInner || !rest.label ? {} : _objectSpread(_objectSpread({}, rest), {}, {
49
+ width: width
50
+ });
51
+ }, [isInner, rest, width]);
52
+ var handleClear = useCallback(function (e) {
53
+ var _e$currentTarget$pare;
54
+ (_e$currentTarget$pare = e.currentTarget.parentElement) === null || _e$currentTarget$pare === void 0 || (_e$currentTarget$pare = _e$currentTarget$pare.querySelector("input,textarea,button")) === null || _e$currentTarget$pare === void 0 || _e$currentTarget$pare.focus();
55
+ onClear === null || onClear === void 0 || onClear();
56
+ }, [onClear]);
57
+ var doFocus = useCallback(function (e) {
58
+ var _e$currentTarget$pare2;
59
+ (_e$currentTarget$pare2 = e.currentTarget.parentElement) === null || _e$currentTarget$pare2 === void 0 || (_e$currentTarget$pare2 = _e$currentTarget$pare2.querySelector("input,textarea,button")) === null || _e$currentTarget$pare2 === void 0 || _e$currentTarget$pare2.focus();
60
+ }, [onClear]);
61
+ useEffect(function () {
62
+ isExpanded && setTimeout(function () {
63
+ var _wrapperRef$current;
64
+ return wrapperRef === null || wrapperRef === void 0 || (_wrapperRef$current = wrapperRef.current) === null || _wrapperRef$current === void 0 || (_wrapperRef$current = _wrapperRef$current.querySelector("textarea")) === null || _wrapperRef$current === void 0 ? void 0 : _wrapperRef$current.focus();
65
+ }, 50);
66
+ }, [isExpanded]);
67
+ var el = /*#__PURE__*/jsxs("div", {
68
+ role: "none",
69
+ ref: wrapperRef,
70
+ className: classNames(classes.inputWrapper, invalid && classes.invalidWrapper, !noOutline && !noBorder && "bg-[var(--fabric-input)] outline overflow-hidden", "flex flex-nowrap items-center", textarea ? "rounded-capped group" : "rounded", isExpanded ? "fixed inset-4 z-[99]" : floatingExpand ? "absolute inset-x-0" : "relative", !noOutline && "after:absolute after:bottom-0 after:h-px after:bg-primary-500 after:mx-auto"),
71
+ onKeyDown: function onKeyDown(e) {
72
+ return e.key === "Escape" && setExpanded(false);
73
+ },
74
+ children: [decorateStart && /*#__PURE__*/jsx("div", {
75
+ onClickCapture: doFocus,
76
+ className: classNames(classes.decorate, "text-muted leading-none flex-content", isString(decorateStart) && "px-1"),
77
+ children: decorateStart
78
+ }), /*#__PURE__*/jsx(ErrorIcon, {
79
+ invalid: invalid,
80
+ error: error
81
+ }), children, allowClear && showClear && /*#__PURE__*/jsx(Icon, {
82
+ icon: CoreIcons.close,
83
+ onClick: handleClear,
84
+ className: classNames("text-tint-500 hover:text-tint-700 p-1 z-1 flex-content", classes.inputClear, textarea && "self-start mt-1 me-1")
85
+ }), decorateEnd && /*#__PURE__*/jsx("div", {
86
+ onClickCapture: doFocus,
87
+ className: classNames(classes.decorate, "text-muted leading-none flex-content", isString(decorateEnd) && "px-1"),
88
+ children: decorateEnd
89
+ }), textarea && /*#__PURE__*/jsx(Icon, {
90
+ onClick: function onClick() {
91
+ return setExpanded(!isExpanded);
92
+ },
93
+ icon: isExpanded ? CoreIcons.arrowCollapse : CoreIcons.arrowExpand,
94
+ className: "hidden group-hover:block absolute top-0 end-0 bg-base hover:bg-tint-200 p-1 opacity-65 rounded-capped"
95
+ })]
96
+ });
97
+ return /*#__PURE__*/jsx(Wrapper, _objectSpread(_objectSpread({}, wrapperProps), {}, {
98
+ children: /*#__PURE__*/jsxs("div", {
99
+ ref: elRef,
100
+ className: "flex-1 relative min-h-8",
101
+ style: {
102
+ width: width,
103
+ flexBasis: width
104
+ },
105
+ children: [!isExpanded && el, isExpanded && /*#__PURE__*/jsx(FloatingPortal, {
106
+ root: (_elRef$current = elRef.current) === null || _elRef$current === void 0 ? void 0 : _elRef$current.closest(".theme-base"),
107
+ children: el
108
+ })]
109
+ })
110
+ }));
111
+ };
112
+
113
+ export { InputWrapper };
@@ -0,0 +1,352 @@
1
+ 'use client';
2
+
3
+ import _defineProperty from '@babel/runtime/helpers/defineProperty';
4
+ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
5
+ import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
6
+ import { useIsRtl } from '@react-fabric/core';
7
+ import { useState, useMemo, useRef, useCallback, useEffect } from 'react';
8
+ import { getBoundingClientRect, linearInterpolator, sortNumList } from './utils.js';
9
+
10
+ var useGetLatest = function useGetLatest(val) {
11
+ var ref = useRef(val);
12
+ ref.current = val;
13
+ return useCallback(function () {
14
+ return ref.current;
15
+ }, []);
16
+ };
17
+ var useRanger = function useRanger(opts) {
18
+ var _opts$tickSize;
19
+ var tickSize = (_opts$tickSize = opts.tickSize) !== null && _opts$tickSize !== void 0 ? _opts$tickSize : 10;
20
+ var values = opts.values;
21
+ var min = opts.min;
22
+ var max = opts.max;
23
+ var isVertical = opts.vertical;
24
+ var controlledTicks = opts.ticks;
25
+ var steps = "steps" in opts ? opts.steps : undefined;
26
+ var onChange = opts.onChange;
27
+ var onDrag = opts.onDrag;
28
+ var onDragStart = opts.onDragStart;
29
+ var onDragEnd = opts.onDragEnd;
30
+ var stepSize = "stepSize" in opts ? opts.stepSize : 1;
31
+ var _useState = useState(),
32
+ _useState2 = _slicedToArray(_useState, 2),
33
+ activeHandleIndex = _useState2[0],
34
+ setActiveHandleIndex = _useState2[1];
35
+ var _useState3 = useState(),
36
+ _useState4 = _slicedToArray(_useState3, 2),
37
+ segmentStart = _useState4[0],
38
+ setSegmentStart = _useState4[1];
39
+ var _useState5 = useState(),
40
+ _useState6 = _slicedToArray(_useState5, 2),
41
+ tempValues = _useState6[0],
42
+ setTempValues = _useState6[1];
43
+ var isRtl = useIsRtl();
44
+ var positionProp = useMemo(function () {
45
+ return isVertical ? "bottom" : isRtl ? "right" : "left";
46
+ }, [isVertical, isRtl]);
47
+ var marginProp = useMemo(function () {
48
+ return isVertical ? "marginBottom" : isRtl ? "marginRight" : "marginLeft";
49
+ }, [isVertical, isRtl]);
50
+ var getLatest = useGetLatest({
51
+ activeHandleIndex: activeHandleIndex,
52
+ segmentStart: segmentStart,
53
+ onChange: onChange,
54
+ onDrag: onDrag,
55
+ onDragStart: onDragStart,
56
+ onDragEnd: onDragEnd,
57
+ values: values,
58
+ tempValues: tempValues
59
+ });
60
+ var trackElRef = useRef(null);
61
+ var segmentElRef = useRef(null);
62
+ var getValueForClientX = useCallback(function (clientX) {
63
+ var trackDims = getBoundingClientRect(trackElRef.current);
64
+ return linearInterpolator.getValueForClientX(clientX, trackDims, min, max, isRtl);
65
+ }, [max, min, isRtl]);
66
+ var getValueForClientY = useCallback(function (clientY) {
67
+ var trackDims = getBoundingClientRect(trackElRef.current);
68
+ return linearInterpolator.getValueForClientY(clientY, trackDims, min, max);
69
+ }, [max, min]);
70
+ var getNextStep = useCallback(function (val, direction) {
71
+ if (steps) {
72
+ var currIndex = steps.indexOf(val);
73
+ var nextIndex = currIndex + direction;
74
+ if (nextIndex >= 0 && nextIndex < steps.length) {
75
+ return steps[nextIndex];
76
+ } else {
77
+ return val;
78
+ }
79
+ } else {
80
+ if (process.env.NODE_ENV !== "production") {
81
+ if (typeof stepSize === "undefined") {
82
+ throw new Error("Warning: The option `stepSize` is expected in `useRanger`, but its value is `undefined`");
83
+ }
84
+ }
85
+ var nextVal = val + stepSize * direction;
86
+ if (nextVal >= min && nextVal <= max) {
87
+ return nextVal;
88
+ } else {
89
+ return val;
90
+ }
91
+ }
92
+ }, [max, min, stepSize, steps]);
93
+ var roundToStep = useCallback(function (val) {
94
+ var left = min;
95
+ var right = max;
96
+ if (steps) {
97
+ steps.forEach(function (step) {
98
+ if (step <= val && step > left) {
99
+ left = step;
100
+ }
101
+ if (step >= val && step < right) {
102
+ right = step;
103
+ }
104
+ });
105
+ } else {
106
+ if (process.env.NODE_ENV !== "production") {
107
+ if (typeof stepSize === "undefined") {
108
+ throw new Error("Warning: The option `stepSize` is expected in `useRanger`, but its value is `undefined`");
109
+ }
110
+ }
111
+ while (left < val && left + stepSize < val) {
112
+ left += stepSize;
113
+ }
114
+ right = Math.min(left + stepSize, max);
115
+ }
116
+ if (val - left < right - val) {
117
+ return +left.toFixed(3);
118
+ }
119
+ return +right.toFixed(3);
120
+ }, [max, min, stepSize, steps]);
121
+ var handleDrag = useCallback(function (e) {
122
+ var _getLatest = getLatest(),
123
+ activeHandleIndex = _getLatest.activeHandleIndex,
124
+ onDrag = _getLatest.onDrag;
125
+ var clientX =
126
+ // @ts-expect-error ignore
127
+ e.type === "touchmove" ? e.changedTouches[0].clientX : e.clientX;
128
+ var clientY =
129
+ // @ts-expect-error ignore
130
+ e.type === "touchmove" ? e.changedTouches[0].clientY : e.clientY;
131
+ var newValue = isVertical ? getValueForClientY(clientY) : getValueForClientX(clientX);
132
+ var newRoundedValue = roundToStep(newValue);
133
+ var newValues = [].concat(_toConsumableArray(values.slice(0, activeHandleIndex)), [newRoundedValue], _toConsumableArray(values.slice(+activeHandleIndex + 1)));
134
+ onDrag === null || onDrag === void 0 || onDrag(newValues);
135
+ setTempValues(newValues);
136
+ }, [getLatest, getValueForClientX, getValueForClientY, roundToStep, values, isVertical]);
137
+ var handleSegmentDrag = useCallback(function (e) {
138
+ var _getLatest2 = getLatest(),
139
+ _getLatest2$segmentSt = _slicedToArray(_getLatest2.segmentStart, 2),
140
+ segX = _getLatest2$segmentSt[0],
141
+ segY = _getLatest2$segmentSt[1],
142
+ tempValues = _getLatest2.tempValues,
143
+ onDrag = _getLatest2.onDrag;
144
+ var _getBoundingClientRec = getBoundingClientRect(segmentElRef.current),
145
+ left = _getBoundingClientRec.left,
146
+ bottom = _getBoundingClientRec.bottom;
147
+ var clientX =
148
+ // @ts-expect-error ignore
149
+ e.type === "touchmove" ? e.changedTouches[0].clientX : e.clientX;
150
+ var clientY =
151
+ // @ts-expect-error ignore
152
+ e.type === "touchmove" ? e.changedTouches[0].clientY : e.clientY;
153
+ var start = isVertical ? getValueForClientX(bottom + (clientY - segY)) : getValueForClientX(left + (clientX - segX));
154
+ var diff = roundToStep(start) - tempValues[0];
155
+ if (+tempValues[1] + diff > max) diff = 0;
156
+ var newValues = [+tempValues[0] + diff, +tempValues[1] + diff];
157
+ if (tempValues[0] !== newValues[0] || tempValues[1] !== newValues[1]) {
158
+ onDrag === null || onDrag === void 0 || onDrag(newValues);
159
+ setTempValues(newValues);
160
+ setSegmentStart([clientX, clientY]);
161
+ }
162
+ }, [getLatest, getValueForClientX, getValueForClientY, roundToStep, values, isVertical, max]);
163
+ var handleKeyDown = useCallback(function (e, i) {
164
+ var _getLatest3 = getLatest(),
165
+ values = _getLatest3.values,
166
+ onChange = _getLatest3.onChange;
167
+ if (["ArrowLeft", "ArrowUp", "ArrowRight", "ArrowDown"].includes(e.key)) {
168
+ setActiveHandleIndex(i);
169
+ var direction = ["ArrowLeft", "ArrowDown"].includes(e.key) ? -1 : 1;
170
+ var _newValue = +getNextStep(values[i], direction).toFixed(3);
171
+ var newValues = [].concat(_toConsumableArray(values.slice(0, i)), [_newValue], _toConsumableArray(values.slice(i + 1)));
172
+ var sortedValues = sortNumList(newValues);
173
+ onChange === null || onChange === void 0 || onChange(sortedValues);
174
+ e.stopPropagation();
175
+ e.preventDefault();
176
+ }
177
+ }, [getLatest, getNextStep]);
178
+ var handlePress = useCallback(function (e, i) {
179
+ var _getLatest4 = getLatest(),
180
+ onDragStart = _getLatest4.onDragStart,
181
+ values = _getLatest4.values;
182
+ if (i === -1) {
183
+ var clientX = e.type === "touchmove" ?
184
+ // @ts-expect-error ignore
185
+ e.nativeEvent.changedTouches[0].clientX :
186
+ // @ts-expect-error ignore
187
+ e.nativeEvent.clientX;
188
+ var clientY = e.type === "touchmove" ?
189
+ // @ts-expect-error ignore
190
+ e.nativeEvent.changedTouches[0].clientY :
191
+ // @ts-expect-error ignore
192
+ e.nativeEvent.clientY;
193
+ setTempValues(values);
194
+ setSegmentStart([clientX, clientY]);
195
+ } else {
196
+ setActiveHandleIndex(i);
197
+ }
198
+ var handleRelease = function handleRelease(e) {
199
+ var _getLatest5 = getLatest(),
200
+ tempValues = _getLatest5.tempValues,
201
+ onChange = _getLatest5.onChange,
202
+ onDragEnd = _getLatest5.onDragEnd;
203
+ document.removeEventListener("mousemove", i === -1 ? handleSegmentDrag : handleDrag);
204
+ document.removeEventListener("touchmove", i === -1 ? handleSegmentDrag : handleDrag);
205
+ document.removeEventListener("mouseup", handleRelease, {
206
+ capture: true
207
+ });
208
+ document.removeEventListener("touchend", handleRelease, {
209
+ capture: true
210
+ });
211
+ var sortedValues = sortNumList(tempValues || values);
212
+ onDragEnd === null || onDragEnd === void 0 || onDragEnd();
213
+ onChange === null || onChange === void 0 || onChange(sortedValues);
214
+ setActiveHandleIndex(undefined);
215
+ setSegmentStart(undefined);
216
+ setTempValues(undefined);
217
+ };
218
+ onDragStart === null || onDragStart === void 0 || onDragStart();
219
+ document.addEventListener("mousemove", i === -1 ? handleSegmentDrag : handleDrag);
220
+ document.addEventListener("touchmove", i === -1 ? handleSegmentDrag : handleDrag);
221
+ document.addEventListener("mouseup", handleRelease, {
222
+ capture: true
223
+ });
224
+ document.addEventListener("touchend", handleRelease, {
225
+ capture: true
226
+ });
227
+ }, [getLatest, handleDrag]);
228
+ var getPercentageForValue = useCallback(function (val) {
229
+ return linearInterpolator.getPercentageForValue(val, min, max);
230
+ }, [max, min]);
231
+ var trackClicked = useCallback(function (e) {
232
+ var _getLatest6 = getLatest(),
233
+ onChange = _getLatest6.onChange,
234
+ values = _getLatest6.values;
235
+ var clientX = e.type === "touchmove" ?
236
+ // @ts-expect-error ignore
237
+ e.changedTouches[0].clientX :
238
+ // @ts-expect-error ignore
239
+ e.clientX;
240
+ var clientY = e.type === "touchmove" ?
241
+ // @ts-expect-error ignore
242
+ e.changedTouches[0].clientY :
243
+ // @ts-expect-error ignore
244
+ e.clientY;
245
+ var newValue = isVertical ? getValueForClientY(clientY) : getValueForClientX(clientX);
246
+ var newRoundedValue = roundToStep(newValue);
247
+ var newValues = [newRoundedValue];
248
+ if (values.length === 2) {
249
+ if (values[0] > newRoundedValue) newValues = [newRoundedValue, values[1]];else if (values[1] < newRoundedValue) newValues = [values[0], newRoundedValue];else {
250
+ var diff = (values[1] - values[0]) / 2;
251
+ if (values[0] + diff >= newRoundedValue) newValues = [newRoundedValue, values[1]];
252
+ if (values[1] - diff <= newRoundedValue) newValues = [values[0], newRoundedValue];
253
+ }
254
+ }
255
+ onChange(newValues);
256
+ }, [getLatest, getValueForClientX, getValueForClientY, isVertical]);
257
+ useEffect(function () {
258
+ var _trackElRef$current;
259
+ (_trackElRef$current = trackElRef.current) === null || _trackElRef$current === void 0 || _trackElRef$current.addEventListener("mousedown", trackClicked);
260
+ }, [trackClicked]);
261
+ // Build the ticks
262
+ var ticks = useMemo(function () {
263
+ var ticks = controlledTicks !== null && controlledTicks !== void 0 ? controlledTicks : steps;
264
+ if (!ticks) {
265
+ ticks = [min];
266
+ var count = tickSize - tickSize % stepSize;
267
+ while (ticks[ticks.length - 1] < max - count) {
268
+ ticks.push(ticks[ticks.length - 1] + count);
269
+ }
270
+ ticks.push(max);
271
+ }
272
+ return ticks.map(function (value, i) {
273
+ return {
274
+ key: i,
275
+ value: value,
276
+ percentage: getPercentageForValue(value),
277
+ styles: _defineProperty(_defineProperty({}, positionProp, "".concat(getPercentageForValue(value), "%")), marginProp, "-0.125rem")
278
+ };
279
+ });
280
+ }, [controlledTicks, getPercentageForValue, max, min, steps, tickSize, positionProp, marginProp]);
281
+ var segments = useMemo(function () {
282
+ var sortedValues = sortNumList(tempValues !== null && tempValues !== void 0 ? tempValues : values);
283
+ return [].concat(_toConsumableArray(sortedValues), [max]).map(function (value, i) {
284
+ var active = i === sortedValues.length - 1;
285
+ return {
286
+ key: i,
287
+ value: value,
288
+ active: active,
289
+ props: active ? {
290
+ ref: segmentElRef,
291
+ onMouseDownCapture: function onMouseDownCapture(e) {
292
+ e.stopPropagation();
293
+ e.persist();
294
+ handlePress(e, -1);
295
+ },
296
+ onTouchStartCapture: function onTouchStartCapture(e) {
297
+ e.stopPropagation();
298
+ e.persist();
299
+ handlePress(e, -1);
300
+ }
301
+ } : {},
302
+ styles: function () {
303
+ var left = getPercentageForValue(sortedValues[i - 1] ? sortedValues[i - 1] : min);
304
+ var width = getPercentageForValue(value) - left;
305
+ return _defineProperty(_defineProperty({}, positionProp, "".concat(left, "%")), isVertical ? "height" : "width", "".concat(width, "%"));
306
+ }()
307
+ };
308
+ });
309
+ }, [getPercentageForValue, max, min, tempValues, values, positionProp, marginProp]);
310
+ var handles = useMemo(function () {
311
+ return (tempValues !== null && tempValues !== void 0 ? tempValues : values).map(function (value, i) {
312
+ return {
313
+ key: i,
314
+ value: value,
315
+ percentage: getPercentageForValue(value),
316
+ active: i === activeHandleIndex,
317
+ props: {
318
+ role: "slider",
319
+ "aria-valuemin": min,
320
+ "aria-valuemax": max,
321
+ "aria-valuenow": value,
322
+ onKeyDown: function onKeyDown(e) {
323
+ e.stopPropagation();
324
+ e.persist();
325
+ handleKeyDown(e, i);
326
+ },
327
+ onMouseDownCapture: function onMouseDownCapture(e) {
328
+ e.stopPropagation();
329
+ e.persist();
330
+ handlePress(e, i);
331
+ },
332
+ onTouchStartCapture: function onTouchStartCapture(e) {
333
+ e.stopPropagation();
334
+ e.persist();
335
+ handlePress(e, i);
336
+ }
337
+ },
338
+ styles: _defineProperty(_defineProperty({}, positionProp, "".concat(getPercentageForValue(value), "%")), marginProp, "-0.375rem"),
339
+ valueStyles: _defineProperty({}, positionProp, "".concat(getPercentageForValue(value), "%"))
340
+ };
341
+ });
342
+ }, [activeHandleIndex, getPercentageForValue, handleKeyDown, handlePress, min, max, tempValues, values, positionProp, marginProp]);
343
+ return {
344
+ trackElRef: trackElRef,
345
+ activeHandleIndex: activeHandleIndex,
346
+ ticks: ticks,
347
+ segments: segments,
348
+ handles: handles
349
+ };
350
+ };
351
+
352
+ export { useRanger };
@@ -0,0 +1,47 @@
1
+ 'use client';
2
+
3
+ import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
4
+
5
+ /**
6
+ * react-ranger
7
+ * https://github.com/TanStack/ranger
8
+ */
9
+
10
+ var getBoundingClientRect = function getBoundingClientRect(element) {
11
+ var rect = element.getBoundingClientRect();
12
+ return {
13
+ top: Math.ceil(rect.top),
14
+ left: Math.ceil(rect.left),
15
+ right: Math.ceil(rect.right),
16
+ width: Math.ceil(rect.width),
17
+ bottom: Math.ceil(rect.bottom),
18
+ height: Math.ceil(rect.height)
19
+ };
20
+ };
21
+ var sortNumList = function sortNumList(arr) {
22
+ return _toConsumableArray(arr).map(Number).sort(function (a, b) {
23
+ return a - b;
24
+ });
25
+ };
26
+ var linearInterpolator = {
27
+ getPercentageForValue: function getPercentageForValue(val, min, max) {
28
+ return Math.max(0, Math.min(100, (val - min) / (max - min) * 100));
29
+ },
30
+ getValueForClientX: function getValueForClientX(clientX, trackDims, min, max, isRtl) {
31
+ var left = trackDims.left,
32
+ right = trackDims.right,
33
+ width = trackDims.width;
34
+ var percentageValue = (isRtl ? right - clientX : clientX - left) / width;
35
+ var value = (max - min) * percentageValue;
36
+ return value + min;
37
+ },
38
+ getValueForClientY: function getValueForClientY(clientY, trackDims, min, max) {
39
+ var height = trackDims.height,
40
+ bottom = trackDims.bottom;
41
+ var percentageValue = (bottom - clientY) / height;
42
+ var value = (max - min) * percentageValue;
43
+ return value + min;
44
+ }
45
+ };
46
+
47
+ export { getBoundingClientRect, linearInterpolator, sortNumList };