Skip to content

Commit

Permalink
[TIMOB-25855] Android: TextArea lines and maxLines support (#9927)
Browse files Browse the repository at this point in the history
* android: lines and maxLines

* doc update, updateTextField method, range check

* Update TextArea.yml

Fixed typo.

* Update TextArea.yml

* Update TextArea.yml

* test
  • Loading branch information
m1ga authored and hansemannn committed Jun 27, 2018
1 parent a13ae91 commit cb97b36
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
TiC.PROPERTY_HINT_TEXT_COLOR,
TiC.PROPERTY_HINT_TYPE,
TiC.PROPERTY_KEYBOARD_TYPE,
TiC.PROPERTY_LINES,
TiC.PROPERTY_MAX_LINES,
TiC.PROPERTY_MAX_LENGTH,
TiC.PROPERTY_PASSWORD_MASK,
TiC.PROPERTY_TEXT_ALIGN,
Expand All @@ -58,6 +60,8 @@ public TextAreaProxy()
{
super();
defaultValues.put(TiC.PROPERTY_VALUE, "");
defaultValues.put(TiC.PROPERTY_LINES, -1);
defaultValues.put(TiC.PROPERTY_MAX_LINES, -1);
defaultValues.put(TiC.PROPERTY_MAX_LENGTH, -1);
defaultValues.put(TiC.PROPERTY_FULLSCREEN, true);
defaultValues.put(TiC.PROPERTY_EDITABLE, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public class TiUIText extends TiUIView implements TextWatcher, OnEditorActionLis
private int maxLength = -1;
private boolean isTruncatingText = false;
private boolean disableChangeEvent = false;
private int viewHeightInLines;
private int maxLines = Integer.MAX_VALUE;

protected TiUIEditText tv;
protected TextInputLayout textInputLayout;
Expand Down Expand Up @@ -251,9 +253,40 @@ public void processProperties(KrollDict d)
tv.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN);
}
}

if (d.containsKey(TiC.PROPERTY_LINES)) {
if (!field) {
this.viewHeightInLines = TiConvert.toInt(d.get(TiC.PROPERTY_LINES), 0);
updateTextField();
}
}

if (d.containsKey(TiC.PROPERTY_MAX_LINES)) {
if (!field) {
int value = TiConvert.toInt(d.get(TiC.PROPERTY_MAX_LINES), Integer.MAX_VALUE);
if (value < 1) {
value = Integer.MAX_VALUE;
}
this.maxLines = value;
updateTextField();
}
}

disableChangeEvent = false;
}

private void updateTextField()
{
if (!field) {
if (this.viewHeightInLines > 0) {
tv.setLines(this.viewHeightInLines);
} else {
tv.setMinLines(0);
}
tv.setMaxLines((this.maxLines > 0) ? this.maxLines : 1);
}
}

private void setTextPadding(HashMap<String, Object> d)
{
int paddingLeft = textInputLayout.getPaddingLeft();
Expand Down Expand Up @@ -370,6 +403,20 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
if (!TiConvert.toBoolean(newValue, true)) {
tv.setImeOptions(EditorInfo.IME_FLAG_NO_FULLSCREEN);
}
} else if (key.equals(TiC.PROPERTY_LINES)) {
if (!field) {
this.viewHeightInLines = TiConvert.toInt(newValue, 0);
updateTextField();
}
} else if (key.equals(TiC.PROPERTY_MAX_LINES)) {
if (!field) {
int value = TiConvert.toInt(newValue, Integer.MAX_VALUE);
if (value < 1) {
value = Integer.MAX_VALUE;
}
this.maxLines = value;
updateTextField();
}
} else {
super.propertyChanged(key, oldValue, newValue, proxy);
}
Expand Down
16 changes: 16 additions & 0 deletions apidoc/Titanium/UI/TextArea.yml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,14 @@ properties:
default: <Titanium.UI.KEYBOARD_DEFAULT>
platforms: [android, iphone, ipad]

- name: lines
summary: Number of lines tall the text area height will be, if set.
description: Sets the height of the text area by line height. Set to -1 to respect the view's height property instead.
default: 1
type: Number
since: { android: "7.4.0" }
platforms: [android]

- name: maxLength
summary: Maximum length of text field input.
description: Any attempt to input text beyond this length (including pasting a string
Expand All @@ -364,6 +372,14 @@ properties:
since: {android: "3.0.0", iphone: "3.0.0", ipad: "3.0.0"}
platforms: [android, iphone, ipad]

- name: maxLines
summary: Maximum line count of text field input.
description: Sets the maximum of lines that the field will be extended to when pressing `Return`.
default: -1
type: Number
since: { android: "7.4.0" }
platforms: [android]

- name: padding
summary: Sets the left and right padding of this TextArea. The text will always be vertically centered.
type: TextAreaPadding
Expand Down
180 changes: 180 additions & 0 deletions tests/Resources/ti.ui.textarea.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Appcelerator Titanium Mobile
* Copyright (c) 2011-Present by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
/* eslint-env mocha */
/* global Ti, Titanium */
/* eslint no-unused-expressions: "off" */
'use strict';
var should = require('./utilities/assertions');

describe('Titanium.UI.TextArea', function () {
var win;

afterEach(function () {
if (win) {
win.close();
}
win = null;
});

it('apiName', function () {
var textArea = Ti.UI.createTextArea({
value: 'this is some text'
});
should(textArea).have.readOnlyProperty('apiName').which.is.a.String;
should(textArea.apiName).be.eql('Ti.UI.TextArea');
});

it('value', function () {
var textArea = Ti.UI.createTextArea({
value: 'this is some text'
});
should(textArea.value).be.a.String;
should(textArea.getValue).be.a.Function;
should(textArea.value).eql('this is some text');
should(textArea.getValue()).eql('this is some text');
textArea.value = 'other text';
should(textArea.value).eql('other text');
should(textArea.getValue()).eql('other text');
});

it('editable', function () {
var textArea = Ti.UI.createTextArea();
should(textArea.editable).be.a.Boolean;
should(textArea.editable).be.true;
textArea.setEditable(false);
should(textArea.editable).be.false;
});

it.ios('scrollsToTop', function () {
var textArea = Ti.UI.createTextArea();
should(textArea.scrollsToTop).be.a.Boolean;
should(textArea.scrollsToTop).be.true;
textArea.setScrollsToTop(false);
should(textArea.scrollsToTop).be.false;
});

it('backgroundColor', function () {
var textArea = Ti.UI.createTextArea({ backgroundColor: 'red' });
should(textArea.backgroundColor).be.a.String;
should(textArea.backgroundColor).eql('red');
textArea.setBackgroundColor('white');
should(textArea.backgroundColor).eql('white');
});

it.windowsMissing('padding', function () {
var textArea = Ti.UI.createTextArea({
value: 'this is some text',
padding: {
left: 20,
right: 20
}
});
should(textArea.padding).be.a.Object;
should(textArea.getPadding).be.a.Function;
should(textArea.setPadding).be.a.Function;

should(textArea.padding.left).eql(20);
should(textArea.padding.right).eql(20);
should(textArea.getPadding().left).eql(20);
should(textArea.getPadding().right).eql(20);

textArea.setPadding({
left: 10,
right: 10
});

should(textArea.padding.left).eql(10);
should(textArea.padding.right).eql(10);
should(textArea.getPadding().left).eql(10);
should(textArea.getPadding().right).eql(10);
});

// Tests adding and removing a TextArea's focus.
it.ios('focus-blur', function (finish) {
var textArea;
this.timeout(5000);
win = Ti.UI.createWindow({ layout: 'vertical' });

// First TextArea is needed to receive default focus on startup
// and to receive focus when second TextArea has lost focus.
textArea = Ti.UI.createTextArea({
width: Ti.UI.FILL,
height: Ti.UI.SIZE,
});
win.add(textArea);

// Second TextArea is used to test focus/blur handling.
textArea = Ti.UI.createTextArea({
width: Ti.UI.FILL,
height: Ti.UI.SIZE,
});
textArea.addEventListener('focus', function () {
// Focus has been received. Now test removing focus.
setTimeout(function () {
textArea.blur();
}, 500);
});
textArea.addEventListener('blur', function () {
// Focus has been lost. The test was finished successfully. (Timeout means failure.)
finish();
});
win.add(textArea);

// Start the test when the window has been opened.
win.addEventListener('postlayout', function () {
setTimeout(function () {
textArea.focus();
}, 500);
});
win.open();
});

it.ios('textAlign', function () {
var textArea = Ti.UI.createTextArea({
value: 'this is some text',
textAlign: Titanium.UI.TEXT_ALIGNMENT_CENTER
});
should(textArea.textAlign).be.a.Number;
should(textArea.getTextAlign).be.a.Function;
should(textArea.textAlign).eql(Titanium.UI.TEXT_ALIGNMENT_CENTER);
should(textArea.getTextAlign()).eql(Titanium.UI.TEXT_ALIGNMENT_CENTER);
textArea.textAlign = Titanium.UI.TEXT_ALIGNMENT_RIGHT;
should(textArea.textAlign).eql(Titanium.UI.TEXT_ALIGNMENT_RIGHT);
should(textArea.getTextAlign()).eql(Titanium.UI.TEXT_ALIGNMENT_RIGHT);
});

it.ios('verticalAlign', function () {
var textArea = Ti.UI.createTextArea({
value: 'this is some text',
verticalAlign: Titanium.UI.TEXT_VERTICAL_ALIGNMENT_BOTTOM
});
should(textArea.verticalAlign).be.a.Number;
should(textArea.getVerticalAlign).be.a.Function;
should(textArea.verticalAlign).eql(Titanium.UI.TEXT_VERTICAL_ALIGNMENT_BOTTOM);
should(textArea.getVerticalAlign()).eql(Titanium.UI.TEXT_VERTICAL_ALIGNMENT_BOTTOM);
textArea.verticalAlign = Titanium.UI.TEXT_VERTICAL_ALIGNMENT_TOP;
should(textArea.verticalAlign).eql(Titanium.UI.TEXT_VERTICAL_ALIGNMENT_TOP);
should(textArea.getVerticalAlign()).eql(Titanium.UI.TEXT_VERTICAL_ALIGNMENT_TOP);
});

it.android('lines', function () {
var textArea = Ti.UI.createTextArea({
lines: 1,
maxLines: 5
});
should(textArea.lines).be.a.Number;
should(textArea.getLines).be.a.Function;
should(textArea.maxLines).be.a.Number;
should(textArea.getMaxLines).be.a.Function;
should(textArea.getLines()).eql(1);
should(textArea.maxLines).eql(5);
textArea.lines = 2;
textArea.setMaxLines(6);
should(textArea.lines).eql(2);
should(textArea.getMaxLines()).eql(6);
});
});

0 comments on commit cb97b36

Please sign in to comment.