Skip to content

Commit

Permalink
fix(ios): maxlength property not working in hippy-vue
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwcg authored and zoomchan-cxj committed Mar 21, 2023
1 parent c363ab6 commit 2690adc
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 113 deletions.
5 changes: 0 additions & 5 deletions ios/sdk/component/textinput/HippyTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,6 @@ - (void)didMoveToWindow {
}
}

- (void)dealloc {
_responderDelegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

@interface HippyTextField () <HippyUITextFieldResponseDelegate>
Expand Down
220 changes: 112 additions & 108 deletions ios/sdk/component/textinput/HippyTextView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,6 @@ - (void)didMoveToWindow {
}
}

- (void)dealloc {
_responderDelegate = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

@interface HippyTextView () <HippyUITextViewResponseDelegate>
Expand Down Expand Up @@ -361,23 +356,20 @@ - (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor {

- (void)setContentInset:(UIEdgeInsets)contentInset {
[super setContentInset:contentInset];
// _contentInset = contentInset;
[self updateFrames];
}

- (void)textFieldEditChanged:(NSNotification *)obj {
if (self.isFirstResponder && _maxLength) {
UITextView *textField = (UITextView *)obj.object;
- (void)checkMaxLengthAndAlterTextView:(UITextView *)textField {
//TODO: This old special logic needs to be optimized.
if (self.isFirstResponder && self.maxLength) {
NSInteger theMaxLength = self.maxLength.integerValue;
NSString *toBeString = textField.text;
NSString *lang = [textField.textInputMode primaryLanguage];
if ([lang isEqualToString:@"zh-Hans"]) // 简体中文输入
{
NSString *toBeString = textField.text;

UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];

if (!position) {
if (toBeString.length > theMaxLength) {
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:theMaxLength];
Expand All @@ -404,97 +396,8 @@ - (void)textFieldEditChanged:(NSNotification *)obj {
}
}

- (BOOL)textView:(HippyUITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if (_onKeyPress) {
NSString *resultKey = text;
if ([text isEqualToString:@" "]) {
resultKey = @"space";
} else if ([text isEqualToString:@""]) {
resultKey = @"backspace";
} else if ([text isEqualToString:@"\n"]) {
resultKey = @"enter";
}
_onKeyPress(@{ @"key": resultKey });
}

if (textView.textWasPasted) {
textView.textWasPasted = NO;
} else {
if (_blurOnSubmit && [text isEqualToString:@"\n"]) {
// TODO: the purpose of blurOnSubmit on HippyextField is to decide if the
// field should lose focus when return is pressed or not. We're cheating a
// bit here by using it on HippyextView to decide if return character should
// submit the form, or be entered into the field.
//
// The reason this is cheating is because there's no way to specify that
// you want the return key to be swallowed *and* have the field retain
// focus (which was what blurOnSubmit was originally for). For the case
// where _blurOnSubmit = YES, this is still the correct and expected
// behavior though, so we'll leave the don't-blur-or-add-newline problem
// to be solved another day.
[self resignFirstResponder];
if (_onBlur) {
_onBlur(@{});
}
return NO;
}
}

// So we need to track that there is a native update in flight just in case JS manages to come back around and update
// things /before/ UITextView can update itself asynchronously. If there is a native update in flight, we defer the
// JS update when it comes in and apply the deferred update once textViewDidChange fires with the native update applied.
if (_blockTextShouldChange) {
return NO;
}

_nativeUpdatesInFlight = YES;

if (range.location + range.length > _predictedText.length) {
// _predictedText got out of sync in a bad way, so let's just force sync it. Haven't been able to repro this, but
// it's causing a real crash here: #6523822
_predictedText = textView.text;
}

NSString *previousText = [_predictedText substringWithRange:range];
if (_predictedText) {
_predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text];
} else {
_predictedText = text;
}

if (_onTextInput) {
_onTextInput(@{
@"text": text,
@"previousText": previousText ?: @"",
@"range": @ { @"start": @(range.location), @"end": @(range.location + range.length) },
@"eventCount": @(_nativeEventCount),
});
}

return YES;
}

- (void)textViewDidChangeSelection:(HippyUITextView *)textView {
if (_onSelectionChange && textView.selectedTextRange != _previousSelectionRange
&& ![textView.selectedTextRange isEqual:_previousSelectionRange]) {
_previousSelectionRange = textView.selectedTextRange;

UITextRange *selection = textView.selectedTextRange;
NSInteger start = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selection.start];
NSInteger end = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selection.end];
_onSelectionChange(@{
@"selection": @ {
@"start": @(start),
@"end": @(end),
},
});

if (_onChangeText) {
_onChangeText(@{
@"text": self.text,
});
}
}
- (void)textFieldEditChanged:(NSNotification *)obj {
[self checkMaxLengthAndAlterTextView:(UITextView *)obj.object];
}

- (NSString *)text {
Expand Down Expand Up @@ -575,6 +478,9 @@ - (BOOL)autoCorrect {
return _textView.autocorrectionType == UITextAutocorrectionTypeYes;
}


#pragma mark - UITextViewDelegate

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
if (_selectTextOnFocus) {
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -619,6 +525,84 @@ static BOOL findMismatch(NSString *first, NSString *second, NSRange *firstRange,
return YES;
}

- (BOOL)textView:(HippyUITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
if (_onKeyPress) {
NSString *resultKey = text;
if ([text isEqualToString:@" "]) {
resultKey = @"space";
} else if ([text isEqualToString:@""]) {
resultKey = @"backspace";
} else if ([text isEqualToString:@"\n"]) {
resultKey = @"enter";
}
_onKeyPress(@{ @"key": resultKey });
}

if (textView.textWasPasted) {
textView.textWasPasted = NO;
} else {
if (_blurOnSubmit && [text isEqualToString:@"\n"]) {
// TODO: the purpose of blurOnSubmit on HippyextField is to decide if the
// field should lose focus when return is pressed or not. We're cheating a
// bit here by using it on HippyextView to decide if return character should
// submit the form, or be entered into the field.
//
// The reason this is cheating is because there's no way to specify that
// you want the return key to be swallowed *and* have the field retain
// focus (which was what blurOnSubmit was originally for). For the case
// where _blurOnSubmit = YES, this is still the correct and expected
// behavior though, so we'll leave the don't-blur-or-add-newline problem
// to be solved another day.
[self resignFirstResponder];
if (_onBlur) {
_onBlur(@{});
}
return NO;
}
}

// So we need to track that there is a native update in flight just in case JS manages to come back around and update
// things /before/ UITextView can update itself asynchronously. If there is a native update in flight, we defer the
// JS update when it comes in and apply the deferred update once textViewDidChange fires with the native update applied.
if (_blockTextShouldChange) {
return NO;
}

// maxlength related
if (self.maxLength != nil) {
if (textView.text.length > self.maxLength.integerValue &&
text.length > range.length) {
return NO;
}
}

_nativeUpdatesInFlight = YES;

if (range.location + range.length > _predictedText.length) {
// _predictedText got out of sync in a bad way, so let's just force sync it. Haven't been able to repro this, but
// it's causing a real crash here: #6523822
_predictedText = textView.text;
}

NSString *previousText = [_predictedText substringWithRange:range];
if (_predictedText) {
_predictedText = [_predictedText stringByReplacingCharactersInRange:range withString:text];
} else {
_predictedText = text;
}

if (_onTextInput) {
_onTextInput(@{
@"text": text,
@"previousText": previousText ?: @"",
@"range": @ { @"start": @(range.location), @"end": @(range.location + range.length) },
@"eventCount": @(_nativeEventCount),
});
}

return YES;
}

- (void)textViewDidChange:(UITextView *)textView {
[self updatePlaceholderVisibility];
[self updateContentSize];
Expand All @@ -643,6 +627,9 @@ - (void)textViewDidChange:(UITextView *)textView {
if (!self.hippyTag || !_onChangeText) {
return;
}

// check maxLength before send event to js
[self checkMaxLengthAndAlterTextView:textView];

// When the context size increases, iOS updates the contentSize twice; once
// with a lower height, then again with the correct height. To prevent a
Expand All @@ -666,14 +653,32 @@ - (void)textViewDidChange:(UITextView *)textView {
});
}

- (void)textViewDidEndEditing:(__unused UITextView *)textView {
if (_onEndEditing) {
_onEndEditing(@{
@"text": textView.text,
- (void)textViewDidChangeSelection:(HippyUITextView *)textView {
if (_onSelectionChange && textView.selectedTextRange != _previousSelectionRange
&& ![textView.selectedTextRange isEqual:_previousSelectionRange]) {
_previousSelectionRange = textView.selectedTextRange;

UITextRange *selection = textView.selectedTextRange;
NSInteger start = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selection.start];
NSInteger end = [textView offsetFromPosition:textView.beginningOfDocument toPosition:selection.end];
_onSelectionChange(@{
@"selection": @ {
@"start": @(start),
@"end": @(end),
},
});

if (_onChangeText) {
_onChangeText(@{
@"text": self.text,
});
}
}
}


#pragma mark - Responder Chain

- (BOOL)isFirstResponder {
return [_textView isFirstResponder];
}
Expand Down Expand Up @@ -727,7 +732,6 @@ - (void)setFontSize:(NSNumber *)fontSize {
- (void)setDefaultValue:(NSString *)defaultValue {
if (defaultValue) {
[self setText:defaultValue];

_defaultValue = defaultValue;
}
}
Expand Down

0 comments on commit 2690adc

Please sign in to comment.