-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
Fix StyleSheet 'textAlign' for AndroidTextInput. Closes #2702 #4481
Changes from 1 commit
e514f70
4281d91
ad16ca2
a9068cd
7e26860
847747a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,8 @@ | |
|
||
import javax.annotation.Nullable; | ||
|
||
import java.util.Map; | ||
import java.util.Map; | ||
import java.util.HashMap; | ||
|
||
import android.graphics.PorterDuff; | ||
import android.os.SystemClock; | ||
|
@@ -29,6 +30,7 @@ | |
|
||
import com.facebook.infer.annotation.Assertions; | ||
import com.facebook.react.bridge.JSApplicationCausedNativeException; | ||
import com.facebook.react.bridge.JSApplicationIllegalArgumentException; | ||
import com.facebook.react.bridge.ReactContext; | ||
import com.facebook.react.bridge.ReadableArray; | ||
import com.facebook.react.common.MapBuilder; | ||
|
@@ -59,6 +61,24 @@ public class ReactTextInputManager extends | |
private static final String KEYBOARD_TYPE_NUMERIC = "numeric"; | ||
private static final InputFilter[] EMPTY_FILTERS = new InputFilter[0]; | ||
|
||
private static final Map<String, Map<String, Integer>> TEXT_ALIGN_CONSTANTS; | ||
static { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you don't need a static block for this, you can just continue with this assignment on the line above |
||
TEXT_ALIGN_CONSTANTS = MapBuilder.of( | ||
ViewProps.TEXT_ALIGN, | ||
MapBuilder.of( | ||
"left", Gravity.LEFT, | ||
"right", Gravity.RIGHT, | ||
"auto", Gravity.NO_GRAVITY, | ||
"start", Gravity.START, | ||
"center", Gravity.CENTER_HORIZONTAL, | ||
"end", Gravity.END), | ||
"textAlignVertical", | ||
MapBuilder.of( | ||
"top", Gravity.TOP, | ||
"center", Gravity.CENTER_VERTICAL, | ||
"bottom", Gravity.BOTTOM)); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return REACT_CLASS; | ||
|
@@ -189,13 +209,17 @@ public void setUnderlineColor(ReactEditText view, @Nullable Integer underlineCol | |
} | ||
} | ||
|
||
@ReactProp(name = "textAlign") | ||
public void setTextAlign(ReactEditText view, int gravity) { | ||
@ReactProp(name = ViewProps.TEXT_ALIGN) | ||
public void setTextAlign(ReactEditText view, @Nullable String textAlign) { | ||
Map<String, Integer> constants = TEXT_ALIGN_CONSTANTS.get(ViewProps.TEXT_ALIGN); | ||
int gravity = constants.get(textAlign); | ||
view.setGravityHorizontal(gravity); | ||
} | ||
|
||
@ReactProp(name = "textAlignVertical") | ||
public void setTextAlignVertical(ReactEditText view, int gravity) { | ||
public void setTextAlignVertical(ReactEditText view, @Nullable String textAlignVertical) { | ||
Map<String, Integer> constants = TEXT_ALIGN_CONSTANTS.get("textAlignVertical"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't use hash maps for this in java. They tend not to be very efficient (compering to alternative approaches) when you have a small number of keys. I'd recommend to at least have a separate maps for |
||
int gravity = constants.get(textAlignVertical); | ||
view.setGravityVertical(gravity); | ||
} | ||
|
||
|
@@ -417,19 +441,4 @@ public boolean onEditorAction(TextView v, int actionId, KeyEvent keyEvent) { | |
} | ||
}); | ||
} | ||
|
||
@Override | ||
public @Nullable Map getExportedViewConstants() { | ||
return MapBuilder.of( | ||
"TextAlign", | ||
MapBuilder.of( | ||
"start", Gravity.START, | ||
"center", Gravity.CENTER_HORIZONTAL, | ||
"end", Gravity.END), | ||
"TextAlignVertical", | ||
MapBuilder.of( | ||
"top", Gravity.TOP, | ||
"center", Gravity.CENTER_VERTICAL, | ||
"bottom", Gravity.BOTTOM)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'd still prefer this to be moved into styles as opposed to being a view prop. Just for the sake of consistency with the Text element.
Note that currently
style
property ofTextInput
element use the samepropType
asText
(see here), which allowstextAlign
(see here). This means that usingtextAlign
forTextInput
instyle
won't result in a warning.I think we should add
textAlignVertical
to thestyle
definition (with an annotation it's android-only) and remove it frompropTypes
definition forTextInput