-
Notifications
You must be signed in to change notification settings - Fork 7
chore: move rc-util to @rc-component/util #50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9c75794
chore: move rc-util to @rc-components
yoyo837 5fe3fdc
update
yoyo837 71eeb29
fix
yoyo837 89ae370
fix
yoyo837 0a1eda0
Apply suggestion from @gemini-code-assist[bot]
yoyo837 86ecf9c
remove babel
yoyo837 bc162f4
change comment
yoyo837 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import { supportRef } from 'rc-util/lib/ref'; | ||
| import { supportRef } from '@rc-component/util/lib/ref'; | ||
| import * as React from 'react'; | ||
|
|
||
| export type CompareProps<T extends React.ComponentType<any>> = ( | ||
| prevProps: Readonly<React.ComponentProps<T>>, | ||
| nextProps: Readonly<React.ComponentProps<T>>, | ||
| ) => boolean; | ||
|
|
||
| type ImmutableProps<T extends React.ComponentType<any>> = Omit<React.ComponentProps<T>, 'ref'>; | ||
|
|
||
| /** | ||
| * Create Immutable pair for `makeImmutable` and `responseImmutable`. | ||
| */ | ||
|
|
@@ -32,24 +34,24 @@ export default function createImmutable() { | |
| function makeImmutable<T extends React.ComponentType<any>>( | ||
| Component: T, | ||
| shouldTriggerRender?: CompareProps<T>, | ||
| ): T { | ||
| ): React.ComponentType<React.ComponentProps<T>> { | ||
| const refAble = supportRef(Component); | ||
|
|
||
| const ImmutableComponent = function (props: any, ref: any) { | ||
| const ImmutableComponent = (props: ImmutableProps<T>, ref: React.Ref<any>) => { | ||
| const refProps = refAble ? { ref } : {}; | ||
| const renderTimesRef = React.useRef(0); | ||
| const prevProps = React.useRef(props); | ||
|
|
||
| // If parent has the context, we do not wrap it | ||
| const mark = useImmutableMark(); | ||
| if (mark !== null) { | ||
| return <Component {...props} {...refProps} />; | ||
| return <Component {...(props as any)} {...refProps} />; | ||
| } | ||
|
|
||
| if ( | ||
| // Always trigger re-render if not provide `notTriggerRender` | ||
| // Always trigger re-render if `shouldTriggerRender` is not provided | ||
| !shouldTriggerRender || | ||
| shouldTriggerRender(prevProps.current, props) | ||
| shouldTriggerRender(prevProps.current as any, props as any) | ||
| ) { | ||
| renderTimesRef.current += 1; | ||
| } | ||
|
|
@@ -58,7 +60,7 @@ export default function createImmutable() { | |
|
|
||
| return ( | ||
| <ImmutableContext.Provider value={renderTimesRef.current}> | ||
| <Component {...props} {...refProps} /> | ||
| <Component {...(props as any)} {...refProps} /> | ||
| </ImmutableContext.Provider> | ||
| ); | ||
| }; | ||
|
|
@@ -67,7 +69,9 @@ export default function createImmutable() { | |
| ImmutableComponent.displayName = `ImmutableRoot(${Component.displayName || Component.name})`; | ||
| } | ||
|
|
||
| return refAble ? React.forwardRef(ImmutableComponent) : (ImmutableComponent as any); | ||
| return refAble | ||
| ? (React.forwardRef(ImmutableComponent) as React.ComponentType<React.ComponentProps<T>>) | ||
| : (ImmutableComponent as unknown as React.ComponentType<React.ComponentProps<T>>); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -77,14 +81,13 @@ export default function createImmutable() { | |
| function responseImmutable<T extends React.ComponentType<any>>( | ||
| Component: T, | ||
| propsAreEqual?: CompareProps<T>, | ||
| ): T { | ||
| ): React.ComponentType<React.ComponentProps<T>> { | ||
| const refAble = supportRef(Component); | ||
|
Comment on lines
81
to
85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major responseImmutable 同样存在 ref 与类型坍缩问题;memo 比较函数的入参与内部 props 类型不一致
建议:添加重载以保留 - function responseImmutable<T extends React.ComponentType<any>>(
- Component: T,
- propsAreEqual?: CompareProps<T>,
- ): React.ComponentType<React.ComponentProps<T>> {
+ // Overloads: preserve accurate exotic component types
+ function responseImmutable<T extends React.ForwardRefExoticComponent<any>>(
+ Component: T,
+ propsAreEqual?: CompareProps<T>,
+ ): React.MemoExoticComponent<T>;
+ function responseImmutable<T extends React.ComponentType<any>>(
+ Component: T,
+ propsAreEqual?: CompareProps<T>,
+ ): React.MemoExoticComponent<React.ComponentType<React.ComponentProps<T>>>;
+ function responseImmutable<
+ T extends React.ComponentType<any> | React.ForwardRefExoticComponent<any>
+ >(Component: T, propsAreEqual?: CompareProps<any>) {
@@
- return refAble
- ? (React.memo(React.forwardRef(ImmutableComponent), propsAreEqual) as React.ComponentType<
- React.ComponentProps<T>
- >)
- : (React.memo(ImmutableComponent, propsAreEqual) as unknown as React.ComponentType<
- React.ComponentProps<T>
- >);
+ return refAble
+ ? React.memo(React.forwardRef(ImmutableComponent), propsAreEqual)
+ : React.memo(ImmutableComponent, propsAreEqual);
}Also applies to: 100-105 🤖 Prompt for AI Agents |
||
|
|
||
| const ImmutableComponent = function (props: any, ref: any) { | ||
| const ImmutableComponent = (props: ImmutableProps<T>, ref: React.Ref<any>) => { | ||
| const refProps = refAble ? { ref } : {}; | ||
| useImmutableMark(); | ||
|
|
||
| return <Component {...props} {...refProps} />; | ||
| return <Component {...(props as any)} {...refProps} />; | ||
| }; | ||
|
|
||
| if (process.env.NODE_ENV !== 'production') { | ||
|
|
@@ -94,8 +97,12 @@ export default function createImmutable() { | |
| } | ||
|
|
||
| return refAble | ||
| ? React.memo(React.forwardRef(ImmutableComponent), propsAreEqual) | ||
| : (React.memo(ImmutableComponent, propsAreEqual) as any); | ||
| ? (React.memo(React.forwardRef(ImmutableComponent), propsAreEqual) as React.ComponentType< | ||
| React.ComponentProps<T> | ||
| >) | ||
| : (React.memo(ImmutableComponent, propsAreEqual) as unknown as React.ComponentType< | ||
| React.ComponentProps<T> | ||
| >); | ||
| } | ||
|
|
||
| return { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
返回类型从原始组件坍缩为 ComponentType 导致 ref 能力丢失,且禁止直接传入 forwardRef 组件
T extends React.ComponentType<any>会拒绝React.ForwardRefExoticComponent与部分ExoticComponent,这在库场景里是破坏性的。React.ComponentType<...>会丢失ref的类型信息(消费端<Comp ref=... />可能报错/无提示)。建议:为
makeImmutable提供函数重载以允许forwardRef入参,并在refAble分支返回真实的ForwardRefExoticComponent,避免丢失ref类型。Also applies to: 72-75