@react-stately/utils 3.9.1 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/import.mjs +2 -105
- package/dist/main.js +6 -109
- package/dist/main.js.map +1 -1
- package/dist/module.js +2 -105
- package/dist/module.js.map +1 -1
- package/dist/number.main.js +55 -0
- package/dist/number.main.js.map +1 -0
- package/dist/number.mjs +48 -0
- package/dist/number.module.js +48 -0
- package/dist/number.module.js.map +1 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/useControlledState.main.js +74 -0
- package/dist/useControlledState.main.js.map +1 -0
- package/dist/useControlledState.mjs +69 -0
- package/dist/useControlledState.module.js +69 -0
- package/dist/useControlledState.module.js.map +1 -0
- package/package.json +2 -2
- package/src/number.ts +17 -12
package/dist/import.mjs
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {useControlledState as $458b0a5536c1a7cf$export$40bfa8c7b0832715} from "./useControlledState.mjs";
|
|
2
|
+
import {clamp as $9446cca9a3875146$export$7d15b64cf5a3a4c4, snapValueToStep as $9446cca9a3875146$export$cb6e0bb50bc19463, toFixedNumber as $9446cca9a3875146$export$b6268554fba451f} from "./number.mjs";
|
|
2
3
|
|
|
3
4
|
/*
|
|
4
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
-
* governing permissions and limitations under the License.
|
|
13
|
-
*/ /*
|
|
14
5
|
* Copyright 2020 Adobe. All rights reserved.
|
|
15
6
|
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
16
7
|
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
@@ -21,100 +12,6 @@ import {useState as $6imuh$useState, useRef as $6imuh$useRef, useEffect as $6imu
|
|
|
21
12
|
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
22
13
|
* governing permissions and limitations under the License.
|
|
23
14
|
*/
|
|
24
|
-
function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
25
|
-
let [stateValue, setStateValue] = (0, $6imuh$useState)(value || defaultValue);
|
|
26
|
-
let isControlledRef = (0, $6imuh$useRef)(value !== undefined);
|
|
27
|
-
let isControlled = value !== undefined;
|
|
28
|
-
(0, $6imuh$useEffect)(()=>{
|
|
29
|
-
let wasControlled = isControlledRef.current;
|
|
30
|
-
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
|
|
31
|
-
isControlledRef.current = isControlled;
|
|
32
|
-
}, [
|
|
33
|
-
isControlled
|
|
34
|
-
]);
|
|
35
|
-
let currentValue = isControlled ? value : stateValue;
|
|
36
|
-
let setValue = (0, $6imuh$useCallback)((value, ...args)=>{
|
|
37
|
-
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
38
|
-
if (onChange) {
|
|
39
|
-
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
|
|
40
|
-
}
|
|
41
|
-
if (!isControlled) // If uncontrolled, mutate the currentValue local variable so that
|
|
42
|
-
// calling setState multiple times with the same value only emits onChange once.
|
|
43
|
-
// We do not use a ref for this because we specifically _do_ want the value to
|
|
44
|
-
// reset every render, and assigning to a ref in render breaks aborted suspended renders.
|
|
45
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
46
|
-
currentValue = value;
|
|
47
|
-
};
|
|
48
|
-
if (typeof value === "function") {
|
|
49
|
-
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
50
|
-
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
51
|
-
// when someone using useControlledState calls setControlledState(myFunc)
|
|
52
|
-
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
53
|
-
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning
|
|
54
|
-
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same
|
|
55
|
-
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
56
|
-
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
57
|
-
onChangeCaller(interceptedValue, ...args);
|
|
58
|
-
if (!isControlled) return interceptedValue;
|
|
59
|
-
return oldValue;
|
|
60
|
-
};
|
|
61
|
-
setStateValue(updateFunction);
|
|
62
|
-
} else {
|
|
63
|
-
if (!isControlled) setStateValue(value);
|
|
64
|
-
onChangeCaller(value, ...args);
|
|
65
|
-
}
|
|
66
|
-
}, [
|
|
67
|
-
isControlled,
|
|
68
|
-
currentValue,
|
|
69
|
-
onChange
|
|
70
|
-
]);
|
|
71
|
-
return [
|
|
72
|
-
currentValue,
|
|
73
|
-
setValue
|
|
74
|
-
];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
/*
|
|
79
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
80
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
81
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
82
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
83
|
-
*
|
|
84
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
85
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
86
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
87
|
-
* governing permissions and limitations under the License.
|
|
88
|
-
*/ /**
|
|
89
|
-
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
90
|
-
*/ function $9446cca9a3875146$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
91
|
-
let newValue = Math.min(Math.max(value, min), max);
|
|
92
|
-
return newValue;
|
|
93
|
-
}
|
|
94
|
-
function $9446cca9a3875146$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
95
|
-
min = Number(min);
|
|
96
|
-
max = Number(max);
|
|
97
|
-
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
98
|
-
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
|
|
99
|
-
if (!isNaN(min)) {
|
|
100
|
-
if (snappedValue < min) snappedValue = min;
|
|
101
|
-
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
|
|
102
|
-
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
103
|
-
// correct floating point behavior by rounding to step precision
|
|
104
|
-
let string = step.toString();
|
|
105
|
-
let index = string.indexOf(".");
|
|
106
|
-
let precision = index >= 0 ? string.length - index : 0;
|
|
107
|
-
if (precision > 0) {
|
|
108
|
-
let pow = Math.pow(10, precision);
|
|
109
|
-
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
110
|
-
}
|
|
111
|
-
return snappedValue;
|
|
112
|
-
}
|
|
113
|
-
function $9446cca9a3875146$export$b6268554fba451f(value, digits, base = 10) {
|
|
114
|
-
const pow = Math.pow(base, digits);
|
|
115
|
-
return Math.round(value * pow) / pow;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
15
|
|
|
119
16
|
|
|
120
17
|
|
package/dist/main.js
CHANGED
|
@@ -1,25 +1,16 @@
|
|
|
1
|
-
var $
|
|
1
|
+
var $8d8fdfab47455712$exports = require("./useControlledState.main.js");
|
|
2
|
+
var $ac8e4d4816275668$exports = require("./number.main.js");
|
|
2
3
|
|
|
3
4
|
|
|
4
5
|
function $parcel$export(e, n, v, s) {
|
|
5
6
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
6
7
|
}
|
|
7
8
|
|
|
8
|
-
$parcel$export(module.exports, "useControlledState", () => $8d8fdfab47455712$
|
|
9
|
-
$parcel$export(module.exports, "clamp", () => $ac8e4d4816275668$
|
|
10
|
-
$parcel$export(module.exports, "snapValueToStep", () => $ac8e4d4816275668$
|
|
11
|
-
$parcel$export(module.exports, "toFixedNumber", () => $ac8e4d4816275668$
|
|
9
|
+
$parcel$export(module.exports, "useControlledState", () => $8d8fdfab47455712$exports.useControlledState);
|
|
10
|
+
$parcel$export(module.exports, "clamp", () => $ac8e4d4816275668$exports.clamp);
|
|
11
|
+
$parcel$export(module.exports, "snapValueToStep", () => $ac8e4d4816275668$exports.snapValueToStep);
|
|
12
|
+
$parcel$export(module.exports, "toFixedNumber", () => $ac8e4d4816275668$exports.toFixedNumber);
|
|
12
13
|
/*
|
|
13
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
14
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
15
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
16
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
17
|
-
*
|
|
18
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
19
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
20
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
21
|
-
* governing permissions and limitations under the License.
|
|
22
|
-
*/ /*
|
|
23
14
|
* Copyright 2020 Adobe. All rights reserved.
|
|
24
15
|
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
25
16
|
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
@@ -30,100 +21,6 @@ $parcel$export(module.exports, "toFixedNumber", () => $ac8e4d4816275668$export$b
|
|
|
30
21
|
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
31
22
|
* governing permissions and limitations under the License.
|
|
32
23
|
*/
|
|
33
|
-
function $8d8fdfab47455712$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
34
|
-
let [stateValue, setStateValue] = (0, $kC0mY$react.useState)(value || defaultValue);
|
|
35
|
-
let isControlledRef = (0, $kC0mY$react.useRef)(value !== undefined);
|
|
36
|
-
let isControlled = value !== undefined;
|
|
37
|
-
(0, $kC0mY$react.useEffect)(()=>{
|
|
38
|
-
let wasControlled = isControlledRef.current;
|
|
39
|
-
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
|
|
40
|
-
isControlledRef.current = isControlled;
|
|
41
|
-
}, [
|
|
42
|
-
isControlled
|
|
43
|
-
]);
|
|
44
|
-
let currentValue = isControlled ? value : stateValue;
|
|
45
|
-
let setValue = (0, $kC0mY$react.useCallback)((value, ...args)=>{
|
|
46
|
-
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
47
|
-
if (onChange) {
|
|
48
|
-
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
|
|
49
|
-
}
|
|
50
|
-
if (!isControlled) // If uncontrolled, mutate the currentValue local variable so that
|
|
51
|
-
// calling setState multiple times with the same value only emits onChange once.
|
|
52
|
-
// We do not use a ref for this because we specifically _do_ want the value to
|
|
53
|
-
// reset every render, and assigning to a ref in render breaks aborted suspended renders.
|
|
54
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
55
|
-
currentValue = value;
|
|
56
|
-
};
|
|
57
|
-
if (typeof value === "function") {
|
|
58
|
-
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
59
|
-
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
60
|
-
// when someone using useControlledState calls setControlledState(myFunc)
|
|
61
|
-
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
62
|
-
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning
|
|
63
|
-
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same
|
|
64
|
-
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
65
|
-
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
66
|
-
onChangeCaller(interceptedValue, ...args);
|
|
67
|
-
if (!isControlled) return interceptedValue;
|
|
68
|
-
return oldValue;
|
|
69
|
-
};
|
|
70
|
-
setStateValue(updateFunction);
|
|
71
|
-
} else {
|
|
72
|
-
if (!isControlled) setStateValue(value);
|
|
73
|
-
onChangeCaller(value, ...args);
|
|
74
|
-
}
|
|
75
|
-
}, [
|
|
76
|
-
isControlled,
|
|
77
|
-
currentValue,
|
|
78
|
-
onChange
|
|
79
|
-
]);
|
|
80
|
-
return [
|
|
81
|
-
currentValue,
|
|
82
|
-
setValue
|
|
83
|
-
];
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
/*
|
|
88
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
89
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
90
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
91
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
92
|
-
*
|
|
93
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
94
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
95
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
96
|
-
* governing permissions and limitations under the License.
|
|
97
|
-
*/ /**
|
|
98
|
-
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
99
|
-
*/ function $ac8e4d4816275668$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
100
|
-
let newValue = Math.min(Math.max(value, min), max);
|
|
101
|
-
return newValue;
|
|
102
|
-
}
|
|
103
|
-
function $ac8e4d4816275668$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
104
|
-
min = Number(min);
|
|
105
|
-
max = Number(max);
|
|
106
|
-
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
107
|
-
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
|
|
108
|
-
if (!isNaN(min)) {
|
|
109
|
-
if (snappedValue < min) snappedValue = min;
|
|
110
|
-
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
|
|
111
|
-
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
112
|
-
// correct floating point behavior by rounding to step precision
|
|
113
|
-
let string = step.toString();
|
|
114
|
-
let index = string.indexOf(".");
|
|
115
|
-
let precision = index >= 0 ? string.length - index : 0;
|
|
116
|
-
if (precision > 0) {
|
|
117
|
-
let pow = Math.pow(10, precision);
|
|
118
|
-
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
119
|
-
}
|
|
120
|
-
return snappedValue;
|
|
121
|
-
}
|
|
122
|
-
function $ac8e4d4816275668$export$b6268554fba451f(value, digits, base = 10) {
|
|
123
|
-
const pow = Math.pow(base, digits);
|
|
124
|
-
return Math.round(value * pow) / pow;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
24
|
|
|
128
25
|
|
|
129
26
|
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;;;;;;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/utils/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 */\nexport {useControlledState} from './useControlledState';\nexport {clamp, snapValueToStep, toFixedNumber} from './number';\n"],"names":[],"version":3,"file":"main.js.map"}
|
package/dist/module.js
CHANGED
|
@@ -1,16 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {useControlledState as $458b0a5536c1a7cf$export$40bfa8c7b0832715} from "./useControlledState.module.js";
|
|
2
|
+
import {clamp as $9446cca9a3875146$export$7d15b64cf5a3a4c4, snapValueToStep as $9446cca9a3875146$export$cb6e0bb50bc19463, toFixedNumber as $9446cca9a3875146$export$b6268554fba451f} from "./number.module.js";
|
|
2
3
|
|
|
3
4
|
/*
|
|
4
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
*
|
|
9
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
-
* governing permissions and limitations under the License.
|
|
13
|
-
*/ /*
|
|
14
5
|
* Copyright 2020 Adobe. All rights reserved.
|
|
15
6
|
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
16
7
|
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
@@ -21,100 +12,6 @@ import {useState as $6imuh$useState, useRef as $6imuh$useRef, useEffect as $6imu
|
|
|
21
12
|
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
22
13
|
* governing permissions and limitations under the License.
|
|
23
14
|
*/
|
|
24
|
-
function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
25
|
-
let [stateValue, setStateValue] = (0, $6imuh$useState)(value || defaultValue);
|
|
26
|
-
let isControlledRef = (0, $6imuh$useRef)(value !== undefined);
|
|
27
|
-
let isControlled = value !== undefined;
|
|
28
|
-
(0, $6imuh$useEffect)(()=>{
|
|
29
|
-
let wasControlled = isControlledRef.current;
|
|
30
|
-
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
|
|
31
|
-
isControlledRef.current = isControlled;
|
|
32
|
-
}, [
|
|
33
|
-
isControlled
|
|
34
|
-
]);
|
|
35
|
-
let currentValue = isControlled ? value : stateValue;
|
|
36
|
-
let setValue = (0, $6imuh$useCallback)((value, ...args)=>{
|
|
37
|
-
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
38
|
-
if (onChange) {
|
|
39
|
-
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
|
|
40
|
-
}
|
|
41
|
-
if (!isControlled) // If uncontrolled, mutate the currentValue local variable so that
|
|
42
|
-
// calling setState multiple times with the same value only emits onChange once.
|
|
43
|
-
// We do not use a ref for this because we specifically _do_ want the value to
|
|
44
|
-
// reset every render, and assigning to a ref in render breaks aborted suspended renders.
|
|
45
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
46
|
-
currentValue = value;
|
|
47
|
-
};
|
|
48
|
-
if (typeof value === "function") {
|
|
49
|
-
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
50
|
-
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
51
|
-
// when someone using useControlledState calls setControlledState(myFunc)
|
|
52
|
-
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
53
|
-
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning
|
|
54
|
-
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same
|
|
55
|
-
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
56
|
-
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
57
|
-
onChangeCaller(interceptedValue, ...args);
|
|
58
|
-
if (!isControlled) return interceptedValue;
|
|
59
|
-
return oldValue;
|
|
60
|
-
};
|
|
61
|
-
setStateValue(updateFunction);
|
|
62
|
-
} else {
|
|
63
|
-
if (!isControlled) setStateValue(value);
|
|
64
|
-
onChangeCaller(value, ...args);
|
|
65
|
-
}
|
|
66
|
-
}, [
|
|
67
|
-
isControlled,
|
|
68
|
-
currentValue,
|
|
69
|
-
onChange
|
|
70
|
-
]);
|
|
71
|
-
return [
|
|
72
|
-
currentValue,
|
|
73
|
-
setValue
|
|
74
|
-
];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
/*
|
|
79
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
80
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
81
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
82
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
83
|
-
*
|
|
84
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
85
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
86
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
87
|
-
* governing permissions and limitations under the License.
|
|
88
|
-
*/ /**
|
|
89
|
-
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
90
|
-
*/ function $9446cca9a3875146$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
91
|
-
let newValue = Math.min(Math.max(value, min), max);
|
|
92
|
-
return newValue;
|
|
93
|
-
}
|
|
94
|
-
function $9446cca9a3875146$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
95
|
-
min = Number(min);
|
|
96
|
-
max = Number(max);
|
|
97
|
-
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
98
|
-
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
|
|
99
|
-
if (!isNaN(min)) {
|
|
100
|
-
if (snappedValue < min) snappedValue = min;
|
|
101
|
-
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
|
|
102
|
-
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
103
|
-
// correct floating point behavior by rounding to step precision
|
|
104
|
-
let string = step.toString();
|
|
105
|
-
let index = string.indexOf(".");
|
|
106
|
-
let precision = index >= 0 ? string.length - index : 0;
|
|
107
|
-
if (precision > 0) {
|
|
108
|
-
let pow = Math.pow(10, precision);
|
|
109
|
-
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
110
|
-
}
|
|
111
|
-
return snappedValue;
|
|
112
|
-
}
|
|
113
|
-
function $9446cca9a3875146$export$b6268554fba451f(value, digits, base = 10) {
|
|
114
|
-
const pow = Math.pow(base, digits);
|
|
115
|
-
return Math.round(value * pow) / pow;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
15
|
|
|
119
16
|
|
|
120
17
|
|
package/dist/module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;AAAA;;;;;;;;;;CAUC","sources":["packages/@react-stately/utils/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 */\nexport {useControlledState} from './useControlledState';\nexport {clamp, snapValueToStep, toFixedNumber} from './number';\n"],"names":[],"version":3,"file":"module.js.map"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
|
|
2
|
+
function $parcel$export(e, n, v, s) {
|
|
3
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
$parcel$export(module.exports, "clamp", () => $ac8e4d4816275668$export$7d15b64cf5a3a4c4);
|
|
7
|
+
$parcel$export(module.exports, "snapValueToStep", () => $ac8e4d4816275668$export$cb6e0bb50bc19463);
|
|
8
|
+
$parcel$export(module.exports, "toFixedNumber", () => $ac8e4d4816275668$export$b6268554fba451f);
|
|
9
|
+
/*
|
|
10
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
11
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
13
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
16
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
17
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
18
|
+
* governing permissions and limitations under the License.
|
|
19
|
+
*/ /**
|
|
20
|
+
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
21
|
+
*/ function $ac8e4d4816275668$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
22
|
+
let newValue = Math.min(Math.max(value, min), max);
|
|
23
|
+
return newValue;
|
|
24
|
+
}
|
|
25
|
+
function $ac8e4d4816275668$export$e1a7b8e69ef6c52f(value, step) {
|
|
26
|
+
let roundedValue = value;
|
|
27
|
+
let stepString = step.toString();
|
|
28
|
+
let pointIndex = stepString.indexOf(".");
|
|
29
|
+
let precision = pointIndex >= 0 ? stepString.length - pointIndex : 0;
|
|
30
|
+
if (precision > 0) {
|
|
31
|
+
let pow = Math.pow(10, precision);
|
|
32
|
+
roundedValue = Math.round(roundedValue * pow) / pow;
|
|
33
|
+
}
|
|
34
|
+
return roundedValue;
|
|
35
|
+
}
|
|
36
|
+
function $ac8e4d4816275668$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
37
|
+
min = Number(min);
|
|
38
|
+
max = Number(max);
|
|
39
|
+
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
40
|
+
let snappedValue = $ac8e4d4816275668$export$e1a7b8e69ef6c52f(Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder, step);
|
|
41
|
+
if (!isNaN(min)) {
|
|
42
|
+
if (snappedValue < min) snappedValue = min;
|
|
43
|
+
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor($ac8e4d4816275668$export$e1a7b8e69ef6c52f((max - min) / step, step)) * step;
|
|
44
|
+
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor($ac8e4d4816275668$export$e1a7b8e69ef6c52f(max / step, step)) * step;
|
|
45
|
+
// correct floating point behavior by rounding to step precision
|
|
46
|
+
snappedValue = $ac8e4d4816275668$export$e1a7b8e69ef6c52f(snappedValue, step);
|
|
47
|
+
return snappedValue;
|
|
48
|
+
}
|
|
49
|
+
function $ac8e4d4816275668$export$b6268554fba451f(value, digits, base = 10) {
|
|
50
|
+
const pow = Math.pow(base, digits);
|
|
51
|
+
return Math.round(value * pow) / pow;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
//# sourceMappingURL=number.main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC,GAED;;CAEC,GACM,SAAS,0CAAM,KAAa,EAAE,MAAc,CAAC,QAAQ,EAAE,MAAc,QAAQ;IAClF,IAAI,WAAW,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,OAAO,MAAM;IAC9C,OAAO;AACT;AAEO,SAAS,0CAAqB,KAAa,EAAE,IAAY;IAC9D,IAAI,eAAe;IACnB,IAAI,aAAa,KAAK,QAAQ;IAC9B,IAAI,aAAa,WAAW,OAAO,CAAC;IACpC,IAAI,YAAY,cAAc,IAAI,WAAW,MAAM,GAAG,aAAa;IACnE,IAAI,YAAY,GAAG;QACjB,IAAI,MAAM,KAAK,GAAG,CAAC,IAAI;QACvB,eAAe,KAAK,KAAK,CAAC,eAAe,OAAO;IAClD;IACA,OAAO;AACT;AAEO,SAAS,0CAAgB,KAAa,EAAE,GAAuB,EAAE,GAAuB,EAAE,IAAY;IAC3G,MAAM,OAAO;IACb,MAAM,OAAO;IACb,IAAI,YAAa,AAAC,CAAA,QAAS,CAAA,MAAM,OAAO,IAAI,GAAE,CAAC,IAAK;IACpD,IAAI,eAAe,0CAAqB,KAAK,GAAG,CAAC,aAAa,KAAK,OAC/D,QAAQ,KAAK,IAAI,CAAC,aAAc,CAAA,OAAO,KAAK,GAAG,CAAC,UAAS,IACzD,QAAQ,WAAW;IAEvB,IAAI,CAAC,MAAM,MAAM;QACf,IAAI,eAAe,KACjB,eAAe;aACV,IAAI,CAAC,MAAM,QAAQ,eAAe,KACvC,eAAe,MAAM,KAAK,KAAK,CAAC,0CAAqB,AAAC,CAAA,MAAM,GAAE,IAAK,MAAM,SAAS;IAEtF,OAAO,IAAI,CAAC,MAAM,QAAQ,eAAe,KACvC,eAAe,KAAK,KAAK,CAAC,0CAAqB,MAAM,MAAM,SAAS;IAGtE,gEAAgE;IAChE,eAAe,0CAAqB,cAAc;IAElD,OAAO;AACT;AAGO,SAAS,yCAAc,KAAa,EAAE,MAAc,EAAE,OAAe,EAAE;IAC5E,MAAM,MAAM,KAAK,GAAG,CAAC,MAAM;IAE3B,OAAO,KAAK,KAAK,CAAC,QAAQ,OAAO;AACnC","sources":["packages/@react-stately/utils/src/number.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\n/**\n * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.\n */\nexport function clamp(value: number, min: number = -Infinity, max: number = Infinity): number {\n let newValue = Math.min(Math.max(value, min), max);\n return newValue;\n}\n\nexport function roundToStepPrecision(value: number, step: number) {\n let roundedValue = value;\n let stepString = step.toString();\n let pointIndex = stepString.indexOf('.');\n let precision = pointIndex >= 0 ? stepString.length - pointIndex : 0;\n if (precision > 0) {\n let pow = Math.pow(10, precision);\n roundedValue = Math.round(roundedValue * pow) / pow;\n }\n return roundedValue;\n}\n\nexport function snapValueToStep(value: number, min: number | undefined, max: number | undefined, step: number): number {\n min = Number(min);\n max = Number(max);\n let remainder = ((value - (isNaN(min) ? 0 : min)) % step);\n let snappedValue = roundToStepPrecision(Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder, step);\n\n if (!isNaN(min)) {\n if (snappedValue < min) {\n snappedValue = min;\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = min + Math.floor(roundToStepPrecision((max - min) / step, step)) * step;\n }\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(roundToStepPrecision(max / step, step)) * step;\n }\n\n // correct floating point behavior by rounding to step precision\n snappedValue = roundToStepPrecision(snappedValue, step);\n\n return snappedValue;\n}\n\n/* Takes a value and rounds off to the number of digits. */\nexport function toFixedNumber(value: number, digits: number, base: number = 10): number {\n const pow = Math.pow(base, digits);\n\n return Math.round(value * pow) / pow;\n}\n"],"names":[],"version":3,"file":"number.main.js.map"}
|
package/dist/number.mjs
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/ /**
|
|
12
|
+
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
13
|
+
*/ function $9446cca9a3875146$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
14
|
+
let newValue = Math.min(Math.max(value, min), max);
|
|
15
|
+
return newValue;
|
|
16
|
+
}
|
|
17
|
+
function $9446cca9a3875146$export$e1a7b8e69ef6c52f(value, step) {
|
|
18
|
+
let roundedValue = value;
|
|
19
|
+
let stepString = step.toString();
|
|
20
|
+
let pointIndex = stepString.indexOf(".");
|
|
21
|
+
let precision = pointIndex >= 0 ? stepString.length - pointIndex : 0;
|
|
22
|
+
if (precision > 0) {
|
|
23
|
+
let pow = Math.pow(10, precision);
|
|
24
|
+
roundedValue = Math.round(roundedValue * pow) / pow;
|
|
25
|
+
}
|
|
26
|
+
return roundedValue;
|
|
27
|
+
}
|
|
28
|
+
function $9446cca9a3875146$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
29
|
+
min = Number(min);
|
|
30
|
+
max = Number(max);
|
|
31
|
+
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
32
|
+
let snappedValue = $9446cca9a3875146$export$e1a7b8e69ef6c52f(Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder, step);
|
|
33
|
+
if (!isNaN(min)) {
|
|
34
|
+
if (snappedValue < min) snappedValue = min;
|
|
35
|
+
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor($9446cca9a3875146$export$e1a7b8e69ef6c52f((max - min) / step, step)) * step;
|
|
36
|
+
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor($9446cca9a3875146$export$e1a7b8e69ef6c52f(max / step, step)) * step;
|
|
37
|
+
// correct floating point behavior by rounding to step precision
|
|
38
|
+
snappedValue = $9446cca9a3875146$export$e1a7b8e69ef6c52f(snappedValue, step);
|
|
39
|
+
return snappedValue;
|
|
40
|
+
}
|
|
41
|
+
function $9446cca9a3875146$export$b6268554fba451f(value, digits, base = 10) {
|
|
42
|
+
const pow = Math.pow(base, digits);
|
|
43
|
+
return Math.round(value * pow) / pow;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
export {$9446cca9a3875146$export$7d15b64cf5a3a4c4 as clamp, $9446cca9a3875146$export$e1a7b8e69ef6c52f as roundToStepPrecision, $9446cca9a3875146$export$cb6e0bb50bc19463 as snapValueToStep, $9446cca9a3875146$export$b6268554fba451f as toFixedNumber};
|
|
48
|
+
//# sourceMappingURL=number.mjs.map
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
3
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
5
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
6
|
+
*
|
|
7
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
8
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
9
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
|
+
* governing permissions and limitations under the License.
|
|
11
|
+
*/ /**
|
|
12
|
+
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
13
|
+
*/ function $9446cca9a3875146$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
14
|
+
let newValue = Math.min(Math.max(value, min), max);
|
|
15
|
+
return newValue;
|
|
16
|
+
}
|
|
17
|
+
function $9446cca9a3875146$export$e1a7b8e69ef6c52f(value, step) {
|
|
18
|
+
let roundedValue = value;
|
|
19
|
+
let stepString = step.toString();
|
|
20
|
+
let pointIndex = stepString.indexOf(".");
|
|
21
|
+
let precision = pointIndex >= 0 ? stepString.length - pointIndex : 0;
|
|
22
|
+
if (precision > 0) {
|
|
23
|
+
let pow = Math.pow(10, precision);
|
|
24
|
+
roundedValue = Math.round(roundedValue * pow) / pow;
|
|
25
|
+
}
|
|
26
|
+
return roundedValue;
|
|
27
|
+
}
|
|
28
|
+
function $9446cca9a3875146$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
29
|
+
min = Number(min);
|
|
30
|
+
max = Number(max);
|
|
31
|
+
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
32
|
+
let snappedValue = $9446cca9a3875146$export$e1a7b8e69ef6c52f(Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder, step);
|
|
33
|
+
if (!isNaN(min)) {
|
|
34
|
+
if (snappedValue < min) snappedValue = min;
|
|
35
|
+
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor($9446cca9a3875146$export$e1a7b8e69ef6c52f((max - min) / step, step)) * step;
|
|
36
|
+
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor($9446cca9a3875146$export$e1a7b8e69ef6c52f(max / step, step)) * step;
|
|
37
|
+
// correct floating point behavior by rounding to step precision
|
|
38
|
+
snappedValue = $9446cca9a3875146$export$e1a7b8e69ef6c52f(snappedValue, step);
|
|
39
|
+
return snappedValue;
|
|
40
|
+
}
|
|
41
|
+
function $9446cca9a3875146$export$b6268554fba451f(value, digits, base = 10) {
|
|
42
|
+
const pow = Math.pow(base, digits);
|
|
43
|
+
return Math.round(value * pow) / pow;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
export {$9446cca9a3875146$export$7d15b64cf5a3a4c4 as clamp, $9446cca9a3875146$export$e1a7b8e69ef6c52f as roundToStepPrecision, $9446cca9a3875146$export$cb6e0bb50bc19463 as snapValueToStep, $9446cca9a3875146$export$b6268554fba451f as toFixedNumber};
|
|
48
|
+
//# sourceMappingURL=number.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":"AAAA;;;;;;;;;;CAUC,GAED;;CAEC,GACM,SAAS,0CAAM,KAAa,EAAE,MAAc,CAAC,QAAQ,EAAE,MAAc,QAAQ;IAClF,IAAI,WAAW,KAAK,GAAG,CAAC,KAAK,GAAG,CAAC,OAAO,MAAM;IAC9C,OAAO;AACT;AAEO,SAAS,0CAAqB,KAAa,EAAE,IAAY;IAC9D,IAAI,eAAe;IACnB,IAAI,aAAa,KAAK,QAAQ;IAC9B,IAAI,aAAa,WAAW,OAAO,CAAC;IACpC,IAAI,YAAY,cAAc,IAAI,WAAW,MAAM,GAAG,aAAa;IACnE,IAAI,YAAY,GAAG;QACjB,IAAI,MAAM,KAAK,GAAG,CAAC,IAAI;QACvB,eAAe,KAAK,KAAK,CAAC,eAAe,OAAO;IAClD;IACA,OAAO;AACT;AAEO,SAAS,0CAAgB,KAAa,EAAE,GAAuB,EAAE,GAAuB,EAAE,IAAY;IAC3G,MAAM,OAAO;IACb,MAAM,OAAO;IACb,IAAI,YAAa,AAAC,CAAA,QAAS,CAAA,MAAM,OAAO,IAAI,GAAE,CAAC,IAAK;IACpD,IAAI,eAAe,0CAAqB,KAAK,GAAG,CAAC,aAAa,KAAK,OAC/D,QAAQ,KAAK,IAAI,CAAC,aAAc,CAAA,OAAO,KAAK,GAAG,CAAC,UAAS,IACzD,QAAQ,WAAW;IAEvB,IAAI,CAAC,MAAM,MAAM;QACf,IAAI,eAAe,KACjB,eAAe;aACV,IAAI,CAAC,MAAM,QAAQ,eAAe,KACvC,eAAe,MAAM,KAAK,KAAK,CAAC,0CAAqB,AAAC,CAAA,MAAM,GAAE,IAAK,MAAM,SAAS;IAEtF,OAAO,IAAI,CAAC,MAAM,QAAQ,eAAe,KACvC,eAAe,KAAK,KAAK,CAAC,0CAAqB,MAAM,MAAM,SAAS;IAGtE,gEAAgE;IAChE,eAAe,0CAAqB,cAAc;IAElD,OAAO;AACT;AAGO,SAAS,yCAAc,KAAa,EAAE,MAAc,EAAE,OAAe,EAAE;IAC5E,MAAM,MAAM,KAAK,GAAG,CAAC,MAAM;IAE3B,OAAO,KAAK,KAAK,CAAC,QAAQ,OAAO;AACnC","sources":["packages/@react-stately/utils/src/number.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\n/**\n * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.\n */\nexport function clamp(value: number, min: number = -Infinity, max: number = Infinity): number {\n let newValue = Math.min(Math.max(value, min), max);\n return newValue;\n}\n\nexport function roundToStepPrecision(value: number, step: number) {\n let roundedValue = value;\n let stepString = step.toString();\n let pointIndex = stepString.indexOf('.');\n let precision = pointIndex >= 0 ? stepString.length - pointIndex : 0;\n if (precision > 0) {\n let pow = Math.pow(10, precision);\n roundedValue = Math.round(roundedValue * pow) / pow;\n }\n return roundedValue;\n}\n\nexport function snapValueToStep(value: number, min: number | undefined, max: number | undefined, step: number): number {\n min = Number(min);\n max = Number(max);\n let remainder = ((value - (isNaN(min) ? 0 : min)) % step);\n let snappedValue = roundToStepPrecision(Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder, step);\n\n if (!isNaN(min)) {\n if (snappedValue < min) {\n snappedValue = min;\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = min + Math.floor(roundToStepPrecision((max - min) / step, step)) * step;\n }\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(roundToStepPrecision(max / step, step)) * step;\n }\n\n // correct floating point behavior by rounding to step precision\n snappedValue = roundToStepPrecision(snappedValue, step);\n\n return snappedValue;\n}\n\n/* Takes a value and rounds off to the number of digits. */\nexport function toFixedNumber(value: number, digits: number, base: number = 10): number {\n const pow = Math.pow(base, digits);\n\n return Math.round(value * pow) / pow;\n}\n"],"names":[],"version":3,"file":"number.module.js.map"}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAcA,mCAAmC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,GAAG,SAAS,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,CAAC;AAChM,mCAAmC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,GAAG,SAAS,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,CAAC;ACHhM;;GAEG;AACH,sBAAsB,KAAK,EAAE,MAAM,EAAE,GAAG,GAAE,MAAkB,EAAE,GAAG,GAAE,MAAiB,GAAG,MAAM,CAG5F;
|
|
1
|
+
{"mappings":"AAcA,mCAAmC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,GAAG,SAAS,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,CAAC;AAChM,mCAAmC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,GAAG,SAAS,EAAE,YAAY,EAAE,OAAO,CAAC,CAAC,EAAE,SAAS,CAAC,EAAE,QAAQ,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,CAAC,CAAC;ACHhM;;GAEG;AACH,sBAAsB,KAAK,EAAE,MAAM,EAAE,GAAG,GAAE,MAAkB,EAAE,GAAG,GAAE,MAAiB,GAAG,MAAM,CAG5F;AAcD,gCAAgC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,EAAE,GAAG,EAAE,MAAM,GAAG,SAAS,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAsBrH;AAGD,8BAA8B,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,IAAI,GAAE,MAAW,GAAG,MAAM,CAItF","sources":["packages/@react-stately/utils/src/packages/@react-stately/utils/src/useControlledState.ts","packages/@react-stately/utils/src/packages/@react-stately/utils/src/number.ts","packages/@react-stately/utils/src/packages/@react-stately/utils/src/index.ts","packages/@react-stately/utils/src/index.ts"],"sourcesContent":[null,null,null,"/*\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 */\nexport {useControlledState} from './useControlledState';\nexport {clamp, snapValueToStep, toFixedNumber} from './number';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
var $ecn6s$react = require("react");
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
function $parcel$export(e, n, v, s) {
|
|
5
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
$parcel$export(module.exports, "useControlledState", () => $8d8fdfab47455712$export$40bfa8c7b0832715);
|
|
9
|
+
/*
|
|
10
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
11
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
12
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
13
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
14
|
+
*
|
|
15
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
16
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
17
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
18
|
+
* governing permissions and limitations under the License.
|
|
19
|
+
*/
|
|
20
|
+
function $8d8fdfab47455712$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
21
|
+
let [stateValue, setStateValue] = (0, $ecn6s$react.useState)(value || defaultValue);
|
|
22
|
+
let isControlledRef = (0, $ecn6s$react.useRef)(value !== undefined);
|
|
23
|
+
let isControlled = value !== undefined;
|
|
24
|
+
(0, $ecn6s$react.useEffect)(()=>{
|
|
25
|
+
let wasControlled = isControlledRef.current;
|
|
26
|
+
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
|
|
27
|
+
isControlledRef.current = isControlled;
|
|
28
|
+
}, [
|
|
29
|
+
isControlled
|
|
30
|
+
]);
|
|
31
|
+
let currentValue = isControlled ? value : stateValue;
|
|
32
|
+
let setValue = (0, $ecn6s$react.useCallback)((value, ...args)=>{
|
|
33
|
+
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
34
|
+
if (onChange) {
|
|
35
|
+
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
|
|
36
|
+
}
|
|
37
|
+
if (!isControlled) // If uncontrolled, mutate the currentValue local variable so that
|
|
38
|
+
// calling setState multiple times with the same value only emits onChange once.
|
|
39
|
+
// We do not use a ref for this because we specifically _do_ want the value to
|
|
40
|
+
// reset every render, and assigning to a ref in render breaks aborted suspended renders.
|
|
41
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
42
|
+
currentValue = value;
|
|
43
|
+
};
|
|
44
|
+
if (typeof value === "function") {
|
|
45
|
+
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
46
|
+
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
47
|
+
// when someone using useControlledState calls setControlledState(myFunc)
|
|
48
|
+
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
49
|
+
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning
|
|
50
|
+
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same
|
|
51
|
+
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
52
|
+
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
53
|
+
onChangeCaller(interceptedValue, ...args);
|
|
54
|
+
if (!isControlled) return interceptedValue;
|
|
55
|
+
return oldValue;
|
|
56
|
+
};
|
|
57
|
+
setStateValue(updateFunction);
|
|
58
|
+
} else {
|
|
59
|
+
if (!isControlled) setStateValue(value);
|
|
60
|
+
onChangeCaller(value, ...args);
|
|
61
|
+
}
|
|
62
|
+
}, [
|
|
63
|
+
isControlled,
|
|
64
|
+
currentValue,
|
|
65
|
+
onChange
|
|
66
|
+
]);
|
|
67
|
+
return [
|
|
68
|
+
currentValue,
|
|
69
|
+
setValue
|
|
70
|
+
];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
//# sourceMappingURL=useControlledState.main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;;;;;;;AAAA;;;;;;;;;;CAUC;AAMM,SAAS,0CAA6B,KAAQ,EAAE,YAAe,EAAE,QAAyC;IAC/G,IAAI,CAAC,YAAY,cAAc,GAAG,CAAA,GAAA,qBAAO,EAAE,SAAS;IAEpD,IAAI,kBAAkB,CAAA,GAAA,mBAAK,EAAE,UAAU;IACvC,IAAI,eAAe,UAAU;IAC7B,CAAA,GAAA,sBAAQ,EAAE;QACR,IAAI,gBAAgB,gBAAgB,OAAO;QAC3C,IAAI,kBAAkB,cACpB,QAAQ,IAAI,CAAC,CAAC,+BAA+B,EAAE,gBAAgB,eAAe,eAAe,IAAI,EAAE,eAAe,eAAe,eAAe,CAAC,CAAC;QAEpJ,gBAAgB,OAAO,GAAG;IAC5B,GAAG;QAAC;KAAa;IAEjB,IAAI,eAAe,eAAe,QAAQ;IAC1C,IAAI,WAAW,CAAA,GAAA,wBAAU,EAAE,CAAC,OAAO,GAAG;QACpC,IAAI,iBAAiB,CAAC,OAAO,GAAG;YAC9B,IAAI,UACF;gBAAA,IAAI,CAAC,OAAO,EAAE,CAAC,cAAc,QAC3B,SAAS,UAAU;YACrB;YAEF,IAAI,CAAC,cACH,kEAAkE;YAClE,gFAAgF;YAChF,8EAA8E;YAC9E,yFAAyF;YACzF,uDAAuD;YACvD,eAAe;QAEnB;QAEA,IAAI,OAAO,UAAU,YAAY;YAC/B,QAAQ,IAAI,CAAC;YACb,oGAAoG;YACpG,yEAAyE;YACzE,kIAAkI;YAClI,iKAAiK;YACjK,yIAAyI;YACzI,IAAI,iBAAiB,CAAC,UAAU,GAAG;gBACjC,IAAI,mBAAmB,MAAM,eAAe,eAAe,aAAa;gBACxE,eAAe,qBAAqB;gBACpC,IAAI,CAAC,cACH,OAAO;gBAET,OAAO;YACT;YACA,cAAc;QAChB,OAAO;YACL,IAAI,CAAC,cACH,cAAc;YAEhB,eAAe,UAAU;QAC3B;IACF,GAAG;QAAC;QAAc;QAAc;KAAS;IAEzC,OAAO;QAAC;QAAc;KAAS;AACjC","sources":["packages/@react-stately/utils/src/useControlledState.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 {useCallback, useEffect, useRef, useState} from 'react';\n\nexport function useControlledState<T, C = T>(value: Exclude<T, undefined>, defaultValue: Exclude<T, undefined> | undefined, onChange?: (v: C, ...args: any[]) => void): [T, (value: T) => void];\nexport function useControlledState<T, C = T>(value: Exclude<T, undefined> | undefined, defaultValue: Exclude<T, undefined>, onChange?: (v: C, ...args: any[]) => void): [T, (value: T) => void];\nexport function useControlledState<T, C = T>(value: T, defaultValue: T, onChange?: (v: C, ...args: any[]) => void): [T, (value: T) => void] {\n let [stateValue, setStateValue] = useState(value || defaultValue);\n\n let isControlledRef = useRef(value !== undefined);\n let isControlled = value !== undefined;\n useEffect(() => {\n let wasControlled = isControlledRef.current;\n if (wasControlled !== isControlled) {\n console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);\n }\n isControlledRef.current = isControlled;\n }, [isControlled]);\n\n let currentValue = isControlled ? value : stateValue;\n let setValue = useCallback((value, ...args) => {\n let onChangeCaller = (value, ...onChangeArgs) => {\n if (onChange) {\n if (!Object.is(currentValue, value)) {\n onChange(value, ...onChangeArgs);\n }\n }\n if (!isControlled) {\n // If uncontrolled, mutate the currentValue local variable so that\n // calling setState multiple times with the same value only emits onChange once.\n // We do not use a ref for this because we specifically _do_ want the value to\n // reset every render, and assigning to a ref in render breaks aborted suspended renders.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n currentValue = value;\n }\n };\n\n if (typeof value === 'function') {\n console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');\n // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates\n // when someone using useControlledState calls setControlledState(myFunc)\n // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc\n // if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning\n // otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same\n let updateFunction = (oldValue, ...functionArgs) => {\n let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);\n onChangeCaller(interceptedValue, ...args);\n if (!isControlled) {\n return interceptedValue;\n }\n return oldValue;\n };\n setStateValue(updateFunction);\n } else {\n if (!isControlled) {\n setStateValue(value);\n }\n onChangeCaller(value, ...args);\n }\n }, [isControlled, currentValue, onChange]);\n\n return [currentValue, setValue];\n}\n"],"names":[],"version":3,"file":"useControlledState.main.js.map"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {useState as $3whtM$useState, useRef as $3whtM$useRef, useEffect as $3whtM$useEffect, useCallback as $3whtM$useCallback} from "react";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
15
|
+
let [stateValue, setStateValue] = (0, $3whtM$useState)(value || defaultValue);
|
|
16
|
+
let isControlledRef = (0, $3whtM$useRef)(value !== undefined);
|
|
17
|
+
let isControlled = value !== undefined;
|
|
18
|
+
(0, $3whtM$useEffect)(()=>{
|
|
19
|
+
let wasControlled = isControlledRef.current;
|
|
20
|
+
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
|
|
21
|
+
isControlledRef.current = isControlled;
|
|
22
|
+
}, [
|
|
23
|
+
isControlled
|
|
24
|
+
]);
|
|
25
|
+
let currentValue = isControlled ? value : stateValue;
|
|
26
|
+
let setValue = (0, $3whtM$useCallback)((value, ...args)=>{
|
|
27
|
+
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
28
|
+
if (onChange) {
|
|
29
|
+
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
|
|
30
|
+
}
|
|
31
|
+
if (!isControlled) // If uncontrolled, mutate the currentValue local variable so that
|
|
32
|
+
// calling setState multiple times with the same value only emits onChange once.
|
|
33
|
+
// We do not use a ref for this because we specifically _do_ want the value to
|
|
34
|
+
// reset every render, and assigning to a ref in render breaks aborted suspended renders.
|
|
35
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
36
|
+
currentValue = value;
|
|
37
|
+
};
|
|
38
|
+
if (typeof value === "function") {
|
|
39
|
+
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
40
|
+
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
41
|
+
// when someone using useControlledState calls setControlledState(myFunc)
|
|
42
|
+
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
43
|
+
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning
|
|
44
|
+
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same
|
|
45
|
+
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
46
|
+
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
47
|
+
onChangeCaller(interceptedValue, ...args);
|
|
48
|
+
if (!isControlled) return interceptedValue;
|
|
49
|
+
return oldValue;
|
|
50
|
+
};
|
|
51
|
+
setStateValue(updateFunction);
|
|
52
|
+
} else {
|
|
53
|
+
if (!isControlled) setStateValue(value);
|
|
54
|
+
onChangeCaller(value, ...args);
|
|
55
|
+
}
|
|
56
|
+
}, [
|
|
57
|
+
isControlled,
|
|
58
|
+
currentValue,
|
|
59
|
+
onChange
|
|
60
|
+
]);
|
|
61
|
+
return [
|
|
62
|
+
currentValue,
|
|
63
|
+
setValue
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
export {$458b0a5536c1a7cf$export$40bfa8c7b0832715 as useControlledState};
|
|
69
|
+
//# sourceMappingURL=useControlledState.mjs.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {useState as $3whtM$useState, useRef as $3whtM$useRef, useEffect as $3whtM$useEffect, useCallback as $3whtM$useCallback} from "react";
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
5
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
7
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
10
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
11
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
12
|
+
* governing permissions and limitations under the License.
|
|
13
|
+
*/
|
|
14
|
+
function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
15
|
+
let [stateValue, setStateValue] = (0, $3whtM$useState)(value || defaultValue);
|
|
16
|
+
let isControlledRef = (0, $3whtM$useRef)(value !== undefined);
|
|
17
|
+
let isControlled = value !== undefined;
|
|
18
|
+
(0, $3whtM$useEffect)(()=>{
|
|
19
|
+
let wasControlled = isControlledRef.current;
|
|
20
|
+
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
|
|
21
|
+
isControlledRef.current = isControlled;
|
|
22
|
+
}, [
|
|
23
|
+
isControlled
|
|
24
|
+
]);
|
|
25
|
+
let currentValue = isControlled ? value : stateValue;
|
|
26
|
+
let setValue = (0, $3whtM$useCallback)((value, ...args)=>{
|
|
27
|
+
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
28
|
+
if (onChange) {
|
|
29
|
+
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
|
|
30
|
+
}
|
|
31
|
+
if (!isControlled) // If uncontrolled, mutate the currentValue local variable so that
|
|
32
|
+
// calling setState multiple times with the same value only emits onChange once.
|
|
33
|
+
// We do not use a ref for this because we specifically _do_ want the value to
|
|
34
|
+
// reset every render, and assigning to a ref in render breaks aborted suspended renders.
|
|
35
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
36
|
+
currentValue = value;
|
|
37
|
+
};
|
|
38
|
+
if (typeof value === "function") {
|
|
39
|
+
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
40
|
+
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
41
|
+
// when someone using useControlledState calls setControlledState(myFunc)
|
|
42
|
+
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
43
|
+
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning
|
|
44
|
+
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same
|
|
45
|
+
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
46
|
+
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
47
|
+
onChangeCaller(interceptedValue, ...args);
|
|
48
|
+
if (!isControlled) return interceptedValue;
|
|
49
|
+
return oldValue;
|
|
50
|
+
};
|
|
51
|
+
setStateValue(updateFunction);
|
|
52
|
+
} else {
|
|
53
|
+
if (!isControlled) setStateValue(value);
|
|
54
|
+
onChangeCaller(value, ...args);
|
|
55
|
+
}
|
|
56
|
+
}, [
|
|
57
|
+
isControlled,
|
|
58
|
+
currentValue,
|
|
59
|
+
onChange
|
|
60
|
+
]);
|
|
61
|
+
return [
|
|
62
|
+
currentValue,
|
|
63
|
+
setValue
|
|
64
|
+
];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
export {$458b0a5536c1a7cf$export$40bfa8c7b0832715 as useControlledState};
|
|
69
|
+
//# sourceMappingURL=useControlledState.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"mappings":";;AAAA;;;;;;;;;;CAUC;AAMM,SAAS,0CAA6B,KAAQ,EAAE,YAAe,EAAE,QAAyC;IAC/G,IAAI,CAAC,YAAY,cAAc,GAAG,CAAA,GAAA,eAAO,EAAE,SAAS;IAEpD,IAAI,kBAAkB,CAAA,GAAA,aAAK,EAAE,UAAU;IACvC,IAAI,eAAe,UAAU;IAC7B,CAAA,GAAA,gBAAQ,EAAE;QACR,IAAI,gBAAgB,gBAAgB,OAAO;QAC3C,IAAI,kBAAkB,cACpB,QAAQ,IAAI,CAAC,CAAC,+BAA+B,EAAE,gBAAgB,eAAe,eAAe,IAAI,EAAE,eAAe,eAAe,eAAe,CAAC,CAAC;QAEpJ,gBAAgB,OAAO,GAAG;IAC5B,GAAG;QAAC;KAAa;IAEjB,IAAI,eAAe,eAAe,QAAQ;IAC1C,IAAI,WAAW,CAAA,GAAA,kBAAU,EAAE,CAAC,OAAO,GAAG;QACpC,IAAI,iBAAiB,CAAC,OAAO,GAAG;YAC9B,IAAI,UACF;gBAAA,IAAI,CAAC,OAAO,EAAE,CAAC,cAAc,QAC3B,SAAS,UAAU;YACrB;YAEF,IAAI,CAAC,cACH,kEAAkE;YAClE,gFAAgF;YAChF,8EAA8E;YAC9E,yFAAyF;YACzF,uDAAuD;YACvD,eAAe;QAEnB;QAEA,IAAI,OAAO,UAAU,YAAY;YAC/B,QAAQ,IAAI,CAAC;YACb,oGAAoG;YACpG,yEAAyE;YACzE,kIAAkI;YAClI,iKAAiK;YACjK,yIAAyI;YACzI,IAAI,iBAAiB,CAAC,UAAU,GAAG;gBACjC,IAAI,mBAAmB,MAAM,eAAe,eAAe,aAAa;gBACxE,eAAe,qBAAqB;gBACpC,IAAI,CAAC,cACH,OAAO;gBAET,OAAO;YACT;YACA,cAAc;QAChB,OAAO;YACL,IAAI,CAAC,cACH,cAAc;YAEhB,eAAe,UAAU;QAC3B;IACF,GAAG;QAAC;QAAc;QAAc;KAAS;IAEzC,OAAO;QAAC;QAAc;KAAS;AACjC","sources":["packages/@react-stately/utils/src/useControlledState.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 {useCallback, useEffect, useRef, useState} from 'react';\n\nexport function useControlledState<T, C = T>(value: Exclude<T, undefined>, defaultValue: Exclude<T, undefined> | undefined, onChange?: (v: C, ...args: any[]) => void): [T, (value: T) => void];\nexport function useControlledState<T, C = T>(value: Exclude<T, undefined> | undefined, defaultValue: Exclude<T, undefined>, onChange?: (v: C, ...args: any[]) => void): [T, (value: T) => void];\nexport function useControlledState<T, C = T>(value: T, defaultValue: T, onChange?: (v: C, ...args: any[]) => void): [T, (value: T) => void] {\n let [stateValue, setStateValue] = useState(value || defaultValue);\n\n let isControlledRef = useRef(value !== undefined);\n let isControlled = value !== undefined;\n useEffect(() => {\n let wasControlled = isControlledRef.current;\n if (wasControlled !== isControlled) {\n console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);\n }\n isControlledRef.current = isControlled;\n }, [isControlled]);\n\n let currentValue = isControlled ? value : stateValue;\n let setValue = useCallback((value, ...args) => {\n let onChangeCaller = (value, ...onChangeArgs) => {\n if (onChange) {\n if (!Object.is(currentValue, value)) {\n onChange(value, ...onChangeArgs);\n }\n }\n if (!isControlled) {\n // If uncontrolled, mutate the currentValue local variable so that\n // calling setState multiple times with the same value only emits onChange once.\n // We do not use a ref for this because we specifically _do_ want the value to\n // reset every render, and assigning to a ref in render breaks aborted suspended renders.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n currentValue = value;\n }\n };\n\n if (typeof value === 'function') {\n console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');\n // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates\n // when someone using useControlledState calls setControlledState(myFunc)\n // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc\n // if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning\n // otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same\n let updateFunction = (oldValue, ...functionArgs) => {\n let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);\n onChangeCaller(interceptedValue, ...args);\n if (!isControlled) {\n return interceptedValue;\n }\n return oldValue;\n };\n setStateValue(updateFunction);\n } else {\n if (!isControlled) {\n setStateValue(value);\n }\n onChangeCaller(value, ...args);\n }\n }, [isControlled, currentValue, onChange]);\n\n return [currentValue, setValue];\n}\n"],"names":[],"version":3,"file":"useControlledState.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.10.0",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -30,5 +30,5 @@
|
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "f645f29edc1322153fd60af4640cbcab1d992dbd"
|
|
34
34
|
}
|
package/src/number.ts
CHANGED
|
@@ -18,33 +18,38 @@ export function clamp(value: number, min: number = -Infinity, max: number = Infi
|
|
|
18
18
|
return newValue;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
export function roundToStepPrecision(value: number, step: number) {
|
|
22
|
+
let roundedValue = value;
|
|
23
|
+
let stepString = step.toString();
|
|
24
|
+
let pointIndex = stepString.indexOf('.');
|
|
25
|
+
let precision = pointIndex >= 0 ? stepString.length - pointIndex : 0;
|
|
26
|
+
if (precision > 0) {
|
|
27
|
+
let pow = Math.pow(10, precision);
|
|
28
|
+
roundedValue = Math.round(roundedValue * pow) / pow;
|
|
29
|
+
}
|
|
30
|
+
return roundedValue;
|
|
31
|
+
}
|
|
32
|
+
|
|
21
33
|
export function snapValueToStep(value: number, min: number | undefined, max: number | undefined, step: number): number {
|
|
22
34
|
min = Number(min);
|
|
23
35
|
max = Number(max);
|
|
24
36
|
let remainder = ((value - (isNaN(min) ? 0 : min)) % step);
|
|
25
|
-
let snappedValue = Math.abs(remainder) * 2 >= step
|
|
37
|
+
let snappedValue = roundToStepPrecision(Math.abs(remainder) * 2 >= step
|
|
26
38
|
? value + Math.sign(remainder) * (step - Math.abs(remainder))
|
|
27
|
-
: value - remainder;
|
|
39
|
+
: value - remainder, step);
|
|
28
40
|
|
|
29
41
|
if (!isNaN(min)) {
|
|
30
42
|
if (snappedValue < min) {
|
|
31
43
|
snappedValue = min;
|
|
32
44
|
} else if (!isNaN(max) && snappedValue > max) {
|
|
33
|
-
snappedValue = min + Math.floor((max - min) / step) * step;
|
|
45
|
+
snappedValue = min + Math.floor(roundToStepPrecision((max - min) / step, step)) * step;
|
|
34
46
|
}
|
|
35
47
|
} else if (!isNaN(max) && snappedValue > max) {
|
|
36
|
-
snappedValue = Math.floor(max / step) * step;
|
|
48
|
+
snappedValue = Math.floor(roundToStepPrecision(max / step, step)) * step;
|
|
37
49
|
}
|
|
38
50
|
|
|
39
51
|
// correct floating point behavior by rounding to step precision
|
|
40
|
-
|
|
41
|
-
let index = string.indexOf('.');
|
|
42
|
-
let precision = index >= 0 ? string.length - index : 0;
|
|
43
|
-
|
|
44
|
-
if (precision > 0) {
|
|
45
|
-
let pow = Math.pow(10, precision);
|
|
46
|
-
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
47
|
-
}
|
|
52
|
+
snappedValue = roundToStepPrecision(snappedValue, step);
|
|
48
53
|
|
|
49
54
|
return snappedValue;
|
|
50
55
|
}
|