@react-stately/slider 3.5.3-nightly.4555 → 3.5.3-nightly.4560
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/dist/import.mjs +1 -167
- package/dist/main.js +2 -168
- package/dist/main.js.map +1 -1
- package/dist/module.js +1 -167
- package/dist/module.js.map +1 -1
- package/dist/useSliderState.main.js +176 -0
- package/dist/useSliderState.main.js.map +1 -0
- package/dist/useSliderState.mjs +171 -0
- package/dist/useSliderState.module.js +171 -0
- package/dist/useSliderState.module.js.map +1 -0
- package/package.json +5 -5
package/dist/import.mjs
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {useMemo as $aTwux$useMemo, useCallback as $aTwux$useCallback, useState as $aTwux$useState, useRef as $aTwux$useRef} from "react";
|
|
1
|
+
import {useSliderState as $28f99e3e86e6ec45$export$e5fda3247f5d67f9} from "./useSliderState.mjs";
|
|
3
2
|
|
|
4
3
|
/*
|
|
5
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
6
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
7
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
8
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
11
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
12
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
13
|
-
* governing permissions and limitations under the License.
|
|
14
|
-
*/ /*
|
|
15
4
|
* Copyright 2020 Adobe. All rights reserved.
|
|
16
5
|
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
17
6
|
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
@@ -23,161 +12,6 @@ import {useMemo as $aTwux$useMemo, useCallback as $aTwux$useCallback, useState a
|
|
|
23
12
|
* governing permissions and limitations under the License.
|
|
24
13
|
*/
|
|
25
14
|
|
|
26
|
-
const $28f99e3e86e6ec45$var$DEFAULT_MIN_VALUE = 0;
|
|
27
|
-
const $28f99e3e86e6ec45$var$DEFAULT_MAX_VALUE = 100;
|
|
28
|
-
const $28f99e3e86e6ec45$var$DEFAULT_STEP_VALUE = 1;
|
|
29
|
-
function $28f99e3e86e6ec45$export$e5fda3247f5d67f9(props) {
|
|
30
|
-
const { isDisabled: isDisabled = false, minValue: minValue = $28f99e3e86e6ec45$var$DEFAULT_MIN_VALUE, maxValue: maxValue = $28f99e3e86e6ec45$var$DEFAULT_MAX_VALUE, numberFormatter: formatter, step: step = $28f99e3e86e6ec45$var$DEFAULT_STEP_VALUE, orientation: orientation = "horizontal" } = props;
|
|
31
|
-
// Page step should be at least equal to step and always a multiple of the step.
|
|
32
|
-
let pageSize = (0, $aTwux$useMemo)(()=>{
|
|
33
|
-
let calcPageSize = (maxValue - minValue) / 10;
|
|
34
|
-
calcPageSize = (0, $aTwux$snapValueToStep)(calcPageSize, 0, calcPageSize + step, step);
|
|
35
|
-
return Math.max(calcPageSize, step);
|
|
36
|
-
}, [
|
|
37
|
-
step,
|
|
38
|
-
maxValue,
|
|
39
|
-
minValue
|
|
40
|
-
]);
|
|
41
|
-
let restrictValues = (0, $aTwux$useCallback)((values)=>values === null || values === void 0 ? void 0 : values.map((val, idx)=>{
|
|
42
|
-
let min = idx === 0 ? minValue : val[idx - 1];
|
|
43
|
-
let max = idx === values.length - 1 ? maxValue : val[idx + 1];
|
|
44
|
-
return (0, $aTwux$snapValueToStep)(val, min, max, step);
|
|
45
|
-
}), [
|
|
46
|
-
minValue,
|
|
47
|
-
maxValue,
|
|
48
|
-
step
|
|
49
|
-
]);
|
|
50
|
-
let value = (0, $aTwux$useMemo)(()=>restrictValues($28f99e3e86e6ec45$var$convertValue(props.value)), [
|
|
51
|
-
props.value
|
|
52
|
-
]);
|
|
53
|
-
let defaultValue = (0, $aTwux$useMemo)(()=>{
|
|
54
|
-
var _convertValue;
|
|
55
|
-
return restrictValues((_convertValue = $28f99e3e86e6ec45$var$convertValue(props.defaultValue)) !== null && _convertValue !== void 0 ? _convertValue : [
|
|
56
|
-
minValue
|
|
57
|
-
]);
|
|
58
|
-
}, [
|
|
59
|
-
props.defaultValue,
|
|
60
|
-
minValue
|
|
61
|
-
]);
|
|
62
|
-
let onChange = $28f99e3e86e6ec45$var$createOnChange(props.value, props.defaultValue, props.onChange);
|
|
63
|
-
let onChangeEnd = $28f99e3e86e6ec45$var$createOnChange(props.value, props.defaultValue, props.onChangeEnd);
|
|
64
|
-
const [values, setValuesState] = (0, $aTwux$useControlledState)(value, defaultValue, onChange);
|
|
65
|
-
const [isDraggings, setDraggingsState] = (0, $aTwux$useState)(new Array(values.length).fill(false));
|
|
66
|
-
const isEditablesRef = (0, $aTwux$useRef)(new Array(values.length).fill(true));
|
|
67
|
-
const [focusedIndex, setFocusedIndex] = (0, $aTwux$useState)(undefined);
|
|
68
|
-
const valuesRef = (0, $aTwux$useRef)(values);
|
|
69
|
-
const isDraggingsRef = (0, $aTwux$useRef)(isDraggings);
|
|
70
|
-
let setValues = (values)=>{
|
|
71
|
-
valuesRef.current = values;
|
|
72
|
-
setValuesState(values);
|
|
73
|
-
};
|
|
74
|
-
let setDraggings = (draggings)=>{
|
|
75
|
-
isDraggingsRef.current = draggings;
|
|
76
|
-
setDraggingsState(draggings);
|
|
77
|
-
};
|
|
78
|
-
function getValuePercent(value) {
|
|
79
|
-
return (value - minValue) / (maxValue - minValue);
|
|
80
|
-
}
|
|
81
|
-
function getThumbMinValue(index) {
|
|
82
|
-
return index === 0 ? minValue : values[index - 1];
|
|
83
|
-
}
|
|
84
|
-
function getThumbMaxValue(index) {
|
|
85
|
-
return index === values.length - 1 ? maxValue : values[index + 1];
|
|
86
|
-
}
|
|
87
|
-
function isThumbEditable(index) {
|
|
88
|
-
return isEditablesRef.current[index];
|
|
89
|
-
}
|
|
90
|
-
function setThumbEditable(index, editable) {
|
|
91
|
-
isEditablesRef.current[index] = editable;
|
|
92
|
-
}
|
|
93
|
-
function updateValue(index, value) {
|
|
94
|
-
if (isDisabled || !isThumbEditable(index)) return;
|
|
95
|
-
const thisMin = getThumbMinValue(index);
|
|
96
|
-
const thisMax = getThumbMaxValue(index);
|
|
97
|
-
// Round value to multiple of step, clamp value between min and max
|
|
98
|
-
value = (0, $aTwux$snapValueToStep)(value, thisMin, thisMax, step);
|
|
99
|
-
let newValues = $28f99e3e86e6ec45$var$replaceIndex(valuesRef.current, index, value);
|
|
100
|
-
setValues(newValues);
|
|
101
|
-
}
|
|
102
|
-
function updateDragging(index, dragging) {
|
|
103
|
-
if (isDisabled || !isThumbEditable(index)) return;
|
|
104
|
-
if (dragging) valuesRef.current = values;
|
|
105
|
-
const wasDragging = isDraggingsRef.current[index];
|
|
106
|
-
isDraggingsRef.current = $28f99e3e86e6ec45$var$replaceIndex(isDraggingsRef.current, index, dragging);
|
|
107
|
-
setDraggings(isDraggingsRef.current);
|
|
108
|
-
// Call onChangeEnd if no handles are dragging.
|
|
109
|
-
if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) onChangeEnd(valuesRef.current);
|
|
110
|
-
}
|
|
111
|
-
function getFormattedValue(value) {
|
|
112
|
-
return formatter.format(value);
|
|
113
|
-
}
|
|
114
|
-
function setThumbPercent(index, percent) {
|
|
115
|
-
updateValue(index, getPercentValue(percent));
|
|
116
|
-
}
|
|
117
|
-
function getRoundedValue(value) {
|
|
118
|
-
return Math.round((value - minValue) / step) * step + minValue;
|
|
119
|
-
}
|
|
120
|
-
function getPercentValue(percent) {
|
|
121
|
-
const val = percent * (maxValue - minValue) + minValue;
|
|
122
|
-
return (0, $aTwux$clamp)(getRoundedValue(val), minValue, maxValue);
|
|
123
|
-
}
|
|
124
|
-
function incrementThumb(index, stepSize = 1) {
|
|
125
|
-
let s = Math.max(stepSize, step);
|
|
126
|
-
updateValue(index, (0, $aTwux$snapValueToStep)(values[index] + s, minValue, maxValue, step));
|
|
127
|
-
}
|
|
128
|
-
function decrementThumb(index, stepSize = 1) {
|
|
129
|
-
let s = Math.max(stepSize, step);
|
|
130
|
-
updateValue(index, (0, $aTwux$snapValueToStep)(values[index] - s, minValue, maxValue, step));
|
|
131
|
-
}
|
|
132
|
-
return {
|
|
133
|
-
values: values,
|
|
134
|
-
getThumbValue: (index)=>values[index],
|
|
135
|
-
setThumbValue: updateValue,
|
|
136
|
-
setThumbPercent: setThumbPercent,
|
|
137
|
-
isThumbDragging: (index)=>isDraggings[index],
|
|
138
|
-
setThumbDragging: updateDragging,
|
|
139
|
-
focusedThumb: focusedIndex,
|
|
140
|
-
setFocusedThumb: setFocusedIndex,
|
|
141
|
-
getThumbPercent: (index)=>getValuePercent(values[index]),
|
|
142
|
-
getValuePercent: getValuePercent,
|
|
143
|
-
getThumbValueLabel: (index)=>getFormattedValue(values[index]),
|
|
144
|
-
getFormattedValue: getFormattedValue,
|
|
145
|
-
getThumbMinValue: getThumbMinValue,
|
|
146
|
-
getThumbMaxValue: getThumbMaxValue,
|
|
147
|
-
getPercentValue: getPercentValue,
|
|
148
|
-
isThumbEditable: isThumbEditable,
|
|
149
|
-
setThumbEditable: setThumbEditable,
|
|
150
|
-
incrementThumb: incrementThumb,
|
|
151
|
-
decrementThumb: decrementThumb,
|
|
152
|
-
step: step,
|
|
153
|
-
pageSize: pageSize,
|
|
154
|
-
orientation: orientation,
|
|
155
|
-
isDisabled: isDisabled
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
function $28f99e3e86e6ec45$var$replaceIndex(array, index, value) {
|
|
159
|
-
if (array[index] === value) return array;
|
|
160
|
-
return [
|
|
161
|
-
...array.slice(0, index),
|
|
162
|
-
value,
|
|
163
|
-
...array.slice(index + 1)
|
|
164
|
-
];
|
|
165
|
-
}
|
|
166
|
-
function $28f99e3e86e6ec45$var$convertValue(value) {
|
|
167
|
-
if (value == null) return undefined;
|
|
168
|
-
return Array.isArray(value) ? value : [
|
|
169
|
-
value
|
|
170
|
-
];
|
|
171
|
-
}
|
|
172
|
-
function $28f99e3e86e6ec45$var$createOnChange(value, defaultValue, onChange) {
|
|
173
|
-
return (newValue)=>{
|
|
174
|
-
if (typeof value === "number" || typeof defaultValue === "number") onChange === null || onChange === void 0 ? void 0 : onChange(newValue[0]);
|
|
175
|
-
else onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
15
|
|
|
182
16
|
export {$28f99e3e86e6ec45$export$e5fda3247f5d67f9 as useSliderState};
|
|
183
17
|
//# sourceMappingURL=module.js.map
|
package/dist/main.js
CHANGED
|
@@ -1,23 +1,12 @@
|
|
|
1
|
-
var $
|
|
2
|
-
var $ltQvC$react = require("react");
|
|
1
|
+
var $e86753598efd0f02$exports = require("./useSliderState.main.js");
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
function $parcel$export(e, n, v, s) {
|
|
6
5
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
7
6
|
}
|
|
8
7
|
|
|
9
|
-
$parcel$export(module.exports, "useSliderState", () => $e86753598efd0f02$
|
|
8
|
+
$parcel$export(module.exports, "useSliderState", () => $e86753598efd0f02$exports.useSliderState);
|
|
10
9
|
/*
|
|
11
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
12
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
13
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
14
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
-
*
|
|
16
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
17
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
18
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
19
|
-
* governing permissions and limitations under the License.
|
|
20
|
-
*/ /*
|
|
21
10
|
* Copyright 2020 Adobe. All rights reserved.
|
|
22
11
|
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
23
12
|
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
@@ -29,160 +18,5 @@ $parcel$export(module.exports, "useSliderState", () => $e86753598efd0f02$export$
|
|
|
29
18
|
* governing permissions and limitations under the License.
|
|
30
19
|
*/
|
|
31
20
|
|
|
32
|
-
const $e86753598efd0f02$var$DEFAULT_MIN_VALUE = 0;
|
|
33
|
-
const $e86753598efd0f02$var$DEFAULT_MAX_VALUE = 100;
|
|
34
|
-
const $e86753598efd0f02$var$DEFAULT_STEP_VALUE = 1;
|
|
35
|
-
function $e86753598efd0f02$export$e5fda3247f5d67f9(props) {
|
|
36
|
-
const { isDisabled: isDisabled = false, minValue: minValue = $e86753598efd0f02$var$DEFAULT_MIN_VALUE, maxValue: maxValue = $e86753598efd0f02$var$DEFAULT_MAX_VALUE, numberFormatter: formatter, step: step = $e86753598efd0f02$var$DEFAULT_STEP_VALUE, orientation: orientation = "horizontal" } = props;
|
|
37
|
-
// Page step should be at least equal to step and always a multiple of the step.
|
|
38
|
-
let pageSize = (0, $ltQvC$react.useMemo)(()=>{
|
|
39
|
-
let calcPageSize = (maxValue - minValue) / 10;
|
|
40
|
-
calcPageSize = (0, $ltQvC$reactstatelyutils.snapValueToStep)(calcPageSize, 0, calcPageSize + step, step);
|
|
41
|
-
return Math.max(calcPageSize, step);
|
|
42
|
-
}, [
|
|
43
|
-
step,
|
|
44
|
-
maxValue,
|
|
45
|
-
minValue
|
|
46
|
-
]);
|
|
47
|
-
let restrictValues = (0, $ltQvC$react.useCallback)((values)=>values === null || values === void 0 ? void 0 : values.map((val, idx)=>{
|
|
48
|
-
let min = idx === 0 ? minValue : val[idx - 1];
|
|
49
|
-
let max = idx === values.length - 1 ? maxValue : val[idx + 1];
|
|
50
|
-
return (0, $ltQvC$reactstatelyutils.snapValueToStep)(val, min, max, step);
|
|
51
|
-
}), [
|
|
52
|
-
minValue,
|
|
53
|
-
maxValue,
|
|
54
|
-
step
|
|
55
|
-
]);
|
|
56
|
-
let value = (0, $ltQvC$react.useMemo)(()=>restrictValues($e86753598efd0f02$var$convertValue(props.value)), [
|
|
57
|
-
props.value
|
|
58
|
-
]);
|
|
59
|
-
let defaultValue = (0, $ltQvC$react.useMemo)(()=>{
|
|
60
|
-
var _convertValue;
|
|
61
|
-
return restrictValues((_convertValue = $e86753598efd0f02$var$convertValue(props.defaultValue)) !== null && _convertValue !== void 0 ? _convertValue : [
|
|
62
|
-
minValue
|
|
63
|
-
]);
|
|
64
|
-
}, [
|
|
65
|
-
props.defaultValue,
|
|
66
|
-
minValue
|
|
67
|
-
]);
|
|
68
|
-
let onChange = $e86753598efd0f02$var$createOnChange(props.value, props.defaultValue, props.onChange);
|
|
69
|
-
let onChangeEnd = $e86753598efd0f02$var$createOnChange(props.value, props.defaultValue, props.onChangeEnd);
|
|
70
|
-
const [values, setValuesState] = (0, $ltQvC$reactstatelyutils.useControlledState)(value, defaultValue, onChange);
|
|
71
|
-
const [isDraggings, setDraggingsState] = (0, $ltQvC$react.useState)(new Array(values.length).fill(false));
|
|
72
|
-
const isEditablesRef = (0, $ltQvC$react.useRef)(new Array(values.length).fill(true));
|
|
73
|
-
const [focusedIndex, setFocusedIndex] = (0, $ltQvC$react.useState)(undefined);
|
|
74
|
-
const valuesRef = (0, $ltQvC$react.useRef)(values);
|
|
75
|
-
const isDraggingsRef = (0, $ltQvC$react.useRef)(isDraggings);
|
|
76
|
-
let setValues = (values)=>{
|
|
77
|
-
valuesRef.current = values;
|
|
78
|
-
setValuesState(values);
|
|
79
|
-
};
|
|
80
|
-
let setDraggings = (draggings)=>{
|
|
81
|
-
isDraggingsRef.current = draggings;
|
|
82
|
-
setDraggingsState(draggings);
|
|
83
|
-
};
|
|
84
|
-
function getValuePercent(value) {
|
|
85
|
-
return (value - minValue) / (maxValue - minValue);
|
|
86
|
-
}
|
|
87
|
-
function getThumbMinValue(index) {
|
|
88
|
-
return index === 0 ? minValue : values[index - 1];
|
|
89
|
-
}
|
|
90
|
-
function getThumbMaxValue(index) {
|
|
91
|
-
return index === values.length - 1 ? maxValue : values[index + 1];
|
|
92
|
-
}
|
|
93
|
-
function isThumbEditable(index) {
|
|
94
|
-
return isEditablesRef.current[index];
|
|
95
|
-
}
|
|
96
|
-
function setThumbEditable(index, editable) {
|
|
97
|
-
isEditablesRef.current[index] = editable;
|
|
98
|
-
}
|
|
99
|
-
function updateValue(index, value) {
|
|
100
|
-
if (isDisabled || !isThumbEditable(index)) return;
|
|
101
|
-
const thisMin = getThumbMinValue(index);
|
|
102
|
-
const thisMax = getThumbMaxValue(index);
|
|
103
|
-
// Round value to multiple of step, clamp value between min and max
|
|
104
|
-
value = (0, $ltQvC$reactstatelyutils.snapValueToStep)(value, thisMin, thisMax, step);
|
|
105
|
-
let newValues = $e86753598efd0f02$var$replaceIndex(valuesRef.current, index, value);
|
|
106
|
-
setValues(newValues);
|
|
107
|
-
}
|
|
108
|
-
function updateDragging(index, dragging) {
|
|
109
|
-
if (isDisabled || !isThumbEditable(index)) return;
|
|
110
|
-
if (dragging) valuesRef.current = values;
|
|
111
|
-
const wasDragging = isDraggingsRef.current[index];
|
|
112
|
-
isDraggingsRef.current = $e86753598efd0f02$var$replaceIndex(isDraggingsRef.current, index, dragging);
|
|
113
|
-
setDraggings(isDraggingsRef.current);
|
|
114
|
-
// Call onChangeEnd if no handles are dragging.
|
|
115
|
-
if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) onChangeEnd(valuesRef.current);
|
|
116
|
-
}
|
|
117
|
-
function getFormattedValue(value) {
|
|
118
|
-
return formatter.format(value);
|
|
119
|
-
}
|
|
120
|
-
function setThumbPercent(index, percent) {
|
|
121
|
-
updateValue(index, getPercentValue(percent));
|
|
122
|
-
}
|
|
123
|
-
function getRoundedValue(value) {
|
|
124
|
-
return Math.round((value - minValue) / step) * step + minValue;
|
|
125
|
-
}
|
|
126
|
-
function getPercentValue(percent) {
|
|
127
|
-
const val = percent * (maxValue - minValue) + minValue;
|
|
128
|
-
return (0, $ltQvC$reactstatelyutils.clamp)(getRoundedValue(val), minValue, maxValue);
|
|
129
|
-
}
|
|
130
|
-
function incrementThumb(index, stepSize = 1) {
|
|
131
|
-
let s = Math.max(stepSize, step);
|
|
132
|
-
updateValue(index, (0, $ltQvC$reactstatelyutils.snapValueToStep)(values[index] + s, minValue, maxValue, step));
|
|
133
|
-
}
|
|
134
|
-
function decrementThumb(index, stepSize = 1) {
|
|
135
|
-
let s = Math.max(stepSize, step);
|
|
136
|
-
updateValue(index, (0, $ltQvC$reactstatelyutils.snapValueToStep)(values[index] - s, minValue, maxValue, step));
|
|
137
|
-
}
|
|
138
|
-
return {
|
|
139
|
-
values: values,
|
|
140
|
-
getThumbValue: (index)=>values[index],
|
|
141
|
-
setThumbValue: updateValue,
|
|
142
|
-
setThumbPercent: setThumbPercent,
|
|
143
|
-
isThumbDragging: (index)=>isDraggings[index],
|
|
144
|
-
setThumbDragging: updateDragging,
|
|
145
|
-
focusedThumb: focusedIndex,
|
|
146
|
-
setFocusedThumb: setFocusedIndex,
|
|
147
|
-
getThumbPercent: (index)=>getValuePercent(values[index]),
|
|
148
|
-
getValuePercent: getValuePercent,
|
|
149
|
-
getThumbValueLabel: (index)=>getFormattedValue(values[index]),
|
|
150
|
-
getFormattedValue: getFormattedValue,
|
|
151
|
-
getThumbMinValue: getThumbMinValue,
|
|
152
|
-
getThumbMaxValue: getThumbMaxValue,
|
|
153
|
-
getPercentValue: getPercentValue,
|
|
154
|
-
isThumbEditable: isThumbEditable,
|
|
155
|
-
setThumbEditable: setThumbEditable,
|
|
156
|
-
incrementThumb: incrementThumb,
|
|
157
|
-
decrementThumb: decrementThumb,
|
|
158
|
-
step: step,
|
|
159
|
-
pageSize: pageSize,
|
|
160
|
-
orientation: orientation,
|
|
161
|
-
isDisabled: isDisabled
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
function $e86753598efd0f02$var$replaceIndex(array, index, value) {
|
|
165
|
-
if (array[index] === value) return array;
|
|
166
|
-
return [
|
|
167
|
-
...array.slice(0, index),
|
|
168
|
-
value,
|
|
169
|
-
...array.slice(index + 1)
|
|
170
|
-
];
|
|
171
|
-
}
|
|
172
|
-
function $e86753598efd0f02$var$convertValue(value) {
|
|
173
|
-
if (value == null) return undefined;
|
|
174
|
-
return Array.isArray(value) ? value : [
|
|
175
|
-
value
|
|
176
|
-
];
|
|
177
|
-
}
|
|
178
|
-
function $e86753598efd0f02$var$createOnChange(value, defaultValue, onChange) {
|
|
179
|
-
return (newValue)=>{
|
|
180
|
-
if (typeof value === "number" || typeof defaultValue === "number") onChange === null || onChange === void 0 ? void 0 : onChange(newValue[0]);
|
|
181
|
-
else onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
21
|
|
|
188
22
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;;AAyID,MAAM,0CAAoB;AAC1B,MAAM,0CAAoB;AAC1B,MAAM,2CAAqB;AAYpB,SAAS,0CAA4C,KAA4B;IACtF,MAAM,cACJ,aAAa,iBACb,WAAW,mDACX,WAAW,yCACX,iBAAiB,SAAS,QAC1B,OAAO,uDACP,cAAc,cACf,GAAG;IAEJ,gFAAgF;IAChF,IAAI,WAAW,CAAA,GAAA,oBAAM,EAAE;QACrB,IAAI,eAAe,AAAC,CAAA,WAAW,QAAO,IAAK;QAC3C,eAAe,CAAA,GAAA,wCAAc,EAAE,cAAc,GAAG,eAAe,MAAM;QACrE,OAAO,KAAK,GAAG,CAAC,cAAc;IAChC,GAAG;QAAC;QAAM;QAAU;KAAS;IAE7B,IAAI,iBAAiB,CAAA,GAAA,wBAAU,EAAE,CAAC,SAAqB,mBAAA,6BAAA,OAAQ,GAAG,CA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M;AAC/C;AAEA,SAAS,qCAAe,KAAK,EAAE,YAAY,EAAE,QAAQ;IACnD,OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,qBAAA,+BAAA,SAAW,QAAQ,CAAC,EAAE;aAEtB,qBAAA,+BAAA,SAAW;IAEf;AACF;;CDzUC","sources":["packages/@react-stately/slider/src/index.ts","packages/@react-stately/slider/src/useSliderState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useSliderState} from './useSliderState';\n\nexport type {SliderStateOptions} from './useSliderState';\nexport type {SliderState} from './useSliderState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {clamp, snapValueToStep, useControlledState} from '@react-stately/utils';\nimport {Orientation} from '@react-types/shared';\nimport {SliderProps} from '@react-types/slider';\nimport {useCallback, useMemo, useRef, useState} from 'react';\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[],\n /**\n * Get the value for the specified thumb.\n * @param index\n */\n getThumbValue(index: number): number,\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void,\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void,\n\n /**\n * Whether the specific thumb is being dragged.\n * @param index\n */\n isThumbDragging(index: number): boolean,\n /**\n * Set is dragging on the specified thumb.\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void,\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined,\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n * @param index\n */\n setFocusedThumb(index: number | undefined): void,\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n * @param index\n */\n getThumbPercent(index: number): number,\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n * @param index\n */\n getValuePercent(value: number): number,\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n * @param index\n */\n getThumbValueLabel(index: number): string,\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n * @param index\n */\n getFormattedValue(value: number): string,\n\n /**\n * Returns the min allowed value for the specified thumb.\n * @param index\n */\n getThumbMinValue(index: number): number,\n\n /**\n * Returns the max allowed value for the specified thumb.\n * @param index\n */\n getThumbMaxValue(index: number): number,\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n * @param percent\n */\n getPercentValue(percent: number): number,\n\n /**\n * Returns if the specified thumb is editable.\n * @param index\n */\n isThumbEditable(index: number): boolean,\n\n /**\n * Set the specified thumb's editable state.\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void,\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void,\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void,\n\n /**\n * The step amount for the slider.\n */\n readonly step: number,\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number,\n\n /** The orientation of the slider. */\n readonly orientation: Orientation,\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n * @param props\n */\nexport function useSliderState<T extends number | number[]>(props: SliderStateOptions<T>): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback((values: number[]) => values?.map((val, idx) => {\n let min = idx === 0 ? minValue : val[idx - 1];\n let max = idx === values.length - 1 ? maxValue : val[idx + 1];\n return snapValueToStep(val, min, max, step);\n }), [minValue, maxValue, step]);\n\n let value = useMemo(() => restrictValues(convertValue(props.value)), [props.value]);\n let defaultValue = useMemo(() => restrictValues(convertValue(props.defaultValue) ?? [minValue]), [props.defaultValue, minValue]);\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(\n value,\n defaultValue,\n onChange\n );\n const [isDraggings, setDraggingsState] = useState<boolean[]>(new Array(values.length).fill(false));\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n function getFormattedValue(value: number) {\n return formatter.format(value);\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"main.js.map"}
|
|
1
|
+
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/slider/src/index.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useSliderState} from './useSliderState';\n\nexport type {SliderStateOptions} from './useSliderState';\nexport type {SliderState} from './useSliderState';\n"],"names":[],"version":3,"file":"main.js.map"}
|
package/dist/module.js
CHANGED
|
@@ -1,17 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {useMemo as $aTwux$useMemo, useCallback as $aTwux$useCallback, useState as $aTwux$useState, useRef as $aTwux$useRef} from "react";
|
|
1
|
+
import {useSliderState as $28f99e3e86e6ec45$export$e5fda3247f5d67f9} from "./useSliderState.module.js";
|
|
3
2
|
|
|
4
3
|
/*
|
|
5
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
6
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
7
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
8
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
-
*
|
|
10
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
11
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
12
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
13
|
-
* governing permissions and limitations under the License.
|
|
14
|
-
*/ /*
|
|
15
4
|
* Copyright 2020 Adobe. All rights reserved.
|
|
16
5
|
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
17
6
|
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
@@ -23,161 +12,6 @@ import {useMemo as $aTwux$useMemo, useCallback as $aTwux$useCallback, useState a
|
|
|
23
12
|
* governing permissions and limitations under the License.
|
|
24
13
|
*/
|
|
25
14
|
|
|
26
|
-
const $28f99e3e86e6ec45$var$DEFAULT_MIN_VALUE = 0;
|
|
27
|
-
const $28f99e3e86e6ec45$var$DEFAULT_MAX_VALUE = 100;
|
|
28
|
-
const $28f99e3e86e6ec45$var$DEFAULT_STEP_VALUE = 1;
|
|
29
|
-
function $28f99e3e86e6ec45$export$e5fda3247f5d67f9(props) {
|
|
30
|
-
const { isDisabled: isDisabled = false, minValue: minValue = $28f99e3e86e6ec45$var$DEFAULT_MIN_VALUE, maxValue: maxValue = $28f99e3e86e6ec45$var$DEFAULT_MAX_VALUE, numberFormatter: formatter, step: step = $28f99e3e86e6ec45$var$DEFAULT_STEP_VALUE, orientation: orientation = "horizontal" } = props;
|
|
31
|
-
// Page step should be at least equal to step and always a multiple of the step.
|
|
32
|
-
let pageSize = (0, $aTwux$useMemo)(()=>{
|
|
33
|
-
let calcPageSize = (maxValue - minValue) / 10;
|
|
34
|
-
calcPageSize = (0, $aTwux$snapValueToStep)(calcPageSize, 0, calcPageSize + step, step);
|
|
35
|
-
return Math.max(calcPageSize, step);
|
|
36
|
-
}, [
|
|
37
|
-
step,
|
|
38
|
-
maxValue,
|
|
39
|
-
minValue
|
|
40
|
-
]);
|
|
41
|
-
let restrictValues = (0, $aTwux$useCallback)((values)=>values === null || values === void 0 ? void 0 : values.map((val, idx)=>{
|
|
42
|
-
let min = idx === 0 ? minValue : val[idx - 1];
|
|
43
|
-
let max = idx === values.length - 1 ? maxValue : val[idx + 1];
|
|
44
|
-
return (0, $aTwux$snapValueToStep)(val, min, max, step);
|
|
45
|
-
}), [
|
|
46
|
-
minValue,
|
|
47
|
-
maxValue,
|
|
48
|
-
step
|
|
49
|
-
]);
|
|
50
|
-
let value = (0, $aTwux$useMemo)(()=>restrictValues($28f99e3e86e6ec45$var$convertValue(props.value)), [
|
|
51
|
-
props.value
|
|
52
|
-
]);
|
|
53
|
-
let defaultValue = (0, $aTwux$useMemo)(()=>{
|
|
54
|
-
var _convertValue;
|
|
55
|
-
return restrictValues((_convertValue = $28f99e3e86e6ec45$var$convertValue(props.defaultValue)) !== null && _convertValue !== void 0 ? _convertValue : [
|
|
56
|
-
minValue
|
|
57
|
-
]);
|
|
58
|
-
}, [
|
|
59
|
-
props.defaultValue,
|
|
60
|
-
minValue
|
|
61
|
-
]);
|
|
62
|
-
let onChange = $28f99e3e86e6ec45$var$createOnChange(props.value, props.defaultValue, props.onChange);
|
|
63
|
-
let onChangeEnd = $28f99e3e86e6ec45$var$createOnChange(props.value, props.defaultValue, props.onChangeEnd);
|
|
64
|
-
const [values, setValuesState] = (0, $aTwux$useControlledState)(value, defaultValue, onChange);
|
|
65
|
-
const [isDraggings, setDraggingsState] = (0, $aTwux$useState)(new Array(values.length).fill(false));
|
|
66
|
-
const isEditablesRef = (0, $aTwux$useRef)(new Array(values.length).fill(true));
|
|
67
|
-
const [focusedIndex, setFocusedIndex] = (0, $aTwux$useState)(undefined);
|
|
68
|
-
const valuesRef = (0, $aTwux$useRef)(values);
|
|
69
|
-
const isDraggingsRef = (0, $aTwux$useRef)(isDraggings);
|
|
70
|
-
let setValues = (values)=>{
|
|
71
|
-
valuesRef.current = values;
|
|
72
|
-
setValuesState(values);
|
|
73
|
-
};
|
|
74
|
-
let setDraggings = (draggings)=>{
|
|
75
|
-
isDraggingsRef.current = draggings;
|
|
76
|
-
setDraggingsState(draggings);
|
|
77
|
-
};
|
|
78
|
-
function getValuePercent(value) {
|
|
79
|
-
return (value - minValue) / (maxValue - minValue);
|
|
80
|
-
}
|
|
81
|
-
function getThumbMinValue(index) {
|
|
82
|
-
return index === 0 ? minValue : values[index - 1];
|
|
83
|
-
}
|
|
84
|
-
function getThumbMaxValue(index) {
|
|
85
|
-
return index === values.length - 1 ? maxValue : values[index + 1];
|
|
86
|
-
}
|
|
87
|
-
function isThumbEditable(index) {
|
|
88
|
-
return isEditablesRef.current[index];
|
|
89
|
-
}
|
|
90
|
-
function setThumbEditable(index, editable) {
|
|
91
|
-
isEditablesRef.current[index] = editable;
|
|
92
|
-
}
|
|
93
|
-
function updateValue(index, value) {
|
|
94
|
-
if (isDisabled || !isThumbEditable(index)) return;
|
|
95
|
-
const thisMin = getThumbMinValue(index);
|
|
96
|
-
const thisMax = getThumbMaxValue(index);
|
|
97
|
-
// Round value to multiple of step, clamp value between min and max
|
|
98
|
-
value = (0, $aTwux$snapValueToStep)(value, thisMin, thisMax, step);
|
|
99
|
-
let newValues = $28f99e3e86e6ec45$var$replaceIndex(valuesRef.current, index, value);
|
|
100
|
-
setValues(newValues);
|
|
101
|
-
}
|
|
102
|
-
function updateDragging(index, dragging) {
|
|
103
|
-
if (isDisabled || !isThumbEditable(index)) return;
|
|
104
|
-
if (dragging) valuesRef.current = values;
|
|
105
|
-
const wasDragging = isDraggingsRef.current[index];
|
|
106
|
-
isDraggingsRef.current = $28f99e3e86e6ec45$var$replaceIndex(isDraggingsRef.current, index, dragging);
|
|
107
|
-
setDraggings(isDraggingsRef.current);
|
|
108
|
-
// Call onChangeEnd if no handles are dragging.
|
|
109
|
-
if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) onChangeEnd(valuesRef.current);
|
|
110
|
-
}
|
|
111
|
-
function getFormattedValue(value) {
|
|
112
|
-
return formatter.format(value);
|
|
113
|
-
}
|
|
114
|
-
function setThumbPercent(index, percent) {
|
|
115
|
-
updateValue(index, getPercentValue(percent));
|
|
116
|
-
}
|
|
117
|
-
function getRoundedValue(value) {
|
|
118
|
-
return Math.round((value - minValue) / step) * step + minValue;
|
|
119
|
-
}
|
|
120
|
-
function getPercentValue(percent) {
|
|
121
|
-
const val = percent * (maxValue - minValue) + minValue;
|
|
122
|
-
return (0, $aTwux$clamp)(getRoundedValue(val), minValue, maxValue);
|
|
123
|
-
}
|
|
124
|
-
function incrementThumb(index, stepSize = 1) {
|
|
125
|
-
let s = Math.max(stepSize, step);
|
|
126
|
-
updateValue(index, (0, $aTwux$snapValueToStep)(values[index] + s, minValue, maxValue, step));
|
|
127
|
-
}
|
|
128
|
-
function decrementThumb(index, stepSize = 1) {
|
|
129
|
-
let s = Math.max(stepSize, step);
|
|
130
|
-
updateValue(index, (0, $aTwux$snapValueToStep)(values[index] - s, minValue, maxValue, step));
|
|
131
|
-
}
|
|
132
|
-
return {
|
|
133
|
-
values: values,
|
|
134
|
-
getThumbValue: (index)=>values[index],
|
|
135
|
-
setThumbValue: updateValue,
|
|
136
|
-
setThumbPercent: setThumbPercent,
|
|
137
|
-
isThumbDragging: (index)=>isDraggings[index],
|
|
138
|
-
setThumbDragging: updateDragging,
|
|
139
|
-
focusedThumb: focusedIndex,
|
|
140
|
-
setFocusedThumb: setFocusedIndex,
|
|
141
|
-
getThumbPercent: (index)=>getValuePercent(values[index]),
|
|
142
|
-
getValuePercent: getValuePercent,
|
|
143
|
-
getThumbValueLabel: (index)=>getFormattedValue(values[index]),
|
|
144
|
-
getFormattedValue: getFormattedValue,
|
|
145
|
-
getThumbMinValue: getThumbMinValue,
|
|
146
|
-
getThumbMaxValue: getThumbMaxValue,
|
|
147
|
-
getPercentValue: getPercentValue,
|
|
148
|
-
isThumbEditable: isThumbEditable,
|
|
149
|
-
setThumbEditable: setThumbEditable,
|
|
150
|
-
incrementThumb: incrementThumb,
|
|
151
|
-
decrementThumb: decrementThumb,
|
|
152
|
-
step: step,
|
|
153
|
-
pageSize: pageSize,
|
|
154
|
-
orientation: orientation,
|
|
155
|
-
isDisabled: isDisabled
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
function $28f99e3e86e6ec45$var$replaceIndex(array, index, value) {
|
|
159
|
-
if (array[index] === value) return array;
|
|
160
|
-
return [
|
|
161
|
-
...array.slice(0, index),
|
|
162
|
-
value,
|
|
163
|
-
...array.slice(index + 1)
|
|
164
|
-
];
|
|
165
|
-
}
|
|
166
|
-
function $28f99e3e86e6ec45$var$convertValue(value) {
|
|
167
|
-
if (value == null) return undefined;
|
|
168
|
-
return Array.isArray(value) ? value : [
|
|
169
|
-
value
|
|
170
|
-
];
|
|
171
|
-
}
|
|
172
|
-
function $28f99e3e86e6ec45$var$createOnChange(value, defaultValue, onChange) {
|
|
173
|
-
return (newValue)=>{
|
|
174
|
-
if (typeof value === "number" || typeof defaultValue === "number") onChange === null || onChange === void 0 ? void 0 : onChange(newValue[0]);
|
|
175
|
-
else onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
15
|
|
|
182
16
|
export {$28f99e3e86e6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,UAAU,UAAU;IAC5E;IAEA,OAAO;QACL,QAAQ;QACR,eAAe,CAAC,QAAkB,MAAM,CAAC,MAAM;QAC/C,eAAe;yBACf;QACA,iBAAiB,CAAC,QAAkB,WAAW,CAAC,MAAM;QACtD,kBAAkB;QAClB,cAAc;QACd,iBAAiB;QACjB,iBAAiB,CAAC,QAAkB,gBAAgB,MAAM,CAAC,MAAM;yBACjE;QACA,oBAAoB,CAAC,QAAkB,kBAAkB,MAAM,CAAC,MAAM;2BACtE;0BACA;0BACA;yBACA;yBACA;0BACA;wBACA;wBACA;cACA;kBACA;qBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAgB,KAAU,EAAE,KAAa,EAAE,KAAQ;IAC1D,IAAI,KAAK,CAAC,MAAM,KAAK,OACnB,OAAO;IAGT,OAAO;WAAI,MAAM,KAAK,CAAC,GAAG;QAAQ;WAAU,MAAM,KAAK,CAAC,QAAQ;KAAG;AACrE;AAEA,SAAS,mCAAa,KAAwB;IAC5C,IAAI,SAAS,MACX,OAAO;IAGT,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C;AAEA,SAAS,qCAAe,KAAK,EAAE,YAAY,EAAE,QAAQ;IACnD,OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,qBAAA,+BAAA,SAAW,QAAQ,CAAC,EAAE;aAEtB,qBAAA,+BAAA,SAAW;IAEf;AACF;;CDzUC","sources":["packages/@react-stately/slider/src/index.ts","packages/@react-stately/slider/src/useSliderState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useSliderState} from './useSliderState';\n\nexport type {SliderStateOptions} from './useSliderState';\nexport type {SliderState} from './useSliderState';\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {clamp, snapValueToStep, useControlledState} from '@react-stately/utils';\nimport {Orientation} from '@react-types/shared';\nimport {SliderProps} from '@react-types/slider';\nimport {useCallback, useMemo, useRef, useState} from 'react';\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[],\n /**\n * Get the value for the specified thumb.\n * @param index\n */\n getThumbValue(index: number): number,\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void,\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void,\n\n /**\n * Whether the specific thumb is being dragged.\n * @param index\n */\n isThumbDragging(index: number): boolean,\n /**\n * Set is dragging on the specified thumb.\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void,\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined,\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n * @param index\n */\n setFocusedThumb(index: number | undefined): void,\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n * @param index\n */\n getThumbPercent(index: number): number,\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n * @param index\n */\n getValuePercent(value: number): number,\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n * @param index\n */\n getThumbValueLabel(index: number): string,\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n * @param index\n */\n getFormattedValue(value: number): string,\n\n /**\n * Returns the min allowed value for the specified thumb.\n * @param index\n */\n getThumbMinValue(index: number): number,\n\n /**\n * Returns the max allowed value for the specified thumb.\n * @param index\n */\n getThumbMaxValue(index: number): number,\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n * @param percent\n */\n getPercentValue(percent: number): number,\n\n /**\n * Returns if the specified thumb is editable.\n * @param index\n */\n isThumbEditable(index: number): boolean,\n\n /**\n * Set the specified thumb's editable state.\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void,\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void,\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void,\n\n /**\n * The step amount for the slider.\n */\n readonly step: number,\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number,\n\n /** The orientation of the slider. */\n readonly orientation: Orientation,\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n * @param props\n */\nexport function useSliderState<T extends number | number[]>(props: SliderStateOptions<T>): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback((values: number[]) => values?.map((val, idx) => {\n let min = idx === 0 ? minValue : val[idx - 1];\n let max = idx === values.length - 1 ? maxValue : val[idx + 1];\n return snapValueToStep(val, min, max, step);\n }), [minValue, maxValue, step]);\n\n let value = useMemo(() => restrictValues(convertValue(props.value)), [props.value]);\n let defaultValue = useMemo(() => restrictValues(convertValue(props.defaultValue) ?? [minValue]), [props.defaultValue, minValue]);\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(\n value,\n defaultValue,\n onChange\n );\n const [isDraggings, setDraggingsState] = useState<boolean[]>(new Array(values.length).fill(false));\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n function getFormattedValue(value: number) {\n return formatter.format(value);\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"module.js.map"}
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/slider/src/index.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nexport {useSliderState} from './useSliderState';\n\nexport type {SliderStateOptions} from './useSliderState';\nexport type {SliderState} from './useSliderState';\n"],"names":[],"version":3,"file":"module.js.map"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
var $1NNdE$reactstatelyutils = require("@react-stately/utils");
|
|
2
|
+
var $1NNdE$react = require("react");
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
function $parcel$export(e, n, v, s) {
|
|
6
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
$parcel$export(module.exports, "useSliderState", () => $e86753598efd0f02$export$e5fda3247f5d67f9);
|
|
10
|
+
/*
|
|
11
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
12
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
13
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
14
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
15
|
+
*
|
|
16
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
17
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
18
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
19
|
+
* governing permissions and limitations under the License.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const $e86753598efd0f02$var$DEFAULT_MIN_VALUE = 0;
|
|
23
|
+
const $e86753598efd0f02$var$DEFAULT_MAX_VALUE = 100;
|
|
24
|
+
const $e86753598efd0f02$var$DEFAULT_STEP_VALUE = 1;
|
|
25
|
+
function $e86753598efd0f02$export$e5fda3247f5d67f9(props) {
|
|
26
|
+
const { isDisabled: isDisabled = false, minValue: minValue = $e86753598efd0f02$var$DEFAULT_MIN_VALUE, maxValue: maxValue = $e86753598efd0f02$var$DEFAULT_MAX_VALUE, numberFormatter: formatter, step: step = $e86753598efd0f02$var$DEFAULT_STEP_VALUE, orientation: orientation = "horizontal" } = props;
|
|
27
|
+
// Page step should be at least equal to step and always a multiple of the step.
|
|
28
|
+
let pageSize = (0, $1NNdE$react.useMemo)(()=>{
|
|
29
|
+
let calcPageSize = (maxValue - minValue) / 10;
|
|
30
|
+
calcPageSize = (0, $1NNdE$reactstatelyutils.snapValueToStep)(calcPageSize, 0, calcPageSize + step, step);
|
|
31
|
+
return Math.max(calcPageSize, step);
|
|
32
|
+
}, [
|
|
33
|
+
step,
|
|
34
|
+
maxValue,
|
|
35
|
+
minValue
|
|
36
|
+
]);
|
|
37
|
+
let restrictValues = (0, $1NNdE$react.useCallback)((values)=>values === null || values === void 0 ? void 0 : values.map((val, idx)=>{
|
|
38
|
+
let min = idx === 0 ? minValue : val[idx - 1];
|
|
39
|
+
let max = idx === values.length - 1 ? maxValue : val[idx + 1];
|
|
40
|
+
return (0, $1NNdE$reactstatelyutils.snapValueToStep)(val, min, max, step);
|
|
41
|
+
}), [
|
|
42
|
+
minValue,
|
|
43
|
+
maxValue,
|
|
44
|
+
step
|
|
45
|
+
]);
|
|
46
|
+
let value = (0, $1NNdE$react.useMemo)(()=>restrictValues($e86753598efd0f02$var$convertValue(props.value)), [
|
|
47
|
+
props.value
|
|
48
|
+
]);
|
|
49
|
+
let defaultValue = (0, $1NNdE$react.useMemo)(()=>{
|
|
50
|
+
var _convertValue;
|
|
51
|
+
return restrictValues((_convertValue = $e86753598efd0f02$var$convertValue(props.defaultValue)) !== null && _convertValue !== void 0 ? _convertValue : [
|
|
52
|
+
minValue
|
|
53
|
+
]);
|
|
54
|
+
}, [
|
|
55
|
+
props.defaultValue,
|
|
56
|
+
minValue
|
|
57
|
+
]);
|
|
58
|
+
let onChange = $e86753598efd0f02$var$createOnChange(props.value, props.defaultValue, props.onChange);
|
|
59
|
+
let onChangeEnd = $e86753598efd0f02$var$createOnChange(props.value, props.defaultValue, props.onChangeEnd);
|
|
60
|
+
const [values, setValuesState] = (0, $1NNdE$reactstatelyutils.useControlledState)(value, defaultValue, onChange);
|
|
61
|
+
const [isDraggings, setDraggingsState] = (0, $1NNdE$react.useState)(new Array(values.length).fill(false));
|
|
62
|
+
const isEditablesRef = (0, $1NNdE$react.useRef)(new Array(values.length).fill(true));
|
|
63
|
+
const [focusedIndex, setFocusedIndex] = (0, $1NNdE$react.useState)(undefined);
|
|
64
|
+
const valuesRef = (0, $1NNdE$react.useRef)(values);
|
|
65
|
+
const isDraggingsRef = (0, $1NNdE$react.useRef)(isDraggings);
|
|
66
|
+
let setValues = (values)=>{
|
|
67
|
+
valuesRef.current = values;
|
|
68
|
+
setValuesState(values);
|
|
69
|
+
};
|
|
70
|
+
let setDraggings = (draggings)=>{
|
|
71
|
+
isDraggingsRef.current = draggings;
|
|
72
|
+
setDraggingsState(draggings);
|
|
73
|
+
};
|
|
74
|
+
function getValuePercent(value) {
|
|
75
|
+
return (value - minValue) / (maxValue - minValue);
|
|
76
|
+
}
|
|
77
|
+
function getThumbMinValue(index) {
|
|
78
|
+
return index === 0 ? minValue : values[index - 1];
|
|
79
|
+
}
|
|
80
|
+
function getThumbMaxValue(index) {
|
|
81
|
+
return index === values.length - 1 ? maxValue : values[index + 1];
|
|
82
|
+
}
|
|
83
|
+
function isThumbEditable(index) {
|
|
84
|
+
return isEditablesRef.current[index];
|
|
85
|
+
}
|
|
86
|
+
function setThumbEditable(index, editable) {
|
|
87
|
+
isEditablesRef.current[index] = editable;
|
|
88
|
+
}
|
|
89
|
+
function updateValue(index, value) {
|
|
90
|
+
if (isDisabled || !isThumbEditable(index)) return;
|
|
91
|
+
const thisMin = getThumbMinValue(index);
|
|
92
|
+
const thisMax = getThumbMaxValue(index);
|
|
93
|
+
// Round value to multiple of step, clamp value between min and max
|
|
94
|
+
value = (0, $1NNdE$reactstatelyutils.snapValueToStep)(value, thisMin, thisMax, step);
|
|
95
|
+
let newValues = $e86753598efd0f02$var$replaceIndex(valuesRef.current, index, value);
|
|
96
|
+
setValues(newValues);
|
|
97
|
+
}
|
|
98
|
+
function updateDragging(index, dragging) {
|
|
99
|
+
if (isDisabled || !isThumbEditable(index)) return;
|
|
100
|
+
if (dragging) valuesRef.current = values;
|
|
101
|
+
const wasDragging = isDraggingsRef.current[index];
|
|
102
|
+
isDraggingsRef.current = $e86753598efd0f02$var$replaceIndex(isDraggingsRef.current, index, dragging);
|
|
103
|
+
setDraggings(isDraggingsRef.current);
|
|
104
|
+
// Call onChangeEnd if no handles are dragging.
|
|
105
|
+
if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) onChangeEnd(valuesRef.current);
|
|
106
|
+
}
|
|
107
|
+
function getFormattedValue(value) {
|
|
108
|
+
return formatter.format(value);
|
|
109
|
+
}
|
|
110
|
+
function setThumbPercent(index, percent) {
|
|
111
|
+
updateValue(index, getPercentValue(percent));
|
|
112
|
+
}
|
|
113
|
+
function getRoundedValue(value) {
|
|
114
|
+
return Math.round((value - minValue) / step) * step + minValue;
|
|
115
|
+
}
|
|
116
|
+
function getPercentValue(percent) {
|
|
117
|
+
const val = percent * (maxValue - minValue) + minValue;
|
|
118
|
+
return (0, $1NNdE$reactstatelyutils.clamp)(getRoundedValue(val), minValue, maxValue);
|
|
119
|
+
}
|
|
120
|
+
function incrementThumb(index, stepSize = 1) {
|
|
121
|
+
let s = Math.max(stepSize, step);
|
|
122
|
+
updateValue(index, (0, $1NNdE$reactstatelyutils.snapValueToStep)(values[index] + s, minValue, maxValue, step));
|
|
123
|
+
}
|
|
124
|
+
function decrementThumb(index, stepSize = 1) {
|
|
125
|
+
let s = Math.max(stepSize, step);
|
|
126
|
+
updateValue(index, (0, $1NNdE$reactstatelyutils.snapValueToStep)(values[index] - s, minValue, maxValue, step));
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
values: values,
|
|
130
|
+
getThumbValue: (index)=>values[index],
|
|
131
|
+
setThumbValue: updateValue,
|
|
132
|
+
setThumbPercent: setThumbPercent,
|
|
133
|
+
isThumbDragging: (index)=>isDraggings[index],
|
|
134
|
+
setThumbDragging: updateDragging,
|
|
135
|
+
focusedThumb: focusedIndex,
|
|
136
|
+
setFocusedThumb: setFocusedIndex,
|
|
137
|
+
getThumbPercent: (index)=>getValuePercent(values[index]),
|
|
138
|
+
getValuePercent: getValuePercent,
|
|
139
|
+
getThumbValueLabel: (index)=>getFormattedValue(values[index]),
|
|
140
|
+
getFormattedValue: getFormattedValue,
|
|
141
|
+
getThumbMinValue: getThumbMinValue,
|
|
142
|
+
getThumbMaxValue: getThumbMaxValue,
|
|
143
|
+
getPercentValue: getPercentValue,
|
|
144
|
+
isThumbEditable: isThumbEditable,
|
|
145
|
+
setThumbEditable: setThumbEditable,
|
|
146
|
+
incrementThumb: incrementThumb,
|
|
147
|
+
decrementThumb: decrementThumb,
|
|
148
|
+
step: step,
|
|
149
|
+
pageSize: pageSize,
|
|
150
|
+
orientation: orientation,
|
|
151
|
+
isDisabled: isDisabled
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function $e86753598efd0f02$var$replaceIndex(array, index, value) {
|
|
155
|
+
if (array[index] === value) return array;
|
|
156
|
+
return [
|
|
157
|
+
...array.slice(0, index),
|
|
158
|
+
value,
|
|
159
|
+
...array.slice(index + 1)
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
function $e86753598efd0f02$var$convertValue(value) {
|
|
163
|
+
if (value == null) return undefined;
|
|
164
|
+
return Array.isArray(value) ? value : [
|
|
165
|
+
value
|
|
166
|
+
];
|
|
167
|
+
}
|
|
168
|
+
function $e86753598efd0f02$var$createOnChange(value, defaultValue, onChange) {
|
|
169
|
+
return (newValue)=>{
|
|
170
|
+
if (typeof value === "number" || typeof defaultValue === "number") onChange === null || onChange === void 0 ? void 0 : onChange(newValue[0]);
|
|
171
|
+
else onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
//# sourceMappingURL=useSliderState.main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;;AAAA;;;;;;;;;;CAUC;;AAyID,MAAM,0CAAoB;AAC1B,MAAM,0CAAoB;AAC1B,MAAM,2CAAqB;AAYpB,SAAS,0CAA4C,KAA4B;IACtF,MAAM,cACJ,aAAa,iBACb,WAAW,mDACX,WAAW,yCACX,iBAAiB,SAAS,QAC1B,OAAO,uDACP,cAAc,cACf,GAAG;IAEJ,gFAAgF;IAChF,IAAI,WAAW,CAAA,GAAA,oBAAM,EAAE;QACrB,IAAI,eAAe,AAAC,CAAA,WAAW,QAAO,IAAK;QAC3C,eAAe,CAAA,GAAA,wCAAc,EAAE,cAAc,GAAG,eAAe,MAAM;QACrE,OAAO,KAAK,GAAG,CAAC,cAAc;IAChC,GAAG;QAAC;QAAM;QAAU;KAAS;IAE7B,IAAI,iBAAiB,CAAA,GAAA,wBAAU,EAAE,CAAC,SAAqB,mBAAA,6BAAA,OAAQ,GAAG,CAAC,CAAC,KAAK;YACvE,IAAI,MAAM,QAAQ,IAAI,WAAW,GAAG,CAAC,M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OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,qBAAA,+BAAA,SAAW,QAAQ,CAAC,EAAE;aAEtB,qBAAA,+BAAA,SAAW;IAEf;AACF","sources":["packages/@react-stately/slider/src/useSliderState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {clamp, snapValueToStep, useControlledState} from '@react-stately/utils';\nimport {Orientation} from '@react-types/shared';\nimport {SliderProps} from '@react-types/slider';\nimport {useCallback, useMemo, useRef, useState} from 'react';\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[],\n /**\n * Get the value for the specified thumb.\n * @param index\n */\n getThumbValue(index: number): number,\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void,\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void,\n\n /**\n * Whether the specific thumb is being dragged.\n * @param index\n */\n isThumbDragging(index: number): boolean,\n /**\n * Set is dragging on the specified thumb.\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void,\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined,\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n * @param index\n */\n setFocusedThumb(index: number | undefined): void,\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n * @param index\n */\n getThumbPercent(index: number): number,\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n * @param index\n */\n getValuePercent(value: number): number,\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n * @param index\n */\n getThumbValueLabel(index: number): string,\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n * @param index\n */\n getFormattedValue(value: number): string,\n\n /**\n * Returns the min allowed value for the specified thumb.\n * @param index\n */\n getThumbMinValue(index: number): number,\n\n /**\n * Returns the max allowed value for the specified thumb.\n * @param index\n */\n getThumbMaxValue(index: number): number,\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n * @param percent\n */\n getPercentValue(percent: number): number,\n\n /**\n * Returns if the specified thumb is editable.\n * @param index\n */\n isThumbEditable(index: number): boolean,\n\n /**\n * Set the specified thumb's editable state.\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void,\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void,\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void,\n\n /**\n * The step amount for the slider.\n */\n readonly step: number,\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number,\n\n /** The orientation of the slider. */\n readonly orientation: Orientation,\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n * @param props\n */\nexport function useSliderState<T extends number | number[]>(props: SliderStateOptions<T>): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback((values: number[]) => values?.map((val, idx) => {\n let min = idx === 0 ? minValue : val[idx - 1];\n let max = idx === values.length - 1 ? maxValue : val[idx + 1];\n return snapValueToStep(val, min, max, step);\n }), [minValue, maxValue, step]);\n\n let value = useMemo(() => restrictValues(convertValue(props.value)), [props.value]);\n let defaultValue = useMemo(() => restrictValues(convertValue(props.defaultValue) ?? [minValue]), [props.defaultValue, minValue]);\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(\n value,\n defaultValue,\n onChange\n );\n const [isDraggings, setDraggingsState] = useState<boolean[]>(new Array(values.length).fill(false));\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n function getFormattedValue(value: number) {\n return formatter.format(value);\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"useSliderState.main.js.map"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import {snapValueToStep as $BT4Uh$snapValueToStep, useControlledState as $BT4Uh$useControlledState, clamp as $BT4Uh$clamp} from "@react-stately/utils";
|
|
2
|
+
import {useMemo as $BT4Uh$useMemo, useCallback as $BT4Uh$useCallback, useState as $BT4Uh$useState, useRef as $BT4Uh$useRef} from "react";
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
6
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
8
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
11
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
12
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
13
|
+
* governing permissions and limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const $28f99e3e86e6ec45$var$DEFAULT_MIN_VALUE = 0;
|
|
17
|
+
const $28f99e3e86e6ec45$var$DEFAULT_MAX_VALUE = 100;
|
|
18
|
+
const $28f99e3e86e6ec45$var$DEFAULT_STEP_VALUE = 1;
|
|
19
|
+
function $28f99e3e86e6ec45$export$e5fda3247f5d67f9(props) {
|
|
20
|
+
const { isDisabled: isDisabled = false, minValue: minValue = $28f99e3e86e6ec45$var$DEFAULT_MIN_VALUE, maxValue: maxValue = $28f99e3e86e6ec45$var$DEFAULT_MAX_VALUE, numberFormatter: formatter, step: step = $28f99e3e86e6ec45$var$DEFAULT_STEP_VALUE, orientation: orientation = "horizontal" } = props;
|
|
21
|
+
// Page step should be at least equal to step and always a multiple of the step.
|
|
22
|
+
let pageSize = (0, $BT4Uh$useMemo)(()=>{
|
|
23
|
+
let calcPageSize = (maxValue - minValue) / 10;
|
|
24
|
+
calcPageSize = (0, $BT4Uh$snapValueToStep)(calcPageSize, 0, calcPageSize + step, step);
|
|
25
|
+
return Math.max(calcPageSize, step);
|
|
26
|
+
}, [
|
|
27
|
+
step,
|
|
28
|
+
maxValue,
|
|
29
|
+
minValue
|
|
30
|
+
]);
|
|
31
|
+
let restrictValues = (0, $BT4Uh$useCallback)((values)=>values === null || values === void 0 ? void 0 : values.map((val, idx)=>{
|
|
32
|
+
let min = idx === 0 ? minValue : val[idx - 1];
|
|
33
|
+
let max = idx === values.length - 1 ? maxValue : val[idx + 1];
|
|
34
|
+
return (0, $BT4Uh$snapValueToStep)(val, min, max, step);
|
|
35
|
+
}), [
|
|
36
|
+
minValue,
|
|
37
|
+
maxValue,
|
|
38
|
+
step
|
|
39
|
+
]);
|
|
40
|
+
let value = (0, $BT4Uh$useMemo)(()=>restrictValues($28f99e3e86e6ec45$var$convertValue(props.value)), [
|
|
41
|
+
props.value
|
|
42
|
+
]);
|
|
43
|
+
let defaultValue = (0, $BT4Uh$useMemo)(()=>{
|
|
44
|
+
var _convertValue;
|
|
45
|
+
return restrictValues((_convertValue = $28f99e3e86e6ec45$var$convertValue(props.defaultValue)) !== null && _convertValue !== void 0 ? _convertValue : [
|
|
46
|
+
minValue
|
|
47
|
+
]);
|
|
48
|
+
}, [
|
|
49
|
+
props.defaultValue,
|
|
50
|
+
minValue
|
|
51
|
+
]);
|
|
52
|
+
let onChange = $28f99e3e86e6ec45$var$createOnChange(props.value, props.defaultValue, props.onChange);
|
|
53
|
+
let onChangeEnd = $28f99e3e86e6ec45$var$createOnChange(props.value, props.defaultValue, props.onChangeEnd);
|
|
54
|
+
const [values, setValuesState] = (0, $BT4Uh$useControlledState)(value, defaultValue, onChange);
|
|
55
|
+
const [isDraggings, setDraggingsState] = (0, $BT4Uh$useState)(new Array(values.length).fill(false));
|
|
56
|
+
const isEditablesRef = (0, $BT4Uh$useRef)(new Array(values.length).fill(true));
|
|
57
|
+
const [focusedIndex, setFocusedIndex] = (0, $BT4Uh$useState)(undefined);
|
|
58
|
+
const valuesRef = (0, $BT4Uh$useRef)(values);
|
|
59
|
+
const isDraggingsRef = (0, $BT4Uh$useRef)(isDraggings);
|
|
60
|
+
let setValues = (values)=>{
|
|
61
|
+
valuesRef.current = values;
|
|
62
|
+
setValuesState(values);
|
|
63
|
+
};
|
|
64
|
+
let setDraggings = (draggings)=>{
|
|
65
|
+
isDraggingsRef.current = draggings;
|
|
66
|
+
setDraggingsState(draggings);
|
|
67
|
+
};
|
|
68
|
+
function getValuePercent(value) {
|
|
69
|
+
return (value - minValue) / (maxValue - minValue);
|
|
70
|
+
}
|
|
71
|
+
function getThumbMinValue(index) {
|
|
72
|
+
return index === 0 ? minValue : values[index - 1];
|
|
73
|
+
}
|
|
74
|
+
function getThumbMaxValue(index) {
|
|
75
|
+
return index === values.length - 1 ? maxValue : values[index + 1];
|
|
76
|
+
}
|
|
77
|
+
function isThumbEditable(index) {
|
|
78
|
+
return isEditablesRef.current[index];
|
|
79
|
+
}
|
|
80
|
+
function setThumbEditable(index, editable) {
|
|
81
|
+
isEditablesRef.current[index] = editable;
|
|
82
|
+
}
|
|
83
|
+
function updateValue(index, value) {
|
|
84
|
+
if (isDisabled || !isThumbEditable(index)) return;
|
|
85
|
+
const thisMin = getThumbMinValue(index);
|
|
86
|
+
const thisMax = getThumbMaxValue(index);
|
|
87
|
+
// Round value to multiple of step, clamp value between min and max
|
|
88
|
+
value = (0, $BT4Uh$snapValueToStep)(value, thisMin, thisMax, step);
|
|
89
|
+
let newValues = $28f99e3e86e6ec45$var$replaceIndex(valuesRef.current, index, value);
|
|
90
|
+
setValues(newValues);
|
|
91
|
+
}
|
|
92
|
+
function updateDragging(index, dragging) {
|
|
93
|
+
if (isDisabled || !isThumbEditable(index)) return;
|
|
94
|
+
if (dragging) valuesRef.current = values;
|
|
95
|
+
const wasDragging = isDraggingsRef.current[index];
|
|
96
|
+
isDraggingsRef.current = $28f99e3e86e6ec45$var$replaceIndex(isDraggingsRef.current, index, dragging);
|
|
97
|
+
setDraggings(isDraggingsRef.current);
|
|
98
|
+
// Call onChangeEnd if no handles are dragging.
|
|
99
|
+
if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) onChangeEnd(valuesRef.current);
|
|
100
|
+
}
|
|
101
|
+
function getFormattedValue(value) {
|
|
102
|
+
return formatter.format(value);
|
|
103
|
+
}
|
|
104
|
+
function setThumbPercent(index, percent) {
|
|
105
|
+
updateValue(index, getPercentValue(percent));
|
|
106
|
+
}
|
|
107
|
+
function getRoundedValue(value) {
|
|
108
|
+
return Math.round((value - minValue) / step) * step + minValue;
|
|
109
|
+
}
|
|
110
|
+
function getPercentValue(percent) {
|
|
111
|
+
const val = percent * (maxValue - minValue) + minValue;
|
|
112
|
+
return (0, $BT4Uh$clamp)(getRoundedValue(val), minValue, maxValue);
|
|
113
|
+
}
|
|
114
|
+
function incrementThumb(index, stepSize = 1) {
|
|
115
|
+
let s = Math.max(stepSize, step);
|
|
116
|
+
updateValue(index, (0, $BT4Uh$snapValueToStep)(values[index] + s, minValue, maxValue, step));
|
|
117
|
+
}
|
|
118
|
+
function decrementThumb(index, stepSize = 1) {
|
|
119
|
+
let s = Math.max(stepSize, step);
|
|
120
|
+
updateValue(index, (0, $BT4Uh$snapValueToStep)(values[index] - s, minValue, maxValue, step));
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
values: values,
|
|
124
|
+
getThumbValue: (index)=>values[index],
|
|
125
|
+
setThumbValue: updateValue,
|
|
126
|
+
setThumbPercent: setThumbPercent,
|
|
127
|
+
isThumbDragging: (index)=>isDraggings[index],
|
|
128
|
+
setThumbDragging: updateDragging,
|
|
129
|
+
focusedThumb: focusedIndex,
|
|
130
|
+
setFocusedThumb: setFocusedIndex,
|
|
131
|
+
getThumbPercent: (index)=>getValuePercent(values[index]),
|
|
132
|
+
getValuePercent: getValuePercent,
|
|
133
|
+
getThumbValueLabel: (index)=>getFormattedValue(values[index]),
|
|
134
|
+
getFormattedValue: getFormattedValue,
|
|
135
|
+
getThumbMinValue: getThumbMinValue,
|
|
136
|
+
getThumbMaxValue: getThumbMaxValue,
|
|
137
|
+
getPercentValue: getPercentValue,
|
|
138
|
+
isThumbEditable: isThumbEditable,
|
|
139
|
+
setThumbEditable: setThumbEditable,
|
|
140
|
+
incrementThumb: incrementThumb,
|
|
141
|
+
decrementThumb: decrementThumb,
|
|
142
|
+
step: step,
|
|
143
|
+
pageSize: pageSize,
|
|
144
|
+
orientation: orientation,
|
|
145
|
+
isDisabled: isDisabled
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function $28f99e3e86e6ec45$var$replaceIndex(array, index, value) {
|
|
149
|
+
if (array[index] === value) return array;
|
|
150
|
+
return [
|
|
151
|
+
...array.slice(0, index),
|
|
152
|
+
value,
|
|
153
|
+
...array.slice(index + 1)
|
|
154
|
+
];
|
|
155
|
+
}
|
|
156
|
+
function $28f99e3e86e6ec45$var$convertValue(value) {
|
|
157
|
+
if (value == null) return undefined;
|
|
158
|
+
return Array.isArray(value) ? value : [
|
|
159
|
+
value
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
function $28f99e3e86e6ec45$var$createOnChange(value, defaultValue, onChange) {
|
|
163
|
+
return (newValue)=>{
|
|
164
|
+
if (typeof value === "number" || typeof defaultValue === "number") onChange === null || onChange === void 0 ? void 0 : onChange(newValue[0]);
|
|
165
|
+
else onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
export {$28f99e3e86e6ec45$export$e5fda3247f5d67f9 as useSliderState};
|
|
171
|
+
//# sourceMappingURL=useSliderState.mjs.map
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import {snapValueToStep as $BT4Uh$snapValueToStep, useControlledState as $BT4Uh$useControlledState, clamp as $BT4Uh$clamp} from "@react-stately/utils";
|
|
2
|
+
import {useMemo as $BT4Uh$useMemo, useCallback as $BT4Uh$useCallback, useState as $BT4Uh$useState, useRef as $BT4Uh$useRef} from "react";
|
|
3
|
+
|
|
4
|
+
/*
|
|
5
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
6
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
8
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
11
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
12
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
13
|
+
* governing permissions and limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const $28f99e3e86e6ec45$var$DEFAULT_MIN_VALUE = 0;
|
|
17
|
+
const $28f99e3e86e6ec45$var$DEFAULT_MAX_VALUE = 100;
|
|
18
|
+
const $28f99e3e86e6ec45$var$DEFAULT_STEP_VALUE = 1;
|
|
19
|
+
function $28f99e3e86e6ec45$export$e5fda3247f5d67f9(props) {
|
|
20
|
+
const { isDisabled: isDisabled = false, minValue: minValue = $28f99e3e86e6ec45$var$DEFAULT_MIN_VALUE, maxValue: maxValue = $28f99e3e86e6ec45$var$DEFAULT_MAX_VALUE, numberFormatter: formatter, step: step = $28f99e3e86e6ec45$var$DEFAULT_STEP_VALUE, orientation: orientation = "horizontal" } = props;
|
|
21
|
+
// Page step should be at least equal to step and always a multiple of the step.
|
|
22
|
+
let pageSize = (0, $BT4Uh$useMemo)(()=>{
|
|
23
|
+
let calcPageSize = (maxValue - minValue) / 10;
|
|
24
|
+
calcPageSize = (0, $BT4Uh$snapValueToStep)(calcPageSize, 0, calcPageSize + step, step);
|
|
25
|
+
return Math.max(calcPageSize, step);
|
|
26
|
+
}, [
|
|
27
|
+
step,
|
|
28
|
+
maxValue,
|
|
29
|
+
minValue
|
|
30
|
+
]);
|
|
31
|
+
let restrictValues = (0, $BT4Uh$useCallback)((values)=>values === null || values === void 0 ? void 0 : values.map((val, idx)=>{
|
|
32
|
+
let min = idx === 0 ? minValue : val[idx - 1];
|
|
33
|
+
let max = idx === values.length - 1 ? maxValue : val[idx + 1];
|
|
34
|
+
return (0, $BT4Uh$snapValueToStep)(val, min, max, step);
|
|
35
|
+
}), [
|
|
36
|
+
minValue,
|
|
37
|
+
maxValue,
|
|
38
|
+
step
|
|
39
|
+
]);
|
|
40
|
+
let value = (0, $BT4Uh$useMemo)(()=>restrictValues($28f99e3e86e6ec45$var$convertValue(props.value)), [
|
|
41
|
+
props.value
|
|
42
|
+
]);
|
|
43
|
+
let defaultValue = (0, $BT4Uh$useMemo)(()=>{
|
|
44
|
+
var _convertValue;
|
|
45
|
+
return restrictValues((_convertValue = $28f99e3e86e6ec45$var$convertValue(props.defaultValue)) !== null && _convertValue !== void 0 ? _convertValue : [
|
|
46
|
+
minValue
|
|
47
|
+
]);
|
|
48
|
+
}, [
|
|
49
|
+
props.defaultValue,
|
|
50
|
+
minValue
|
|
51
|
+
]);
|
|
52
|
+
let onChange = $28f99e3e86e6ec45$var$createOnChange(props.value, props.defaultValue, props.onChange);
|
|
53
|
+
let onChangeEnd = $28f99e3e86e6ec45$var$createOnChange(props.value, props.defaultValue, props.onChangeEnd);
|
|
54
|
+
const [values, setValuesState] = (0, $BT4Uh$useControlledState)(value, defaultValue, onChange);
|
|
55
|
+
const [isDraggings, setDraggingsState] = (0, $BT4Uh$useState)(new Array(values.length).fill(false));
|
|
56
|
+
const isEditablesRef = (0, $BT4Uh$useRef)(new Array(values.length).fill(true));
|
|
57
|
+
const [focusedIndex, setFocusedIndex] = (0, $BT4Uh$useState)(undefined);
|
|
58
|
+
const valuesRef = (0, $BT4Uh$useRef)(values);
|
|
59
|
+
const isDraggingsRef = (0, $BT4Uh$useRef)(isDraggings);
|
|
60
|
+
let setValues = (values)=>{
|
|
61
|
+
valuesRef.current = values;
|
|
62
|
+
setValuesState(values);
|
|
63
|
+
};
|
|
64
|
+
let setDraggings = (draggings)=>{
|
|
65
|
+
isDraggingsRef.current = draggings;
|
|
66
|
+
setDraggingsState(draggings);
|
|
67
|
+
};
|
|
68
|
+
function getValuePercent(value) {
|
|
69
|
+
return (value - minValue) / (maxValue - minValue);
|
|
70
|
+
}
|
|
71
|
+
function getThumbMinValue(index) {
|
|
72
|
+
return index === 0 ? minValue : values[index - 1];
|
|
73
|
+
}
|
|
74
|
+
function getThumbMaxValue(index) {
|
|
75
|
+
return index === values.length - 1 ? maxValue : values[index + 1];
|
|
76
|
+
}
|
|
77
|
+
function isThumbEditable(index) {
|
|
78
|
+
return isEditablesRef.current[index];
|
|
79
|
+
}
|
|
80
|
+
function setThumbEditable(index, editable) {
|
|
81
|
+
isEditablesRef.current[index] = editable;
|
|
82
|
+
}
|
|
83
|
+
function updateValue(index, value) {
|
|
84
|
+
if (isDisabled || !isThumbEditable(index)) return;
|
|
85
|
+
const thisMin = getThumbMinValue(index);
|
|
86
|
+
const thisMax = getThumbMaxValue(index);
|
|
87
|
+
// Round value to multiple of step, clamp value between min and max
|
|
88
|
+
value = (0, $BT4Uh$snapValueToStep)(value, thisMin, thisMax, step);
|
|
89
|
+
let newValues = $28f99e3e86e6ec45$var$replaceIndex(valuesRef.current, index, value);
|
|
90
|
+
setValues(newValues);
|
|
91
|
+
}
|
|
92
|
+
function updateDragging(index, dragging) {
|
|
93
|
+
if (isDisabled || !isThumbEditable(index)) return;
|
|
94
|
+
if (dragging) valuesRef.current = values;
|
|
95
|
+
const wasDragging = isDraggingsRef.current[index];
|
|
96
|
+
isDraggingsRef.current = $28f99e3e86e6ec45$var$replaceIndex(isDraggingsRef.current, index, dragging);
|
|
97
|
+
setDraggings(isDraggingsRef.current);
|
|
98
|
+
// Call onChangeEnd if no handles are dragging.
|
|
99
|
+
if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) onChangeEnd(valuesRef.current);
|
|
100
|
+
}
|
|
101
|
+
function getFormattedValue(value) {
|
|
102
|
+
return formatter.format(value);
|
|
103
|
+
}
|
|
104
|
+
function setThumbPercent(index, percent) {
|
|
105
|
+
updateValue(index, getPercentValue(percent));
|
|
106
|
+
}
|
|
107
|
+
function getRoundedValue(value) {
|
|
108
|
+
return Math.round((value - minValue) / step) * step + minValue;
|
|
109
|
+
}
|
|
110
|
+
function getPercentValue(percent) {
|
|
111
|
+
const val = percent * (maxValue - minValue) + minValue;
|
|
112
|
+
return (0, $BT4Uh$clamp)(getRoundedValue(val), minValue, maxValue);
|
|
113
|
+
}
|
|
114
|
+
function incrementThumb(index, stepSize = 1) {
|
|
115
|
+
let s = Math.max(stepSize, step);
|
|
116
|
+
updateValue(index, (0, $BT4Uh$snapValueToStep)(values[index] + s, minValue, maxValue, step));
|
|
117
|
+
}
|
|
118
|
+
function decrementThumb(index, stepSize = 1) {
|
|
119
|
+
let s = Math.max(stepSize, step);
|
|
120
|
+
updateValue(index, (0, $BT4Uh$snapValueToStep)(values[index] - s, minValue, maxValue, step));
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
values: values,
|
|
124
|
+
getThumbValue: (index)=>values[index],
|
|
125
|
+
setThumbValue: updateValue,
|
|
126
|
+
setThumbPercent: setThumbPercent,
|
|
127
|
+
isThumbDragging: (index)=>isDraggings[index],
|
|
128
|
+
setThumbDragging: updateDragging,
|
|
129
|
+
focusedThumb: focusedIndex,
|
|
130
|
+
setFocusedThumb: setFocusedIndex,
|
|
131
|
+
getThumbPercent: (index)=>getValuePercent(values[index]),
|
|
132
|
+
getValuePercent: getValuePercent,
|
|
133
|
+
getThumbValueLabel: (index)=>getFormattedValue(values[index]),
|
|
134
|
+
getFormattedValue: getFormattedValue,
|
|
135
|
+
getThumbMinValue: getThumbMinValue,
|
|
136
|
+
getThumbMaxValue: getThumbMaxValue,
|
|
137
|
+
getPercentValue: getPercentValue,
|
|
138
|
+
isThumbEditable: isThumbEditable,
|
|
139
|
+
setThumbEditable: setThumbEditable,
|
|
140
|
+
incrementThumb: incrementThumb,
|
|
141
|
+
decrementThumb: decrementThumb,
|
|
142
|
+
step: step,
|
|
143
|
+
pageSize: pageSize,
|
|
144
|
+
orientation: orientation,
|
|
145
|
+
isDisabled: isDisabled
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
function $28f99e3e86e6ec45$var$replaceIndex(array, index, value) {
|
|
149
|
+
if (array[index] === value) return array;
|
|
150
|
+
return [
|
|
151
|
+
...array.slice(0, index),
|
|
152
|
+
value,
|
|
153
|
+
...array.slice(index + 1)
|
|
154
|
+
];
|
|
155
|
+
}
|
|
156
|
+
function $28f99e3e86e6ec45$var$convertValue(value) {
|
|
157
|
+
if (value == null) return undefined;
|
|
158
|
+
return Array.isArray(value) ? value : [
|
|
159
|
+
value
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
function $28f99e3e86e6ec45$var$createOnChange(value, defaultValue, onChange) {
|
|
163
|
+
return (newValue)=>{
|
|
164
|
+
if (typeof value === "number" || typeof defaultValue === "number") onChange === null || onChange === void 0 ? void 0 : onChange(newValue[0]);
|
|
165
|
+
else onChange === null || onChange === void 0 ? void 0 : onChange(newValue);
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
export {$28f99e3e86e6ec45$export$e5fda3247f5d67f9 as useSliderState};
|
|
171
|
+
//# sourceMappingURL=useSliderState.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;AAAA;;;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,kBAAkB;QAClB,cAAc;QACd,iBAAiB;QACjB,iBAAiB,CAAC,QAAkB,gBAAgB,MAAM,CAAC,MAAM;yBACjE;QACA,oBAAoB,CAAC,QAAkB,kBAAkB,MAAM,CAAC,MAAM;2BACtE;0BACA;0BACA;yBACA;yBACA;0BACA;wBACA;wBACA;cACA;kBACA;qBACA;oBACA;IACF;AACF;AAEA,SAAS,mCAAgB,KAAU,EAAE,KAAa,EAAE,KAAQ;IAC1D,IAAI,KAAK,CAAC,MAAM,KAAK,OACnB,OAAO;IAGT,OAAO;WAAI,MAAM,KAAK,CAAC,GAAG;QAAQ;WAAU,MAAM,KAAK,CAAC,QAAQ;KAAG;AACrE;AAEA,SAAS,mCAAa,KAAwB;IAC5C,IAAI,SAAS,MACX,OAAO;IAGT,OAAO,MAAM,OAAO,CAAC,SAAS,QAAQ;QAAC;KAAM;AAC/C;AAEA,SAAS,qCAAe,KAAK,EAAE,YAAY,EAAE,QAAQ;IACnD,OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,qBAAA,+BAAA,SAAW,QAAQ,CAAC,EAAE;aAEtB,qBAAA,+BAAA,SAAW;IAEf;AACF","sources":["packages/@react-stately/slider/src/useSliderState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {clamp, snapValueToStep, useControlledState} from '@react-stately/utils';\nimport {Orientation} from '@react-types/shared';\nimport {SliderProps} from '@react-types/slider';\nimport {useCallback, useMemo, useRef, useState} from 'react';\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[],\n /**\n * Get the value for the specified thumb.\n * @param index\n */\n getThumbValue(index: number): number,\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void,\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void,\n\n /**\n * Whether the specific thumb is being dragged.\n * @param index\n */\n isThumbDragging(index: number): boolean,\n /**\n * Set is dragging on the specified thumb.\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void,\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined,\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n * @param index\n */\n setFocusedThumb(index: number | undefined): void,\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n * @param index\n */\n getThumbPercent(index: number): number,\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n * @param index\n */\n getValuePercent(value: number): number,\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n * @param index\n */\n getThumbValueLabel(index: number): string,\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n * @param index\n */\n getFormattedValue(value: number): string,\n\n /**\n * Returns the min allowed value for the specified thumb.\n * @param index\n */\n getThumbMinValue(index: number): number,\n\n /**\n * Returns the max allowed value for the specified thumb.\n * @param index\n */\n getThumbMaxValue(index: number): number,\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n * @param percent\n */\n getPercentValue(percent: number): number,\n\n /**\n * Returns if the specified thumb is editable.\n * @param index\n */\n isThumbEditable(index: number): boolean,\n\n /**\n * Set the specified thumb's editable state.\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void,\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void,\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void,\n\n /**\n * The step amount for the slider.\n */\n readonly step: number,\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number,\n\n /** The orientation of the slider. */\n readonly orientation: Orientation,\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n * @param props\n */\nexport function useSliderState<T extends number | number[]>(props: SliderStateOptions<T>): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback((values: number[]) => values?.map((val, idx) => {\n let min = idx === 0 ? minValue : val[idx - 1];\n let max = idx === values.length - 1 ? maxValue : val[idx + 1];\n return snapValueToStep(val, min, max, step);\n }), [minValue, maxValue, step]);\n\n let value = useMemo(() => restrictValues(convertValue(props.value)), [props.value]);\n let defaultValue = useMemo(() => restrictValues(convertValue(props.defaultValue) ?? [minValue]), [props.defaultValue, minValue]);\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(\n value,\n defaultValue,\n onChange\n );\n const [isDraggings, setDraggingsState] = useState<boolean[]>(new Array(values.length).fill(false));\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n function getFormattedValue(value: number) {\n return formatter.format(value);\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"useSliderState.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/slider",
|
|
3
|
-
"version": "3.5.3-nightly.
|
|
3
|
+
"version": "3.5.3-nightly.4560+72775d272",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -22,9 +22,9 @@
|
|
|
22
22
|
"url": "https://github.com/adobe/react-spectrum"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@react-stately/utils": "3.0.0-nightly.
|
|
26
|
-
"@react-types/shared": "3.0.0-nightly.
|
|
27
|
-
"@react-types/slider": "3.7.2-nightly.
|
|
25
|
+
"@react-stately/utils": "3.0.0-nightly.2848+72775d272",
|
|
26
|
+
"@react-types/shared": "3.0.0-nightly.2848+72775d272",
|
|
27
|
+
"@react-types/slider": "3.7.2-nightly.4560+72775d272",
|
|
28
28
|
"@swc/helpers": "^0.5.0"
|
|
29
29
|
},
|
|
30
30
|
"peerDependencies": {
|
|
@@ -33,5 +33,5 @@
|
|
|
33
33
|
"publishConfig": {
|
|
34
34
|
"access": "public"
|
|
35
35
|
},
|
|
36
|
-
"gitHead": "
|
|
36
|
+
"gitHead": "72775d272be04cfa32d26ec821bc68513d92a72b"
|
|
37
37
|
}
|