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

[Backport 10_0_X] fix(ios): fixed setSelection issue of textfield #12666

Merged
merged 3 commits into from
Mar 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 0 additions & 12 deletions apidoc/Titanium/UI/TabGroup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,6 @@ methods:
platforms: [android]
since: 3.6.0

- name: getActiveTab
summary: Gets the currently-active tab.
returns:
type: Titanium.UI.Tab

- name: open
summary: Opens the tab group and makes it visible.

Expand All @@ -268,13 +263,6 @@ methods:
summary: Tab to remove.
type: Titanium.UI.Tab

- name: setActiveTab
summary: Selects the currently active tab in a tab group.
parameters:
- name: indexOrObject
summary: Index or object of the tab to switch to.
type: [Number, Titanium.UI.Tab]

- name: getTabs
summary: Gets all tabs that are managed by the tab group.
returns:
Expand Down
2 changes: 1 addition & 1 deletion iphone/Classes/TiUITextWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
- (void)textWidget:(UIView<UITextInputTraits> *)tw didFocusWithText:(NSString *)value;
- (void)textWidget:(UIView<UITextInputTraits> *)tw didBlurWithText:(NSString *)value;
- (void)setValue_:(id)text;
- (void)setSelectionFrom:(id)start to:(id)end;
- (void)setSelectionFrom:(NSInteger)start to:(NSInteger)end;
#pragma mark - Titanium Internal Use Only
- (void)updateKeyboardStatus;
- (NSDictionary *)selectedRange;
Expand Down
6 changes: 3 additions & 3 deletions iphone/Classes/TiUITextWidget.m
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,14 @@ - (NSDictionary *)selectedRange
return nil;
}

- (void)setSelectionFrom:(id)start to:(id)end
- (void)setSelectionFrom:(NSInteger)start to:(NSInteger)end
{
UIView<UITextInput> *textView = (UIView<UITextInput> *)[self textWidgetView];
if ([textView conformsToProtocol:@protocol(UITextInput)]) {
if ([textView becomeFirstResponder] || [textView isFirstResponder]) {
UITextPosition *beginning = textView.beginningOfDocument;
UITextPosition *startPos = [textView positionFromPosition:beginning offset:[TiUtils intValue:start]];
UITextPosition *endPos = [textView positionFromPosition:beginning offset:[TiUtils intValue:end]];
UITextPosition *startPos = [textView positionFromPosition:beginning offset:start];
UITextPosition *endPos = [textView positionFromPosition:beginning offset:end];
UITextRange *textRange;
textRange = [textView textRangeFromPosition:startPos toPosition:endPos];
[textView setSelectedTextRange:textRange];
Expand Down
9 changes: 5 additions & 4 deletions iphone/Classes/TiUITextWidgetProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,11 @@ - (NSDictionary *)selection
return nil;
}

- (void)setSelection:(id)arg withObject:(id)property
- (void)setSelection:(id)args
{
NSInteger start = [TiUtils intValue:arg def:-1];
NSInteger end = [TiUtils intValue:property def:-1];
ENSURE_ARG_COUNT(args, 2);
NSInteger start = [TiUtils intValue:args[0] def:-1];
NSInteger end = [TiUtils intValue:args[1] def:-1];
NSString *curValue = [TiUtils stringValue:[self valueForKey:@"value"]];
NSInteger textLength = [curValue length];
if ((start < 0) || (start > textLength) || (end < 0) || (end > textLength)) {
Expand All @@ -316,7 +317,7 @@ - (void)setSelection:(id)arg withObject:(id)property
}
TiThreadPerformOnMainThread(
^{
[(TiUITextWidget *)[self view] setSelectionFrom:arg to:property];
[(TiUITextWidget *)[self view] setSelectionFrom:start to:end];
},
NO);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/Resources/ti.ui.textfield.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,32 @@ describe('Titanium.UI.TextField', () => {
should(textFieldA.hasText()).be.true();
should(textFieldB.hasText()).be.true();
});

it('#setSelection', function (finish) {
this.timeout(5000);
const textField = Ti.UI.createTextField({
value: 'Lorem ipsum dolor sit amet.',
width: Ti.UI.SIZE
});
win = Ti.UI.createWindow({
backgroundColor: '#ddd'
});
win.add(textField);
win.addEventListener('postlayout', function listener () {
win.removeEventListener('postlayout', listener);
textField.setSelection(0, 5);
setTimeout(function (e) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • ⚠️ tests/Resources/ti.ui.textfield.test.js line 415 – 'e' is defined but never used. Allowed unused args must match /^_.+/u. (no-unused-vars)

try {
should(textField.selection.length).eql(5);
should(textField.selection.location).eql(0);
} catch (err) {
return finish(err);
}
finish();
}, 1000);
});
win.open();
});
});

describe('events', () => {
Expand Down