@rc-component/util 1.0.0 → 1.0.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/es/Children/toArray.js +3 -4
- package/es/Dom/contains.js +1 -1
- package/es/Dom/dynamicCSS.js +50 -60
- package/es/Dom/findDOMNode.js +3 -5
- package/es/Dom/focus.js +13 -21
- package/es/Dom/isVisible.js +10 -8
- package/es/Dom/scrollLocker.js +63 -89
- package/es/Dom/shadow.js +1 -2
- package/es/Dom/styleChecker.js +9 -9
- package/es/KeyCode.js +4 -2
- package/es/Portal.js +15 -16
- package/es/PortalWrapper.js +150 -175
- package/es/React/isFragment.js +4 -5
- package/es/React/render.js +32 -63
- package/es/composeProps.js +9 -16
- package/es/getScrollBarSize.js +19 -15
- package/es/hooks/useEffect.js +3 -5
- package/es/hooks/useEvent.js +2 -8
- package/es/hooks/useId.js +10 -23
- package/es/hooks/useLayoutEffect.js +8 -8
- package/es/hooks/useMemo.js +1 -1
- package/es/hooks/useMergedState.js +23 -34
- package/es/hooks/useMobile.js +3 -12
- package/es/hooks/useState.js +4 -13
- package/es/hooks/useSyncState.js +4 -14
- package/es/isEqual.js +9 -14
- package/es/isMobile.js +3 -3
- package/es/omit.js +2 -2
- package/es/pickAttrs.js +29 -17
- package/es/proxyObject.js +2 -2
- package/es/raf.js +12 -23
- package/es/ref.js +16 -30
- package/es/setStyle.js +8 -8
- package/es/test/domHook.js +19 -25
- package/es/utils/get.js +2 -2
- package/es/utils/set.js +19 -38
- package/es/warning.js +7 -11
- package/lib/Children/toArray.js +3 -4
- package/lib/Dom/contains.js +1 -1
- package/lib/Dom/dynamicCSS.js +50 -60
- package/lib/Dom/findDOMNode.js +3 -5
- package/lib/Dom/focus.js +13 -21
- package/lib/Dom/isVisible.js +12 -9
- package/lib/Dom/scrollLocker.js +64 -88
- package/lib/Dom/shadow.js +1 -2
- package/lib/Dom/styleChecker.js +9 -9
- package/lib/KeyCode.js +4 -2
- package/lib/Portal.js +15 -16
- package/lib/PortalWrapper.js +154 -177
- package/lib/React/isFragment.js +4 -5
- package/lib/React/render.js +34 -65
- package/lib/composeProps.js +9 -16
- package/lib/getScrollBarSize.js +19 -15
- package/lib/hooks/useEffect.js +5 -8
- package/lib/hooks/useEvent.js +4 -11
- package/lib/hooks/useId.js +12 -25
- package/lib/hooks/useLayoutEffect.js +11 -11
- package/lib/hooks/useMemo.js +3 -4
- package/lib/hooks/useMergedState.js +24 -35
- package/lib/hooks/useMobile.js +3 -12
- package/lib/hooks/useState.js +6 -16
- package/lib/hooks/useSyncState.js +6 -17
- package/lib/index.js +8 -8
- package/lib/isEqual.js +9 -14
- package/lib/isMobile.js +5 -4
- package/lib/omit.js +2 -2
- package/lib/pickAttrs.js +29 -17
- package/lib/proxyObject.js +2 -2
- package/lib/raf.js +12 -23
- package/lib/ref.js +23 -31
- package/lib/setStyle.js +8 -8
- package/lib/test/domHook.js +19 -25
- package/lib/utils/get.js +2 -2
- package/lib/utils/set.js +19 -38
- package/lib/warning.js +8 -11
- package/package.json +3 -3
package/lib/isMobile.js
CHANGED
|
@@ -4,10 +4,11 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
var _default =
|
|
7
|
+
var _default = () => {
|
|
8
8
|
if (typeof navigator === 'undefined' || typeof window === 'undefined') {
|
|
9
9
|
return false;
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(agent) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(agent
|
|
13
|
-
};
|
|
11
|
+
const agent = navigator.userAgent || navigator.vendor || window.opera;
|
|
12
|
+
return /(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino|android|ipad|playbook|silk/i.test(agent) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw-(n|u)|c55\/|capi|ccwa|cdm-|cell|chtm|cldc|cmd-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc-s|devi|dica|dmob|do(c|p)o|ds(12|-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(-|_)|g1 u|g560|gene|gf-5|g-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd-(m|p|t)|hei-|hi(pt|ta)|hp( i|ip)|hs-c|ht(c(-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i-(20|go|ma)|i230|iac( |-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|-[a-w])|libw|lynx|m1-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|-([1-8]|c))|phil|pire|pl(ay|uc)|pn-2|po(ck|rt|se)|prox|psio|pt-g|qa-a|qc(07|12|21|32|60|-[2-7]|i-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h-|oo|p-)|sdk\/|se(c(-|0|1)|47|mc|nd|ri)|sgh-|shar|sie(-|m)|sk-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h-|v-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl-|tdg-|tel(i|m)|tim-|t-mo|to(pl|sh)|ts(70|m-|m3|m5)|tx-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas-|your|zeto|zte-/i.test(agent?.substr(0, 4));
|
|
13
|
+
};
|
|
14
|
+
exports.default = _default;
|
package/lib/omit.js
CHANGED
|
@@ -5,9 +5,9 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = omit;
|
|
7
7
|
function omit(obj, fields) {
|
|
8
|
-
|
|
8
|
+
const clone = Object.assign({}, obj);
|
|
9
9
|
if (Array.isArray(fields)) {
|
|
10
|
-
fields.forEach(
|
|
10
|
+
fields.forEach(key => {
|
|
11
11
|
delete clone[key];
|
|
12
12
|
});
|
|
13
13
|
}
|
package/lib/pickAttrs.js
CHANGED
|
@@ -4,19 +4,30 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = pickAttrs;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
const attributes = `accept acceptCharset accessKey action allowFullScreen allowTransparency
|
|
8
|
+
alt async autoComplete autoFocus autoPlay capture cellPadding cellSpacing challenge
|
|
9
|
+
charSet checked classID className colSpan cols content contentEditable contextMenu
|
|
10
|
+
controls coords crossOrigin data dateTime default defer dir disabled download draggable
|
|
11
|
+
encType form formAction formEncType formMethod formNoValidate formTarget frameBorder
|
|
12
|
+
headers height hidden high href hrefLang htmlFor httpEquiv icon id inputMode integrity
|
|
13
|
+
is keyParams keyType kind label lang list loop low manifest marginHeight marginWidth max maxLength media
|
|
14
|
+
mediaGroup method min minLength multiple muted name noValidate nonce open
|
|
15
|
+
optimum pattern placeholder poster preload radioGroup readOnly rel required
|
|
16
|
+
reversed role rowSpan rows sandbox scope scoped scrolling seamless selected
|
|
17
|
+
shape size sizes span spellCheck src srcDoc srcLang srcSet start step style
|
|
18
|
+
summary tabIndex target title type useMap value width wmode wrap`;
|
|
19
|
+
const eventsName = `onCopy onCut onPaste onCompositionEnd onCompositionStart onCompositionUpdate onKeyDown
|
|
20
|
+
onKeyPress onKeyUp onFocus onBlur onChange onInput onSubmit onClick onContextMenu onDoubleClick
|
|
21
|
+
onDrag onDragEnd onDragEnter onDragExit onDragLeave onDragOver onDragStart onDrop onMouseDown
|
|
22
|
+
onMouseEnter onMouseLeave onMouseMove onMouseOut onMouseOver onMouseUp onSelect onTouchCancel
|
|
23
|
+
onTouchEnd onTouchMove onTouchStart onScroll onWheel onAbort onCanPlay onCanPlayThrough
|
|
24
|
+
onDurationChange onEmptied onEncrypted onEnded onError onLoadedData onLoadedMetadata
|
|
25
|
+
onLoadStart onPause onPlay onPlaying onProgress onRateChange onSeeked onSeeking onStalled onSuspend onTimeUpdate onVolumeChange onWaiting onLoad onError`;
|
|
26
|
+
const propList = `${attributes} ${eventsName}`.split(/[\s\n]+/);
|
|
16
27
|
|
|
17
28
|
/* eslint-enable max-len */
|
|
18
|
-
|
|
19
|
-
|
|
29
|
+
const ariaPrefix = 'aria-';
|
|
30
|
+
const dataPrefix = 'data-';
|
|
20
31
|
function match(key, prefix) {
|
|
21
32
|
return key.indexOf(prefix) === 0;
|
|
22
33
|
}
|
|
@@ -25,9 +36,8 @@ function match(key, prefix) {
|
|
|
25
36
|
* @param props Passed props
|
|
26
37
|
* @param ariaOnly boolean | { aria?: boolean; data?: boolean; attr?: boolean; } filter config
|
|
27
38
|
*/
|
|
28
|
-
function pickAttrs(props) {
|
|
29
|
-
|
|
30
|
-
var mergedConfig;
|
|
39
|
+
function pickAttrs(props, ariaOnly = false) {
|
|
40
|
+
let mergedConfig;
|
|
31
41
|
if (ariaOnly === false) {
|
|
32
42
|
mergedConfig = {
|
|
33
43
|
aria: true,
|
|
@@ -39,10 +49,12 @@ function pickAttrs(props) {
|
|
|
39
49
|
aria: true
|
|
40
50
|
};
|
|
41
51
|
} else {
|
|
42
|
-
mergedConfig =
|
|
52
|
+
mergedConfig = {
|
|
53
|
+
...ariaOnly
|
|
54
|
+
};
|
|
43
55
|
}
|
|
44
|
-
|
|
45
|
-
Object.keys(props).forEach(
|
|
56
|
+
const attrs = {};
|
|
57
|
+
Object.keys(props).forEach(key => {
|
|
46
58
|
if (
|
|
47
59
|
// Aria
|
|
48
60
|
mergedConfig.aria && (key === 'role' || match(key, ariaPrefix)) ||
|
package/lib/proxyObject.js
CHANGED
|
@@ -10,13 +10,13 @@ exports.default = proxyObject;
|
|
|
10
10
|
function proxyObject(obj, extendProps) {
|
|
11
11
|
if (typeof Proxy !== 'undefined' && obj) {
|
|
12
12
|
return new Proxy(obj, {
|
|
13
|
-
get
|
|
13
|
+
get(target, prop) {
|
|
14
14
|
if (extendProps[prop]) {
|
|
15
15
|
return extendProps[prop];
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
// Proxy origin property
|
|
19
|
-
|
|
19
|
+
const originProp = target[prop];
|
|
20
20
|
return typeof originProp === 'function' ? originProp.bind(target) : originProp;
|
|
21
21
|
}
|
|
22
22
|
});
|
package/lib/raf.js
CHANGED
|
@@ -4,29 +4,20 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
};
|
|
10
|
-
var caf = function caf(num) {
|
|
11
|
-
return clearTimeout(num);
|
|
12
|
-
};
|
|
7
|
+
let raf = callback => +setTimeout(callback, 16);
|
|
8
|
+
let caf = num => clearTimeout(num);
|
|
13
9
|
if (typeof window !== 'undefined' && 'requestAnimationFrame' in window) {
|
|
14
|
-
raf =
|
|
15
|
-
|
|
16
|
-
};
|
|
17
|
-
caf = function caf(handle) {
|
|
18
|
-
return window.cancelAnimationFrame(handle);
|
|
19
|
-
};
|
|
10
|
+
raf = callback => window.requestAnimationFrame(callback);
|
|
11
|
+
caf = handle => window.cancelAnimationFrame(handle);
|
|
20
12
|
}
|
|
21
|
-
|
|
22
|
-
|
|
13
|
+
let rafUUID = 0;
|
|
14
|
+
const rafIds = new Map();
|
|
23
15
|
function cleanup(id) {
|
|
24
16
|
rafIds.delete(id);
|
|
25
17
|
}
|
|
26
|
-
|
|
27
|
-
var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1;
|
|
18
|
+
const wrapperRaf = (callback, times = 1) => {
|
|
28
19
|
rafUUID += 1;
|
|
29
|
-
|
|
20
|
+
const id = rafUUID;
|
|
30
21
|
function callRef(leftTimes) {
|
|
31
22
|
if (leftTimes === 0) {
|
|
32
23
|
// Clean up
|
|
@@ -36,7 +27,7 @@ var wrapperRaf = function wrapperRaf(callback) {
|
|
|
36
27
|
callback();
|
|
37
28
|
} else {
|
|
38
29
|
// Next raf
|
|
39
|
-
|
|
30
|
+
const realId = raf(() => {
|
|
40
31
|
callRef(leftTimes - 1);
|
|
41
32
|
});
|
|
42
33
|
|
|
@@ -47,14 +38,12 @@ var wrapperRaf = function wrapperRaf(callback) {
|
|
|
47
38
|
callRef(times);
|
|
48
39
|
return id;
|
|
49
40
|
};
|
|
50
|
-
wrapperRaf.cancel =
|
|
51
|
-
|
|
41
|
+
wrapperRaf.cancel = id => {
|
|
42
|
+
const realId = rafIds.get(id);
|
|
52
43
|
cleanup(id);
|
|
53
44
|
return caf(realId);
|
|
54
45
|
};
|
|
55
46
|
if (process.env.NODE_ENV !== 'production') {
|
|
56
|
-
wrapperRaf.ids =
|
|
57
|
-
return rafIds;
|
|
58
|
-
};
|
|
47
|
+
wrapperRaf.ids = () => rafIds;
|
|
59
48
|
}
|
|
60
49
|
var _default = exports.default = wrapperRaf;
|
package/lib/ref.js
CHANGED
|
@@ -9,11 +9,10 @@ var _reactIs = require("react-is");
|
|
|
9
9
|
var _useMemo = _interopRequireDefault(require("./hooks/useMemo"));
|
|
10
10
|
var _isFragment = _interopRequireDefault(require("./React/isFragment"));
|
|
11
11
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
-
|
|
13
|
-
var fillRef = exports.fillRef = function fillRef(ref, node) {
|
|
12
|
+
const fillRef = (ref, node) => {
|
|
14
13
|
if (typeof ref === 'function') {
|
|
15
14
|
ref(node);
|
|
16
|
-
} else if (
|
|
15
|
+
} else if (typeof ref === 'object' && ref && 'current' in ref) {
|
|
17
16
|
ref.current = node;
|
|
18
17
|
}
|
|
19
18
|
};
|
|
@@ -21,36 +20,26 @@ var fillRef = exports.fillRef = function fillRef(ref, node) {
|
|
|
21
20
|
/**
|
|
22
21
|
* Merge refs into one ref function to support ref passing.
|
|
23
22
|
*/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
var refList = refs.filter(Boolean);
|
|
23
|
+
exports.fillRef = fillRef;
|
|
24
|
+
const composeRef = (...refs) => {
|
|
25
|
+
const refList = refs.filter(Boolean);
|
|
29
26
|
if (refList.length <= 1) {
|
|
30
27
|
return refList[0];
|
|
31
28
|
}
|
|
32
|
-
return
|
|
33
|
-
refs.forEach(
|
|
29
|
+
return node => {
|
|
30
|
+
refs.forEach(ref => {
|
|
34
31
|
fillRef(ref, node);
|
|
35
32
|
});
|
|
36
33
|
};
|
|
37
34
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
return (0, _useMemo.default)(function () {
|
|
43
|
-
return composeRef.apply(void 0, refs);
|
|
44
|
-
},
|
|
35
|
+
exports.composeRef = composeRef;
|
|
36
|
+
const useComposeRef = (...refs) => {
|
|
37
|
+
return (0, _useMemo.default)(() => composeRef(...refs),
|
|
45
38
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
46
|
-
refs,
|
|
47
|
-
return prev.length !== next.length || prev.every(function (ref, i) {
|
|
48
|
-
return ref !== next[i];
|
|
49
|
-
});
|
|
50
|
-
});
|
|
39
|
+
refs, (prev, next) => prev.length !== next.length || prev.every((ref, i) => ref !== next[i]));
|
|
51
40
|
};
|
|
52
|
-
|
|
53
|
-
|
|
41
|
+
exports.useComposeRef = useComposeRef;
|
|
42
|
+
const supportRef = nodeOrComponent => {
|
|
54
43
|
if (!nodeOrComponent) {
|
|
55
44
|
return false;
|
|
56
45
|
}
|
|
@@ -59,23 +48,24 @@ var supportRef = exports.supportRef = function supportRef(nodeOrComponent) {
|
|
|
59
48
|
if (isReactElement(nodeOrComponent) && nodeOrComponent.props.propertyIsEnumerable('ref')) {
|
|
60
49
|
return true;
|
|
61
50
|
}
|
|
62
|
-
|
|
51
|
+
const type = (0, _reactIs.isMemo)(nodeOrComponent) ? nodeOrComponent.type.type : nodeOrComponent.type;
|
|
63
52
|
|
|
64
53
|
// Function component node
|
|
65
|
-
if (typeof type === 'function' && !
|
|
54
|
+
if (typeof type === 'function' && !type.prototype?.render && type.$$typeof !== _reactIs.ForwardRef) {
|
|
66
55
|
return false;
|
|
67
56
|
}
|
|
68
57
|
|
|
69
58
|
// Class component
|
|
70
|
-
if (typeof nodeOrComponent === 'function' && !
|
|
59
|
+
if (typeof nodeOrComponent === 'function' && !nodeOrComponent.prototype?.render && nodeOrComponent.$$typeof !== _reactIs.ForwardRef) {
|
|
71
60
|
return false;
|
|
72
61
|
}
|
|
73
62
|
return true;
|
|
74
63
|
};
|
|
64
|
+
exports.supportRef = supportRef;
|
|
75
65
|
function isReactElement(node) {
|
|
76
66
|
return /*#__PURE__*/(0, _react.isValidElement)(node) && !(0, _isFragment.default)(node);
|
|
77
67
|
}
|
|
78
|
-
|
|
68
|
+
const supportNodeRef = node => {
|
|
79
69
|
return isReactElement(node) && supportRef(node);
|
|
80
70
|
};
|
|
81
71
|
|
|
@@ -84,13 +74,15 @@ var supportNodeRef = exports.supportNodeRef = function supportNodeRef(node) {
|
|
|
84
74
|
* But a property from `props.ref`.
|
|
85
75
|
* To check if `props.ref` exist or fallback to `ref`.
|
|
86
76
|
*/
|
|
87
|
-
|
|
77
|
+
exports.supportNodeRef = supportNodeRef;
|
|
78
|
+
const getNodeRef = node => {
|
|
88
79
|
if (node && isReactElement(node)) {
|
|
89
|
-
|
|
80
|
+
const ele = node;
|
|
90
81
|
|
|
91
82
|
// Source from:
|
|
92
83
|
// https://github.com/mui/material-ui/blob/master/packages/mui-utils/src/getReactNodeRef/getReactNodeRef.ts
|
|
93
84
|
return ele.props.propertyIsEnumerable('ref') ? ele.props.ref : ele.ref;
|
|
94
85
|
}
|
|
95
86
|
return null;
|
|
96
|
-
};
|
|
87
|
+
};
|
|
88
|
+
exports.getNodeRef = getNodeRef;
|
package/lib/setStyle.js
CHANGED
|
@@ -10,21 +10,21 @@ exports.default = void 0;
|
|
|
10
10
|
* https://github.com/ant-design/ant-design/issues/19393
|
|
11
11
|
*
|
|
12
12
|
*/
|
|
13
|
-
function setStyle(style) {
|
|
14
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
13
|
+
function setStyle(style, options = {}) {
|
|
15
14
|
if (!style) {
|
|
16
15
|
return {};
|
|
17
16
|
}
|
|
18
|
-
|
|
19
|
-
element =
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
const {
|
|
18
|
+
element = document.body
|
|
19
|
+
} = options;
|
|
20
|
+
const oldStyle = {};
|
|
21
|
+
const styleKeys = Object.keys(style);
|
|
22
22
|
|
|
23
23
|
// IE browser compatible
|
|
24
|
-
styleKeys.forEach(
|
|
24
|
+
styleKeys.forEach(key => {
|
|
25
25
|
oldStyle[key] = element.style[key];
|
|
26
26
|
});
|
|
27
|
-
styleKeys.forEach(
|
|
27
|
+
styleKeys.forEach(key => {
|
|
28
28
|
element.style[key] = style[key];
|
|
29
29
|
});
|
|
30
30
|
return oldStyle;
|
package/lib/test/domHook.js
CHANGED
|
@@ -5,55 +5,47 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.spyElementPrototype = spyElementPrototype;
|
|
7
7
|
exports.spyElementPrototypes = spyElementPrototypes;
|
|
8
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
9
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
10
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
11
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
12
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
13
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
14
8
|
/* eslint-disable @typescript-eslint/ban-types */
|
|
15
9
|
/* eslint-disable no-param-reassign */
|
|
16
|
-
|
|
10
|
+
const NO_EXIST = {
|
|
17
11
|
__NOT_EXIST: true
|
|
18
12
|
};
|
|
19
13
|
function spyElementPrototypes(elementClass, properties) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
propNames.forEach(
|
|
23
|
-
|
|
14
|
+
const propNames = Object.keys(properties);
|
|
15
|
+
const originDescriptors = {};
|
|
16
|
+
propNames.forEach(propName => {
|
|
17
|
+
const originDescriptor = Object.getOwnPropertyDescriptor(elementClass.prototype, propName);
|
|
24
18
|
originDescriptors[propName] = originDescriptor || NO_EXIST;
|
|
25
|
-
|
|
19
|
+
const spyProp = properties[propName];
|
|
26
20
|
if (typeof spyProp === 'function') {
|
|
27
21
|
// If is a function
|
|
28
|
-
elementClass.prototype[propName] = function spyFunc() {
|
|
29
|
-
|
|
30
|
-
args[_key] = arguments[_key];
|
|
31
|
-
}
|
|
32
|
-
return spyProp.call.apply(spyProp, [this, originDescriptor].concat(args));
|
|
22
|
+
elementClass.prototype[propName] = function spyFunc(...args) {
|
|
23
|
+
return spyProp.call(this, originDescriptor, ...args);
|
|
33
24
|
};
|
|
34
25
|
} else {
|
|
35
26
|
// Otherwise tread as a property
|
|
36
|
-
Object.defineProperty(elementClass.prototype, propName,
|
|
37
|
-
|
|
27
|
+
Object.defineProperty(elementClass.prototype, propName, {
|
|
28
|
+
...spyProp,
|
|
29
|
+
set(value) {
|
|
38
30
|
if (spyProp.set) {
|
|
39
31
|
return spyProp.set.call(this, originDescriptor, value);
|
|
40
32
|
}
|
|
41
33
|
return originDescriptor.set(value);
|
|
42
34
|
},
|
|
43
|
-
get
|
|
35
|
+
get() {
|
|
44
36
|
if (spyProp.get) {
|
|
45
37
|
return spyProp.get.call(this, originDescriptor);
|
|
46
38
|
}
|
|
47
39
|
return originDescriptor.get();
|
|
48
40
|
},
|
|
49
41
|
configurable: true
|
|
50
|
-
})
|
|
42
|
+
});
|
|
51
43
|
}
|
|
52
44
|
});
|
|
53
45
|
return {
|
|
54
|
-
mockRestore
|
|
55
|
-
propNames.forEach(
|
|
56
|
-
|
|
46
|
+
mockRestore() {
|
|
47
|
+
propNames.forEach(propName => {
|
|
48
|
+
const originDescriptor = originDescriptors[propName];
|
|
57
49
|
if (originDescriptor === NO_EXIST) {
|
|
58
50
|
delete elementClass.prototype[propName];
|
|
59
51
|
} else if (typeof originDescriptor === 'function') {
|
|
@@ -66,6 +58,8 @@ function spyElementPrototypes(elementClass, properties) {
|
|
|
66
58
|
};
|
|
67
59
|
}
|
|
68
60
|
function spyElementPrototype(Element, propName, property) {
|
|
69
|
-
return spyElementPrototypes(Element,
|
|
61
|
+
return spyElementPrototypes(Element, {
|
|
62
|
+
[propName]: property
|
|
63
|
+
});
|
|
70
64
|
}
|
|
71
65
|
/* eslint-enable */
|
package/lib/utils/get.js
CHANGED
|
@@ -5,8 +5,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.default = get;
|
|
7
7
|
function get(entity, path) {
|
|
8
|
-
|
|
9
|
-
for (
|
|
8
|
+
let current = entity;
|
|
9
|
+
for (let i = 0; i < path.length; i += 1) {
|
|
10
10
|
if (current === null || current === undefined) {
|
|
11
11
|
return undefined;
|
|
12
12
|
}
|
package/lib/utils/set.js
CHANGED
|
@@ -7,35 +7,20 @@ exports.default = set;
|
|
|
7
7
|
exports.merge = merge;
|
|
8
8
|
var _get = _interopRequireDefault(require("./get"));
|
|
9
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
|
-
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
12
|
-
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
13
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
14
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
15
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
16
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
17
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
18
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
19
|
-
function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableRest(); }
|
|
20
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
21
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
22
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
23
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
24
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
25
10
|
function internalSet(entity, paths, value, removeIfUndefined) {
|
|
26
11
|
if (!paths.length) {
|
|
27
12
|
return value;
|
|
28
13
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
restPath = _paths.slice(1);
|
|
32
|
-
var clone;
|
|
14
|
+
const [path, ...restPath] = paths;
|
|
15
|
+
let clone;
|
|
33
16
|
if (!entity && typeof path === 'number') {
|
|
34
17
|
clone = [];
|
|
35
18
|
} else if (Array.isArray(entity)) {
|
|
36
|
-
clone =
|
|
19
|
+
clone = [...entity];
|
|
37
20
|
} else {
|
|
38
|
-
clone =
|
|
21
|
+
clone = {
|
|
22
|
+
...entity
|
|
23
|
+
};
|
|
39
24
|
}
|
|
40
25
|
|
|
41
26
|
// Delete prop if `removeIfUndefined` and value is undefined
|
|
@@ -46,8 +31,7 @@ function internalSet(entity, paths, value, removeIfUndefined) {
|
|
|
46
31
|
}
|
|
47
32
|
return clone;
|
|
48
33
|
}
|
|
49
|
-
function set(entity, paths, value) {
|
|
50
|
-
var removeIfUndefined = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
|
|
34
|
+
function set(entity, paths, value, removeIfUndefined = false) {
|
|
51
35
|
// Do nothing if `removeIfUndefined` and parent object not exist
|
|
52
36
|
if (paths.length && removeIfUndefined && value === undefined && !(0, _get.default)(entity, paths.slice(0, -1))) {
|
|
53
37
|
return entity;
|
|
@@ -55,40 +39,37 @@ function set(entity, paths, value) {
|
|
|
55
39
|
return internalSet(entity, paths, value, removeIfUndefined);
|
|
56
40
|
}
|
|
57
41
|
function isObject(obj) {
|
|
58
|
-
return
|
|
42
|
+
return typeof obj === 'object' && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
|
|
59
43
|
}
|
|
60
44
|
function createEmpty(source) {
|
|
61
45
|
return Array.isArray(source) ? [] : {};
|
|
62
46
|
}
|
|
63
|
-
|
|
47
|
+
const keys = typeof Reflect === 'undefined' ? Object.keys : Reflect.ownKeys;
|
|
64
48
|
|
|
65
49
|
/**
|
|
66
50
|
* Merge objects which will create
|
|
67
51
|
*/
|
|
68
|
-
function merge() {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
var clone = createEmpty(sources[0]);
|
|
73
|
-
sources.forEach(function (src) {
|
|
52
|
+
function merge(...sources) {
|
|
53
|
+
let clone = createEmpty(sources[0]);
|
|
54
|
+
sources.forEach(src => {
|
|
74
55
|
function internalMerge(path, parentLoopSet) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
56
|
+
const loopSet = new Set(parentLoopSet);
|
|
57
|
+
const value = (0, _get.default)(src, path);
|
|
58
|
+
const isArr = Array.isArray(value);
|
|
78
59
|
if (isArr || isObject(value)) {
|
|
79
60
|
// Only add not loop obj
|
|
80
61
|
if (!loopSet.has(value)) {
|
|
81
62
|
loopSet.add(value);
|
|
82
|
-
|
|
63
|
+
const originValue = (0, _get.default)(clone, path);
|
|
83
64
|
if (isArr) {
|
|
84
65
|
// Array will always be override
|
|
85
66
|
clone = set(clone, path, []);
|
|
86
|
-
} else if (!originValue ||
|
|
67
|
+
} else if (!originValue || typeof originValue !== 'object') {
|
|
87
68
|
// Init container if not exist
|
|
88
69
|
clone = set(clone, path, createEmpty(value));
|
|
89
70
|
}
|
|
90
|
-
keys(value).forEach(
|
|
91
|
-
internalMerge([
|
|
71
|
+
keys(value).forEach(key => {
|
|
72
|
+
internalMerge([...path, key], loopSet);
|
|
92
73
|
});
|
|
93
74
|
}
|
|
94
75
|
} else {
|
package/lib/warning.js
CHANGED
|
@@ -12,14 +12,14 @@ exports.resetWarned = resetWarned;
|
|
|
12
12
|
exports.warning = warning;
|
|
13
13
|
exports.warningOnce = warningOnce;
|
|
14
14
|
/* eslint-disable no-console */
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
let warned = {};
|
|
16
|
+
const preWarningFns = [];
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Pre warning enable you to parse content before console.error.
|
|
20
20
|
* Modify to null will prevent warning.
|
|
21
21
|
*/
|
|
22
|
-
|
|
22
|
+
const preMessage = fn => {
|
|
23
23
|
preWarningFns.push(fn);
|
|
24
24
|
};
|
|
25
25
|
|
|
@@ -34,13 +34,12 @@ var preMessage = exports.preMessage = function preMessage(fn) {
|
|
|
34
34
|
* warning(1 === 2, 'some error'); // print some error
|
|
35
35
|
* ```
|
|
36
36
|
*/
|
|
37
|
+
exports.preMessage = preMessage;
|
|
37
38
|
function warning(valid, message) {
|
|
38
39
|
if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
|
|
39
|
-
|
|
40
|
-
return preMessageFn(msg !== null && msg !== void 0 ? msg : '', 'warning');
|
|
41
|
-
}, message);
|
|
40
|
+
const finalMessage = preWarningFns.reduce((msg, preMessageFn) => preMessageFn(msg ?? '', 'warning'), message);
|
|
42
41
|
if (finalMessage) {
|
|
43
|
-
console.error(
|
|
42
|
+
console.error(`Warning: ${finalMessage}`);
|
|
44
43
|
}
|
|
45
44
|
}
|
|
46
45
|
}
|
|
@@ -48,11 +47,9 @@ function warning(valid, message) {
|
|
|
48
47
|
/** @see Similar to {@link warning} */
|
|
49
48
|
function note(valid, message) {
|
|
50
49
|
if (process.env.NODE_ENV !== 'production' && !valid && console !== undefined) {
|
|
51
|
-
|
|
52
|
-
return preMessageFn(msg !== null && msg !== void 0 ? msg : '', 'note');
|
|
53
|
-
}, message);
|
|
50
|
+
const finalMessage = preWarningFns.reduce((msg, preMessageFn) => preMessageFn(msg ?? '', 'note'), message);
|
|
54
51
|
if (finalMessage) {
|
|
55
|
-
console.warn(
|
|
52
|
+
console.warn(`Note: ${finalMessage}`);
|
|
56
53
|
}
|
|
57
54
|
}
|
|
58
55
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rc-component/util",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Common Utils For React Component",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"coverage": "npm test -- --coverage",
|
|
28
28
|
"lint": "eslint src/ --ext .tsx,.ts & eslint tests/ --ext .tsx,.ts",
|
|
29
29
|
"prepare": "husky install",
|
|
30
|
-
"prepublishOnly": "npm run compile ",
|
|
30
|
+
"prepublishOnly": "npm run compile && np --yolo --no-publish",
|
|
31
31
|
"start": "dumi dev",
|
|
32
32
|
"test": "rc-test"
|
|
33
33
|
},
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"react-is": "^18.2.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@rc-component/father-plugin": "
|
|
44
|
+
"@rc-component/father-plugin": "^2.0.1",
|
|
45
45
|
"@testing-library/react": "^16.0.0",
|
|
46
46
|
"@types/jest": "^29.4.0",
|
|
47
47
|
"@types/node": "^22.5.5",
|