11package com .variabletextinput ;
2+
3+ import android .graphics .PorterDuff ;
4+ import android .graphics .drawable .Drawable ;
5+ import android .os .Build ;
6+ import android .text .InputFilter ;
7+ import android .text .Layout ;
28import android .util .Log ;
9+ import android .view .Gravity ;
310import android .view .inputmethod .EditorInfo ;
411import android .widget .EditText ;
512import androidx .annotation .NonNull ;
613import androidx .annotation .Nullable ;
14+
15+ import com .facebook .react .bridge .JSApplicationIllegalArgumentException ;
716import com .facebook .react .bridge .ReactApplicationContext ;
817import com .facebook .react .bridge .ReadableArray ;
918import com .facebook .react .common .MapBuilder ;
19+ import com .facebook .react .uimanager .PixelUtil ;
1020import com .facebook .react .uimanager .SimpleViewManager ;
1121import com .facebook .react .uimanager .Spacing ;
1222import com .facebook .react .uimanager .ThemedReactContext ;
1323import com .facebook .react .uimanager .ViewProps ;
1424import com .facebook .react .uimanager .annotations .ReactProp ;
1525import com .facebook .react .uimanager .annotations .ReactPropGroup ;
26+ import com .facebook .yoga .YogaConstants ;
1627import com .variabletextinput .view .VariableTextInput ;
1728
1829import java .util .HashMap ;
30+ import java .util .LinkedList ;
1931import java .util .Map ;
2032public class VariableTextInputViewManager extends SimpleViewManager <VariableTextInput > {
2133 private enum RNTONATIVEMETHOD {
@@ -35,6 +47,9 @@ public String getName(){
3547 private static final int [] PADDING_TYPES = {
3648 Spacing .ALL , Spacing .LEFT , Spacing .RIGHT , Spacing .TOP , Spacing .BOTTOM ,
3749 };
50+ private static final int [] SPACING_TYPES = {
51+ Spacing .ALL , Spacing .LEFT , Spacing .RIGHT , Spacing .TOP , Spacing .BOTTOM ,
52+ };
3853 public static final String REACT_CLASS = "VariableTextInputView" ;
3954 ReactApplicationContext mCallerContext ;
4055 VariableTextInput editText ;
@@ -63,6 +78,12 @@ public void setColor(VariableTextInput view, @Nullable Integer color) {
6378 view .setTextColor (color );
6479 }
6580 }
81+ @ ReactProp (name = ViewProps .BACKGROUND_COLOR , customType = "Color" )
82+ public void setBackGroundColor (VariableTextInput view , @ Nullable Integer color ) {
83+ if (color != null ) {
84+ view .setBackGroundColor (color );
85+ }
86+ }
6687 @ ReactProp (name = ViewProps .FONT_SIZE , customType = "FontColor" )
6788 public void setFontSize (VariableTextInput view , @ Nullable Integer fontSize ) {
6889 if (fontSize != null ) {
@@ -81,7 +102,10 @@ public void setSelectionColor(VariableTextInput view, @Nullable Integer color) {
81102 view .setHighlightColor (color );
82103 }
83104 }
84-
105+ @ ReactProp (name = "maxLength" )
106+ public void setMaxLength (VariableTextInput view , @ Nullable Integer maxLength ) {
107+ view .setMaxLength (maxLength );
108+ }
85109 @ ReactProp (name = "handlesColor" , customType = "Color" )
86110 public void setHandlesColor (VariableTextInput view , @ Nullable Integer color ) {
87111 if (color != null ) {
@@ -92,9 +116,63 @@ public void setHandlesColor(VariableTextInput view, @Nullable Integer color) {
92116 @ ReactPropGroup (names = {
93117 "padding" , "paddingLeft" , "paddingRight" , "paddingTop" , "paddingBottom"
94118 }, customType = "String" )
95- public void setBorderColor (VariableTextInput view , int index , Integer padding ) {
119+ public void setPadding (VariableTextInput view , int index , Integer padding ) {
96120 view .setContentPadding (PADDING_TYPES [index ], padding );
97121 }
122+ @ ReactPropGroup (
123+ names = {
124+ ViewProps .BORDER_RADIUS ,
125+ ViewProps .BORDER_TOP_LEFT_RADIUS ,
126+ ViewProps .BORDER_TOP_RIGHT_RADIUS ,
127+ ViewProps .BORDER_BOTTOM_RIGHT_RADIUS ,
128+ ViewProps .BORDER_BOTTOM_LEFT_RADIUS
129+ },
130+ defaultFloat = YogaConstants .UNDEFINED )
131+ public void setBorderRadius (VariableTextInput view , int index , float borderRadius ) {
132+ if (!YogaConstants .isUndefined (borderRadius )) {
133+ borderRadius = PixelUtil .toPixelFromDIP (borderRadius );
134+ }
135+
136+ if (index == 0 ) {
137+ view .setBorderRadius (borderRadius );
138+ } else {
139+ view .setBorderRadius (borderRadius , index - 1 );
140+ }
141+ }
142+ @ ReactPropGroup (
143+ names = {
144+ ViewProps .BORDER_WIDTH ,
145+ ViewProps .BORDER_LEFT_WIDTH ,
146+ ViewProps .BORDER_RIGHT_WIDTH ,
147+ ViewProps .BORDER_TOP_WIDTH ,
148+ ViewProps .BORDER_BOTTOM_WIDTH ,
149+ },
150+ defaultFloat = YogaConstants .UNDEFINED )
151+ public void setBorderWidth (VariableTextInput view , int index , float width ) {
152+ if (!YogaConstants .isUndefined (width )) {
153+ width = PixelUtil .toPixelFromDIP (width );
154+ }
155+ view .setBorderWidth (SPACING_TYPES [index ], width );
156+ }
157+ @ ReactPropGroup (
158+ names = {
159+ "borderColor" ,
160+ "borderLeftColor" ,
161+ "borderRightColor" ,
162+ "borderTopColor" ,
163+ "borderBottomColor"
164+ },
165+ customType = "Color" )
166+ public void setBorderColor (VariableTextInput view , int index , Integer color ) {
167+ float rgbComponent =
168+ color == null ? YogaConstants .UNDEFINED : (float ) ((int ) color & 0x00FFFFFF );
169+ float alphaComponent = color == null ? YogaConstants .UNDEFINED : (float ) ((int ) color >>> 24 );
170+ view .setBorderColor (SPACING_TYPES [index ], rgbComponent , alphaComponent );
171+ }
172+ @ ReactProp (name = "borderStyle" )
173+ public void setBorderStyle (VariableTextInput view , @ Nullable String borderStyle ) {
174+ view .setBorderStyle (borderStyle );
175+ }
98176 @ ReactProp (name = "autoFocus" )
99177 public void setAutoFocus (VariableTextInput view , boolean autoFocus ) {
100178 view .setAutoFocus (autoFocus );
@@ -113,15 +191,49 @@ public void blur(VariableTextInput view) {
113191 public void setPlaceholder (VariableTextInput view , String placeholder ){
114192 view .setPlaceholder (placeholder );
115193 }
194+ @ ReactProp (name ="placeholderTextColor" ,customType = "Color" )
195+ public void setPlaceholderTextColor (VariableTextInput view , @ Nullable Integer placeholderColor ){
196+ view .setPlaceholderColor (placeholderColor );
197+ }
116198 @ ReactProp (name = "selectionColor" , customType = "Color" )
117199 public void setSelectionColor (EditText view , @ Nullable Integer color ) {
118200 if (color != null ) {
119201 view .setHighlightColor (color );
120202 }
121203 }
122- @ ReactProp (name ="underlineColorAndroid" ,customType ="Color" )
123- public void setUnderLineColorAndroid (VariableTextInput view ,@ Nullable Integer color ){
124- view .setUnderLineColorAndroid (color );
204+ @ ReactProp (name = "underlineColorAndroid" , customType = "Color" )
205+ public void setUnderlineColor (VariableTextInput view , @ Nullable Integer underlineColor ) {
206+ // Drawable.mutate() can sometimes crash due to an AOSP bug:
207+ // See https://code.google.com/p/android/issues/detail?id=191754 for more info
208+ Drawable background = view .getBackground ();
209+ Drawable drawableToMutate = background ;
210+
211+ if (background == null ) {
212+ return ;
213+ }
214+
215+ if (background .getConstantState () != null ) {
216+ try {
217+ drawableToMutate = background .mutate ();
218+ } catch (NullPointerException e ) {
219+ // FLog.e(TAG, "NullPointerException when setting underlineColorAndroid for TextInput", e);
220+ }
221+ }
222+
223+ if (underlineColor == null ) {
224+ drawableToMutate .clearColorFilter ();
225+ } else {
226+ // fixes underlineColor transparent not working on API 21
227+ // re-sets the TextInput underlineColor https://bit.ly/3M4alr6
228+ if (Build .VERSION .SDK_INT == Build .VERSION_CODES .LOLLIPOP ) {
229+ int bottomBorderColor = view .getBorderColor (Spacing .BOTTOM );
230+ setBorderColor (view , Spacing .START , underlineColor );
231+ drawableToMutate .setColorFilter (underlineColor , PorterDuff .Mode .SRC_IN );
232+ setBorderColor (view , Spacing .START , bottomBorderColor );
233+ } else {
234+ drawableToMutate .setColorFilter (underlineColor , PorterDuff .Mode .SRC_IN );
235+ }
236+ }
125237 }
126238 @ ReactProp (name = "keyboardType" )
127239 public void setKeyboardType (VariableTextInput view , String keyboardType ) {
0 commit comments