@react-stately/utils 3.5.2 → 3.6.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 +114 -0
- package/package.json +7 -2
package/dist/import.mjs
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {useState as $6imuh$useState, useRef as $6imuh$useRef, useCallback as $6imuh$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
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
15
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
16
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
17
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
18
|
+
*
|
|
19
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
20
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
21
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
22
|
+
* governing permissions and limitations under the License.
|
|
23
|
+
*/
|
|
24
|
+
function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
25
|
+
let [stateValue, setStateValue] = (0, $6imuh$useState)(value || defaultValue);
|
|
26
|
+
let ref = (0, $6imuh$useRef)(value !== undefined);
|
|
27
|
+
let wasControlled = ref.current;
|
|
28
|
+
let isControlled = value !== undefined;
|
|
29
|
+
// Internal state reference for useCallback
|
|
30
|
+
let stateRef = (0, $6imuh$useRef)(stateValue);
|
|
31
|
+
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
|
|
32
|
+
ref.current = isControlled;
|
|
33
|
+
let setValue = (0, $6imuh$useCallback)((value, ...args)=>{
|
|
34
|
+
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
35
|
+
if (onChange) {
|
|
36
|
+
if (!Object.is(stateRef.current, value)) onChange(value, ...onChangeArgs);
|
|
37
|
+
}
|
|
38
|
+
if (!isControlled) stateRef.current = value;
|
|
39
|
+
};
|
|
40
|
+
if (typeof value === "function") {
|
|
41
|
+
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
42
|
+
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
43
|
+
// when someone using useControlledState calls setControlledState(myFunc)
|
|
44
|
+
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
45
|
+
// 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
|
|
46
|
+
// 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
|
|
47
|
+
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
48
|
+
let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);
|
|
49
|
+
onChangeCaller(interceptedValue, ...args);
|
|
50
|
+
if (!isControlled) return interceptedValue;
|
|
51
|
+
return oldValue;
|
|
52
|
+
};
|
|
53
|
+
setStateValue(updateFunction);
|
|
54
|
+
} else {
|
|
55
|
+
if (!isControlled) setStateValue(value);
|
|
56
|
+
onChangeCaller(value, ...args);
|
|
57
|
+
}
|
|
58
|
+
}, [
|
|
59
|
+
isControlled,
|
|
60
|
+
onChange
|
|
61
|
+
]);
|
|
62
|
+
// If a controlled component's value prop changes, we need to update stateRef
|
|
63
|
+
if (isControlled) stateRef.current = value;
|
|
64
|
+
else value = stateValue;
|
|
65
|
+
return [
|
|
66
|
+
value,
|
|
67
|
+
setValue
|
|
68
|
+
];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
/*
|
|
73
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
74
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
75
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
76
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
77
|
+
*
|
|
78
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
79
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
80
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
81
|
+
* governing permissions and limitations under the License.
|
|
82
|
+
*/ /**
|
|
83
|
+
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
84
|
+
*/ function $9446cca9a3875146$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
85
|
+
let newValue = Math.min(Math.max(value, min), max);
|
|
86
|
+
return newValue;
|
|
87
|
+
}
|
|
88
|
+
function $9446cca9a3875146$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
89
|
+
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
90
|
+
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
|
|
91
|
+
if (!isNaN(min)) {
|
|
92
|
+
if (snappedValue < min) snappedValue = min;
|
|
93
|
+
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
|
|
94
|
+
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
95
|
+
// correct floating point behavior by rounding to step precision
|
|
96
|
+
let string = step.toString();
|
|
97
|
+
let index = string.indexOf(".");
|
|
98
|
+
let precision = index >= 0 ? string.length - index : 0;
|
|
99
|
+
if (precision > 0) {
|
|
100
|
+
let pow = Math.pow(10, precision);
|
|
101
|
+
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
102
|
+
}
|
|
103
|
+
return snappedValue;
|
|
104
|
+
}
|
|
105
|
+
function $9446cca9a3875146$export$b6268554fba451f(value, digits, base = 10) {
|
|
106
|
+
const pow = Math.pow(base, digits);
|
|
107
|
+
return Math.round(value * pow) / pow;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
export {$458b0a5536c1a7cf$export$40bfa8c7b0832715 as useControlledState, $9446cca9a3875146$export$7d15b64cf5a3a4c4 as clamp, $9446cca9a3875146$export$cb6e0bb50bc19463 as snapValueToStep, $9446cca9a3875146$export$b6268554fba451f as toFixedNumber};
|
|
114
|
+
//# sourceMappingURL=module.js.map
|
package/package.json
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/utils",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
7
7
|
"module": "dist/module.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
"types": "./dist/types.d.ts",
|
|
10
|
+
"import": "./dist/import.mjs",
|
|
11
|
+
"require": "./dist/main.js"
|
|
12
|
+
},
|
|
8
13
|
"types": "dist/types.d.ts",
|
|
9
14
|
"source": "src/index.ts",
|
|
10
15
|
"files": [
|
|
@@ -25,5 +30,5 @@
|
|
|
25
30
|
"publishConfig": {
|
|
26
31
|
"access": "public"
|
|
27
32
|
},
|
|
28
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "a0efee84aa178cb1a202951dfd6d8de02b292307"
|
|
29
34
|
}
|