Skip to content

Commit

Permalink
fix(ios): fixed setSelection issue of textfield (#12654)
Browse files Browse the repository at this point in the history
Fixes TIMOB-28404
  • Loading branch information
vijaysingh-axway committed Mar 26, 2021
1 parent 42691e2 commit 4316f54
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
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) {
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

0 comments on commit 4316f54

Please sign in to comment.