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

Blur Magic Code input when click outside #24012

Merged
merged 33 commits into from
Sep 27, 2023
Merged

Conversation

wojtus7
Copy link
Contributor

@wojtus7 wojtus7 commented Aug 1, 2023

Details

In order to make blur possible I needed to refactor the magic component. Instead of 6 separate text inputs there is only one wrapped inside TapGestureHandler that determine which number should be inputed. This resulted is some code that is simpler, taps are handled by gesture handler and right-click/context menu/context is handled by native text input. but at the same time created small issue that paste button shows only on the beginning of the input.

Fixed Issues

$ #18244
PROPOSAL: #18244 (comment)

Tests

  1. Log out
  2. Provide email address
  3. Try to break the magic code input (Click at random places, use arrows, backspace, change tabs and go back on web etc)
  • Verify that no errors appear in the JS console

Offline tests

QA Steps

  1. Log out
  2. Provide email address
  3. Try to break the magic code input (Click at random places, use arrows, backspace, change tabs and go back on web etc)
  • 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 the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • 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 grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is 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
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • 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 code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • 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
bez.nazwy.mov
Mobile Web - Chrome
Screenrecorder-2023-08-01-17-22-47-275.mp4
Mobile Web - Safari
Nagranie.z.ekranu.2023-08-1.o.14.42.25.mov
Desktop
bez.nazwy.mov
iOS
Nagranie.z.ekranu.2023-08-1.o.14.44.35.mov
Android
Nagranie.z.ekranu.2023-08-1.o.14.41.23.mov

@wojtus7 wojtus7 requested a review from a team as a code owner August 1, 2023 16:03
@melvin-bot melvin-bot bot requested review from Santhosh-Sellavel and removed request for a team August 1, 2023 16:03
@melvin-bot
Copy link

melvin-bot bot commented Aug 1, 2023

@Santhosh-Sellavel 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]

@Santhosh-Sellavel
Copy link
Collaborator

Workswell, so far so good. But will continue testing this further

@Santhosh-Sellavel
Copy link
Collaborator

Resolve conflicts, thanks!

@Santhosh-Sellavel
Copy link
Collaborator

Tests well mostly one issue I spotted here, tab/shift+tab does not focus on the next/previous code input like we have arrow navigation

Screen.Recording.2023-08-08.at.12.29.58.AM.mov

cc: @dangrous

@Santhosh-Sellavel
Copy link
Collaborator

Santhosh-Sellavel commented Aug 7, 2023

What should we do here @dangrous?

@dangrous
Copy link
Contributor

dangrous commented Aug 7, 2023

Oh hm, because it's one big input, yeah. Can we add a listener for the tab key if the focus is in the component?

@wojtus7
Copy link
Contributor Author

wojtus7 commented Aug 8, 2023

I tested it and it seems like it's kinda hard to change behaviour of tab without any hacky solution behind it.
But should it work like this in that way in the first place? I always thought that tab should focus to the next input/button, and tab working like before - targeting every single digit space in the code was only - was only highlighting that there was some strange solution with multiple inputs, like we were not treating code as single input space and tab working like it is right now is a good change? Also now there is clear difference between arrows and tab, and I think there should be? 🫠

@Santhosh-Sellavel
Copy link
Collaborator

I tested it and it seems like it's kinda hard to change behaviour of tab without any hacky solution behind it.

Not sure about this though. What we do here, is a kind of magic trick creating impression that there a six input to get each value of the code. But behind the scenes, there is a different story right, can we call it a hack?

But should it work like this in that way in the first place?

Yes, It should be like how it was before.

I always thought that tab should focus to the next input/button, and tab working like before - targeting every single digit space in the code was only - was only highlighting that there was some strange solution with multiple inputs, like we were not treating code as single input space and tab working like it is right now is a good change?

No,For each number, we have a separate field for a customer POV, so the tab/shift-tab should focus on the next/prev field, that's the basic expectation

Also now there is clear difference between arrows and tab, and I think there should be? 🫠

We don't need to compare arrow/tab. Arrow is an addon to navigate through the field, the tab is an accessibility feature

cc: @dangrous thoughts?

@mczernek
Copy link
Contributor

mczernek commented Aug 9, 2023

Santhosh-Sellavel I see it differently. From my perspective customer is entering one value - namely magic code. Hence, navigating between characters within one field is based on arrows. Tab press in my mind switches to next input, which should logically correspond to next inputed value. Here - it should go from Magic Code to whatever is next.
Personally, I consider previous solution hacky.

The way I look at it is that Magic Code Input in my mind is in fact ONE input but with styling that underlines each character separately. Even from code perspective it is actually one input, so I believe it should be treated as such.

If we look at it as a reusable component, it could theoretically be used as part of some screen that contains more than one input - say for entering postal code or phone number somewhere. Wouldn't you expect then Tab to switch you to next field of form, rather than next character of input?

Having said all that, I think that IDEAL solution would be to have it configurable with some prop. But given it might be hard and not really used ever, I'd stick to what we've got.

@dangrous
Copy link
Contributor

dangrous commented Aug 9, 2023

Yeah that's an interesting point. Because tab is primarily used for accessibility, and from an accessibility perspective, for example a blind user would actively want it to act as one input, vs. 6. I think you've swayed me, @mczernek! I don't think we have to worry about that issue.

@Santhosh-Sellavel
Copy link
Collaborator

Yeah that's an interesting point. Because the tab is primarily used for accessibility, and from an accessibility perspective, for example, a blind user would actively want it to act as one input, vs. 6. I think you've swayed me, @mczernek! I don't think we have to worry about that issue.

Cool, then. If it haunts back us let's handle it along with accessibility milestones. As I see accessibility is not a priority ATM.

@dangrous
Copy link
Contributor

That works for me!

@Santhosh-Sellavel
Copy link
Collaborator

Santhosh-Sellavel commented Aug 11, 2023

Moved here

@Santhosh-Sellavel
Copy link
Collaborator

Bug: Backspace not working on mWeb Safari after unfocusing and focusing the input.

Screen.Recording.2023-08-12.at.2.17.57.AM.mov

Copy link
Collaborator

@Santhosh-Sellavel Santhosh-Sellavel left a comment

Choose a reason for hiding this comment

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

Check this @wojtus7

@Santhosh-Sellavel
Copy link
Collaborator

bump @wojtus7

@wojtus7
Copy link
Contributor Author

wojtus7 commented Sep 22, 2023

I'm afraid we can't do much about placement of the paste button without some hacky solution 😞

But I am working rn to fix keyboard behaviour and make it uniform on web and native

@wojtus7
Copy link
Contributor Author

wojtus7 commented Sep 22, 2023

@dangrous while trying to disable focus after error I found this code and comment:
image

I can remove it but just want to make sure we don't regress pretty fresh fix here, and maybe we should just fix highlight of the selected input?
But at the same time I agree that refocus after error is kinda strange.

@dangrous
Copy link
Contributor

dangrous commented Sep 25, 2023

Hey @wotjus try pulling main - I don't see that comment any more (so maybe it's okay for us to unfocus)

I'm afraid we can't do much about placement of the paste button without some hacky solution 😞

That's fine, that was a relatively minor issue. As long as it still pops up wherever you click (which it seems like it does), fine by me.

@wojtus7
Copy link
Contributor Author

wojtus7 commented Sep 25, 2023

Now with refocus logic removed, all platforms behave the same: (No keyboard, all digits red - user has to touch/click some space to edit it)

Nagranie.z.ekranu.2023-09-25.o.20.25.05.mov

@dangrous can you confirm this is what we wanted?

@Santhosh-Sellavel
Copy link
Collaborator

Yes this is what we wanted.

@Santhosh-Sellavel
Copy link
Collaborator

All good, tests well for me!

Copy link
Collaborator

@Santhosh-Sellavel Santhosh-Sellavel left a comment

Choose a reason for hiding this comment

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

LGTM, All yours @dangrous!

@dangrous
Copy link
Contributor

Nice! Yep that's what I think expected behavior should be. Will look through this in a moment, and we can merge!

Copy link
Contributor

@dangrous dangrous left a comment

Choose a reason for hiding this comment

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

Looking great! One tiny change, then ready to merge!

src/components/MagicCodeInput.js Outdated Show resolved Hide resolved
Co-authored-by: Daniel Gale-Rosen <5487802+dangrous@users.noreply.github.com>
Copy link
Contributor

@dangrous dangrous left a comment

Choose a reason for hiding this comment

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

Thanks for pushing this over the line!

@dangrous dangrous merged commit 3965726 into Expensify:main Sep 27, 2023
11 checks passed
@melvin-bot
Copy link

melvin-bot bot commented Sep 27, 2023

Congrats, that’s your 5th PR merged! 🎉 Do you know about the ContributorPlus role? It’s an opportunity to earn more in the Expensify Open Source community. Keep up the great work - thanks!

@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/dangrous in version: 1.3.75-0 🚀

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

@OSBotify
Copy link
Contributor

OSBotify commented Oct 2, 2023

🚀 Deployed to production by https://github.com/mountiny in version: 1.3.75-12 🚀

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

@OSBotify
Copy link
Contributor

OSBotify commented Oct 2, 2023

🚀 Deployed to staging by https://github.com/dangrous in version: 1.3.76-0 🚀

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

@OSBotify
Copy link
Contributor

OSBotify commented Oct 3, 2023

🚀 Deployed to production by https://github.com/mountiny in version: 1.3.76-6 🚀

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

@@ -91,22 +95,40 @@ const composeToString = (value) => _.map(value, (v) => (v === undefined || v ===
const getInputPlaceholderSlots = (length) => Array.from(Array(length).keys());

function MagicCodeInput(props) {
const inputRefs = useRef([]);
const [input, setInput] = useState('');
const inputRefs = useRef();
Copy link
Contributor

Choose a reason for hiding this comment

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

When we change from array to single inputRefs here, we forgot to update those lines as well

const formElement = inputRefs.current as HTMLFormElement | null;
(formElement?.[indexToFocus] as HTMLInputElement)?.focus();

It caused a bug here #50315. More details: #50315 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

oh huh, thanks!

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.

7 participants