@react-stately/utils 3.6.0 → 3.7.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 +20 -14
- package/dist/main.js +19 -13
- package/dist/main.js.map +1 -1
- package/dist/module.js +20 -14
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/useControlledState.ts +21 -22
package/dist/import.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {useState as $6imuh$useState, useRef as $6imuh$useRef, useCallback as $6imuh$useCallback} from "react";
|
|
1
|
+
import {useState as $6imuh$useState, useRef as $6imuh$useRef, useEffect as $6imuh$useEffect, useCallback as $6imuh$useCallback} from "react";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* Copyright 2020 Adobe. All rights reserved.
|
|
@@ -23,19 +23,27 @@ import {useState as $6imuh$useState, useRef as $6imuh$useRef, useCallback as $6i
|
|
|
23
23
|
*/
|
|
24
24
|
function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
25
25
|
let [stateValue, setStateValue] = (0, $6imuh$useState)(value || defaultValue);
|
|
26
|
-
let
|
|
27
|
-
let wasControlled = ref.current;
|
|
26
|
+
let isControlledRef = (0, $6imuh$useRef)(value !== undefined);
|
|
28
27
|
let isControlled = value !== undefined;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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;
|
|
33
36
|
let setValue = (0, $6imuh$useCallback)((value, ...args)=>{
|
|
34
37
|
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
35
38
|
if (onChange) {
|
|
36
|
-
if (!Object.is(
|
|
39
|
+
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
|
|
37
40
|
}
|
|
38
|
-
if (!isControlled)
|
|
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;
|
|
39
47
|
};
|
|
40
48
|
if (typeof value === "function") {
|
|
41
49
|
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
@@ -45,7 +53,7 @@ function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange
|
|
|
45
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
|
|
46
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
|
|
47
55
|
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
48
|
-
let interceptedValue = value(isControlled ?
|
|
56
|
+
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
49
57
|
onChangeCaller(interceptedValue, ...args);
|
|
50
58
|
if (!isControlled) return interceptedValue;
|
|
51
59
|
return oldValue;
|
|
@@ -57,13 +65,11 @@ function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange
|
|
|
57
65
|
}
|
|
58
66
|
}, [
|
|
59
67
|
isControlled,
|
|
68
|
+
currentValue,
|
|
60
69
|
onChange
|
|
61
70
|
]);
|
|
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
71
|
return [
|
|
66
|
-
|
|
72
|
+
currentValue,
|
|
67
73
|
setValue
|
|
68
74
|
];
|
|
69
75
|
}
|
package/dist/main.js
CHANGED
|
@@ -31,19 +31,27 @@ $parcel$export(module.exports, "toFixedNumber", () => $ac8e4d4816275668$export$b
|
|
|
31
31
|
*/
|
|
32
32
|
function $8d8fdfab47455712$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
33
33
|
let [stateValue, setStateValue] = (0, $kC0mY$react.useState)(value || defaultValue);
|
|
34
|
-
let
|
|
35
|
-
let wasControlled = ref.current;
|
|
34
|
+
let isControlledRef = (0, $kC0mY$react.useRef)(value !== undefined);
|
|
36
35
|
let isControlled = value !== undefined;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
36
|
+
(0, $kC0mY$react.useEffect)(()=>{
|
|
37
|
+
let wasControlled = isControlledRef.current;
|
|
38
|
+
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? "controlled" : "uncontrolled"} to ${isControlled ? "controlled" : "uncontrolled"}.`);
|
|
39
|
+
isControlledRef.current = isControlled;
|
|
40
|
+
}, [
|
|
41
|
+
isControlled
|
|
42
|
+
]);
|
|
43
|
+
let currentValue = isControlled ? value : stateValue;
|
|
41
44
|
let setValue = (0, $kC0mY$react.useCallback)((value, ...args)=>{
|
|
42
45
|
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
43
46
|
if (onChange) {
|
|
44
|
-
if (!Object.is(
|
|
47
|
+
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
|
|
45
48
|
}
|
|
46
|
-
if (!isControlled)
|
|
49
|
+
if (!isControlled) // If uncontrolled, mutate the currentValue local variable so that
|
|
50
|
+
// calling setState multiple times with the same value only emits onChange once.
|
|
51
|
+
// We do not use a ref for this because we specifically _do_ want the value to
|
|
52
|
+
// reset every render, and assigning to a ref in render breaks aborted suspended renders.
|
|
53
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
54
|
+
currentValue = value;
|
|
47
55
|
};
|
|
48
56
|
if (typeof value === "function") {
|
|
49
57
|
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
@@ -53,7 +61,7 @@ function $8d8fdfab47455712$export$40bfa8c7b0832715(value, defaultValue, onChange
|
|
|
53
61
|
// 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
62
|
// 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
63
|
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
56
|
-
let interceptedValue = value(isControlled ?
|
|
64
|
+
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
57
65
|
onChangeCaller(interceptedValue, ...args);
|
|
58
66
|
if (!isControlled) return interceptedValue;
|
|
59
67
|
return oldValue;
|
|
@@ -65,13 +73,11 @@ function $8d8fdfab47455712$export$40bfa8c7b0832715(value, defaultValue, onChange
|
|
|
65
73
|
}
|
|
66
74
|
}, [
|
|
67
75
|
isControlled,
|
|
76
|
+
currentValue,
|
|
68
77
|
onChange
|
|
69
78
|
]);
|
|
70
|
-
// If a controlled component's value prop changes, we need to update stateRef
|
|
71
|
-
if (isControlled) stateRef.current = value;
|
|
72
|
-
else value = stateValue;
|
|
73
79
|
return [
|
|
74
|
-
|
|
80
|
+
currentValue,
|
|
75
81
|
setValue
|
|
76
82
|
];
|
|
77
83
|
}
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC
|
|
1
|
+
{"mappings":";;;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;AAIM,SAAS,0CACd,KAAQ,EACR,YAAe,EACf,QAA4C;IAE5C,IAAI,CAAC,YAAY,cAAc,GAAG,CAAA,GAAA,qBAAO,EAAE,SAAS;I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O;AACT;AAEO,SAAS,0CAAgB,KAAa,EAAE,GAAW,EAAE,GAAW,EAAE,IAAY;IACnF,IAAI,YAAa,AAAC,CAAA,QAAS,CAAA,MAAM,OAAO,IAAI,GAAE,CAAC,IAAK;IACpD,IAAI,eAAe,KAAK,IAAI,aAAa,KAAK,OAC1C,QAAQ,KAAK,KAAK,aAAc,CAAA,OAAO,KAAK,IAAI,UAAS,IACzD,QAAQ;IAEZ,IAAI,CAAC,MAAM,MAAM;QACf,IAAI,eAAe,KACjB,eAAe;aACV,IAAI,CAAC,MAAM,QAAQ,eAAe,KACvC,eAAe,MAAM,KAAK,MAAM,AAAC,CAAA,MAAM,GAAE,IAAK,QAAQ;IAE1D,OAAO,IAAI,CAAC,MAAM,QAAQ,eAAe,KACvC,eAAe,KAAK,MAAM,MAAM,QAAQ;IAG1C,gEAAgE;IAChE,IAAI,SAAS,KAAK;IAClB,IAAI,QAAQ,OAAO,QAAQ;IAC3B,IAAI,YAAY,SAAS,IAAI,OAAO,SAAS,QAAQ;IAErD,IAAI,YAAY,GAAG;QACjB,IAAI,MAAM,KAAK,IAAI,IAAI;QACvB,eAAe,KAAK,MAAM,eAAe,OAAO;IAClD;IAEA,OAAO;AACT;AAGO,SAAS,yCAAc,KAAa,EAAE,MAAc,EAAE,OAAe,EAAE;IAC5E,MAAM,MAAM,KAAK,IAAI,MAAM;IAE3B,OAAO,KAAK,MAAM,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, useEffect, 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\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","/*\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,4 +1,4 @@
|
|
|
1
|
-
import {useState as $6imuh$useState, useRef as $6imuh$useRef, useCallback as $6imuh$useCallback} from "react";
|
|
1
|
+
import {useState as $6imuh$useState, useRef as $6imuh$useRef, useEffect as $6imuh$useEffect, useCallback as $6imuh$useCallback} from "react";
|
|
2
2
|
|
|
3
3
|
/*
|
|
4
4
|
* Copyright 2020 Adobe. All rights reserved.
|
|
@@ -23,19 +23,27 @@ import {useState as $6imuh$useState, useRef as $6imuh$useRef, useCallback as $6i
|
|
|
23
23
|
*/
|
|
24
24
|
function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange) {
|
|
25
25
|
let [stateValue, setStateValue] = (0, $6imuh$useState)(value || defaultValue);
|
|
26
|
-
let
|
|
27
|
-
let wasControlled = ref.current;
|
|
26
|
+
let isControlledRef = (0, $6imuh$useRef)(value !== undefined);
|
|
28
27
|
let isControlled = value !== undefined;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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;
|
|
33
36
|
let setValue = (0, $6imuh$useCallback)((value, ...args)=>{
|
|
34
37
|
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
35
38
|
if (onChange) {
|
|
36
|
-
if (!Object.is(
|
|
39
|
+
if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
|
|
37
40
|
}
|
|
38
|
-
if (!isControlled)
|
|
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;
|
|
39
47
|
};
|
|
40
48
|
if (typeof value === "function") {
|
|
41
49
|
console.warn("We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320");
|
|
@@ -45,7 +53,7 @@ function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange
|
|
|
45
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
|
|
46
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
|
|
47
55
|
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
48
|
-
let interceptedValue = value(isControlled ?
|
|
56
|
+
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
49
57
|
onChangeCaller(interceptedValue, ...args);
|
|
50
58
|
if (!isControlled) return interceptedValue;
|
|
51
59
|
return oldValue;
|
|
@@ -57,13 +65,11 @@ function $458b0a5536c1a7cf$export$40bfa8c7b0832715(value, defaultValue, onChange
|
|
|
57
65
|
}
|
|
58
66
|
}, [
|
|
59
67
|
isControlled,
|
|
68
|
+
currentValue,
|
|
60
69
|
onChange
|
|
61
70
|
]);
|
|
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
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AAM,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, useEffect, 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\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","/*\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,
|
|
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,CAwDzC;AC9DD;;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.
|
|
3
|
+
"version": "3.7.0",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -25,10 +25,10 @@
|
|
|
25
25
|
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@swc/helpers": "^0.
|
|
28
|
+
"@swc/helpers": "^0.5.0"
|
|
29
29
|
},
|
|
30
30
|
"publishConfig": {
|
|
31
31
|
"access": "public"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "504e40e0a50c1b20ed0fb3ba9561a263b6d5565e"
|
|
34
34
|
}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* governing permissions and limitations under the License.
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import {useCallback, useRef, useState} from 'react';
|
|
13
|
+
import {useCallback, useEffect, useRef, useState} from 'react';
|
|
14
14
|
|
|
15
15
|
export function useControlledState<T>(
|
|
16
16
|
value: T,
|
|
@@ -18,26 +18,32 @@ export function useControlledState<T>(
|
|
|
18
18
|
onChange: (value: T, ...args: any[]) => void
|
|
19
19
|
): [T, (value: T, ...args: any[]) => void] {
|
|
20
20
|
let [stateValue, setStateValue] = useState(value || defaultValue);
|
|
21
|
-
let ref = useRef(value !== undefined);
|
|
22
|
-
let wasControlled = ref.current;
|
|
23
|
-
let isControlled = value !== undefined;
|
|
24
|
-
// Internal state reference for useCallback
|
|
25
|
-
let stateRef = useRef(stateValue);
|
|
26
|
-
if (wasControlled !== isControlled) {
|
|
27
|
-
console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);
|
|
28
|
-
}
|
|
29
21
|
|
|
30
|
-
|
|
22
|
+
let isControlledRef = useRef(value !== undefined);
|
|
23
|
+
let isControlled = value !== undefined;
|
|
24
|
+
useEffect(() => {
|
|
25
|
+
let wasControlled = isControlledRef.current;
|
|
26
|
+
if (wasControlled !== isControlled) {
|
|
27
|
+
console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);
|
|
28
|
+
}
|
|
29
|
+
isControlledRef.current = isControlled;
|
|
30
|
+
}, [isControlled]);
|
|
31
31
|
|
|
32
|
+
let currentValue = isControlled ? value : stateValue;
|
|
32
33
|
let setValue = useCallback((value, ...args) => {
|
|
33
34
|
let onChangeCaller = (value, ...onChangeArgs) => {
|
|
34
35
|
if (onChange) {
|
|
35
|
-
if (!Object.is(
|
|
36
|
+
if (!Object.is(currentValue, value)) {
|
|
36
37
|
onChange(value, ...onChangeArgs);
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
if (!isControlled) {
|
|
40
|
-
|
|
41
|
+
// 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;
|
|
41
47
|
}
|
|
42
48
|
};
|
|
43
49
|
|
|
@@ -49,7 +55,7 @@ export function useControlledState<T>(
|
|
|
49
55
|
// 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
56
|
// 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
57
|
let updateFunction = (oldValue, ...functionArgs) => {
|
|
52
|
-
let interceptedValue = value(isControlled ?
|
|
58
|
+
let interceptedValue = value(isControlled ? currentValue : oldValue, ...functionArgs);
|
|
53
59
|
onChangeCaller(interceptedValue, ...args);
|
|
54
60
|
if (!isControlled) {
|
|
55
61
|
return interceptedValue;
|
|
@@ -63,14 +69,7 @@ export function useControlledState<T>(
|
|
|
63
69
|
}
|
|
64
70
|
onChangeCaller(value, ...args);
|
|
65
71
|
}
|
|
66
|
-
}, [isControlled, onChange]);
|
|
67
|
-
|
|
68
|
-
// If a controlled component's value prop changes, we need to update stateRef
|
|
69
|
-
if (isControlled) {
|
|
70
|
-
stateRef.current = value;
|
|
71
|
-
} else {
|
|
72
|
-
value = stateValue;
|
|
73
|
-
}
|
|
72
|
+
}, [isControlled, currentValue, onChange]);
|
|
74
73
|
|
|
75
|
-
return [
|
|
74
|
+
return [currentValue, setValue];
|
|
76
75
|
}
|