@react-fabric/form 1.0.0-beta.8 → 1.0.1
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.
- package/{src/input/ArrayInput.module.css → css/_arrayInput.css} +8 -8
- package/css/_colorPicker.css +95 -0
- package/{src/internal/Field.module.css → css/_field.css} +94 -33
- package/{src/input/File.module.css → css/_file.css} +21 -6
- package/css/styles.css +27 -0
- package/dist/cjs/builder/FileField.js +107 -78
- package/dist/cjs/builder/FormField.js +60 -39
- package/dist/cjs/builder/SchemaEditor.js +72 -0
- package/dist/cjs/builder/components/AddSchemaFields.js +64 -0
- package/dist/cjs/builder/components/SchemaArrayForm.js +146 -0
- package/dist/cjs/builder/components/SchemaField.js +423 -0
- package/dist/cjs/form/Controller.js +66 -19
- package/dist/cjs/form/Form.js +97 -39
- package/dist/cjs/form/useForm.js +32 -0
- package/dist/cjs/hooks/useFileUploader.js +74 -45
- package/dist/cjs/hooks/useFormBuilder.js +85 -31
- package/dist/cjs/i18n/{ar.json.js → ar/form.json.js} +5 -4
- package/dist/cjs/i18n/{en.json.js → en/form.json.js} +5 -5
- package/dist/cjs/i18n/index.js +4 -4
- package/dist/cjs/index.js +41 -2
- package/dist/cjs/input/ArrayInput.js +259 -135
- package/dist/cjs/input/AvatarInput.js +99 -63
- package/dist/cjs/input/Checkbox.js +24 -25
- package/dist/cjs/input/ColorInput.js +138 -81
- package/dist/cjs/input/ColorPicker.js +78 -95
- package/dist/cjs/input/DateInput.js +192 -0
- package/dist/cjs/input/Field.js +21 -20
- package/dist/cjs/input/File.js +85 -80
- package/dist/cjs/input/Hidden.js +27 -13
- package/dist/cjs/input/Input.js +49 -48
- package/dist/cjs/input/Number.js +46 -44
- package/dist/cjs/input/Password.js +51 -49
- package/dist/cjs/input/Radio.js +26 -16
- package/dist/cjs/input/RadioGroup.js +26 -11
- package/dist/cjs/input/{Range.js → RangeSlider.js} +61 -66
- package/dist/cjs/input/Search.js +58 -64
- package/dist/cjs/input/Slider.js +57 -66
- package/dist/cjs/input/Switch.js +29 -27
- package/dist/cjs/input/Textarea.js +65 -53
- package/dist/cjs/internal/ErrorIcon.js +9 -9
- package/dist/cjs/internal/FieldWrapper.js +23 -13
- package/dist/cjs/internal/InputWrapper.js +85 -87
- package/dist/cjs/ranger/index.js +7 -7
- package/dist/cjs/ranger/utils.js +9 -4
- package/dist/cjs/select/AutoComplete.js +93 -76
- package/dist/cjs/select/DualList.js +235 -154
- package/dist/cjs/select/List.js +122 -89
- package/dist/cjs/select/Options.js +58 -54
- package/dist/cjs/select/Select.js +119 -89
- package/dist/cjs/select/helpers.js +5 -5
- package/dist/cjs/select/useSelect.js +83 -58
- package/dist/cjs/types/schema.js +43 -0
- package/dist/cjs/utils/eventHandlers.js +16 -6
- package/dist/cjs/utils/getColor.js +6 -6
- package/dist/cjs/utils/getProps.js +49 -0
- package/dist/esm/builder/FileField.js +109 -80
- package/dist/esm/builder/FormField.js +60 -39
- package/dist/esm/builder/SchemaEditor.js +72 -0
- package/dist/esm/builder/components/AddSchemaFields.js +64 -0
- package/dist/esm/builder/components/SchemaArrayForm.js +146 -0
- package/dist/esm/builder/components/SchemaField.js +423 -0
- package/dist/esm/form/Controller.js +66 -19
- package/dist/esm/form/Form.js +97 -39
- package/dist/esm/form/useForm.js +32 -0
- package/dist/esm/hooks/useFileUploader.js +75 -46
- package/dist/esm/hooks/useFormBuilder.js +84 -30
- package/dist/esm/i18n/{ar.json.js → ar/form.json.js} +5 -4
- package/dist/esm/i18n/{en.json.js → en/form.json.js} +5 -5
- package/dist/esm/i18n/index.js +2 -2
- package/dist/esm/index.js +36 -1
- package/dist/esm/input/ArrayInput.js +260 -136
- package/dist/esm/input/AvatarInput.js +99 -63
- package/dist/esm/input/Checkbox.js +26 -27
- package/dist/esm/input/ColorInput.js +141 -84
- package/dist/esm/input/ColorPicker.js +80 -97
- package/dist/esm/input/DateInput.js +192 -0
- package/dist/esm/input/Field.js +21 -20
- package/dist/esm/input/File.js +89 -84
- package/dist/esm/input/Hidden.js +28 -14
- package/dist/esm/input/Input.js +51 -50
- package/dist/esm/input/Number.js +48 -46
- package/dist/esm/input/Password.js +54 -52
- package/dist/esm/input/Radio.js +27 -17
- package/dist/esm/input/RadioGroup.js +27 -12
- package/dist/esm/input/{Range.js → RangeSlider.js} +62 -67
- package/dist/esm/input/Search.js +61 -67
- package/dist/esm/input/Slider.js +58 -67
- package/dist/esm/input/Switch.js +31 -29
- package/dist/esm/input/Textarea.js +67 -55
- package/dist/esm/internal/ErrorIcon.js +11 -11
- package/dist/esm/internal/FieldWrapper.js +23 -13
- package/dist/esm/internal/InputWrapper.js +88 -90
- package/dist/esm/ranger/index.js +7 -7
- package/dist/esm/ranger/utils.js +9 -4
- package/dist/esm/select/AutoComplete.js +95 -78
- package/dist/esm/select/DualList.js +233 -152
- package/dist/esm/select/List.js +125 -92
- package/dist/esm/select/Options.js +58 -54
- package/dist/esm/select/Select.js +122 -92
- package/dist/esm/select/helpers.js +5 -5
- package/dist/esm/select/useSelect.js +84 -59
- package/{src/input/ColorPicker.module.css → dist/esm/types/schema.js} +18 -27
- package/dist/esm/utils/eventHandlers.js +16 -6
- package/dist/esm/utils/getColor.js +6 -6
- package/dist/esm/utils/getProps.js +49 -0
- package/dist/types/builder/FileField.d.ts +7 -4
- package/dist/types/builder/FormField.d.ts +4 -2
- package/dist/types/builder/SchemaEditor.d.ts +6 -0
- package/dist/types/builder/components/AddSchemaFields.d.ts +2 -0
- package/dist/types/builder/components/SchemaArrayForm.d.ts +2 -0
- package/dist/types/builder/components/SchemaField.d.ts +12 -0
- package/dist/types/form/Controller.d.ts +45 -3
- package/dist/types/form/Form.d.ts +38 -7
- package/dist/types/form/useForm.d.ts +6 -0
- package/dist/types/hooks/useFileUploader.d.ts +24 -3
- package/dist/types/hooks/useFormBuilder.d.ts +41 -3
- package/dist/types/index.d.ts +32 -2
- package/dist/types/input/ArrayInput.d.ts +68 -13
- package/dist/types/input/AvatarInput.d.ts +23 -6
- package/dist/types/input/Checkbox.d.ts +12 -10
- package/dist/types/input/ColorInput.d.ts +15 -5
- package/dist/types/input/ColorPicker.d.ts +10 -2
- package/dist/types/input/DateInput.d.ts +21 -0
- package/dist/types/input/Field.d.ts +9 -36
- package/dist/types/input/File.d.ts +16 -4
- package/dist/types/input/Hidden.d.ts +7 -6
- package/dist/types/input/Input.d.ts +11 -4
- package/dist/types/input/Number.d.ts +9 -4
- package/dist/types/input/Password.d.ts +9 -4
- package/dist/types/input/Radio.d.ts +9 -4
- package/dist/types/input/RadioGroup.d.ts +14 -6
- package/dist/types/input/{Range.d.ts → RangeSlider.d.ts} +11 -6
- package/dist/types/input/Search.d.ts +14 -4
- package/dist/types/input/Slider.d.ts +9 -6
- package/dist/types/input/Switch.d.ts +13 -8
- package/dist/types/input/Textarea.d.ts +15 -4
- package/dist/types/internal/ErrorIcon.d.ts +4 -1
- package/dist/types/internal/FieldWrapper.d.ts +10 -1
- package/dist/types/internal/InputWrapper.d.ts +19 -1
- package/dist/types/ranger/index.d.ts +7 -3
- package/dist/types/ranger/utils.d.ts +2 -2
- package/dist/types/select/AutoComplete.d.ts +7 -2
- package/dist/types/select/DualList.d.ts +11 -37
- package/dist/types/select/List.d.ts +7 -2
- package/dist/types/select/Options.d.ts +8 -7
- package/dist/types/select/Select.d.ts +7 -2
- package/dist/types/select/helpers.d.ts +2 -2
- package/dist/types/select/useSelect.d.ts +10 -28
- package/dist/types/types/index.d.ts +73 -39
- package/dist/types/types/schema.d.ts +78 -32
- package/dist/types/utils/eventHandlers.d.ts +4 -4
- package/dist/types/utils/getColor.d.ts +4 -4
- package/dist/types/utils/getProps.d.ts +26 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/package.json +25 -25
- package/src/builder/FileField.tsx +73 -56
- package/src/builder/FormField.tsx +66 -58
- package/src/builder/SchemaEditor.tsx +78 -0
- package/src/builder/components/AddSchemaFields.tsx +64 -0
- package/src/builder/components/SchemaArrayForm.tsx +146 -0
- package/src/builder/components/SchemaField.tsx +405 -0
- package/src/form/Controller.tsx +61 -24
- package/src/form/Form.tsx +74 -50
- package/src/form/useForm.ts +53 -0
- package/src/hooks/useFileUploader.ts +115 -84
- package/src/hooks/useFormBuilder.tsx +80 -41
- package/src/i18n/{ar.json → ar/form.json} +5 -4
- package/src/i18n/{en.json → en/form.json} +5 -5
- package/src/i18n/index.ts +2 -2
- package/src/index.ts +34 -20
- package/src/input/ArrayInput.tsx +255 -173
- package/src/input/AvatarInput.tsx +82 -39
- package/src/input/Checkbox.tsx +30 -54
- package/src/input/ColorInput.tsx +146 -105
- package/src/input/ColorPicker.tsx +76 -102
- package/src/input/DateInput.tsx +225 -0
- package/src/input/Field.tsx +23 -65
- package/src/input/File.tsx +76 -98
- package/src/input/Hidden.tsx +36 -24
- package/src/input/Input.tsx +55 -53
- package/src/input/Number.tsx +49 -54
- package/src/input/Password.tsx +61 -66
- package/src/input/Radio.tsx +28 -37
- package/src/input/RadioGroup.tsx +30 -25
- package/src/input/{Range.tsx → RangeSlider.tsx} +81 -109
- package/src/input/Search.tsx +64 -77
- package/src/input/Slider.tsx +80 -109
- package/src/input/Switch.tsx +39 -53
- package/src/input/Textarea.tsx +72 -59
- package/src/internal/ErrorIcon.tsx +22 -21
- package/src/internal/FieldWrapper.tsx +33 -23
- package/src/internal/InputWrapper.tsx +132 -143
- package/src/ranger/index.tsx +37 -117
- package/src/ranger/utils.ts +12 -11
- package/src/select/AutoComplete.tsx +113 -123
- package/src/select/DualList.tsx +218 -204
- package/src/select/List.tsx +130 -97
- package/src/select/Options.tsx +59 -57
- package/src/select/Select.tsx +148 -122
- package/src/select/helpers.ts +13 -8
- package/src/select/useSelect.tsx +110 -127
- package/src/types/index.ts +76 -45
- package/src/types/schema.ts +97 -40
- package/src/utils/eventHandlers.ts +24 -26
- package/src/utils/getColor.ts +9 -8
- package/src/utils/getProps.ts +47 -0
- package/src/utils/index.ts +1 -0
- package/dist/cjs/input/ArrayInput.module.css +0 -1
- package/dist/cjs/input/ColorPicker.module.css +0 -1
- package/dist/cjs/input/File.module.css +0 -1
- package/dist/cjs/internal/Field.module.css +0 -1
- package/dist/esm/input/ArrayInput.module.css +0 -1
- package/dist/esm/input/ColorPicker.module.css +0 -1
- package/dist/esm/input/File.module.css +0 -1
- package/dist/esm/internal/Field.module.css +0 -1
package/src/ranger/index.tsx
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
* react-ranger
|
|
3
3
|
* https://github.com/TanStack/ranger
|
|
4
4
|
*/
|
|
5
|
+
|
|
6
|
+
/* eslint-disable @eslint-react/web-api-no-leaked-event-listener */
|
|
7
|
+
/* eslint-disable @eslint-react/exhaustive-deps */
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
9
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
10
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
11
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
12
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
13
|
+
|
|
5
14
|
import { useIsRtl } from "@react-fabric/core";
|
|
6
15
|
import {
|
|
7
16
|
type KeyboardEvent as KE,
|
|
@@ -13,11 +22,7 @@ import {
|
|
|
13
22
|
useRef,
|
|
14
23
|
useState,
|
|
15
24
|
} from "react";
|
|
16
|
-
import {
|
|
17
|
-
getBoundingClientRect,
|
|
18
|
-
linearInterpolator,
|
|
19
|
-
sortNumList,
|
|
20
|
-
} from "./utils";
|
|
25
|
+
import { getBoundingClientRect, linearInterpolator, sortNumList } from "./utils";
|
|
21
26
|
|
|
22
27
|
type RangerChangeEvent = (newValue: readonly number[]) => void;
|
|
23
28
|
|
|
@@ -48,7 +53,7 @@ const useGetLatest = (val: AnyObject) => {
|
|
|
48
53
|
}, []);
|
|
49
54
|
};
|
|
50
55
|
|
|
51
|
-
export
|
|
56
|
+
export function useRanger(opts: RangerConfig) {
|
|
52
57
|
const tickSize = opts.tickSize ?? 10;
|
|
53
58
|
const values = opts.values;
|
|
54
59
|
const min = opts.min;
|
|
@@ -67,14 +72,8 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
67
72
|
const [tempValues, setTempValues] = useState<number[]>();
|
|
68
73
|
const isRtl = useIsRtl();
|
|
69
74
|
|
|
70
|
-
const positionProp = useMemo(
|
|
71
|
-
|
|
72
|
-
[isVertical, isRtl],
|
|
73
|
-
);
|
|
74
|
-
const marginProp = useMemo(
|
|
75
|
-
() => (isVertical ? "marginBottom" : isRtl ? "marginRight" : "marginLeft"),
|
|
76
|
-
[isVertical, isRtl],
|
|
77
|
-
);
|
|
75
|
+
const positionProp = useMemo(() => (isVertical ? "bottom" : isRtl ? "right" : "left"), [isVertical, isRtl]);
|
|
76
|
+
const marginProp = useMemo(() => (isVertical ? "marginBottom" : isRtl ? "marginRight" : "marginLeft"), [isVertical, isRtl]);
|
|
78
77
|
|
|
79
78
|
const getLatest = useGetLatest({
|
|
80
79
|
activeHandleIndex,
|
|
@@ -91,25 +90,14 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
91
90
|
const getValueForClientX = useCallback(
|
|
92
91
|
(clientX: number) => {
|
|
93
92
|
const trackDims = getBoundingClientRect(trackElRef.current);
|
|
94
|
-
return linearInterpolator.getValueForClientX(
|
|
95
|
-
clientX,
|
|
96
|
-
trackDims,
|
|
97
|
-
min,
|
|
98
|
-
max,
|
|
99
|
-
isRtl,
|
|
100
|
-
);
|
|
93
|
+
return linearInterpolator.getValueForClientX(clientX, trackDims, min, max, isRtl);
|
|
101
94
|
},
|
|
102
95
|
[max, min, isRtl],
|
|
103
96
|
);
|
|
104
97
|
const getValueForClientY = useCallback(
|
|
105
98
|
(clientY: number) => {
|
|
106
99
|
const trackDims = getBoundingClientRect(trackElRef.current);
|
|
107
|
-
return linearInterpolator.getValueForClientY(
|
|
108
|
-
clientY,
|
|
109
|
-
trackDims,
|
|
110
|
-
min,
|
|
111
|
-
max,
|
|
112
|
-
);
|
|
100
|
+
return linearInterpolator.getValueForClientY(clientY, trackDims, min, max);
|
|
113
101
|
},
|
|
114
102
|
[max, min],
|
|
115
103
|
);
|
|
@@ -127,9 +115,7 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
127
115
|
} else {
|
|
128
116
|
if (process.env.NODE_ENV !== "production") {
|
|
129
117
|
if (typeof stepSize === "undefined") {
|
|
130
|
-
throw new Error(
|
|
131
|
-
"Warning: The option `stepSize` is expected in `useRanger`, but its value is `undefined`",
|
|
132
|
-
);
|
|
118
|
+
throw new Error("Warning: The option `stepSize` is expected in `useRanger`, but its value is `undefined`");
|
|
133
119
|
}
|
|
134
120
|
}
|
|
135
121
|
|
|
@@ -162,9 +148,7 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
162
148
|
} else {
|
|
163
149
|
if (process.env.NODE_ENV !== "production") {
|
|
164
150
|
if (typeof stepSize === "undefined") {
|
|
165
|
-
throw new Error(
|
|
166
|
-
"Warning: The option `stepSize` is expected in `useRanger`, but its value is `undefined`",
|
|
167
|
-
);
|
|
151
|
+
throw new Error("Warning: The option `stepSize` is expected in `useRanger`, but its value is `undefined`");
|
|
168
152
|
}
|
|
169
153
|
}
|
|
170
154
|
|
|
@@ -193,26 +177,13 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
193
177
|
const clientY =
|
|
194
178
|
// @ts-expect-error ignore
|
|
195
179
|
e.type === "touchmove" ? e.changedTouches[0].clientY : e.clientY;
|
|
196
|
-
const newValue = isVertical
|
|
197
|
-
? getValueForClientY(clientY)
|
|
198
|
-
: getValueForClientX(clientX);
|
|
180
|
+
const newValue = isVertical ? getValueForClientY(clientY) : getValueForClientX(clientX);
|
|
199
181
|
const newRoundedValue = roundToStep(newValue);
|
|
200
|
-
const newValues = [
|
|
201
|
-
...values.slice(0, activeHandleIndex),
|
|
202
|
-
...[newRoundedValue],
|
|
203
|
-
...values.slice(+activeHandleIndex + 1),
|
|
204
|
-
];
|
|
182
|
+
const newValues = [...values.slice(0, activeHandleIndex), ...[newRoundedValue], ...values.slice(+activeHandleIndex + 1)];
|
|
205
183
|
onDrag?.(newValues);
|
|
206
184
|
setTempValues(newValues);
|
|
207
185
|
},
|
|
208
|
-
[
|
|
209
|
-
getLatest,
|
|
210
|
-
getValueForClientX,
|
|
211
|
-
getValueForClientY,
|
|
212
|
-
roundToStep,
|
|
213
|
-
values,
|
|
214
|
-
isVertical,
|
|
215
|
-
],
|
|
186
|
+
[getLatest, getValueForClientX, getValueForClientY, roundToStep, values, isVertical],
|
|
216
187
|
);
|
|
217
188
|
const handleSegmentDrag = useCallback(
|
|
218
189
|
(e: MouseEvent | TouchEvent) => {
|
|
@@ -230,9 +201,7 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
230
201
|
// @ts-expect-error ignore
|
|
231
202
|
e.type === "touchmove" ? e.changedTouches[0].clientY : e.clientY;
|
|
232
203
|
|
|
233
|
-
const start = isVertical
|
|
234
|
-
? getValueForClientX(bottom + (clientY - segY))
|
|
235
|
-
: getValueForClientX(left + (clientX - segX));
|
|
204
|
+
const start = isVertical ? getValueForClientX(bottom + (clientY - segY)) : getValueForClientX(left + (clientX - segX));
|
|
236
205
|
let diff = roundToStep(start) - tempValues[0];
|
|
237
206
|
if (+tempValues[1] + diff > max) diff = 0;
|
|
238
207
|
const newValues = [+tempValues[0] + diff, +tempValues[1] + diff];
|
|
@@ -242,15 +211,7 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
242
211
|
setSegmentStart([clientX, clientY]);
|
|
243
212
|
}
|
|
244
213
|
},
|
|
245
|
-
[
|
|
246
|
-
getLatest,
|
|
247
|
-
getValueForClientX,
|
|
248
|
-
getValueForClientY,
|
|
249
|
-
roundToStep,
|
|
250
|
-
values,
|
|
251
|
-
isVertical,
|
|
252
|
-
max,
|
|
253
|
-
],
|
|
214
|
+
[getLatest, getValueForClientX, getValueForClientY, roundToStep, values, isVertical, max],
|
|
254
215
|
);
|
|
255
216
|
const handleKeyDown = useCallback(
|
|
256
217
|
(e: KE, i: number) => {
|
|
@@ -260,11 +221,7 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
260
221
|
setActiveHandleIndex(i);
|
|
261
222
|
const direction = ["ArrowLeft", "ArrowDown"].includes(e.key) ? -1 : 1;
|
|
262
223
|
const newValue = +getNextStep(values[i], direction).toFixed(3);
|
|
263
|
-
const newValues = [
|
|
264
|
-
...values.slice(0, i),
|
|
265
|
-
...[newValue],
|
|
266
|
-
...values.slice(i + 1),
|
|
267
|
-
];
|
|
224
|
+
const newValues = [...values.slice(0, i), ...[newValue], ...values.slice(i + 1)];
|
|
268
225
|
const sortedValues = sortNumList(newValues);
|
|
269
226
|
onChange?.(sortedValues);
|
|
270
227
|
e.stopPropagation();
|
|
@@ -295,17 +252,11 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
295
252
|
} else {
|
|
296
253
|
setActiveHandleIndex(i);
|
|
297
254
|
}
|
|
298
|
-
const handleRelease = (
|
|
255
|
+
const handleRelease = () => {
|
|
299
256
|
const { tempValues, onChange, onDragEnd } = getLatest();
|
|
300
257
|
|
|
301
|
-
document.removeEventListener(
|
|
302
|
-
|
|
303
|
-
i === -1 ? handleSegmentDrag : handleDrag,
|
|
304
|
-
);
|
|
305
|
-
document.removeEventListener(
|
|
306
|
-
"touchmove",
|
|
307
|
-
i === -1 ? handleSegmentDrag : handleDrag,
|
|
308
|
-
);
|
|
258
|
+
document.removeEventListener("mousemove", i === -1 ? handleSegmentDrag : handleDrag);
|
|
259
|
+
document.removeEventListener("touchmove", i === -1 ? handleSegmentDrag : handleDrag);
|
|
309
260
|
document.removeEventListener("mouseup", handleRelease, {
|
|
310
261
|
capture: true,
|
|
311
262
|
});
|
|
@@ -321,14 +272,8 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
321
272
|
};
|
|
322
273
|
|
|
323
274
|
onDragStart?.();
|
|
324
|
-
document.addEventListener(
|
|
325
|
-
|
|
326
|
-
i === -1 ? handleSegmentDrag : handleDrag,
|
|
327
|
-
);
|
|
328
|
-
document.addEventListener(
|
|
329
|
-
"touchmove",
|
|
330
|
-
i === -1 ? handleSegmentDrag : handleDrag,
|
|
331
|
-
);
|
|
275
|
+
document.addEventListener("mousemove", i === -1 ? handleSegmentDrag : handleDrag);
|
|
276
|
+
document.addEventListener("touchmove", i === -1 ? handleSegmentDrag : handleDrag);
|
|
332
277
|
document.addEventListener("mouseup", handleRelease, { capture: true });
|
|
333
278
|
document.addEventListener("touchend", handleRelease, { capture: true });
|
|
334
279
|
},
|
|
@@ -356,22 +301,16 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
356
301
|
: // @ts-expect-error ignore
|
|
357
302
|
e.clientY;
|
|
358
303
|
|
|
359
|
-
const newValue = isVertical
|
|
360
|
-
? getValueForClientY(clientY)
|
|
361
|
-
: getValueForClientX(clientX);
|
|
304
|
+
const newValue = isVertical ? getValueForClientY(clientY) : getValueForClientX(clientX);
|
|
362
305
|
const newRoundedValue = roundToStep(newValue);
|
|
363
306
|
let newValues = [newRoundedValue];
|
|
364
307
|
if (values.length === 2) {
|
|
365
|
-
if (values[0] > newRoundedValue)
|
|
366
|
-
|
|
367
|
-
else if (values[1] < newRoundedValue)
|
|
368
|
-
newValues = [values[0], newRoundedValue];
|
|
308
|
+
if (values[0] > newRoundedValue) newValues = [newRoundedValue, values[1]];
|
|
309
|
+
else if (values[1] < newRoundedValue) newValues = [values[0], newRoundedValue];
|
|
369
310
|
else {
|
|
370
311
|
const diff = (values[1] - values[0]) / 2;
|
|
371
|
-
if (values[0] + diff >= newRoundedValue)
|
|
372
|
-
|
|
373
|
-
if (values[1] - diff <= newRoundedValue)
|
|
374
|
-
newValues = [values[0], newRoundedValue];
|
|
312
|
+
if (values[0] + diff >= newRoundedValue) newValues = [newRoundedValue, values[1]];
|
|
313
|
+
if (values[1] - diff <= newRoundedValue) newValues = [values[0], newRoundedValue];
|
|
375
314
|
}
|
|
376
315
|
}
|
|
377
316
|
onChange(newValues);
|
|
@@ -406,16 +345,7 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
406
345
|
},
|
|
407
346
|
};
|
|
408
347
|
});
|
|
409
|
-
}, [
|
|
410
|
-
controlledTicks,
|
|
411
|
-
getPercentageForValue,
|
|
412
|
-
max,
|
|
413
|
-
min,
|
|
414
|
-
steps,
|
|
415
|
-
tickSize,
|
|
416
|
-
positionProp,
|
|
417
|
-
marginProp,
|
|
418
|
-
]);
|
|
348
|
+
}, [controlledTicks, getPercentageForValue, max, min, steps, tickSize, positionProp, marginProp]);
|
|
419
349
|
const segments = useMemo(() => {
|
|
420
350
|
const sortedValues = sortNumList(tempValues ?? values);
|
|
421
351
|
return [...sortedValues, max].map((value, i) => {
|
|
@@ -440,9 +370,7 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
440
370
|
}
|
|
441
371
|
: {},
|
|
442
372
|
styles: (() => {
|
|
443
|
-
const left = getPercentageForValue(
|
|
444
|
-
sortedValues[i - 1] ? sortedValues[i - 1] : min,
|
|
445
|
-
);
|
|
373
|
+
const left = getPercentageForValue(sortedValues[i - 1] ? sortedValues[i - 1] : min);
|
|
446
374
|
const width: number = getPercentageForValue(value) - left;
|
|
447
375
|
return {
|
|
448
376
|
[positionProp]: `${left}%`,
|
|
@@ -451,15 +379,7 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
451
379
|
})(),
|
|
452
380
|
};
|
|
453
381
|
});
|
|
454
|
-
}, [
|
|
455
|
-
getPercentageForValue,
|
|
456
|
-
max,
|
|
457
|
-
min,
|
|
458
|
-
tempValues,
|
|
459
|
-
values,
|
|
460
|
-
positionProp,
|
|
461
|
-
marginProp,
|
|
462
|
-
]);
|
|
382
|
+
}, [getPercentageForValue, max, min, tempValues, values, positionProp, marginProp]);
|
|
463
383
|
const handles = useMemo(() => {
|
|
464
384
|
return (tempValues ?? values).map((value, i) => {
|
|
465
385
|
return {
|
|
@@ -511,4 +431,4 @@ export const useRanger = (opts: RangerConfig) => {
|
|
|
511
431
|
]);
|
|
512
432
|
|
|
513
433
|
return { trackElRef, activeHandleIndex, ticks, segments, handles };
|
|
514
|
-
}
|
|
434
|
+
}
|
package/src/ranger/utils.ts
CHANGED
|
@@ -3,7 +3,12 @@
|
|
|
3
3
|
* https://github.com/TanStack/ranger
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
7
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
9
|
+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
|
10
|
+
|
|
11
|
+
export function getBoundingClientRect(element: AnyObject) {
|
|
7
12
|
const rect = element.getBoundingClientRect();
|
|
8
13
|
return {
|
|
9
14
|
top: Math.ceil(rect.top),
|
|
@@ -13,10 +18,11 @@ export const getBoundingClientRect = (element: any) => {
|
|
|
13
18
|
bottom: Math.ceil(rect.bottom),
|
|
14
19
|
height: Math.ceil(rect.height),
|
|
15
20
|
};
|
|
16
|
-
}
|
|
21
|
+
}
|
|
17
22
|
|
|
18
|
-
export
|
|
19
|
-
[...arr].map(Number).sort((a, b) => a - b);
|
|
23
|
+
export function sortNumList(arr: ReadonlyArray<number | string>) {
|
|
24
|
+
return [...arr].map(Number).sort((a, b) => a - b);
|
|
25
|
+
}
|
|
20
26
|
|
|
21
27
|
export const linearInterpolator = {
|
|
22
28
|
getPercentageForValue: (val: number, min: number, max: number) => {
|
|
@@ -27,19 +33,14 @@ export const linearInterpolator = {
|
|
|
27
33
|
trackDims: { width: number; left: number; right: number },
|
|
28
34
|
min: number,
|
|
29
35
|
max: number,
|
|
30
|
-
isRtl: boolean
|
|
36
|
+
isRtl: boolean,
|
|
31
37
|
) => {
|
|
32
38
|
const { left, right, width } = trackDims;
|
|
33
39
|
const percentageValue = (isRtl ? right - clientX : clientX - left) / width;
|
|
34
40
|
const value = (max - min) * percentageValue;
|
|
35
41
|
return value + min;
|
|
36
42
|
},
|
|
37
|
-
getValueForClientY: (
|
|
38
|
-
clientY: number,
|
|
39
|
-
trackDims: { height: number; bottom: number },
|
|
40
|
-
min: number,
|
|
41
|
-
max: number
|
|
42
|
-
) => {
|
|
43
|
+
getValueForClientY: (clientY: number, trackDims: { height: number; bottom: number }, min: number, max: number) => {
|
|
43
44
|
const { height, bottom } = trackDims;
|
|
44
45
|
const percentageValue = (bottom - clientY) / height;
|
|
45
46
|
const value = (max - min) * percentageValue;
|