@react-stately/utils 3.2.1 → 3.4.1
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 +93 -124
- package/dist/main.js.map +1 -1
- package/dist/module.js +80 -116
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts +2 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/number.ts +7 -0
- package/src/useControlledState.ts +2 -1
package/dist/main.js
CHANGED
|
@@ -1,141 +1,110 @@
|
|
|
1
|
-
var
|
|
2
|
-
useCallback,
|
|
3
|
-
useRef,
|
|
4
|
-
useState
|
|
5
|
-
} = require("react");
|
|
1
|
+
var $h5yED$react = require("react");
|
|
6
2
|
|
|
7
|
-
function
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
let isControlled = value !== undefined; // Internal state reference for useCallback
|
|
12
|
-
|
|
13
|
-
let stateRef = useRef(stateValue);
|
|
14
|
-
|
|
15
|
-
if (wasControlled !== isControlled) {
|
|
16
|
-
console.warn("WARN: A component changed from " + (wasControlled ? 'controlled' : 'uncontrolled') + " to " + (isControlled ? 'controlled' : 'uncontrolled') + ".");
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
ref.current = isControlled;
|
|
20
|
-
let setValue = useCallback(function (value) {
|
|
21
|
-
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
22
|
-
args[_key - 1] = arguments[_key];
|
|
3
|
+
function $parcel$exportWildcard(dest, source) {
|
|
4
|
+
Object.keys(source).forEach(function(key) {
|
|
5
|
+
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
6
|
+
return;
|
|
23
7
|
}
|
|
24
8
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
onChangeArgs[_key2 - 1] = arguments[_key2];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
onChange(value, ...onChangeArgs);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (!isControlled) {
|
|
37
|
-
stateRef.current = value;
|
|
9
|
+
Object.defineProperty(dest, key, {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
get: function get() {
|
|
12
|
+
return source[key];
|
|
38
13
|
}
|
|
39
|
-
};
|
|
14
|
+
});
|
|
15
|
+
});
|
|
40
16
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
17
|
+
return dest;
|
|
18
|
+
}
|
|
19
|
+
function $parcel$export(e, n, v, s) {
|
|
20
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
21
|
+
}
|
|
22
|
+
var $18ac0fedce9fa6a3$exports = {};
|
|
23
|
+
|
|
24
|
+
$parcel$export($18ac0fedce9fa6a3$exports, "useControlledState", () => $18ac0fedce9fa6a3$export$40bfa8c7b0832715);
|
|
25
|
+
|
|
26
|
+
function $18ac0fedce9fa6a3$export$40bfa8c7b0832715(value1, defaultValue, onChange) {
|
|
27
|
+
let [stateValue, setStateValue] = $h5yED$react.useState(value1 || defaultValue);
|
|
28
|
+
let ref = $h5yED$react.useRef(value1 !== undefined);
|
|
29
|
+
let wasControlled = ref.current;
|
|
30
|
+
let isControlled = value1 !== undefined;
|
|
31
|
+
// Internal state reference for useCallback
|
|
32
|
+
let stateRef = $h5yED$react.useRef(stateValue);
|
|
33
|
+
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);
|
|
34
|
+
ref.current = isControlled;
|
|
35
|
+
let setValue = $h5yED$react.useCallback((value2, ...args)=>{
|
|
36
|
+
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
37
|
+
if (onChange) {
|
|
38
|
+
if (!Object.is(stateRef.current, value)) onChange(value, ...onChangeArgs);
|
|
39
|
+
}
|
|
40
|
+
if (!isControlled) stateRef.current = value;
|
|
41
|
+
};
|
|
42
|
+
if (typeof value2 === 'function') {
|
|
43
|
+
console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');
|
|
44
|
+
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
45
|
+
// when someone using useControlledState calls setControlledState(myFunc)
|
|
46
|
+
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
47
|
+
// 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
|
+
// 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
|
+
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
50
|
+
let interceptedValue = value2(isControlled ? stateRef.current : oldValue, ...functionArgs);
|
|
51
|
+
onChangeCaller(interceptedValue, ...args);
|
|
52
|
+
if (!isControlled) return interceptedValue;
|
|
53
|
+
return oldValue;
|
|
54
|
+
};
|
|
55
|
+
setStateValue(updateFunction);
|
|
56
|
+
} else {
|
|
57
|
+
if (!isControlled) setStateValue(value2);
|
|
58
|
+
onChangeCaller(value2, ...args);
|
|
57
59
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
}
|
|
70
|
-
}, [isControlled, onChange]); // If a controlled component's value prop changes, we need to update stateRef
|
|
71
|
-
|
|
72
|
-
if (isControlled) {
|
|
73
|
-
stateRef.current = value;
|
|
74
|
-
} else {
|
|
75
|
-
value = stateValue;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return [value, setValue];
|
|
60
|
+
}, [
|
|
61
|
+
isControlled,
|
|
62
|
+
onChange
|
|
63
|
+
]);
|
|
64
|
+
// If a controlled component's value prop changes, we need to update stateRef
|
|
65
|
+
if (isControlled) stateRef.current = value1;
|
|
66
|
+
else value1 = stateValue;
|
|
67
|
+
return [
|
|
68
|
+
value1,
|
|
69
|
+
setValue
|
|
70
|
+
];
|
|
79
71
|
}
|
|
80
72
|
|
|
81
|
-
exports.useControlledState = useControlledState;
|
|
82
|
-
|
|
83
|
-
/*
|
|
84
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
85
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
86
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
87
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
88
|
-
*
|
|
89
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
90
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
91
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
92
|
-
* governing permissions and limitations under the License.
|
|
93
|
-
*/
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
97
|
-
*/
|
|
98
|
-
function clamp(value, min, max) {
|
|
99
|
-
if (min === void 0) {
|
|
100
|
-
min = -Infinity;
|
|
101
|
-
}
|
|
102
73
|
|
|
103
|
-
|
|
104
|
-
max = Infinity;
|
|
105
|
-
}
|
|
74
|
+
var $e13121d468f79285$exports = {};
|
|
106
75
|
|
|
107
|
-
|
|
108
|
-
|
|
76
|
+
$parcel$export($e13121d468f79285$exports, "clamp", () => $e13121d468f79285$export$7d15b64cf5a3a4c4);
|
|
77
|
+
$parcel$export($e13121d468f79285$exports, "snapValueToStep", () => $e13121d468f79285$export$cb6e0bb50bc19463);
|
|
78
|
+
$parcel$export($e13121d468f79285$exports, "toFixedNumber", () => $e13121d468f79285$export$b6268554fba451f);
|
|
79
|
+
function $e13121d468f79285$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
80
|
+
let newValue = Math.min(Math.max(value, min), max);
|
|
81
|
+
return newValue;
|
|
109
82
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
83
|
+
function $e13121d468f79285$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
84
|
+
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
85
|
+
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
|
|
86
|
+
if (!isNaN(min)) {
|
|
87
|
+
if (snappedValue < min) snappedValue = min;
|
|
88
|
+
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
|
|
89
|
+
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
90
|
+
// correct floating point behavior by rounding to step precision
|
|
91
|
+
let string = step.toString();
|
|
92
|
+
let index = string.indexOf('.');
|
|
93
|
+
let precision = index >= 0 ? string.length - index : 0;
|
|
94
|
+
if (precision > 0) {
|
|
95
|
+
let pow = Math.pow(10, precision);
|
|
96
|
+
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
122
97
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
98
|
+
return snappedValue;
|
|
99
|
+
}
|
|
100
|
+
function $e13121d468f79285$export$b6268554fba451f(value, digits, base = 10) {
|
|
101
|
+
const pow = Math.pow(base, digits);
|
|
102
|
+
return Math.round(value * pow) / pow;
|
|
103
|
+
}
|
|
127
104
|
|
|
128
|
-
let string = step.toString();
|
|
129
|
-
let index = string.indexOf('.');
|
|
130
|
-
let precision = index >= 0 ? string.length - index : 0;
|
|
131
105
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
135
|
-
}
|
|
106
|
+
$parcel$exportWildcard(module.exports, $18ac0fedce9fa6a3$exports);
|
|
107
|
+
$parcel$exportWildcard(module.exports, $e13121d468f79285$exports);
|
|
136
108
|
|
|
137
|
-
return snappedValue;
|
|
138
|
-
}
|
|
139
109
|
|
|
140
|
-
exports.snapValueToStep = snapValueToStep;
|
|
141
110
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;SCcgB,yCAAkB,CAChC,MAAQ,EACR,YAAe,EACf,QAA4C,EACF,CAAC;IAC3C,GAAG,EAAE,UAAU,EAAE,aAAa,IAAI,qBAAQ,CAAC,MAAK,IAAI,YAAY;IAChE,GAAG,CAAC,GAAG,GAAG,mBAAM,CAAC,MAAK,KAAK,SAAS;IACpC,GAAG,CAAC,aAAa,GAAG,GAAG,CAAC,OAAO;IAC/B,GAAG,CAAC,YAAY,GAAG,MAAK,KAAK,SAAS;IACtC,EAA2C,AAA3C,yCAA2C;IAC3C,GAAG,CAAC,QAAQ,GAAG,mBAAM,CAAC,UAAU;IAChC,EAAE,EAAE,aAAa,KAAK,YAAY,EAChC,OAAO,CAAC,IAAI,EAAE,+BAA+B,EAAE,aAAa,GAAG,CAAY,cAAG,CAAc,cAAC,IAAI,EAAE,YAAY,GAAG,CAAY,cAAG,CAAc,cAAC,CAAC;IAGnJ,GAAG,CAAC,OAAO,GAAG,YAAY;IAE1B,GAAG,CAAC,QAAQ,GAAG,wBAAW,EAAE,MAAK,KAAK,IAAI,GAAK,CAAC;QAC9C,GAAG,CAAC,cAAc,IAAI,KAAK,KAAK,YAAY,GAAK,CAAC;YAChD,EAAE,EAAE,QAAQ,EACV,CAAC;gBAAD,EAAE,GAAG,MAAM,CAAC,EAAE,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AE,EAAE,SAAS;QAChC,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,GAAG,GAAG,IAAI,GAAG;IACrD,CAAC;IAED,MAAM,CAAC,YAAY;AACrB,CAAC;SAGe,wCAAa,CAAC,KAAa,EAAE,MAAc,EAAE,IAAY,GAAG,EAAE,EAAU,CAAC;IACvF,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM;IAEjC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,IAAI,GAAG;AACtC,CAAC;;","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 */\n\nexport * from './useControlledState';\nexport * 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,129 +1,93 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export function useControlledState(value, defaultValue, onChange) {
|
|
3
|
-
let [stateValue, setStateValue] = useState(value || defaultValue);
|
|
4
|
-
let ref = useRef(value !== undefined);
|
|
5
|
-
let wasControlled = ref.current;
|
|
6
|
-
let isControlled = value !== undefined; // Internal state reference for useCallback
|
|
1
|
+
import {useState as $11sjw$useState, useRef as $11sjw$useRef, useCallback as $11sjw$useCallback} from "react";
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
onChangeCaller(interceptedValue, ...args);
|
|
49
|
-
|
|
50
|
-
if (!isControlled) {
|
|
51
|
-
return interceptedValue;
|
|
3
|
+
function $parcel$export(e, n, v, s) {
|
|
4
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
5
|
+
}
|
|
6
|
+
var $bfee1151ccb0650f$exports = {};
|
|
7
|
+
|
|
8
|
+
$parcel$export($bfee1151ccb0650f$exports, "useControlledState", () => $bfee1151ccb0650f$export$40bfa8c7b0832715);
|
|
9
|
+
|
|
10
|
+
function $bfee1151ccb0650f$export$40bfa8c7b0832715(value1, defaultValue, onChange) {
|
|
11
|
+
let [stateValue, setStateValue] = $11sjw$useState(value1 || defaultValue);
|
|
12
|
+
let ref = $11sjw$useRef(value1 !== undefined);
|
|
13
|
+
let wasControlled = ref.current;
|
|
14
|
+
let isControlled = value1 !== undefined;
|
|
15
|
+
// Internal state reference for useCallback
|
|
16
|
+
let stateRef = $11sjw$useRef(stateValue);
|
|
17
|
+
if (wasControlled !== isControlled) console.warn(`WARN: A component changed from ${wasControlled ? 'controlled' : 'uncontrolled'} to ${isControlled ? 'controlled' : 'uncontrolled'}.`);
|
|
18
|
+
ref.current = isControlled;
|
|
19
|
+
let setValue = $11sjw$useCallback((value2, ...args)=>{
|
|
20
|
+
let onChangeCaller = (value, ...onChangeArgs)=>{
|
|
21
|
+
if (onChange) {
|
|
22
|
+
if (!Object.is(stateRef.current, value)) onChange(value, ...onChangeArgs);
|
|
23
|
+
}
|
|
24
|
+
if (!isControlled) stateRef.current = value;
|
|
25
|
+
};
|
|
26
|
+
if (typeof value2 === 'function') {
|
|
27
|
+
console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');
|
|
28
|
+
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
29
|
+
// when someone using useControlledState calls setControlledState(myFunc)
|
|
30
|
+
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
31
|
+
// 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
|
+
// 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
|
+
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
34
|
+
let interceptedValue = value2(isControlled ? stateRef.current : oldValue, ...functionArgs);
|
|
35
|
+
onChangeCaller(interceptedValue, ...args);
|
|
36
|
+
if (!isControlled) return interceptedValue;
|
|
37
|
+
return oldValue;
|
|
38
|
+
};
|
|
39
|
+
setStateValue(updateFunction);
|
|
40
|
+
} else {
|
|
41
|
+
if (!isControlled) setStateValue(value2);
|
|
42
|
+
onChangeCaller(value2, ...args);
|
|
52
43
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
65
|
-
}, [isControlled, onChange]); // If a controlled component's value prop changes, we need to update stateRef
|
|
66
|
-
|
|
67
|
-
if (isControlled) {
|
|
68
|
-
stateRef.current = value;
|
|
69
|
-
} else {
|
|
70
|
-
value = stateValue;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
return [value, setValue];
|
|
44
|
+
}, [
|
|
45
|
+
isControlled,
|
|
46
|
+
onChange
|
|
47
|
+
]);
|
|
48
|
+
// If a controlled component's value prop changes, we need to update stateRef
|
|
49
|
+
if (isControlled) stateRef.current = value1;
|
|
50
|
+
else value1 = stateValue;
|
|
51
|
+
return [
|
|
52
|
+
value1,
|
|
53
|
+
setValue
|
|
54
|
+
];
|
|
74
55
|
}
|
|
75
56
|
|
|
76
|
-
/*
|
|
77
|
-
* Copyright 2020 Adobe. All rights reserved.
|
|
78
|
-
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
|
|
79
|
-
* you may not use this file except in compliance with the License. You may obtain a copy
|
|
80
|
-
* of the License at http://www.apache.org/licenses/LICENSE-2.0
|
|
81
|
-
*
|
|
82
|
-
* Unless required by applicable law or agreed to in writing, software distributed under
|
|
83
|
-
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
|
|
84
|
-
* OF ANY KIND, either express or implied. See the License for the specific language
|
|
85
|
-
* governing permissions and limitations under the License.
|
|
86
|
-
*/
|
|
87
57
|
|
|
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
|
-
*/
|
|
91
|
-
export function clamp(value, min, max) {
|
|
92
|
-
if (min === void 0) {
|
|
93
|
-
min = -Infinity;
|
|
94
|
-
}
|
|
58
|
+
var $48d9f1d165180307$exports = {};
|
|
95
59
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
60
|
+
$parcel$export($48d9f1d165180307$exports, "clamp", () => $48d9f1d165180307$export$7d15b64cf5a3a4c4);
|
|
61
|
+
$parcel$export($48d9f1d165180307$exports, "snapValueToStep", () => $48d9f1d165180307$export$cb6e0bb50bc19463);
|
|
62
|
+
$parcel$export($48d9f1d165180307$exports, "toFixedNumber", () => $48d9f1d165180307$export$b6268554fba451f);
|
|
63
|
+
function $48d9f1d165180307$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
64
|
+
let newValue = Math.min(Math.max(value, min), max);
|
|
65
|
+
return newValue;
|
|
102
66
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
67
|
+
function $48d9f1d165180307$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
68
|
+
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
69
|
+
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
|
|
70
|
+
if (!isNaN(min)) {
|
|
71
|
+
if (snappedValue < min) snappedValue = min;
|
|
72
|
+
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
|
|
73
|
+
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
74
|
+
// correct floating point behavior by rounding to step precision
|
|
75
|
+
let string = step.toString();
|
|
76
|
+
let index = string.indexOf('.');
|
|
77
|
+
let precision = index >= 0 ? string.length - index : 0;
|
|
78
|
+
if (precision > 0) {
|
|
79
|
+
let pow = Math.pow(10, precision);
|
|
80
|
+
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
112
81
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
82
|
+
return snappedValue;
|
|
83
|
+
}
|
|
84
|
+
function $48d9f1d165180307$export$b6268554fba451f(value, digits, base = 10) {
|
|
85
|
+
const pow = Math.pow(base, digits);
|
|
86
|
+
return Math.round(value * pow) / pow;
|
|
87
|
+
}
|
|
116
88
|
|
|
117
89
|
|
|
118
|
-
let string = step.toString();
|
|
119
|
-
let index = string.indexOf('.');
|
|
120
|
-
let precision = index >= 0 ? string.length - index : 0;
|
|
121
90
|
|
|
122
|
-
if (precision > 0) {
|
|
123
|
-
let pow = Math.pow(10, precision);
|
|
124
|
-
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
125
|
-
}
|
|
126
91
|
|
|
127
|
-
|
|
128
|
-
}
|
|
92
|
+
export {$bfee1151ccb0650f$export$40bfa8c7b0832715 as useControlledState, $48d9f1d165180307$export$7d15b64cf5a3a4c4 as clamp, $48d9f1d165180307$export$cb6e0bb50bc19463 as snapValueToStep, $48d9f1d165180307$export$b6268554fba451f as toFixedNumber};
|
|
129
93
|
//# sourceMappingURL=module.js.map
|
package/dist/module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;;;;SCcgB,yCAAkB,CAChC,MAAQ,EACR,YAAe,EACf,QAA4C,EACF,CAAC;IAC3C,GAAG,EAAE,UAAU,EAAE,aAAa,IAAI,eAAQ,CAAC,MAAK,IAAI,YAAY;IAChE,GAAG,CAAC,GAAG,GAAG,aAAM,CAAC,MAAK,KAAK,SAAS;IACpC,GAAG,CAAC,aAAa,GAAG,GAAG,CAAC,OAAO;IAC/B,GAAG,CAAC,YAAY,GAAG,MAAK,KAAK,SAAS;IACtC,EAA2C,AAA3C,yCAA2C;IAC3C,GAAG,CAAC,QAAQ,GAAG,aAAM,CAAC,UAAU;IAChC,EAAE,EAAE,aAAa,KAAK,YAAY,EAChC,OAAO,CAAC,IAAI,EAAE,+BAA+B,EAAE,aAAa,GAAG,CAAY,cAAG,CAAc,cAAC,IAAI,EAAE,YAAY,GAAG,CAAY,cAAG,CAAc,cAAC,CAAC;IAGnJ,GAAG,CAAC,OAAO,GAAG,YAAY;IAE1B,GAAG,CAAC,QAAQ,GAAG,kBAAW,EAAE,MAAK,KAAK,IAAI,GAAK,CAAC;QAC9C,GAAG,CAAC,cAAc,IAAI,KAAK,KAAK,YAAY,GAAK,CAAC;YAChD,EAAE,EAAE,QAAQ,EACV,CAAC;gBAAD,EAAE,GAAG,MAAM,CAAC,EAAE,CAAC,QAAQ,CAAC,OAAO,EAAE,KAAK,GACpC,QAAQ,CAAC,KAAK,KAAK,YAAY;YACjC,CAAC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G;IACrD,CAAC;IAED,MAAM,CAAC,YAAY;AACrB,CAAC;SAGe,wCAAa,CAAC,KAAa,EAAE,MAAc,EAAE,IAAY,GAAG,EAAE,EAAU,CAAC;IACvF,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,MAAM;IAEjC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,GAAG,IAAI,GAAG;AACtC,CAAC;;","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 */\n\nexport * from './useControlledState';\nexport * 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
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export function useControlledState<T>(value: T, defaultValue: T, onChange: (value: T, ...args: any[]) => void): [T, (value: T
|
|
1
|
+
export function useControlledState<T>(value: T, defaultValue: T, onChange: (value: T, ...args: any[]) => void): [T, (value: T, ...args: any[]) => void];
|
|
2
2
|
/**
|
|
3
3
|
* Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.
|
|
4
4
|
*/
|
|
5
5
|
export function clamp(value: number, min?: number, max?: number): number;
|
|
6
6
|
export function snapValueToStep(value: number, min: number, max: number, step: number): number;
|
|
7
|
+
export function toFixedNumber(value: number, digits: number, base?: number): number;
|
|
7
8
|
|
|
8
9
|
//# sourceMappingURL=types.d.ts.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,
|
|
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 */\n\nexport * from './useControlledState';\nexport * 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.4.1",
|
|
4
4
|
"description": "Spectrum UI components in React",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"main": "dist/main.js",
|
|
@@ -25,5 +25,5 @@
|
|
|
25
25
|
"publishConfig": {
|
|
26
26
|
"access": "public"
|
|
27
27
|
},
|
|
28
|
-
"gitHead": "
|
|
28
|
+
"gitHead": "404d41859b7d6f56201d7fc01bd9f22ae3512937"
|
|
29
29
|
}
|
package/src/number.ts
CHANGED
|
@@ -46,3 +46,10 @@ export function snapValueToStep(value: number, min: number, max: number, step: n
|
|
|
46
46
|
|
|
47
47
|
return snappedValue;
|
|
48
48
|
}
|
|
49
|
+
|
|
50
|
+
/* Takes a value and rounds off to the number of digits. */
|
|
51
|
+
export function toFixedNumber(value: number, digits: number, base: number = 10): number {
|
|
52
|
+
const pow = Math.pow(base, digits);
|
|
53
|
+
|
|
54
|
+
return Math.round(value * pow) / pow;
|
|
55
|
+
}
|
|
@@ -16,7 +16,7 @@ export function useControlledState<T>(
|
|
|
16
16
|
value: T,
|
|
17
17
|
defaultValue: T,
|
|
18
18
|
onChange: (value: T, ...args: any[]) => void
|
|
19
|
-
): [T, (value: T
|
|
19
|
+
): [T, (value: T, ...args: any[]) => void] {
|
|
20
20
|
let [stateValue, setStateValue] = useState(value || defaultValue);
|
|
21
21
|
let ref = useRef(value !== undefined);
|
|
22
22
|
let wasControlled = ref.current;
|
|
@@ -42,6 +42,7 @@ export function useControlledState<T>(
|
|
|
42
42
|
};
|
|
43
43
|
|
|
44
44
|
if (typeof value === 'function') {
|
|
45
|
+
console.warn('We can not support a function callback. See Github Issues for details https://github.com/adobe/react-spectrum/issues/2320');
|
|
45
46
|
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
46
47
|
// when someone using useControlledState calls setControlledState(myFunc)
|
|
47
48
|
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|