Skip to content

Commit

Permalink
fix(android): textfield padding (#13513)
Browse files Browse the repository at this point in the history
* Revert "Revert "fix(android): textfield padding (#13279)" (#13512)"

This reverts commit 918388a.

* fix(android): fix input value

* reset padding

---------

Co-authored-by: m1ga <migamiga@web.de>
Co-authored-by: Michael Gangolf <m1ga@users.noreply.github.com>
  • Loading branch information
3 people committed Sep 16, 2024
1 parent 91499ef commit d75fc9e
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class TiUIText extends TiUIView implements TextWatcher, OnEditorActionLis
private int viewHeightInLines;
private int maxLines = Integer.MAX_VALUE;
private int hintTextPadding;
private HashMap<String, Object> defaultPadding = new HashMap<String, Object>();
private InputFilterHandler inputFilterHandler;

protected TiUIEditText tv;
Expand Down Expand Up @@ -159,6 +160,11 @@ public boolean onKey(View v, int keyCode, KeyEvent event)
textInputLayout.addView(this.tv, new TextInputLayout.LayoutParams(
TextInputLayout.LayoutParams.MATCH_PARENT, TextInputLayout.LayoutParams.MATCH_PARENT));

// store default padding
this.defaultPadding.put(TiC.PROPERTY_TOP, this.tv.getPaddingTop());
this.defaultPadding.put(TiC.PROPERTY_RIGHT, this.tv.getPaddingRight());
this.defaultPadding.put(TiC.PROPERTY_BOTTOM, this.tv.getPaddingBottom());
this.defaultPadding.put(TiC.PROPERTY_LEFT, this.tv.getPaddingLeft());
setNativeView(textInputLayout);
}

Expand Down Expand Up @@ -326,28 +332,39 @@ private void updateTextField()

private void setTextPadding(HashMap<String, Object> d)
{
int paddingLeft = textInputLayout.getPaddingLeft();
int paddingRight = textInputLayout.getPaddingRight();
int paddingTop = textInputLayout.getPaddingTop();
int paddingBottom = textInputLayout.getPaddingBottom();

if (d.containsKey(TiC.PROPERTY_LEFT)) {
paddingLeft = TiConvert.toInt(d.get(TiC.PROPERTY_LEFT), 0);
}
int paddingLeft = tv.getPaddingLeft();
int paddingRight = tv.getPaddingRight();
int paddingTop = tv.getPaddingTop();
int paddingBottom = tv.getPaddingBottom();

if (d == null) {
// reset to default padding
paddingLeft = (int) this.defaultPadding.get(TiC.PROPERTY_LEFT);
paddingRight = (int) this.defaultPadding.get(TiC.PROPERTY_RIGHT);
paddingTop = (int) this.defaultPadding.get(TiC.PROPERTY_TOP);
paddingBottom = (int) this.defaultPadding.get(TiC.PROPERTY_BOTTOM);
} else {
if (d.containsKey(TiC.PROPERTY_LEFT)) {
paddingLeft = (int) TiConvert.toTiDimension(TiConvert.toInt(d.get(TiC.PROPERTY_LEFT), 0),
TiDimension.TYPE_LEFT).getAsPixels(textInputLayout);
}

if (d.containsKey(TiC.PROPERTY_RIGHT)) {
paddingRight = TiConvert.toInt(d.get(TiC.PROPERTY_RIGHT), 0);
}
if (d.containsKey(TiC.PROPERTY_RIGHT)) {
paddingRight = (int) TiConvert.toTiDimension(TiConvert.toInt(d.get(TiC.PROPERTY_RIGHT), 0),
TiDimension.TYPE_RIGHT).getAsPixels(textInputLayout);
}

if (d.containsKey(TiC.PROPERTY_TOP)) {
paddingTop = TiConvert.toInt(d.get(TiC.PROPERTY_TOP), 0);
}
if (d.containsKey(TiC.PROPERTY_TOP)) {
paddingTop = (int) TiConvert.toTiDimension(TiConvert.toInt(d.get(TiC.PROPERTY_TOP), 0),
TiDimension.TYPE_TOP).getAsPixels(textInputLayout);
}

if (d.containsKey(TiC.PROPERTY_BOTTOM)) {
paddingBottom = TiConvert.toInt(d.get(TiC.PROPERTY_BOTTOM), 0);
if (d.containsKey(TiC.PROPERTY_BOTTOM)) {
paddingBottom = (int) TiConvert.toTiDimension(TiConvert.toInt(d.get(TiC.PROPERTY_BOTTOM), 0),
TiDimension.TYPE_BOTTOM).getAsPixels(textInputLayout);
}
}

textInputLayout.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
tv.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom);
}

@Override
Expand Down Expand Up @@ -984,6 +1001,9 @@ public void setHintText(int type, CharSequence hintText)
this.textInputLayout.setHintEnabled(true);
}

this.defaultPadding.put(TiC.PROPERTY_TOP, (type == UIModule.HINT_TYPE_ANIMATED)
? this.hintTextPadding : this.defaultPadding.get(TiC.PROPERTY_BOTTOM));

this.tv.setPadding(
this.tv.getPaddingLeft(),
(type == UIModule.HINT_TYPE_ANIMATED) ? this.hintTextPadding : this.tv.getPaddingBottom(),
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions tests/Resources/ti.ui.textfield.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,43 @@ describe('Titanium.UI.TextField', () => {
win.open();
});

it.android('android padding (visual check)', function (finish) {
this.timeout(5000);
const textField = Ti.UI.createTextField({
value: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec nec ullamcorper massa, eget tempor sapien. Phasellus nisi metus, tempus a magna nec, ultricies rutrum lacus. Aliquam sit amet augue suscipit, dignissim tellus eu, consectetur elit. Praesent ligula velit, blandit vel urna sit amet, suscipit euismod nunc.',
width: 100,
height: 40,
backgroundColor: 'white',
color: 'black',
padding: {
top: 10,
bottom: 10
}
});
const bgView = Ti.UI.createView({
width: 200,
height: 100,
backgroundColor: 'red'
});
win = Ti.UI.createWindow({
backgroundColor: '#eee'
});
bgView.add(textField);
win.add(bgView);

win.addEventListener('postlayout', function postlayout() { // FIXME: Support once!
win.removeEventListener('postlayout', postlayout); // only run once
try {
should(textField).matchImage('snapshots/textfieldPadding.png');
} catch (err) {
return finish(err);
}
finish();
});

win.open();
});

describe('.hintText', () => {
let textField;
beforeEach(() => {
Expand Down

0 comments on commit d75fc9e

Please sign in to comment.