@react-stately/utils 3.5.2 → 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.
@@ -0,0 +1,120 @@
1
+ import {useState as $6imuh$useState, useRef as $6imuh$useRef, useEffect as $6imuh$useEffect, 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 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
+ let remainder = (value - (isNaN(min) ? 0 : min)) % step;
96
+ let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
97
+ if (!isNaN(min)) {
98
+ if (snappedValue < min) snappedValue = min;
99
+ else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
100
+ } else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
101
+ // correct floating point behavior by rounding to step precision
102
+ let string = step.toString();
103
+ let index = string.indexOf(".");
104
+ let precision = index >= 0 ? string.length - index : 0;
105
+ if (precision > 0) {
106
+ let pow = Math.pow(10, precision);
107
+ snappedValue = Math.round(snappedValue * pow) / pow;
108
+ }
109
+ return snappedValue;
110
+ }
111
+ function $9446cca9a3875146$export$b6268554fba451f(value, digits, base = 10) {
112
+ const pow = Math.pow(base, digits);
113
+ return Math.round(value * pow) / pow;
114
+ }
115
+
116
+
117
+
118
+
119
+ export {$458b0a5536c1a7cf$export$40bfa8c7b0832715 as useControlledState, $9446cca9a3875146$export$7d15b64cf5a3a4c4 as clamp, $9446cca9a3875146$export$cb6e0bb50bc19463 as snapValueToStep, $9446cca9a3875146$export$b6268554fba451f as toFixedNumber};
120
+ //# sourceMappingURL=module.js.map
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 ref = (0, $kC0mY$react.useRef)(value !== undefined);
35
- let wasControlled = ref.current;
34
+ let isControlledRef = (0, $kC0mY$react.useRef)(value !== undefined);
36
35
  let isControlled = value !== undefined;
37
- // Internal state reference for useCallback
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"}.`);
40
- ref.current = isControlled;
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(stateRef.current, value)) onChange(value, ...onChangeArgs);
47
+ if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
45
48
  }
46
- if (!isControlled) stateRef.current = value;
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 ? stateRef.current : oldValue, ...functionArgs);
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
- value,
80
+ currentValue,
75
81
  setValue
76
82
  ];
77
83
  }
package/dist/main.js.map CHANGED
@@ -1 +1 @@
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,O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QACf,IAAI,eAAe,KACjB,eAAe;aACV,IAAI,CAAC,MAAM,QAAQ,eAAe,KACvC,eAAe,MAAM,KAAK,KAAK,CAAC,AAAC,CAAA,MAAM,GAAE,IAAK,QAAQ;IAE1D,OAAO,IAAI,CAAC,MAAM,QAAQ,eAAe,KACvC,eAAe,KAAK,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"}
1
+ {"mappings":";;;;;;;;;;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;AAIM,SAAS,0CACd,KAAQ,E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QAAQ,EAAE,MAAc,QAAQ;IAClF,IAAI,WAAW,KAAK,IAAI,KAAK,IAAI,OAAO,MAAM;IAC9C,OAAO;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 ref = (0, $6imuh$useRef)(value !== undefined);
27
- let wasControlled = ref.current;
26
+ let isControlledRef = (0, $6imuh$useRef)(value !== undefined);
28
27
  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;
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(stateRef.current, value)) onChange(value, ...onChangeArgs);
39
+ if (!Object.is(currentValue, value)) onChange(value, ...onChangeArgs);
37
40
  }
38
- if (!isControlled) stateRef.current = value;
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 ? stateRef.current : oldValue, ...functionArgs);
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
- value,
72
+ currentValue,
67
73
  setValue
68
74
  ];
69
75
  }
@@ -1 +1 @@
1
- {"mappings":";;AA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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"}
1
+ {"mappings":";;AAAA;;;;;;;;;;ACAA;;;;;;;;;;CAUC;AAIM,SAAS,0CACd,KAAQ,EACR,YAAe,EACf,QAA4C;IAE5C,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;QACpC,IAAI,kBAAkB,cACpB,QAAQ,KAAK,CAAC,+BAA+B,EAAE,gBAAgB,eAAe,eAAe,IAAI,EAAE,eAAe,eAAe,eAAe,CAAC,CAAC;QAEpJ,gBAAgB,UAAU;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,GAAG,cAAc,QAC3B,SAAS,UAAU;YACrB;YAEF,IAAI,CAAC,cACH,kEAAkE;YAClE,g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I,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":"module.js.map"}
@@ -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 */\nexport {useControlledState} from './useControlledState';\nexport {clamp, snapValueToStep, toFixedNumber} from './number';\n"],"names":[],"version":3,"file":"types.d.ts.map"}
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,10 +1,15 @@
1
1
  {
2
2
  "name": "@react-stately/utils",
3
- "version": "3.5.2",
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",
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": [
@@ -20,10 +25,10 @@
20
25
  "react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0"
21
26
  },
22
27
  "dependencies": {
23
- "@swc/helpers": "^0.4.14"
28
+ "@swc/helpers": "^0.5.0"
24
29
  },
25
30
  "publishConfig": {
26
31
  "access": "public"
27
32
  },
28
- "gitHead": "5480d76bd815e239366f92852c76b6831ad2a4fd"
33
+ "gitHead": "504e40e0a50c1b20ed0fb3ba9561a263b6d5565e"
29
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
- ref.current = isControlled;
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(stateRef.current, value)) {
36
+ if (!Object.is(currentValue, value)) {
36
37
  onChange(value, ...onChangeArgs);
37
38
  }
38
39
  }
39
40
  if (!isControlled) {
40
- stateRef.current = value;
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 ? stateRef.current : oldValue, ...functionArgs);
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 [value, setValue];
74
+ return [currentValue, setValue];
76
75
  }