@react-stately/utils 3.0.0-nightly.1349 → 3.0.0-nightly.1356
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 +91 -136
- package/dist/main.js.map +1 -1
- package/dist/module.js +79 -126
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/main.js
CHANGED
|
@@ -1,154 +1,109 @@
|
|
|
1
|
-
var
|
|
2
|
-
useCallback,
|
|
3
|
-
useRef,
|
|
4
|
-
useState
|
|
5
|
-
} = require("react");
|
|
1
|
+
var $1ebWm$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
|
-
};
|
|
40
|
-
|
|
41
|
-
if (typeof value === 'function') {
|
|
42
|
-
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
43
|
-
// when someone using useControlledState calls setControlledState(myFunc)
|
|
44
|
-
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
45
|
-
// if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning
|
|
46
|
-
// otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same
|
|
47
|
-
let updateFunction = function updateFunction(oldValue) {
|
|
48
|
-
for (var _len3 = arguments.length, functionArgs = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) {
|
|
49
|
-
functionArgs[_key3 - 1] = arguments[_key3];
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);
|
|
53
|
-
onChangeCaller(interceptedValue, ...args);
|
|
14
|
+
});
|
|
15
|
+
});
|
|
54
16
|
|
|
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 $28a294aa55e111af$exports = {};
|
|
23
|
+
|
|
24
|
+
$parcel$export($28a294aa55e111af$exports, "useControlledState", () => $28a294aa55e111af$export$40bfa8c7b0832715);
|
|
25
|
+
|
|
26
|
+
function $28a294aa55e111af$export$40bfa8c7b0832715(value1, defaultValue, onChange) {
|
|
27
|
+
let [stateValue, setStateValue] = $1ebWm$react.useState(value1 || defaultValue);
|
|
28
|
+
let ref = $1ebWm$react.useRef(value1 !== undefined);
|
|
29
|
+
let wasControlled = ref.current;
|
|
30
|
+
let isControlled = value1 !== undefined;
|
|
31
|
+
// Internal state reference for useCallback
|
|
32
|
+
let stateRef = $1ebWm$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 = $1ebWm$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
|
+
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
44
|
+
// when someone using useControlledState calls setControlledState(myFunc)
|
|
45
|
+
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
46
|
+
// 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
|
|
47
|
+
// 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
|
|
48
|
+
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
49
|
+
let interceptedValue = value2(isControlled ? stateRef.current : oldValue, ...functionArgs);
|
|
50
|
+
onChangeCaller(interceptedValue, ...args);
|
|
51
|
+
if (!isControlled) return interceptedValue;
|
|
52
|
+
return oldValue;
|
|
53
|
+
};
|
|
54
|
+
setStateValue(updateFunction);
|
|
55
|
+
} else {
|
|
56
|
+
if (!isControlled) setStateValue(value2);
|
|
57
|
+
onChangeCaller(value2, ...args);
|
|
57
58
|
}
|
|
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];
|
|
59
|
+
}, [
|
|
60
|
+
isControlled,
|
|
61
|
+
onChange
|
|
62
|
+
]);
|
|
63
|
+
// If a controlled component's value prop changes, we need to update stateRef
|
|
64
|
+
if (isControlled) stateRef.current = value1;
|
|
65
|
+
else value1 = stateValue;
|
|
66
|
+
return [
|
|
67
|
+
value1,
|
|
68
|
+
setValue
|
|
69
|
+
];
|
|
79
70
|
}
|
|
80
71
|
|
|
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
72
|
|
|
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
|
-
}
|
|
73
|
+
var $e51de1ee634a4bfa$exports = {};
|
|
102
74
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
75
|
+
$parcel$export($e51de1ee634a4bfa$exports, "clamp", () => $e51de1ee634a4bfa$export$7d15b64cf5a3a4c4);
|
|
76
|
+
$parcel$export($e51de1ee634a4bfa$exports, "snapValueToStep", () => $e51de1ee634a4bfa$export$cb6e0bb50bc19463);
|
|
77
|
+
$parcel$export($e51de1ee634a4bfa$exports, "toFixedNumber", () => $e51de1ee634a4bfa$export$b6268554fba451f);
|
|
78
|
+
function $e51de1ee634a4bfa$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
79
|
+
let newValue = Math.min(Math.max(value, min), max);
|
|
80
|
+
return newValue;
|
|
109
81
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
82
|
+
function $e51de1ee634a4bfa$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
83
|
+
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
84
|
+
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
|
|
85
|
+
if (!isNaN(min)) {
|
|
86
|
+
if (snappedValue < min) snappedValue = min;
|
|
87
|
+
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
|
|
88
|
+
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
89
|
+
// correct floating point behavior by rounding to step precision
|
|
90
|
+
let string = step.toString();
|
|
91
|
+
let index = string.indexOf('.');
|
|
92
|
+
let precision = index >= 0 ? string.length - index : 0;
|
|
93
|
+
if (precision > 0) {
|
|
94
|
+
let pow = Math.pow(10, precision);
|
|
95
|
+
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
122
96
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
let string = step.toString();
|
|
129
|
-
let index = string.indexOf('.');
|
|
130
|
-
let precision = index >= 0 ? string.length - index : 0;
|
|
131
|
-
|
|
132
|
-
if (precision > 0) {
|
|
133
|
-
let pow = Math.pow(10, precision);
|
|
134
|
-
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return snappedValue;
|
|
97
|
+
return snappedValue;
|
|
98
|
+
}
|
|
99
|
+
function $e51de1ee634a4bfa$export$b6268554fba451f(value, digits, base = 10) {
|
|
100
|
+
const pow = Math.pow(base, digits);
|
|
101
|
+
return Math.round(value * pow) / pow;
|
|
138
102
|
}
|
|
139
|
-
/* Takes a value and rounds off to the number of digits. */
|
|
140
|
-
|
|
141
103
|
|
|
142
|
-
exports.snapValueToStep = snapValueToStep;
|
|
143
104
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
base = 10;
|
|
147
|
-
}
|
|
105
|
+
$parcel$exportWildcard(module.exports, $28a294aa55e111af$exports);
|
|
106
|
+
$parcel$exportWildcard(module.exports, $e51de1ee634a4bfa$exports);
|
|
148
107
|
|
|
149
|
-
const pow = Math.pow(base, digits);
|
|
150
|
-
return Math.round(value * pow) / pow;
|
|
151
|
-
}
|
|
152
108
|
|
|
153
|
-
exports.toFixedNumber = toFixedNumber;
|
|
154
109
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;SCcgB,yCAAkB,CAChC,MAAQ,EACR,YAAe,EACf,QAA4C,EACsB,CAAC;IACnE,GAAG,EAAE,UAAU,EAAE,aAAa,IAAI,qBAAQ,CAAC,MAAK,IAAI,YAAY;IAChE,GAAG,CAAC,GAAG,GAAG,mBAAM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C1C,YAAY,GAAG,GAAG,GAAG,IAAI,CAAC,KAAK,EAAE,GAAG,GAAG,GAAG,IAAI,IAAI,IAAI,IAAI;IAE9D,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,GAAG,KAAK,YAAY,GAAG,GAAG,EAC1C,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,IAAI,IAAI,IAAI;IAG9C,EAAgE,AAAhE,8DAAgE;IAChE,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ;IAC1B,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,CAAG;IAC9B,GAAG,CAAC,SAAS,GAAG,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,KAAK,GAAG,CAAC;IAEtD,EAAE,EAAE,SAAS,GAAG,CAAC,EAAE,CAAC;QAClB,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,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 | ((prevState: T) => 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 // 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,139 +1,92 @@
|
|
|
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 $d9Bjf$useState, useRef as $d9Bjf$useRef, useCallback as $d9Bjf$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
|
-
let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);
|
|
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 $8b1a26670e051641$exports = {};
|
|
7
|
+
|
|
8
|
+
$parcel$export($8b1a26670e051641$exports, "useControlledState", () => $8b1a26670e051641$export$40bfa8c7b0832715);
|
|
9
|
+
|
|
10
|
+
function $8b1a26670e051641$export$40bfa8c7b0832715(value1, defaultValue, onChange) {
|
|
11
|
+
let [stateValue, setStateValue] = $d9Bjf$useState(value1 || defaultValue);
|
|
12
|
+
let ref = $d9Bjf$useRef(value1 !== undefined);
|
|
13
|
+
let wasControlled = ref.current;
|
|
14
|
+
let isControlled = value1 !== undefined;
|
|
15
|
+
// Internal state reference for useCallback
|
|
16
|
+
let stateRef = $d9Bjf$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 = $d9Bjf$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
|
+
// this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates
|
|
28
|
+
// when someone using useControlledState calls setControlledState(myFunc)
|
|
29
|
+
// this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc
|
|
30
|
+
// 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
|
|
31
|
+
// 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
|
|
32
|
+
let updateFunction = (oldValue, ...functionArgs)=>{
|
|
33
|
+
let interceptedValue = value2(isControlled ? stateRef.current : oldValue, ...functionArgs);
|
|
34
|
+
onChangeCaller(interceptedValue, ...args);
|
|
35
|
+
if (!isControlled) return interceptedValue;
|
|
36
|
+
return oldValue;
|
|
37
|
+
};
|
|
38
|
+
setStateValue(updateFunction);
|
|
39
|
+
} else {
|
|
40
|
+
if (!isControlled) setStateValue(value2);
|
|
41
|
+
onChangeCaller(value2, ...args);
|
|
52
42
|
}
|
|
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];
|
|
43
|
+
}, [
|
|
44
|
+
isControlled,
|
|
45
|
+
onChange
|
|
46
|
+
]);
|
|
47
|
+
// If a controlled component's value prop changes, we need to update stateRef
|
|
48
|
+
if (isControlled) stateRef.current = value1;
|
|
49
|
+
else value1 = stateValue;
|
|
50
|
+
return [
|
|
51
|
+
value1,
|
|
52
|
+
setValue
|
|
53
|
+
];
|
|
74
54
|
}
|
|
75
55
|
|
|
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
|
-
|
|
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
|
-
}
|
|
95
56
|
|
|
96
|
-
|
|
97
|
-
max = Infinity;
|
|
98
|
-
}
|
|
57
|
+
var $0a25742b8430afd6$exports = {};
|
|
99
58
|
|
|
100
|
-
|
|
101
|
-
|
|
59
|
+
$parcel$export($0a25742b8430afd6$exports, "clamp", () => $0a25742b8430afd6$export$7d15b64cf5a3a4c4);
|
|
60
|
+
$parcel$export($0a25742b8430afd6$exports, "snapValueToStep", () => $0a25742b8430afd6$export$cb6e0bb50bc19463);
|
|
61
|
+
$parcel$export($0a25742b8430afd6$exports, "toFixedNumber", () => $0a25742b8430afd6$export$b6268554fba451f);
|
|
62
|
+
function $0a25742b8430afd6$export$7d15b64cf5a3a4c4(value, min = -Infinity, max = Infinity) {
|
|
63
|
+
let newValue = Math.min(Math.max(value, min), max);
|
|
64
|
+
return newValue;
|
|
102
65
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
66
|
+
function $0a25742b8430afd6$export$cb6e0bb50bc19463(value, min, max, step) {
|
|
67
|
+
let remainder = (value - (isNaN(min) ? 0 : min)) % step;
|
|
68
|
+
let snappedValue = Math.abs(remainder) * 2 >= step ? value + Math.sign(remainder) * (step - Math.abs(remainder)) : value - remainder;
|
|
69
|
+
if (!isNaN(min)) {
|
|
70
|
+
if (snappedValue < min) snappedValue = min;
|
|
71
|
+
else if (!isNaN(max) && snappedValue > max) snappedValue = min + Math.floor((max - min) / step) * step;
|
|
72
|
+
} else if (!isNaN(max) && snappedValue > max) snappedValue = Math.floor(max / step) * step;
|
|
73
|
+
// correct floating point behavior by rounding to step precision
|
|
74
|
+
let string = step.toString();
|
|
75
|
+
let index = string.indexOf('.');
|
|
76
|
+
let precision = index >= 0 ? string.length - index : 0;
|
|
77
|
+
if (precision > 0) {
|
|
78
|
+
let pow = Math.pow(10, precision);
|
|
79
|
+
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
112
80
|
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
let index = string.indexOf('.');
|
|
120
|
-
let precision = index >= 0 ? string.length - index : 0;
|
|
81
|
+
return snappedValue;
|
|
82
|
+
}
|
|
83
|
+
function $0a25742b8430afd6$export$b6268554fba451f(value, digits, base = 10) {
|
|
84
|
+
const pow = Math.pow(base, digits);
|
|
85
|
+
return Math.round(value * pow) / pow;
|
|
86
|
+
}
|
|
121
87
|
|
|
122
|
-
if (precision > 0) {
|
|
123
|
-
let pow = Math.pow(10, precision);
|
|
124
|
-
snappedValue = Math.round(snappedValue * pow) / pow;
|
|
125
|
-
}
|
|
126
88
|
|
|
127
|
-
return snappedValue;
|
|
128
|
-
}
|
|
129
|
-
/* Takes a value and rounds off to the number of digits. */
|
|
130
89
|
|
|
131
|
-
export function toFixedNumber(value, digits, base) {
|
|
132
|
-
if (base === void 0) {
|
|
133
|
-
base = 10;
|
|
134
|
-
}
|
|
135
90
|
|
|
136
|
-
|
|
137
|
-
return Math.round(value * pow) / pow;
|
|
138
|
-
}
|
|
91
|
+
export {$8b1a26670e051641$export$40bfa8c7b0832715 as useControlledState, $0a25742b8430afd6$export$7d15b64cf5a3a4c4 as clamp, $0a25742b8430afd6$export$cb6e0bb50bc19463 as snapValueToStep, $0a25742b8430afd6$export$b6268554fba451f as toFixedNumber};
|
|
139
92
|
//# sourceMappingURL=module.js.map
|
package/dist/module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;;;;SCcgB,yCAAkB,CAChC,MAAQ,EACR,YAAe,EACf,QAA4C,EACsB,CAAC;IACnE,GAAG,EAAE,UAAU,EAAE,aAAa,IAAI,eAAQ,CA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AAE,GAAG,KAAK,CAAC,GAAG,KAAK,YAAY,GAAG,GAAG,EAC1C,YAAY,GAAG,GAAG,GAAG,IAAI,CAAC,KAAK,EAAE,GAAG,GAAG,GAAG,IAAI,IAAI,IAAI,IAAI;IAE9D,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,GAAG,KAAK,YAAY,GAAG,GAAG,EAC1C,YAAY,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,GAAG,IAAI,IAAI,IAAI;IAG9C,EAAgE,AAAhE,8DAAgE;IAChE,GAAG,CAAC,MAAM,GAAG,IAAI,CAAC,QAAQ;IAC1B,GAAG,CAAC,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,CAAG;IAC9B,GAAG,CAAC,SAAS,GAAG,KAAK,IAAI,CAAC,GAAG,MAAM,CAAC,MAAM,GAAG,KAAK,GAAG,CAAC;IAEtD,EAAE,EAAE,SAAS,GAAG,CAAC,EAAE,CAAC;QAClB,GAAG,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,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 | ((prevState: T) => 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 // this supports functional updates https://reactjs.org/docs/hooks-reference.html#functional-updates\n // when someone using useControlledState calls setControlledState(myFunc)\n // this will call our useState setState with a function as well which invokes myFunc and calls onChange with the value from myFunc\n // if we're in an uncontrolled state, then we also return the value of myFunc which to setState looks as though it was just called with myFunc from the beginning\n // otherwise we just return the controlled value, which won't cause a rerender because React knows to bail out when the value is the same\n let updateFunction = (oldValue, ...functionArgs) => {\n let interceptedValue = value(isControlled ? stateRef.current : oldValue, ...functionArgs);\n onChangeCaller(interceptedValue, ...args);\n if (!isControlled) {\n return interceptedValue;\n }\n return oldValue;\n };\n setStateValue(updateFunction);\n } else {\n if (!isControlled) {\n setStateValue(value);\n }\n onChangeCaller(value, ...args);\n }\n }, [isControlled, onChange]);\n\n // If a controlled component's value prop changes, we need to update stateRef\n if (isControlled) {\n stateRef.current = value;\n } else {\n value = stateValue;\n }\n\n return [value, setValue];\n}\n","/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\n/**\n * Takes a value and forces it to the closest min/max if it's outside. Also forces it to the closest valid step.\n */\nexport function clamp(value: number, min: number = -Infinity, max: number = Infinity): number {\n let newValue = Math.min(Math.max(value, min), max);\n return newValue;\n}\n\nexport function snapValueToStep(value: number, min: number, max: number, step: number): number {\n let remainder = ((value - (isNaN(min) ? 0 : min)) % step);\n let snappedValue = Math.abs(remainder) * 2 >= step\n ? value + Math.sign(remainder) * (step - Math.abs(remainder))\n : value - remainder;\n\n if (!isNaN(min)) {\n if (snappedValue < min) {\n snappedValue = min;\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = min + Math.floor((max - min) / step) * step;\n }\n } else if (!isNaN(max) && snappedValue > max) {\n snappedValue = Math.floor(max / step) * step;\n }\n\n // correct floating point behavior by rounding to step precision\n let string = step.toString();\n let index = string.indexOf('.');\n let precision = index >= 0 ? string.length - index : 0;\n\n if (precision > 0) {\n let pow = Math.pow(10, precision);\n snappedValue = Math.round(snappedValue * pow) / pow;\n }\n\n return snappedValue;\n}\n\n/* Takes a value and rounds off to the number of digits. */\nexport function toFixedNumber(value: number, digits: number, base: number = 10): number {\n const pow = Math.pow(base, digits);\n\n return Math.round(value * pow) / pow;\n}\n"],"names":[],"version":3,"file":"module.js.map"}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"AAcA,mCAAmC,CAAC,EAClC,KAAK,EAAE,CAAC,EACR,YAAY,EAAE,CAAC,EACf,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,GAC3C,CAAC,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,CAwDjE;AC9DD
|
|
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,GAAG,CAAC,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC,CAwDjE;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 */\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.0.0-nightly.
|
|
3
|
+
"version": "3.0.0-nightly.1356+87960ad25",
|
|
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": "87960ad25c26bc4cd6af506bb5e7e0f06621556c"
|
|
29
29
|
}
|