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

Fix cursor moving while typing quickly and autocorrection triggered in controlled single line TextInput on iOS (New Arch) #46970

Closed
wants to merge 1 commit into from

Commits on Oct 15, 2024

  1. Fix cursor moving while typing quickly and autocorrection triggered i…

    …n controlled single line TextInput on iOS (New Arch) (facebook#46970)
    
    Summary:
    
    Fixes facebook#44157
    
    This one is a bit of a doozy...
    
    During auto-correct in UITextField (used for single line TextInput) iOS will mutate the buffer in two parts, non-atomically. After the first part, after iOS triggers `textFieldDidChange`, selection is in the wrong position. If we set full new AttributedText at this point, we propagate the incorrect cursor position, and it is never restored.
    
    In the common case, where we are not mutating text in the controlled component, we shouldn't need to be setting AttributedString in the first place, and we do have an equality comparison there currently. But it is defeated because attributes are not identical. There are a few sources of that:
    1. NSParagraphStyle is present in backing input, but not the AttributedString we are setting. 
    2. Backing text has an NSShadow with no color (does not render) not in the AttributedText
    3. Event emitter attributes change on each update, and new text does not inherit the attributes.
    
    The first two are part of the backing input `typingAttributes`, even if we set a dictionary without them. To solve for them, we make attribute comparison insensitive to the attribute values in a default initialized control. There is code around here fully falling back to attribute insensitive comparison, which we would ideally fix to instead role into this "effective" attribute comparison.
    
    The event emitter attributes being misaligned is a real problem. We fix in a couple ways.
    1. We treat the attribute values as equal if the backing event emitter is the same
    2. We set paragraph level event emitter as a default attribute so the first typed character receives it
    
    After these fixes, scenario in facebook/react-native-website#4247 no longer repros in new arch. Typing in debug build also subjectively seems faster? (we are not doing second invalidation of the control on every keypress).
    
    Changes which do mutate content may be susceptible to the same style of issue, though on web/`react-dom` in Chrome, this seems to not try to preserve selection at all if the selection is uncontrolled, so this seems like less of an issue. 
    
    I haven't yet looked at old arch, but my guess is we have similar issues there, and could be fixed in similar ways (though, we've been trying to avoid changing it as much as possible, and 0.76+ has new arch as default, so not sure if worth fixing in old impl as well if this is very long running issue).
    
    Changelog:
    [iOS][Fixed] - Fix cursor moving in iOS controlled single line TextInput on Autocorrection (New Arch)
    
    Reviewed By: javache, philIip
    
    Differential Revision: D64121570
    NickGerleman authored and facebook-github-bot committed Oct 15, 2024
    Configuration menu
    Copy the full SHA
    a78f7f3 View commit details
    Browse the repository at this point in the history