Skip to content

Commit

Permalink
[FIX] rich text focus loop (#19240)
Browse files Browse the repository at this point in the history
* check if onBlur event contains text that is different than value

* add check if there is a text in native event

* Prevent re-selection of RichText when native selection changes as a result of resigning focus

* Fix typo

* Check if isSelected only in onSelectionChangeFromAztec

Co-authored-by: Jorge Bernal <jbernal@gmail.com>
  • Loading branch information
dratwas and koke authored Jan 7, 2020
1 parent 4a780ea commit 3eaa314
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/rich-text/src/component/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ export class RichText extends Component {
onBlur( event ) {
this.isTouched = false;

// Check if value is up to date with latest state of native AztecView
if ( event.nativeEvent.text && event.nativeEvent.text !== this.props.value ) {
this.onTextUpdate( event );
}

if ( this.props.onBlur ) {
this.props.onBlur( event );
}
Expand Down Expand Up @@ -457,8 +462,16 @@ export class RichText extends Component {
// Make sure there are changes made to the content before upgrading it upward
this.onTextUpdate( event );

this.onSelectionChange( realStart, realEnd );

// Aztec can send us selection change events after it has lost focus.
// For instance the autocorrect feature will complete a partially written
// word when resigning focus, causing a selection change event.
// Forwarding this selection change could cause this RichText to regain
// focus and start a focus loop.
//
// See https://github.com/wordpress-mobile/gutenberg-mobile/issues/1696
if ( this.props.__unstableIsSelected ) {
this.onSelectionChange( realStart, realEnd );
}
// Update lastEventCount to prevent Aztec from re-rendering the content it just sent
this.lastEventCount = event.nativeEvent.eventCount;

Expand Down

0 comments on commit 3eaa314

Please sign in to comment.