-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[TS migration] Migrate Hoverable #30339
[TS migration] Migrate Hoverable #30339
Conversation
src/components/Hoverable/index.tsx
Outdated
function mapChildren(children: ((isHovered: boolean) => ReactElement) | ReactElement | ReactElement[], callbackParam: boolean): ReactElement & RefAttributes<HTMLElement> { | ||
if (Array.isArray(children) && children.length === 1) { | ||
return children[0]; | ||
} | ||
|
||
if (typeof children === 'function') { | ||
return children(callbackParam); | ||
} | ||
|
||
return children as ReactElement; | ||
} |
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.
Either change it to:
function mapChildren(children: ((isHovered: boolean) => ReactElement) | ReactElement | ReactElement[], callbackParam: boolean): ReactElement & RefAttributes<HTMLElement> {
if (Array.isArray(children)) {
return children[0];
}
if (typeof children === 'function') {
return children(callbackParam);
}
return children;
}
or
function mapChildren(children: ((isHovered: boolean) => ReactElement) | ReactElement | ReactElement[], callbackParam: boolean): (ReactElement | ReactElement[]) & RefAttributes<HTMLElement> {
if (Array.isArray(children) && children.length === 1) {
return children[0];
}
if (typeof children === 'function') {
return children(callbackParam);
}
return children;
}
src/components/Hoverable/index.tsx
Outdated
* because nesting Pressables causes issues where the hovered state of the child cannot be easily propagated to the | ||
* parent. https://github.com/necolas/react-native-web/issues/1875 | ||
*/ | ||
const Hoverable = React.forwardRef<HTMLElement, HoverableProps>( |
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.
Improve it stylistically:
function Hoverable(
{disabled = false, onHoverIn = () => {}, onHoverOut = () => {}, onMouseEnter = () => {}, onMouseLeave = () => {}, children, shouldHandleScroll = false}: HoverableProps,
outerRef: ForwardedRef<HTMLElement>,
)
and move forwardRef to the export line:
export default forwardRef(Hoverable);
This is more accurate with TS guidelines
src/components/Hoverable/index.tsx
Outdated
function assignRef(ref: MutableRefObject<HTMLElement> | ((element: HTMLElement) => void), element: HTMLElement) { | ||
if (!ref) { | ||
return; | ||
} | ||
|
||
if (typeof ref === 'function') { | ||
ref(element); | ||
} else if (_.has(ref, 'current')) { | ||
// eslint-disable-next-line no-param-reassign | ||
ref.current = element; | ||
} | ||
} |
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.
Remove lodash and improve param types:
function assignRef(ref: MutableRefObject<HTMLElement> | ((element: HTMLElement) => void), element: HTMLElement) { | |
if (!ref) { | |
return; | |
} | |
if (typeof ref === 'function') { | |
ref(element); | |
} else if (_.has(ref, 'current')) { | |
// eslint-disable-next-line no-param-reassign | |
ref.current = element; | |
} | |
} | |
function assignRef(ref: ((instance: HTMLElement | null) => void) | MutableRefObject<HTMLElement | null>, element: HTMLElement) { | |
if (!ref) { | |
return; | |
} | |
if (typeof ref === 'function') { | |
ref(element); | |
} else if (ref?.current) { | |
// eslint-disable-next-line no-param-reassign | |
ref.current = element; | |
} | |
} |
src/components/Hoverable/index.tsx
Outdated
const hijackRef = (el: HTMLElement) => { | ||
ref.current = el; | ||
if (child.ref) { | ||
assignRef(child.ref as MutableRefObject<HTMLElement>, el); |
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.
assignRef(child.ref as MutableRefObject<HTMLElement>, el); | |
assignRef(child.ref, el); |
Thanks @blazejkustra! Suggestions implemented. Looks better now. |
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.
lgtm 🚀
@allroundexperts Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
Reviewer Checklist
Screenshots/VideosAndroid: NativeScreen.Recording.2023-11-13.at.2.14.28.PM.movAndroid: mWeb ChromeScreen.Recording.2023-11-13.at.2.13.43.PM.moviOS: NativeUploading Screen Recording 2023-11-13 at 2.12.43 PM.mov… iOS: mWeb SafariScreen.Recording.2023-11-13.at.2.12.07.PM.movMacOS: Chrome / SafariScreen.Recording.2023-11-13.at.2.04.18.PM.movMacOS: DesktopScreen.Recording.2023-11-13.at.2.10.07.PM.mov |
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.
Looks good!
We did not find an internal engineer to review this PR, trying to assign a random engineer to #25042 as well as to this PR... Please reach out for help on Slack if no one gets assigned! |
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
👋 Hey everyone A little bit more context in https://expensify.slack.com/archives/C049HHMV9SM/p1699908253180059 |
if (_.has(ref, 'current')) { | ||
if (typeof ref === 'function') { | ||
ref(element); | ||
} else if (ref?.current) { |
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.
This was overlooked. if (_.has(ref, 'current')) {
is equivalent to:
} else if (ref?.current) { | |
} else if ('current' in ref) { |
Fixing here: #31306
🚀 Deployed to staging by https://github.com/AndrewGable in version: 1.3.99-0 🚀
|
🚀 Deployed to production by https://github.com/roryabraham in version: 1.3.99-0 🚀
|
🚀 Deployed to staging by https://github.com/AndrewGable in version: 1.4.0-0 🚀
|
Details
The Hoverable component is used in multiple places:
Fixed Issues
$ #25042
PROPOSAL:
Tests
Offline tests
Tests
QA Steps
Tests
PR Author Checklist
### Fixed Issues
section aboveTests
sectionOffline steps
sectionQA steps
sectiontoggleReport
and notonIconClick
)myBool && <MyComponent />
.src/languages/*
files and using the translation methodWaiting for Copy
label for a copy review on the original GH to get the correct copy.STYLE.md
) were followedAvatar
, I verified the components usingAvatar
are working as expected)/** comment above it */
this
properly so there are no scoping issues (i.e. foronClick={this.submit}
the methodthis.submit
should be bound tothis
in the constructor)this
are necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);
ifthis.submit
is never passed to a component event handler likeonClick
)StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
)Avatar
is modified, I verified thatAvatar
is working as expected in all cases)ScrollView
component to make it scrollable when more elements are added to the page.main
branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTest
steps.Screenshots/Videos
Android: Native
Screen.Recording.2023-11-07.at.13.09.05.mov
Android: mWeb Chrome
Screen.Recording.2023-11-07.at.12.43.12.mov
iOS: Native
Screen.Recording.2023-11-07.at.13.09.05.mov
iOS: mWeb Safari
Screen.Recording.2023-11-07.at.12.43.12.mov
MacOS: Chrome / Safari
Screen.Recording.2023-11-07.at.12.34.55.mov
MacOS: Desktop
Screen.Recording.2023-11-07.at.12.40.37.mov