Skip to content

Commit

Permalink
Add _isUpdatingPlaceholderText to prevent spurious "did end editing" …
Browse files Browse the repository at this point in the history
…notifications
  • Loading branch information
Adam Gleitman committed Aug 12, 2024
1 parent 23a33f2 commit 6377759
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ @implementation RCTUITextField {
#endif
RCTBackedTextFieldDelegateAdapter *_textInputDelegateAdapter;
NSDictionary<NSAttributedStringKey, id> *_defaultTextAttributes;
BOOL _isUpdatingPlaceholderText; // [macOS]
}

#if TARGET_OS_OSX // [macOS
Expand All @@ -116,6 +117,7 @@ - (instancetype)initWithFrame:(CGRect)frame

_textInputDelegateAdapter = [[RCTBackedTextFieldDelegateAdapter alloc] initWithTextField:self];
_scrollEnabled = YES;
_isUpdatingPlaceholderText = NO; // [macOS]
}

return self;
Expand Down Expand Up @@ -361,8 +363,11 @@ - (void)_updatePlaceholder
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder ?: @""
attributes:[self _placeholderTextAttributes]];
#else // [macOS
// Set _isUpdatingPlaceholderText to manually suppress RCTUITextFieldDelegate's textFieldEndEditing
_isUpdatingPlaceholderText = YES;
self.placeholderAttributedString = [[NSAttributedString alloc] initWithString:self.placeholder ?: @""
attributes:[self _placeholderTextAttributes]];
_isUpdatingPlaceholderText = NO;
#endif // macOS]
}

Expand Down Expand Up @@ -485,10 +490,19 @@ - (void)textDidChange:(NSNotification *)notification
[delegate textFieldDidChange:self];
}
}

- (void)textDidEndEditing:(NSNotification *)notification
{
[super textDidEndEditing:notification];
[super textDidEndEditing:notification];

// [macOS
// On macOS, setting placeholderAttributedString causes AppKit to call textDidEndEditing.
// We don't want this to propagate or else we get unexpected onBlur/onEndEditing events.
if (_isUpdatingPlaceholderText) {
return;
}
// macOS]

id<RCTUITextFieldDelegate> delegate = self.delegate;
if ([delegate respondsToSelector:@selector(textFieldEndEditing:)]) {
[delegate textFieldEndEditing:self];
Expand Down

0 comments on commit 6377759

Please sign in to comment.