Skip to content

Commit 4d731ba

Browse files
chore: move rc-util to @rc-component/util (#50)
* chore: move rc-util to @rc-components * update * fix * fix * Apply suggestion from @gemini-code-assist[bot] Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * remove babel * change comment --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 2c5614e commit 4d731ba

File tree

5 files changed

+31
-42
lines changed

5 files changed

+31
-42
lines changed

now.json

Lines changed: 0 additions & 11 deletions
This file was deleted.

package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@
2929
"coverage": "father test --coverage",
3030
"lint": "eslint src/ docs/ --ext .tsx,.ts,.jsx,.js",
3131
"now-build": "npm run build",
32-
"prepublishOnly": "npm run compile && np --yolo --no-publish",
32+
"prepublishOnly": "npm run compile && rc-np",
3333
"start": "dumi dev",
3434
"test": "rc-test",
3535
"tsc": "tsc --noEmit"
3636
},
3737
"dependencies": {
38-
"@babel/runtime": "^7.10.1",
39-
"rc-util": "^5.27.0"
38+
"@rc-component/util": "^1.3.0"
4039
},
4140
"devDependencies": {
42-
"@rc-component/father-plugin": "^1.0.0",
41+
"@rc-component/father-plugin": "^2.0.2",
42+
"@rc-component/np": "^1.0.4",
4343
"@types/jest": "^29.5.0",
4444
"@types/react": "^18.0.0",
4545
"@types/react-dom": "^18.0.0",
@@ -51,7 +51,6 @@
5151
"eslint-plugin-jest": "^28.2.0",
5252
"eslint-plugin-unicorn": "^52.0.0",
5353
"father": "^4.0.0",
54-
"np": "^10.0.4",
5554
"rc-test": "^7.0.14",
5655
"react": "^18.0.0",
5756
"react-dom": "^18.0.0",

src/Immutable.tsx

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
import { supportRef } from 'rc-util/lib/ref';
1+
import { supportRef } from '@rc-component/util/lib/ref';
22
import * as React from 'react';
33

44
export type CompareProps<T extends React.ComponentType<any>> = (
55
prevProps: Readonly<React.ComponentProps<T>>,
66
nextProps: Readonly<React.ComponentProps<T>>,
77
) => boolean;
88

9+
type ImmutableProps<T extends React.ComponentType<any>> = Omit<React.ComponentProps<T>, 'ref'>;
10+
911
/**
1012
* Create Immutable pair for `makeImmutable` and `responseImmutable`.
1113
*/
@@ -32,24 +34,24 @@ export default function createImmutable() {
3234
function makeImmutable<T extends React.ComponentType<any>>(
3335
Component: T,
3436
shouldTriggerRender?: CompareProps<T>,
35-
): T {
37+
): React.ComponentType<React.ComponentProps<T>> {
3638
const refAble = supportRef(Component);
3739

38-
const ImmutableComponent = function (props: any, ref: any) {
40+
const ImmutableComponent = (props: ImmutableProps<T>, ref: React.Ref<any>) => {
3941
const refProps = refAble ? { ref } : {};
4042
const renderTimesRef = React.useRef(0);
4143
const prevProps = React.useRef(props);
4244

4345
// If parent has the context, we do not wrap it
4446
const mark = useImmutableMark();
4547
if (mark !== null) {
46-
return <Component {...props} {...refProps} />;
48+
return <Component {...(props as any)} {...refProps} />;
4749
}
4850

4951
if (
50-
// Always trigger re-render if not provide `notTriggerRender`
52+
// Always trigger re-render if `shouldTriggerRender` is not provided
5153
!shouldTriggerRender ||
52-
shouldTriggerRender(prevProps.current, props)
54+
shouldTriggerRender(prevProps.current as any, props as any)
5355
) {
5456
renderTimesRef.current += 1;
5557
}
@@ -58,7 +60,7 @@ export default function createImmutable() {
5860

5961
return (
6062
<ImmutableContext.Provider value={renderTimesRef.current}>
61-
<Component {...props} {...refProps} />
63+
<Component {...(props as any)} {...refProps} />
6264
</ImmutableContext.Provider>
6365
);
6466
};
@@ -67,7 +69,9 @@ export default function createImmutable() {
6769
ImmutableComponent.displayName = `ImmutableRoot(${Component.displayName || Component.name})`;
6870
}
6971

70-
return refAble ? React.forwardRef(ImmutableComponent) : (ImmutableComponent as any);
72+
return refAble
73+
? (React.forwardRef(ImmutableComponent) as React.ComponentType<React.ComponentProps<T>>)
74+
: (ImmutableComponent as unknown as React.ComponentType<React.ComponentProps<T>>);
7175
}
7276

7377
/**
@@ -77,14 +81,13 @@ export default function createImmutable() {
7781
function responseImmutable<T extends React.ComponentType<any>>(
7882
Component: T,
7983
propsAreEqual?: CompareProps<T>,
80-
): T {
84+
): React.ComponentType<React.ComponentProps<T>> {
8185
const refAble = supportRef(Component);
8286

83-
const ImmutableComponent = function (props: any, ref: any) {
87+
const ImmutableComponent = (props: ImmutableProps<T>, ref: React.Ref<any>) => {
8488
const refProps = refAble ? { ref } : {};
8589
useImmutableMark();
86-
87-
return <Component {...props} {...refProps} />;
90+
return <Component {...(props as any)} {...refProps} />;
8891
};
8992

9093
if (process.env.NODE_ENV !== 'production') {
@@ -94,8 +97,12 @@ export default function createImmutable() {
9497
}
9598

9699
return refAble
97-
? React.memo(React.forwardRef(ImmutableComponent), propsAreEqual)
98-
: (React.memo(ImmutableComponent, propsAreEqual) as any);
100+
? (React.memo(React.forwardRef(ImmutableComponent), propsAreEqual) as React.ComponentType<
101+
React.ComponentProps<T>
102+
>)
103+
: (React.memo(ImmutableComponent, propsAreEqual) as unknown as React.ComponentType<
104+
React.ComponentProps<T>
105+
>);
99106
}
100107

101108
return {

src/context.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import useEvent from 'rc-util/lib/hooks/useEvent';
2-
import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect';
3-
import isEqual from 'rc-util/lib/isEqual';
1+
import useEvent from '@rc-component/util/lib/hooks/useEvent';
2+
import useLayoutEffect from '@rc-component/util/lib/hooks/useLayoutEffect';
3+
import isEqual from '@rc-component/util/lib/isEqual';
44
import * as React from 'react';
55
import { unstable_batchedUpdates } from 'react-dom';
66

tsconfig.json

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,9 @@
88
"skipLibCheck": true,
99
"esModuleInterop": true,
1010
"paths": {
11-
"@/*": [
12-
"src/*"
13-
],
14-
"@@/*": [
15-
".dumi/tmp/*"
16-
],
17-
"@rc-component/context": [
18-
"src/index.ts"
19-
]
11+
"@/*": ["src/*"],
12+
"@@/*": [".dumi/tmp/*"],
13+
"@rc-component/context": ["src/index.ts"]
2014
}
2115
},
2216
"include": [".dumi/**/*", ".dumirc.ts", "**/*.ts", "**/*.tsx"]

0 commit comments

Comments
 (0)