5
5
* 3. Passing typography preset that includes lineHeight usually cause alignment issues with
6
6
* other elements (leading/trailing accessories). It usually best to set lineHeight with undefined
7
7
*/
8
+ import { isEmpty , trim , omit } from 'lodash' ;
8
9
import React , { useMemo } from 'react' ;
9
10
import { StyleSheet } from 'react-native' ;
10
- import { isEmpty , trim , omit } from 'lodash' ;
11
11
import { asBaseComponent , Constants , forwardRef } from '../../commons/new' ;
12
12
import View from '../view' ;
13
13
import Text from '../text' ;
@@ -92,6 +92,7 @@ const TextField = (props: InternalTextFieldProps) => {
92
92
showMandatoryIndication,
93
93
...others
94
94
} = usePreset ( props ) ;
95
+
95
96
const { ref : leadingAccessoryRef , measurements : leadingAccessoryMeasurements } = useMeasure ( ) ;
96
97
const { onFocus, onBlur, onChangeText, fieldState, validateField, checkValidity} = useFieldState ( others ) ;
97
98
@@ -118,28 +119,23 @@ const TextField = (props: InternalTextFieldProps) => {
118
119
const colorStyle = useMemo ( ( ) => color && { color} , [ color ] ) ;
119
120
const _floatingPlaceholderStyle = useMemo ( ( ) => [ typographyStyle , floatingPlaceholderStyle ] ,
120
121
[ typographyStyle , floatingPlaceholderStyle ] ) ;
121
-
122
122
const fieldStyle = [ fieldStyleProp , dynamicFieldStyle ?.( context , { preset : props . preset } ) ] ;
123
123
const hidePlaceholder = shouldHidePlaceholder ( props , fieldState . isFocused ) ;
124
124
const retainTopMessageSpace = ! floatingPlaceholder && isEmpty ( trim ( label ) ) ;
125
125
const centeredContainerStyle = centered && styles . centeredContainer ;
126
- const _labelStyle = useMemo ( ( ) => ( centered ? [ labelStyle , styles . centeredLabel ] : labelStyle ) ,
127
- [ labelStyle , centered ] ) ;
128
- const _validationMessageStyle = useMemo ( ( ) => {
129
- return centered ? [ validationMessageStyle , styles . centeredValidationMessage ] : validationMessageStyle ;
130
- } , [ validationMessageStyle , centered ] ) ;
126
+ const centeredTextStyle = centered && styles . centeredText ;
127
+ const _labelStyle = useMemo ( ( ) => [ labelStyle , centeredTextStyle ] , [ labelStyle , centeredTextStyle ] ) ;
128
+ const _validationMessageStyle = useMemo ( ( ) => [ validationMessageStyle , centeredTextStyle ] ,
129
+ [ validationMessageStyle , centeredTextStyle ] ) ;
131
130
const hasValue = fieldState . value !== undefined ;
132
- const inputStyle = useMemo ( ( ) => {
133
- return [ typographyStyle , colorStyle , others . style , hasValue && centered && styles . centeredInput ] ;
134
- } , [ typographyStyle , colorStyle , others . style , centered , hasValue ] ) ;
135
- const dummyPlaceholderStyle = useMemo ( ( ) => {
136
- return [ inputStyle , styles . dummyPlaceholder ] ;
137
- } , [ inputStyle ] ) ;
131
+ const inputStyle = useMemo ( ( ) => [ typographyStyle , colorStyle , others . style , hasValue && centeredTextStyle ] ,
132
+ [ typographyStyle , colorStyle , others . style , centeredTextStyle , hasValue ] ) ;
133
+ const dummyPlaceholderStyle = useMemo ( ( ) => [ inputStyle , styles . dummyPlaceholder ] , [ inputStyle ] ) ;
138
134
139
135
return (
140
136
< FieldContext . Provider value = { context } >
141
137
< View { ...containerProps } style = { [ margins , positionStyle , containerStyle , centeredContainerStyle ] } >
142
- < View row spread >
138
+ < View row spread centerV = { Constants . isAndroid } style = { centeredContainerStyle } >
143
139
< Label
144
140
label = { label }
145
141
labelColor = { labelColor }
@@ -161,7 +157,7 @@ const TextField = (props: InternalTextFieldProps) => {
161
157
testID = { `${ props . testID } .validationMessage` }
162
158
/>
163
159
) }
164
- { topTrailingAccessory }
160
+ { topTrailingAccessory && < View > { topTrailingAccessory } </ View > }
165
161
</ View >
166
162
< View style = { [ paddings , fieldStyle ] } row centerV centerH = { centered } >
167
163
{ /* <View row centerV> */ }
@@ -259,6 +255,7 @@ export {
259
255
MandatoryIndication as TextFieldMandatoryIndication ,
260
256
TextFieldValidators
261
257
} ;
258
+
262
259
export default asBaseComponent < TextFieldProps , StaticMembers , TextFieldRef > ( forwardRef ( TextField as any ) , {
263
260
modifiersOptions : {
264
261
margins : true ,
@@ -273,13 +270,7 @@ const styles = StyleSheet.create({
273
270
centeredContainer : {
274
271
alignSelf : 'center'
275
272
} ,
276
- centeredLabel : {
277
- textAlign : 'center'
278
- } ,
279
- centeredInput : {
280
- textAlign : 'center'
281
- } ,
282
- centeredValidationMessage : {
273
+ centeredText : {
283
274
textAlign : 'center'
284
275
} ,
285
276
dummyPlaceholder : {
0 commit comments