@react-stately/utils 3.5.0 → 3.5.2
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/main.js +54 -43
- package/dist/main.js.map +1 -1
- package/dist/module.js +50 -29
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +2 -3
package/dist/main.js
CHANGED
|
@@ -1,82 +1,95 @@
|
|
|
1
1
|
var $kC0mY$react = require("react");
|
|
2
2
|
|
|
3
|
-
function $parcel$exportWildcard(dest, source) {
|
|
4
|
-
Object.keys(source).forEach(function(key) {
|
|
5
|
-
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
6
|
-
return;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
Object.defineProperty(dest, key, {
|
|
10
|
-
enumerable: true,
|
|
11
|
-
get: function get() {
|
|
12
|
-
return source[key];
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
return dest;
|
|
18
|
-
}
|
|
19
3
|
function $parcel$export(e, n, v, s) {
|
|
20
4
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
21
5
|
}
|
|
22
|
-
var $8d8fdfab47455712$exports = {};
|
|
23
|
-
|
|
24
|
-
$parcel$export($8d8fdfab47455712$exports, "useControlledState", () => $8d8fdfab47455712$export$40bfa8c7b0832715);
|
|
25
6
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
7
|
+
$parcel$export(module.exports, "useControlledState", () => $8d8fdfab47455712$export$40bfa8c7b0832715);
|
|
8
|
+
$parcel$export(module.exports, "clamp", () => $ac8e4d4816275668$export$7d15b64cf5a3a4c4);
|
|
9
|
+
$parcel$export(module.exports, "snapValueToStep", () => $ac8e4d4816275668$export$cb6e0bb50bc19463);
|
|
10
|
+
$parcel$export(module.exports, "toFixedNumber", () => $ac8e4d4816275668$export$b6268554fba451f);
|
|
11
|
+
/*
|
|
12
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
13
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
14
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
15
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
16
|
+
*
|
|
17
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
18
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
19
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
20
|
+
* governing permissions and limitations under the License.
|
|
21
|
+
*/ /*
|
|
22
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
23
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
24
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
25
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
26
|
+
*
|
|
27
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
28
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
29
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
30
|
+
* governing permissions and limitations under the License.
|
|
31
|
+
*/
|
|
32
|
+
function $8d8fdfab47455712$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
33
|
+
let [stateValue, setStateValue] = (0, $kC0mY$react.useState)(value || defaultValue);
|
|
34
|
+
let ref = (0, $kC0mY$react.useRef)(value !== undefined);
|
|
29
35
|
let wasControlled = ref.current;
|
|
30
|
-
let isControlled =
|
|
36
|
+
let isControlled = value !== undefined;
|
|
31
37
|
// Internal state reference for useCallback
|
|
32
|
-
let stateRef = $kC0mY$react.useRef(stateValue);
|
|
33
|
-
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ?
|
|
38
|
+
let stateRef = (0, $kC0mY$react.useRef)(stateValue);
|
|
39
|
+
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
|
|
34
40
|
ref.current = isControlled;
|
|
35
|
-
let setValue = $kC0mY$react.useCallback((
|
|
41
|
+
let setValue = (0, $kC0mY$react.useCallback)((value, ...args)=>{
|
|
36
42
|
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
37
43
|
if (onChange) {
|
|
38
44
|
if (!Object.is(stateRef.current, value)) onChange(value, ...onChangeArgs);
|
|
39
45
|
}
|
|
40
46
|
if (!isControlled) stateRef.current = value;
|
|
41
47
|
};
|
|
42
|
-
if (typeof
|
|
43
|
-
console.warn(
|
|
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");
|
|
44
50
|
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
45
51
|
// when someone using useControlledState calls setControlledState(myFunc)
|
|
46
52
|
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
47
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
|
|
48
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
|
|
49
55
|
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
50
|
-
let interceptedValue =
|
|
56
|
+
let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);
|
|
51
57
|
onChangeCaller(interceptedValue, ...args);
|
|
52
58
|
if (!isControlled) return interceptedValue;
|
|
53
59
|
return oldValue;
|
|
54
60
|
};
|
|
55
61
|
setStateValue(updateFunction);
|
|
56
62
|
} else {
|
|
57
|
-
if (!isControlled) setStateValue(
|
|
58
|
-
onChangeCaller(
|
|
63
|
+
if (!isControlled) setStateValue(value);
|
|
64
|
+
onChangeCaller(value, ...args);
|
|
59
65
|
}
|
|
60
66
|
}, [
|
|
61
67
|
isControlled,
|
|
62
68
|
onChange
|
|
63
69
|
]);
|
|
64
70
|
// If a controlled component's value prop changes, we need to update stateRef
|
|
65
|
-
if (isControlled) stateRef.current =
|
|
66
|
-
else
|
|
71
|
+
if (isControlled) stateRef.current = value;
|
|
72
|
+
else value = stateValue;
|
|
67
73
|
return [
|
|
68
|
-
|
|
74
|
+
value,
|
|
69
75
|
setValue
|
|
70
76
|
];
|
|
71
77
|
}
|
|
72
78
|
|
|
73
79
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
+
/*
|
|
81
|
+
* Copyright 2020 Adobe. All rights reserved.
|
|
82
|
+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
83
|
+
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
84
|
+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
85
|
+
*
|
|
86
|
+
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
87
|
+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
88
|
+
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
89
|
+
* governing permissions and limitations under the License.
|
|
90
|
+
*/ /**
|
|
91
|
+
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
92
|
+
*/ function $ac8e4d4816275668$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
80
93
|
let newValue = Math.min(Math.max(value, min), max);
|
|
81
94
|
return newValue;
|
|
82
95
|
}
|
|
@@ -89,7 +102,7 @@ function $ac8e4d4816275668$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
|
89
102
|
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
90
103
|
// correct floating point behavior by rounding to step precision
|
|
91
104
|
let string = step.toString();
|
|
92
|
-
let index = string.indexOf(
|
|
105
|
+
let index = string.indexOf(".");
|
|
93
106
|
let precision = index >= 0 ? string.length - index : 0;
|
|
94
107
|
if (precision > 0) {
|
|
95
108
|
let pow = Math.pow(10, precision);
|
|
@@ -103,8 +116,6 @@ function $ac8e4d4816275668$export$b6268554fba451f(value, digits, base = 10) {
|
|
|
103
116
|
}
|
|
104
117
|
|
|
105
118
|
|
|
106
|
-
$parcel$exportWildcard(module.exports, $8d8fdfab47455712$exports);
|
|
107
|
-
$parcel$exportWildcard(module.exports, $ac8e4d4816275668$exports);
|
|
108
119
|
|
|
109
120
|
|
|
110
121
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC,GAED;AAEO,SAAS,0CACd,KAAQ,EACR,YAAe,EACf,QAA4C,EACF;IAC1C,IAAI,CAAC,YAAY,cAAc,GAAG,CAAA,GAAA,qBAAO,EAAE,SAAS;IACpD,IAAI,MAAM,CAAA,GAAA,mBAAK,EAAE,UAAU;IAC3B,IAAI,gBAAgB,IAAI,OAAO;IAC/B,IAAI,eAAe,UAAU;IAC7B,2CAA2C;IAC3C,IAAI,WAAW,CAAA,GAAA,mBAAK,EAAE;IACtB,IAAI,kBAAkB,cACpB,QAAQ,IAAI,CAAC,CAAC,+BAA+B,EAAE,gBAAgB,eAAe,cAAc,CAAC,IAAI,EAAE,eAAe,eAAe,cAAc,CAAC,CAAC,CAAC;IAGpJ,IAAI,OAAO,GAAG;IAEd,IAAI,WAAW,CAAA,GAAA,wBAAU,EAAE,CAAC,OAAO,GAAG,OAAS;QAC7C,IAAI,iBAAiB,CAAC,OAAO,GAAG,eAAiB;YAC/C,IAAI,UACF;gBAAA,IAAI,CAAC,OAAO,EAAE,CAAC,SAAS,OAAO,EAAE,QAC/B,SAAS,UAAU;YACrB,CACD;YACD,IAAI,CAAC,cACH,SAAS,OAAO,GAAG;QAEvB;QAEA,IAAI,OAAO,UAAU,YAAY;Y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,KAAK,CAAC,MAAM,QAAQ;IAG1C,gEAAgE;IAChE,IAAI,SAAS,KAAK,QAAQ;IAC1B,IAAI,QAAQ,OAAO,OAAO,CAAC;IAC3B,IAAI,YAAY,SAAS,IAAI,OAAO,MAAM,GAAG,QAAQ,CAAC;IAEtD,IAAI,YAAY,GAAG;QACjB,IAAI,MAAM,KAAK,GAAG,CAAC,IAAI;QACvB,eAAe,KAAK,KAAK,CAAC,eAAe,OAAO;IAClD,CAAC;IAED,OAAO;AACT;AAGO,SAAS,yCAAc,KAAa,EAAE,MAAc,EAAE,OAAe,EAAE,EAAU;IACtF,MAAM,MAAM,KAAK,GAAG,CAAC,MAAM;IAE3B,OAAO,KAAK,KAAK,CAAC,QAAQ,OAAO;AACnC;","sources":["packages/@react-stately/utils/src/index.ts","packages/@react-stately/utils/src/useControlledState.ts","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 */\nexport {useControlledState} from './useControlledState';\nexport {clamp, snapValueToStep, toFixedNumber} from './number';\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 {useCallback, useRef, useState} from 'react';\n\nexport function useControlledState<T>(\n value: T,\n defaultValue: T,\n onChange: (value: T, ...args: any[]) => void\n): [T, (value: T, ...args: any[]) => void] {\n let [stateValue, setStateValue] = useState(value || defaultValue);\n let ref = useRef(value !== undefined);\n let wasControlled = ref.current;\n let isControlled = value !== undefined;\n // Internal state reference for useCallback\n let stateRef = useRef(stateValue);\n if (wasControlled !== isControlled) {\n console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);\n }\n\n ref.current = isControlled;\n\n let setValue = useCallback((value, ...args) => {\n let onChangeCaller = (value, ...onChangeArgs) => {\n if (onChange) {\n if (!Object.is(stateRef.current, value)) {\n onChange(value, ...onChangeArgs);\n }\n }\n if (!isControlled) {\n stateRef.current = 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 ? stateRef.current : 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, onChange]);\n\n // If a controlled component's value prop changes, we need to update stateRef\n if (isControlled) {\n stateRef.current = value;\n } else {\n value = stateValue;\n }\n\n return [value, setValue];\n}\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\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 snapValueToStep(value: number, min: number, max: number, step: number): number {\n let remainder = ((value - (isNaN(min) ? 0 : min)) % step);\n let snappedValue = Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder;\n\n if (!isNaN(min)) {\n if (snappedValue < min) {\n snappedValue = min;\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = min + Math.floor((max - min) / step) * step;\n }\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(max / step) * step;\n }\n\n // correct floating point behavior by rounding to step precision\n let string = step.toString();\n let index = string.indexOf('.');\n let precision = index >= 0 ? string.length - index : 0;\n\n if (precision > 0) {\n let pow = Math.pow(10, precision);\n snappedValue = Math.round(snappedValue * pow) / pow;\n }\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":"main.js.map"}
|
package/dist/module.js
CHANGED
|
@@ -1,66 +1,87 @@
|
|
|
1
1
|
import {useState as $6imuh$useState, useRef as $6imuh$useRef, useCallback as $6imuh$useCallback} from "react";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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);
|
|
13
27
|
let wasControlled = ref.current;
|
|
14
|
-
let isControlled =
|
|
28
|
+
let isControlled = value !== undefined;
|
|
15
29
|
// Internal state reference for useCallback
|
|
16
|
-
let stateRef = $6imuh$useRef(stateValue);
|
|
17
|
-
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ?
|
|
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"}.`);
|
|
18
32
|
ref.current = isControlled;
|
|
19
|
-
let setValue = $6imuh$useCallback((
|
|
33
|
+
let setValue = (0, $6imuh$useCallback)((value, ...args)=>{
|
|
20
34
|
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
21
35
|
if (onChange) {
|
|
22
36
|
if (!Object.is(stateRef.current, value)) onChange(value, ...onChangeArgs);
|
|
23
37
|
}
|
|
24
38
|
if (!isControlled) stateRef.current = value;
|
|
25
39
|
};
|
|
26
|
-
if (typeof
|
|
27
|
-
console.warn(
|
|
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");
|
|
28
42
|
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
29
43
|
// when someone using useControlledState calls setControlledState(myFunc)
|
|
30
44
|
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
31
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
|
|
32
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
|
|
33
47
|
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
34
|
-
let interceptedValue =
|
|
48
|
+
let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);
|
|
35
49
|
onChangeCaller(interceptedValue, ...args);
|
|
36
50
|
if (!isControlled) return interceptedValue;
|
|
37
51
|
return oldValue;
|
|
38
52
|
};
|
|
39
53
|
setStateValue(updateFunction);
|
|
40
54
|
} else {
|
|
41
|
-
if (!isControlled) setStateValue(
|
|
42
|
-
onChangeCaller(
|
|
55
|
+
if (!isControlled) setStateValue(value);
|
|
56
|
+
onChangeCaller(value, ...args);
|
|
43
57
|
}
|
|
44
58
|
}, [
|
|
45
59
|
isControlled,
|
|
46
60
|
onChange
|
|
47
61
|
]);
|
|
48
62
|
// If a controlled component's value prop changes, we need to update stateRef
|
|
49
|
-
if (isControlled) stateRef.current =
|
|
50
|
-
else
|
|
63
|
+
if (isControlled) stateRef.current = value;
|
|
64
|
+
else value = stateValue;
|
|
51
65
|
return [
|
|
52
|
-
|
|
66
|
+
value,
|
|
53
67
|
setValue
|
|
54
68
|
];
|
|
55
69
|
}
|
|
56
70
|
|
|
57
71
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
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) {
|
|
64
85
|
let newValue = Math.min(Math.max(value, min), max);
|
|
65
86
|
return newValue;
|
|
66
87
|
}
|
|
@@ -73,7 +94,7 @@ function $9446cca9a3875146$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
|
73
94
|
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
74
95
|
// correct floating point behavior by rounding to step precision
|
|
75
96
|
let string = step.toString();
|
|
76
|
-
let index = string.indexOf(
|
|
97
|
+
let index = string.indexOf(".");
|
|
77
98
|
let precision = index >= 0 ? string.length - index : 0;
|
|
78
99
|
if (precision > 0) {
|
|
79
100
|
let pow = Math.pow(10, precision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AAE,OAAe,EAAE,EAAU;IACtF,MAAM,MAAM,KAAK,GAAG,CAAC,MAAM;IAE3B,OAAO,KAAK,KAAK,CAAC,QAAQ,OAAO;AACnC;","sources":["packages/@react-stately/utils/src/index.ts","packages/@react-stately/utils/src/useControlledState.ts","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 */\nexport {useControlledState} from './useControlledState';\nexport {clamp, snapValueToStep, toFixedNumber} from './number';\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 {useCallback, useRef, useState} from 'react';\n\nexport function useControlledState<T>(\n value: T,\n defaultValue: T,\n onChange: (value: T, ...args: any[]) => void\n): [T, (value: T, ...args: any[]) => void] {\n let [stateValue, setStateValue] = useState(value || defaultValue);\n let ref = useRef(value !== undefined);\n let wasControlled = ref.current;\n let isControlled = value !== undefined;\n // Internal state reference for useCallback\n let stateRef = useRef(stateValue);\n if (wasControlled !== isControlled) {\n console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);\n }\n\n ref.current = isControlled;\n\n let setValue = useCallback((value, ...args) => {\n let onChangeCaller = (value, ...onChangeArgs) => {\n if (onChange) {\n if (!Object.is(stateRef.current, value)) {\n onChange(value, ...onChangeArgs);\n }\n }\n if (!isControlled) {\n stateRef.current = 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 ? stateRef.current : 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, onChange]);\n\n // If a controlled component's value prop changes, we need to update stateRef\n if (isControlled) {\n stateRef.current = value;\n } else {\n value = stateValue;\n }\n\n return [value, setValue];\n}\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\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 snapValueToStep(value: number, min: number, max: number, step: number): number {\n let remainder = ((value - (isNaN(min) ? 0 : min)) % step);\n let snappedValue = Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder;\n\n if (!isNaN(min)) {\n if (snappedValue < min) {\n snappedValue = min;\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = min + Math.floor((max - min) / step) * step;\n }\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(max / step) * step;\n }\n\n // correct floating point behavior by rounding to step precision\n let string = step.toString();\n let index = string.indexOf('.');\n let precision = index >= 0 ? string.length - index : 0;\n\n if (precision > 0) {\n let pow = Math.pow(10, precision);\n snappedValue = Math.round(snappedValue * pow) / pow;\n }\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":"module.js.map"}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAcA,mCAAmC,CAAC,EAClC,KAAK,EAAE,CAAC,EACR,YAAY,EAAE,CAAC,EACf,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAC3C,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,CAyDzC;AC/DD;;GAEG;AACH,sBAAsB,KAAK,EAAE,MAAM,EAAE,GAAG,GAAE,MAAkB,EAAE,GAAG,GAAE,MAAiB,GAAG,MAAM,CAG5F;AAED,gCAAgC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CA2B7F;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 */\
|
|
1
|
+
{"mappings":"AAcA,mCAAmC,CAAC,EAClC,KAAK,EAAE,CAAC,EACR,YAAY,EAAE,CAAC,EACf,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAC3C,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,CAyDzC;AC/DD;;GAEG;AACH,sBAAsB,KAAK,EAAE,MAAM,EAAE,GAAG,GAAE,MAAkB,EAAE,GAAG,GAAE,MAAiB,GAAG,MAAM,CAG5F;AAED,gCAAgC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CA2B7F;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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-stately/utils",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.2",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@
|
|
23
|
+
"@swc/helpers": "^0.4.14"
|
|
24
24
|
},
|
|
25
25
|
"publishConfig": {
|
|
26
26
|
"access": "public"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "5480d76bd815e239366f92852c76b6831ad2a4fd"
|
|
29
29
|
}
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,5 @@
|
|
|
9
9
|
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
|
-
|
|
13
|
-
export
|
|
14
|
-
export * from './number';
|
|
12
|
+
export {useControlledState} from './useControlledState';
|
|
13
|
+
export {clamp, snapValueToStep, toFixedNumber} from './number';
|