@rc-component/util 1.7.0 → 1.8.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/Portal.js +2 -2
- package/es/getScrollBarSize.d.ts +4 -2
- package/es/hooks/useEffect.d.ts +2 -1
- package/es/hooks/useEffect.js +3 -2
- package/es/hooks/useId.d.ts +9 -0
- package/es/hooks/useId.js +17 -0
- package/es/hooks/useState.d.ts +2 -2
- package/es/hooks/useState.js +3 -2
- package/es/hooks/useSyncState.d.ts +2 -2
- package/es/hooks/useSyncState.js +3 -2
- package/es/ref.d.ts +1 -1
- package/lib/Portal.js +2 -2
- package/lib/getScrollBarSize.d.ts +4 -2
- package/lib/hooks/useEffect.d.ts +2 -1
- package/lib/hooks/useEffect.js +3 -2
- package/lib/hooks/useId.d.ts +9 -0
- package/lib/hooks/useId.js +18 -0
- package/lib/hooks/useState.d.ts +2 -2
- package/lib/hooks/useState.js +4 -3
- package/lib/hooks/useSyncState.d.ts +2 -2
- package/lib/hooks/useSyncState.js +3 -2
- package/lib/ref.d.ts +1 -1
- package/package.json +4 -4
package/es/Portal.js
CHANGED
|
@@ -7,8 +7,8 @@ const Portal = /*#__PURE__*/forwardRef((props, ref) => {
|
|
|
7
7
|
getContainer,
|
|
8
8
|
children
|
|
9
9
|
} = props;
|
|
10
|
-
const parentRef = useRef();
|
|
11
|
-
const containerRef = useRef();
|
|
10
|
+
const parentRef = useRef(null);
|
|
11
|
+
const containerRef = useRef(null);
|
|
12
12
|
|
|
13
13
|
// Ref return nothing, only for wrapper check exist
|
|
14
14
|
useImperativeHandle(ref, () => ({}));
|
package/es/getScrollBarSize.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function getTargetScrollBarSize(target: HTMLElement): {
|
|
1
|
+
type ScrollBarSize = {
|
|
3
2
|
width: number;
|
|
4
3
|
height: number;
|
|
5
4
|
};
|
|
5
|
+
export default function getScrollBarSize(fresh?: boolean): number;
|
|
6
|
+
export declare function getTargetScrollBarSize(target: HTMLElement): ScrollBarSize;
|
|
7
|
+
export {};
|
package/es/hooks/useEffect.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
/** As `React.useEffect` but pass origin value in callback and not need care deps length change. */
|
|
2
|
-
|
|
2
|
+
declare function useEffect(callback: (prevDeps: any[]) => void, deps: any[]): void;
|
|
3
|
+
export default useEffect;
|
package/es/hooks/useEffect.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
|
|
3
3
|
/** As `React.useEffect` but pass origin value in callback and not need care deps length change. */
|
|
4
|
-
|
|
4
|
+
function useEffect(callback, deps) {
|
|
5
5
|
const prevRef = React.useRef(deps);
|
|
6
6
|
React.useEffect(() => {
|
|
7
7
|
if (deps.length !== prevRef.current.length || deps.some((dep, index) => dep !== prevRef.current[index])) {
|
|
@@ -9,4 +9,5 @@ export default function useEffect(callback, deps) {
|
|
|
9
9
|
}
|
|
10
10
|
prevRef.current = deps;
|
|
11
11
|
});
|
|
12
|
-
}
|
|
12
|
+
}
|
|
13
|
+
export default useEffect;
|
package/es/hooks/useId.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
1
2
|
/** @private Note only worked in develop env. Not work in production. */
|
|
2
3
|
export declare function resetUuid(): void;
|
|
4
|
+
/**
|
|
5
|
+
* Generate a valid HTML id from prefix and key.
|
|
6
|
+
* Sanitizes the key by replacing invalid characters with hyphens.
|
|
7
|
+
* @param prefix - The prefix for the id
|
|
8
|
+
* @param key - The key from React element, may contain spaces or invalid characters
|
|
9
|
+
* @returns A valid HTML id string
|
|
10
|
+
*/
|
|
11
|
+
export declare function getId(prefix: string, key: React.Key): string;
|
|
3
12
|
declare const _default: (id?: string) => string;
|
|
4
13
|
export default _default;
|
package/es/hooks/useId.js
CHANGED
|
@@ -14,6 +14,23 @@ export function resetUuid() {
|
|
|
14
14
|
uuid = 0;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Generate a valid HTML id from prefix and key.
|
|
20
|
+
* Sanitizes the key by replacing invalid characters with hyphens.
|
|
21
|
+
* @param prefix - The prefix for the id
|
|
22
|
+
* @param key - The key from React element, may contain spaces or invalid characters
|
|
23
|
+
* @returns A valid HTML id string
|
|
24
|
+
*/
|
|
25
|
+
export function getId(prefix, key) {
|
|
26
|
+
// React.Key can be string | number, convert to string first
|
|
27
|
+
const keyStr = String(key);
|
|
28
|
+
|
|
29
|
+
// Valid id characters: letters, digits, hyphen, underscore, colon, period
|
|
30
|
+
// Replace all invalid characters (including spaces) with hyphens to preserve length
|
|
31
|
+
const sanitizedKey = keyStr.replace(/[^a-zA-Z0-9_.:-]/g, '-');
|
|
32
|
+
return `${prefix}-${sanitizedKey}`;
|
|
33
|
+
}
|
|
17
34
|
const useOriginId = getUseId();
|
|
18
35
|
export default useOriginId ?
|
|
19
36
|
// Use React `useId`
|
package/es/hooks/useState.d.ts
CHANGED
|
@@ -10,5 +10,5 @@ ignoreDestroy?: boolean) => void;
|
|
|
10
10
|
* We do not make this auto is to avoid real memory leak.
|
|
11
11
|
* Developer should confirm it's safe to ignore themselves.
|
|
12
12
|
*/
|
|
13
|
-
|
|
14
|
-
export
|
|
13
|
+
declare const useSafeState: <T>(defaultValue?: T | (() => T)) => [T, SetState<T>];
|
|
14
|
+
export default useSafeState;
|
package/es/hooks/useState.js
CHANGED
|
@@ -4,7 +4,7 @@ import * as React from 'react';
|
|
|
4
4
|
* We do not make this auto is to avoid real memory leak.
|
|
5
5
|
* Developer should confirm it's safe to ignore themselves.
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
const useSafeState = defaultValue => {
|
|
8
8
|
const destroyRef = React.useRef(false);
|
|
9
9
|
const [value, setValue] = React.useState(defaultValue);
|
|
10
10
|
React.useEffect(() => {
|
|
@@ -20,4 +20,5 @@ export default function useSafeState(defaultValue) {
|
|
|
20
20
|
setValue(updater);
|
|
21
21
|
}
|
|
22
22
|
return [value, safeSetState];
|
|
23
|
-
}
|
|
23
|
+
};
|
|
24
|
+
export default useSafeState;
|
|
@@ -5,5 +5,5 @@ export type SetState<T> = (nextValue: Updater<T>) => void;
|
|
|
5
5
|
* This is useful when React merge multiple state updates into one.
|
|
6
6
|
* e.g. onTransitionEnd trigger multiple event at once will be merged state update in React.
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
export
|
|
8
|
+
declare function useSyncState<T>(defaultValue?: T): [get: () => T, set: SetState<T>];
|
|
9
|
+
export default useSyncState;
|
package/es/hooks/useSyncState.js
CHANGED
|
@@ -5,7 +5,7 @@ import useEvent from "./useEvent";
|
|
|
5
5
|
* This is useful when React merge multiple state updates into one.
|
|
6
6
|
* e.g. onTransitionEnd trigger multiple event at once will be merged state update in React.
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
function useSyncState(defaultValue) {
|
|
9
9
|
const [, forceUpdate] = React.useReducer(x => x + 1, 0);
|
|
10
10
|
const currentValueRef = React.useRef(defaultValue);
|
|
11
11
|
const getValue = useEvent(() => {
|
|
@@ -16,4 +16,5 @@ export default function useSyncState(defaultValue) {
|
|
|
16
16
|
forceUpdate();
|
|
17
17
|
});
|
|
18
18
|
return [getValue, setValue];
|
|
19
|
-
}
|
|
19
|
+
}
|
|
20
|
+
export default useSyncState;
|
package/es/ref.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export declare const supportRef: (nodeOrComponent: any) => boolean;
|
|
|
9
9
|
interface RefAttributes<T> extends React.Attributes {
|
|
10
10
|
ref: React.Ref<T>;
|
|
11
11
|
}
|
|
12
|
-
export declare const supportNodeRef: <T = any>(node: React.ReactNode) => node is React.ReactElement<
|
|
12
|
+
export declare const supportNodeRef: <T = any>(node: React.ReactNode) => node is React.ReactElement<unknown, string | React.JSXElementConstructor<any>> & RefAttributes<T>;
|
|
13
13
|
/**
|
|
14
14
|
* In React 19. `ref` is not a property from node.
|
|
15
15
|
* But a property from `props.ref`.
|
package/lib/Portal.js
CHANGED
|
@@ -14,8 +14,8 @@ const Portal = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
|
|
|
14
14
|
getContainer,
|
|
15
15
|
children
|
|
16
16
|
} = props;
|
|
17
|
-
const parentRef = (0, _react.useRef)();
|
|
18
|
-
const containerRef = (0, _react.useRef)();
|
|
17
|
+
const parentRef = (0, _react.useRef)(null);
|
|
18
|
+
const containerRef = (0, _react.useRef)(null);
|
|
19
19
|
|
|
20
20
|
// Ref return nothing, only for wrapper check exist
|
|
21
21
|
(0, _react.useImperativeHandle)(ref, () => ({}));
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function getTargetScrollBarSize(target: HTMLElement): {
|
|
1
|
+
type ScrollBarSize = {
|
|
3
2
|
width: number;
|
|
4
3
|
height: number;
|
|
5
4
|
};
|
|
5
|
+
export default function getScrollBarSize(fresh?: boolean): number;
|
|
6
|
+
export declare function getTargetScrollBarSize(target: HTMLElement): ScrollBarSize;
|
|
7
|
+
export {};
|
package/lib/hooks/useEffect.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
/** As `React.useEffect` but pass origin value in callback and not need care deps length change. */
|
|
2
|
-
|
|
2
|
+
declare function useEffect(callback: (prevDeps: any[]) => void, deps: any[]): void;
|
|
3
|
+
export default useEffect;
|
package/lib/hooks/useEffect.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default =
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
var React = _interopRequireWildcard(require("react"));
|
|
8
8
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
9
9
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
@@ -16,4 +16,5 @@ function useEffect(callback, deps) {
|
|
|
16
16
|
}
|
|
17
17
|
prevRef.current = deps;
|
|
18
18
|
});
|
|
19
|
-
}
|
|
19
|
+
}
|
|
20
|
+
var _default = exports.default = useEffect;
|
package/lib/hooks/useId.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
1
2
|
/** @private Note only worked in develop env. Not work in production. */
|
|
2
3
|
export declare function resetUuid(): void;
|
|
4
|
+
/**
|
|
5
|
+
* Generate a valid HTML id from prefix and key.
|
|
6
|
+
* Sanitizes the key by replacing invalid characters with hyphens.
|
|
7
|
+
* @param prefix - The prefix for the id
|
|
8
|
+
* @param key - The key from React element, may contain spaces or invalid characters
|
|
9
|
+
* @returns A valid HTML id string
|
|
10
|
+
*/
|
|
11
|
+
export declare function getId(prefix: string, key: React.Key): string;
|
|
3
12
|
declare const _default: (id?: string) => string;
|
|
4
13
|
export default _default;
|
package/lib/hooks/useId.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
+
exports.getId = getId;
|
|
7
8
|
exports.resetUuid = resetUuid;
|
|
8
9
|
var React = _interopRequireWildcard(require("react"));
|
|
9
10
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
@@ -23,6 +24,23 @@ function resetUuid() {
|
|
|
23
24
|
uuid = 0;
|
|
24
25
|
}
|
|
25
26
|
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Generate a valid HTML id from prefix and key.
|
|
30
|
+
* Sanitizes the key by replacing invalid characters with hyphens.
|
|
31
|
+
* @param prefix - The prefix for the id
|
|
32
|
+
* @param key - The key from React element, may contain spaces or invalid characters
|
|
33
|
+
* @returns A valid HTML id string
|
|
34
|
+
*/
|
|
35
|
+
function getId(prefix, key) {
|
|
36
|
+
// React.Key can be string | number, convert to string first
|
|
37
|
+
const keyStr = String(key);
|
|
38
|
+
|
|
39
|
+
// Valid id characters: letters, digits, hyphen, underscore, colon, period
|
|
40
|
+
// Replace all invalid characters (including spaces) with hyphens to preserve length
|
|
41
|
+
const sanitizedKey = keyStr.replace(/[^a-zA-Z0-9_.:-]/g, '-');
|
|
42
|
+
return `${prefix}-${sanitizedKey}`;
|
|
43
|
+
}
|
|
26
44
|
const useOriginId = getUseId();
|
|
27
45
|
var _default = exports.default = useOriginId ?
|
|
28
46
|
// Use React `useId`
|
package/lib/hooks/useState.d.ts
CHANGED
|
@@ -10,5 +10,5 @@ ignoreDestroy?: boolean) => void;
|
|
|
10
10
|
* We do not make this auto is to avoid real memory leak.
|
|
11
11
|
* Developer should confirm it's safe to ignore themselves.
|
|
12
12
|
*/
|
|
13
|
-
|
|
14
|
-
export
|
|
13
|
+
declare const useSafeState: <T>(defaultValue?: T | (() => T)) => [T, SetState<T>];
|
|
14
|
+
export default useSafeState;
|
package/lib/hooks/useState.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default =
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
var React = _interopRequireWildcard(require("react"));
|
|
8
8
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
9
9
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
@@ -12,7 +12,7 @@ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e;
|
|
|
12
12
|
* We do not make this auto is to avoid real memory leak.
|
|
13
13
|
* Developer should confirm it's safe to ignore themselves.
|
|
14
14
|
*/
|
|
15
|
-
|
|
15
|
+
const useSafeState = defaultValue => {
|
|
16
16
|
const destroyRef = React.useRef(false);
|
|
17
17
|
const [value, setValue] = React.useState(defaultValue);
|
|
18
18
|
React.useEffect(() => {
|
|
@@ -28,4 +28,5 @@ function useSafeState(defaultValue) {
|
|
|
28
28
|
setValue(updater);
|
|
29
29
|
}
|
|
30
30
|
return [value, safeSetState];
|
|
31
|
-
}
|
|
31
|
+
};
|
|
32
|
+
var _default = exports.default = useSafeState;
|
|
@@ -5,5 +5,5 @@ export type SetState<T> = (nextValue: Updater<T>) => void;
|
|
|
5
5
|
* This is useful when React merge multiple state updates into one.
|
|
6
6
|
* e.g. onTransitionEnd trigger multiple event at once will be merged state update in React.
|
|
7
7
|
*/
|
|
8
|
-
|
|
9
|
-
export
|
|
8
|
+
declare function useSyncState<T>(defaultValue?: T): [get: () => T, set: SetState<T>];
|
|
9
|
+
export default useSyncState;
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default =
|
|
6
|
+
exports.default = void 0;
|
|
7
7
|
var React = _interopRequireWildcard(require("react"));
|
|
8
8
|
var _useEvent = _interopRequireDefault(require("./useEvent"));
|
|
9
9
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -25,4 +25,5 @@ function useSyncState(defaultValue) {
|
|
|
25
25
|
forceUpdate();
|
|
26
26
|
});
|
|
27
27
|
return [getValue, setValue];
|
|
28
|
-
}
|
|
28
|
+
}
|
|
29
|
+
var _default = exports.default = useSyncState;
|
package/lib/ref.d.ts
CHANGED
|
@@ -9,7 +9,7 @@ export declare const supportRef: (nodeOrComponent: any) => boolean;
|
|
|
9
9
|
interface RefAttributes<T> extends React.Attributes {
|
|
10
10
|
ref: React.Ref<T>;
|
|
11
11
|
}
|
|
12
|
-
export declare const supportNodeRef: <T = any>(node: React.ReactNode) => node is React.ReactElement<
|
|
12
|
+
export declare const supportNodeRef: <T = any>(node: React.ReactNode) => node is React.ReactElement<unknown, string | React.JSXElementConstructor<any>> & RefAttributes<T>;
|
|
13
13
|
/**
|
|
14
14
|
* In React 19. `ref` is not a property from node.
|
|
15
15
|
* But a property from `props.ref`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rc-component/util",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.8.1",
|
|
4
4
|
"description": "Common Utils For React Component",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
@@ -47,13 +47,13 @@
|
|
|
47
47
|
"@testing-library/react": "^16.0.0",
|
|
48
48
|
"@types/jest": "^30.0.0",
|
|
49
49
|
"@types/node": "^24.6.1",
|
|
50
|
-
"@types/react": "^
|
|
51
|
-
"@types/react-dom": "^
|
|
50
|
+
"@types/react": "^19.2.7",
|
|
51
|
+
"@types/react-dom": "^19.2.2",
|
|
52
52
|
"@types/react-is": "^19.0.0",
|
|
53
53
|
"@types/responselike": "^1.0.0",
|
|
54
54
|
"@types/warning": "^3.0.0",
|
|
55
55
|
"@umijs/fabric": "^3.0.0",
|
|
56
|
-
"cross-env": "^
|
|
56
|
+
"cross-env": "^10.1.0",
|
|
57
57
|
"dumi": "^2.1.3",
|
|
58
58
|
"eslint": "^8.54.0",
|
|
59
59
|
"eslint-plugin-jest": "^29.0.1",
|