1
1
package 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 ;
2
8
import android .util .Log ;
9
+ import android .view .Gravity ;
3
10
import android .view .inputmethod .EditorInfo ;
4
11
import android .widget .EditText ;
5
12
import androidx .annotation .NonNull ;
6
13
import androidx .annotation .Nullable ;
14
+
15
+ import com .facebook .react .bridge .JSApplicationIllegalArgumentException ;
7
16
import com .facebook .react .bridge .ReactApplicationContext ;
8
17
import com .facebook .react .bridge .ReadableArray ;
9
18
import com .facebook .react .common .MapBuilder ;
19
+ import com .facebook .react .uimanager .PixelUtil ;
10
20
import com .facebook .react .uimanager .SimpleViewManager ;
11
21
import com .facebook .react .uimanager .Spacing ;
12
22
import com .facebook .react .uimanager .ThemedReactContext ;
13
23
import com .facebook .react .uimanager .ViewProps ;
14
24
import com .facebook .react .uimanager .annotations .ReactProp ;
15
25
import com .facebook .react .uimanager .annotations .ReactPropGroup ;
26
+ import com .facebook .yoga .YogaConstants ;
16
27
import com .variabletextinput .view .VariableTextInput ;
17
28
18
29
import java .util .HashMap ;
30
+ import java .util .LinkedList ;
19
31
import java .util .Map ;
20
32
public class VariableTextInputViewManager extends SimpleViewManager <VariableTextInput > {
21
33
private enum RNTONATIVEMETHOD {
@@ -35,6 +47,9 @@ public String getName(){
35
47
private static final int [] PADDING_TYPES = {
36
48
Spacing .ALL , Spacing .LEFT , Spacing .RIGHT , Spacing .TOP , Spacing .BOTTOM ,
37
49
};
50
+ private static final int [] SPACING_TYPES = {
51
+ Spacing .ALL , Spacing .LEFT , Spacing .RIGHT , Spacing .TOP , Spacing .BOTTOM ,
52
+ };
38
53
public static final String REACT_CLASS = "VariableTextInputView" ;
39
54
ReactApplicationContext mCallerContext ;
40
55
VariableTextInput editText ;
@@ -63,6 +78,12 @@ public void setColor(VariableTextInput view, @Nullable Integer color) {
63
78
view .setTextColor (color );
64
79
}
65
80
}
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
+ }
66
87
@ ReactProp (name = ViewProps .FONT_SIZE , customType = "FontColor" )
67
88
public void setFontSize (VariableTextInput view , @ Nullable Integer fontSize ) {
68
89
if (fontSize != null ) {
@@ -81,7 +102,10 @@ public void setSelectionColor(VariableTextInput view, @Nullable Integer color) {
81
102
view .setHighlightColor (color );
82
103
}
83
104
}
84
-
105
+ @ ReactProp (name = "maxLength" )
106
+ public void setMaxLength (VariableTextInput view , @ Nullable Integer maxLength ) {
107
+ view .setMaxLength (maxLength );
108
+ }
85
109
@ ReactProp (name = "handlesColor" , customType = "Color" )
86
110
public void setHandlesColor (VariableTextInput view , @ Nullable Integer color ) {
87
111
if (color != null ) {
@@ -92,9 +116,63 @@ public void setHandlesColor(VariableTextInput view, @Nullable Integer color) {
92
116
@ ReactPropGroup (names = {
93
117
"padding" , "paddingLeft" , "paddingRight" , "paddingTop" , "paddingBottom"
94
118
}, customType = "String" )
95
- public void setBorderColor (VariableTextInput view , int index , Integer padding ) {
119
+ public void setPadding (VariableTextInput view , int index , Integer padding ) {
96
120
view .setContentPadding (PADDING_TYPES [index ], padding );
97
121
}
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
+ }
98
176
@ ReactProp (name = "autoFocus" )
99
177
public void setAutoFocus (VariableTextInput view , boolean autoFocus ) {
100
178
view .setAutoFocus (autoFocus );
@@ -113,15 +191,49 @@ public void blur(VariableTextInput view) {
113
191
public void setPlaceholder (VariableTextInput view , String placeholder ){
114
192
view .setPlaceholder (placeholder );
115
193
}
194
+ @ ReactProp (name ="placeholderTextColor" ,customType = "Color" )
195
+ public void setPlaceholderTextColor (VariableTextInput view , @ Nullable Integer placeholderColor ){
196
+ view .setPlaceholderColor (placeholderColor );
197
+ }
116
198
@ ReactProp (name = "selectionColor" , customType = "Color" )
117
199
public void setSelectionColor (EditText view , @ Nullable Integer color ) {
118
200
if (color != null ) {
119
201
view .setHighlightColor (color );
120
202
}
121
203
}
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
+ }
125
237
}
126
238
@ ReactProp (name = "keyboardType" )
127
239
public void setKeyboardType (VariableTextInput view , String keyboardType ) {
0 commit comments