Skip to content
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

Fixed correct original props callback on cloneElement #15990

Merged
merged 5 commits into from
Mar 27, 2023

Conversation

s77rt
Copy link
Contributor

@s77rt s77rt commented Mar 15, 2023

@iwiznia

Details

Quick Fix: Just found out that the original callbacks are not being called correctly.

Fixed Issues

$ #16084

Tests

  1. Login to App
  2. Go to any chat
  3. [Web Only] Verify that you can hover messages (background changes)
  4. [Web Only] Verify that tooltips on the mini context menu works correctly (tooltip should show on hover)
  5. Go to Settings -> Payments then click "Add payment method" button and select "Debit card"
  6. Verify that the form function normally (and if you keep a field empty an error message should be shown)
  • Verify that no errors appear in the JS console

Offline tests

n/a

QA Steps

  1. Login to App
  2. Go to any chat
  3. [Web Only] Verify that you can hover messages (background changes)
  4. [Web Only] Verify that tooltips on the mini context menu works correctly (tooltip should show on hover)
  5. Go to Settings -> Payments then click "Add payment method" button and select "Debit card"
  6. Verify that the form function normally (and if you keep a field empty an error message should be shown)
  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed Issues section above
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
      • If any non-english text was added/modified, I verified the translation was requested/reviewed in #expensify-open-source and it was approved by an internal Expensify engineer. Link to Slack message:
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is correct English and approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Web
web.mp4
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

@s77rt s77rt requested a review from a team as a code owner March 15, 2023 14:30
@melvin-bot melvin-bot bot requested review from cead22 and removed request for a team March 15, 2023 14:31
@MelvinBot
Copy link

@cead22 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]

@s77rt
Copy link
Contributor Author

s77rt commented Mar 15, 2023

cc @iwiznia

Copy link
Contributor

@iwiznia iwiznia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry but I am not sure I get it, aren't both codes equivalent? Is the problem that the previous code did not have the correct context attached to the function and thus if those use this, it would break?

@s77rt
Copy link
Contributor Author

s77rt commented Mar 15, 2023

@iwiznia They are not equivalent, it's not about the context, this plays no role here. The difference is:

  • Before: Calling child.METHOD
  • After: Calling child.props.METHOD

The methods are inside props. Check the return value documented here https://beta.reactjs.org/reference/react/cloneElement#returns

@iwiznia
Copy link
Contributor

iwiznia commented Mar 15, 2023

Ohhh 🤦 I missed that 😄

iwiznia
iwiznia previously approved these changes Mar 15, 2023
@s77rt
Copy link
Contributor Author

s77rt commented Mar 15, 2023

@iwiznia Thanks for the quick review

@s77rt
Copy link
Contributor Author

s77rt commented Mar 17, 2023

@iwiznia Can you please fill the PR reviewer checklist?

@s77rt
Copy link
Contributor Author

s77rt commented Mar 17, 2023

I have pushed a commit that fixes the same issue for Form.js.

The bug is the following piece of code will never work as you'd expect

<Form>
    <TextInput ref={r => this.texInput = r} onBlur={this.callThePolice} />
</Form>

this.texInput will never be set and this.callThePolice will never be called.

This PR should fix that as well.

@cead22
Copy link
Contributor

cead22 commented Mar 21, 2023

@s77rt can you add tests to make sure that each piece of the code you touched is working correctly?

@MelvinBot
Copy link

Hey! I see that you made changes to our Form component. Make sure to update the docs in FORMS.md accordingly. Cheers!

@s77rt
Copy link
Contributor Author

s77rt commented Mar 21, 2023

@cead22 The changes will mostly prevent future bugs and save debug time more than solve a current bug. I have added a video for Web confirming Hoverable, Tooltip and Form are working correctly. I think that would be enough for this case, LMK if something else is needed.

@MelvinBot I'm not modifying anything major on Forms, nothing to be updated here.

@mountiny
Copy link
Contributor

@s77rt I agree with Carlos, it would be good to just note it in general in the QA steps for Applause to go over once this is in staging

@s77rt
Copy link
Contributor Author

s77rt commented Mar 21, 2023

Oh my bad, somehow I read it "tests" but my mind read it "screenshots/videos" 😅
Adding tests...

@s77rt
Copy link
Contributor Author

s77rt commented Mar 21, 2023

Added tests.

@cead22
Copy link
Contributor

cead22 commented Mar 22, 2023

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified tests pass on all platforms & I tested again on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
    • MacOS / Desktop
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified any copy / text shown in the product is localized by adding it to src/languages/* files and using the translation method
    • I verified all numbers, amounts, dates and phone numbers shown in the product are using the localization methods
    • I verified any copy / text that was added to the app is correct English and approved by marketing by adding the Waiting for Copy label for a copy review on the original GH to get the correct copy.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(themeColors.componentBG)
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If a new page is added, I verified it's using the ScrollView component to make it scrollable when more elements are added to the page.
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

Web ![](http://g.recordit.co/jcsbZMsYa0.gif)
Mobile Web - Chrome
Mobile Web - Safari
Desktop
iOS
Android

@cead22
Copy link
Contributor

cead22 commented Mar 22, 2023

@s77rt I found a new error in the console when clicking on an IOU badge on the left hand navigation, and then clicking the cancel button. I tried a couple of times in main and I couldn't reproduce that same issue. Can you check if you get the same result?

Warning: Failed prop type: Invalid prop `pendingAction` of value `[object Object]` supplied to `OfflineWithFeedback`, expected one of ["add","update","delete"].
Warning: Failed prop type: Invalid prop `pendingAction` of value `[object Object]` supplied to `OfflineWithFeedback`, expected one of ["add","update","delete"].
    at OfflineWithFeedback (webpack-internal:///./src/components/OfflineWithFeedback.js:101:85)
    at withNetwork(OfflineWithFeedback)
    at withLocalize(withNetwork(OfflineWithFeedback))
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at TouchableWithoutFeedback (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/TouchableWithoutFeedback/index.js:46:28)
    at IOUPreview (webpack-internal:///./src/components/ReportActionItem/IOUPreview.js:144:13)
    at withOnyx (webpack-internal:///./node_modules/react-native-onyx/dist/web.development.js:2135:73)
    at withLocalize(Component)
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/ScrollView/ScrollViewBase.js:68:24)
    at eval (webpack-internal:///./node_modules/create-react-class/factory.js:897:37)
    at ScrollView
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at FullPageNotFoundView (webpack-internal:///./src/components/BlockingViews/FullPageNotFoundView.js:65:13)
    at withLocalize(FullPageNotFoundView)
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at KeyboardAvoidingView
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at SafeAreaConsumer
    at ScreenWrapper (webpack-internal:///./src/components/ScreenWrapper/index.js:69:86)
    at withNetwork(ScreenWrapper)
    at withOnyx (webpack-internal:///./node_modules/react-native-onyx/dist/web.development.js:2135:73)
    at withKeyboardState(Component)
    at withWindowDimensions(withKeyboardState(Component))
    at WithNavigation (webpack-internal:///./src/components/withNavigation.js:23:93)
    at IOUDetailsModal (webpack-internal:///./src/pages/iou/IOUDetailsModal.js:128:86)
    at withOnyx (webpack-internal:///./node_modules/react-native-onyx/dist/web.development.js:2135:73)
    at withNetwork(Component)
    at withLocalize(withNetwork(Component))
    at StaticContainer (webpack-internal:///./node_modules/@react-navigation/core/lib/module/StaticContainer.js:13:16)
    at EnsureSingleNavigator (webpack-internal:///./node_modules/@react-navigation/core/lib/module/EnsureSingleNavigator.js:17:5)
    at SceneView (webpack-internal:///./node_modules/@react-navigation/core/lib/module/SceneView.js:23:5)
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at CardSheet (webpack-internal:///./node_modules/@react-navigation/stack/lib/module/views/Stack/CardSheet.js:19:5)
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at AnimatedComponent (webpack-internal:///./node_modules/@expensify/react-native-web/dist/vendor/react-native/Animated/createAnimatedComponent.js:59:88)
    at AnimatedComponentWrapper
    at Dummy (webpack-internal:///./node_modules/@react-navigation/stack/lib/module/views/GestureHandler.js:15:5)
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at AnimatedComponent (webpack-internal:///./node_modules/@expensify/react-native-web/dist/vendor/react-native/Animated/createAnimatedComponent.js:59:88)
    at AnimatedComponentWrapper
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at Card (webpack-internal:///./node_modules/@react-navigation/stack/lib/module/views/Stack/Card.js:60:5)
    at CardContainer (webpack-internal:///./node_modules/@react-navigation/stack/lib/module/views/Stack/CardContainer.js:28:5)
    at div
    at eval (webpack-internal:///./node_modules/@expensify/react-native-web/dist/exports/View/index.js:52:25)
    at NativeScreen (webpack-internal:///./node_modules/react-native-screens/lib/module/index.js:58:1)
    at AnimatedComponent (webpack-internal:///./node_modules/@expensify/react-native-web/dist/vendor/react-native/Animated/createAnimatedComponent.js:59:88)
    at AnimatedComponentWrapper

@cead22
Copy link
Contributor

cead22 commented Mar 22, 2023

I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick).

Am I correct that onBlur, onMouseLeave, etc are standard function names that we can't/shouldn't change?

Copy link
Contributor

@cead22 cead22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After going through the reviewer checklist, I wanted to call out a couple of things

src/components/Form.js Outdated Show resolved Hide resolved

// Call the original ref, if any
const {ref} = child;
if (_.isFunction(ref)) {
Copy link
Contributor

@cead22 cead22 Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)

We do this check in many places, where we check if something is a function and if it is we invoke it. Should we replace these instances with Func.invoke like Func.invoke(ref)?
https://github.com/Expensify/expensify-common/blob/36d6f23/lib/Func.jsx#L13

Copy link
Contributor Author

@s77rt s77rt Mar 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary a repeated logic. Although Func.invoke makes the block of code in one line, it may be confusing; We will have to call Func.invoke(undefined, [ref]). Notice that the arguments must be in an array which is an easy thing to miss.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't block on this, since this duplication is already here, but here's why I don't think I agree, just for context

  • Just in this PR, we could replace 3 lines of code with 1, in 5 places, so I don't see why this wouldn't be considered repeated logic
  • We'll have to call Func.invoke(red, [node]) in this case, the params being in an array is as easy to miss as any other piece of code, like passing node to ref(node). Passing arguments to a function in an array is pretty standard in JS when using Function.prototype.apply

@s77rt
Copy link
Contributor Author

s77rt commented Mar 22, 2023

@cead22 Thanks for the review!


I found a new error in the console

I can't reproduce that. Can you repeatedly reproduce it? I don't think it's related to the change here and was mostly a coincidence due to some other factors.


Am I correct that onBlur, onMouseLeave, etc are standard function names that we can't/shouldn't change?

No, the checklist you quoted is referring to the callbacks of those functions, they should have a clear naming of what they do. e.g.:

  • onMouseLeave={this.onMouseLeave} => ❌ Bad
  • onMouseLeave={this.onButtonMouseLeave} => ❌ Bad
  • onMouseLeave={this.hideTooltip} => ✔️ Good

cead22
cead22 previously approved these changes Mar 23, 2023
Copy link
Contributor

@cead22 cead22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't reproduce the console error anymore!


// Call the original ref, if any
const {ref} = child;
if (_.isFunction(ref)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't block on this, since this duplication is already here, but here's why I don't think I agree, just for context

  • Just in this PR, we could replace 3 lines of code with 1, in 5 places, so I don't see why this wouldn't be considered repeated logic
  • We'll have to call Func.invoke(red, [node]) in this case, the params being in an array is as easy to miss as any other piece of code, like passing node to ref(node). Passing arguments to a function in an array is pretty standard in JS when using Function.prototype.apply

@cead22
Copy link
Contributor

cead22 commented Mar 23, 2023

@iwiznia all yours

@iwiznia iwiznia merged commit bc8be8b into Expensify:main Mar 27, 2023
@OSBotify
Copy link
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@OSBotify
Copy link
Contributor

🚀 Deployed to staging by https://github.com/iwiznia in version: 1.2.91-0 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

@OSBotify
Copy link
Contributor

🚀 Deployed to production by https://github.com/luacmartins in version: 1.2.91-1 🚀

platform result
🤖 android 🤖 success ✅
🖥 desktop 🖥 success ✅
🍎 iOS 🍎 success ✅
🕸 web 🕸 success ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants