Skip to content

Commit 7b97866

Browse files
authored
TextField - fix for centered input (#3182)
* TextField - fix for centered input * add Note * update snapshot tests * don't retain validation space if has bottomAccessory * update snapshots * fix text alignment with charcounter
1 parent c9bde16 commit 7b97866

File tree

3 files changed

+52
-45
lines changed

3 files changed

+52
-45
lines changed

demo/src/screens/__tests__/__snapshots__/TextFieldScreen.spec.js.snap

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,7 +1982,7 @@ exports[`TextField Screen renders screen 1`] = `
19821982
undefined,
19831983
undefined,
19841984
[
1985-
undefined,
1985+
{},
19861986
undefined,
19871987
{
19881988
"flex": 1,
@@ -2103,7 +2103,7 @@ exports[`TextField Screen renders screen 1`] = `
21032103
"flexDirection": "row",
21042104
},
21052105
[
2106-
undefined,
2106+
{},
21072107
[
21082108
[
21092109
{
@@ -2144,6 +2144,7 @@ exports[`TextField Screen renders screen 1`] = `
21442144
}
21452145
}
21462146
editable={true}
2147+
maxLength={20}
21472148
onBlur={[Function]}
21482149
onChangeText={[Function]}
21492150
onFocus={[Function]}
@@ -2178,6 +2179,7 @@ exports[`TextField Screen renders screen 1`] = `
21782179
underlineColorAndroid="transparent"
21792180
validate="required"
21802181
validateOnBlur={true}
2182+
validateOnChange={true}
21812183
validationMessage="This field is required. That means you have to enter some value"
21822184
/>
21832185
</View>
@@ -2264,7 +2266,41 @@ exports[`TextField Screen renders screen 1`] = `
22642266
undefined,
22652267
]
22662268
}
2267-
/>
2269+
>
2270+
<Text
2271+
$textNeutral={true}
2272+
fsTagName="unmask"
2273+
style={
2274+
[
2275+
{
2276+
"backgroundColor": "transparent",
2277+
"color": "#20303C",
2278+
"writingDirection": "ltr",
2279+
},
2280+
undefined,
2281+
{
2282+
"color": "#6E7881",
2283+
},
2284+
undefined,
2285+
undefined,
2286+
{},
2287+
undefined,
2288+
undefined,
2289+
undefined,
2290+
[
2291+
{
2292+
"flexGrow": 1,
2293+
"textAlign": "right",
2294+
},
2295+
undefined,
2296+
],
2297+
]
2298+
}
2299+
testID="undefined.charCounter"
2300+
>
2301+
0/20
2302+
</Text>
2303+
</View>
22682304
</View>
22692305
</View>
22702306
<View
@@ -3361,39 +3397,6 @@ exports[`TextField Screen renders screen 1`] = `
33613397
]
33623398
}
33633399
>
3364-
<Text
3365-
$textDangerLight={true}
3366-
style={
3367-
[
3368-
{
3369-
"backgroundColor": "transparent",
3370-
"color": "#20303C",
3371-
"writingDirection": "ltr",
3372-
},
3373-
undefined,
3374-
{
3375-
"color": "#FC3D2F",
3376-
},
3377-
undefined,
3378-
undefined,
3379-
{},
3380-
undefined,
3381-
undefined,
3382-
undefined,
3383-
[
3384-
{
3385-
"flexShrink": 1,
3386-
"minHeight": 20,
3387-
},
3388-
[
3389-
undefined,
3390-
undefined,
3391-
],
3392-
],
3393-
]
3394-
}
3395-
testID="undefined.validationMessage"
3396-
/>
33973400
<Text
33983401
style={
33993402
[
@@ -6006,6 +6009,7 @@ exports[`TextField Screen renders screen 1`] = `
60066009
</View>
60076010
</View>
60086011
<View
6012+
center={true}
60096013
row={true}
60106014
spread={true}
60116015
style={
@@ -6018,22 +6022,22 @@ exports[`TextField Screen renders screen 1`] = `
60186022
{},
60196023
{},
60206024
{
6025+
"alignItems": "center",
60216026
"flexDirection": "row",
6022-
"justifyContent": "space-between",
6027+
"justifyContent": "center",
60236028
},
60246029
undefined,
60256030
]
60266031
}
60276032
>
60286033
<View
6034+
flexG={true}
60296035
style={
60306036
[
60316037
undefined,
60326038
undefined,
60336039
undefined,
6034-
{
6035-
"flex": 1,
6036-
},
6040+
undefined,
60376041
undefined,
60386042
{},
60396043
{},

demo/src/screens/componentScreens/TextFieldScreen.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,14 @@ export default class TextFieldScreen extends Component {
209209
validate="required"
210210
validationMessage="This field is required. That means you have to enter some value"
211211
containerStyle={{flex: 1}}
212+
validateOnChange
212213
validationMessagePosition={errorPosition}
213214
helperText={'Enter first and last name'}
214215
validationIcon={{source: Assets.icons.demo.exclamation, style: {marginTop: 1}}}
215216
topTrailingAccessory={<Icon source={Assets.icons.demo.info} size={16}/>}
216217
preset={preset}
218+
maxLength={20}
219+
showCharCounter
217220
/>
218221
<Button
219222
outline

src/components/textField/index.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const TextField = (props: InternalTextFieldProps) => {
8080
enableErrors, // TODO: rename to enableValidation
8181
validationMessageStyle,
8282
validationMessagePosition = ValidationMessagePosition.BOTTOM,
83-
retainValidationSpace = !helperText,
83+
retainValidationSpace = !helperText && !bottomAccessory,
8484
// Char Counter
8585
showCharCounter,
8686
charCounterStyle,
@@ -123,11 +123,11 @@ const TextField = (props: InternalTextFieldProps) => {
123123
const hidePlaceholder = shouldHidePlaceholder(props, fieldState.isFocused);
124124
const retainTopMessageSpace = !floatingPlaceholder && isEmpty(trim(label));
125125
const centeredContainerStyle = centered && styles.centeredContainer;
126-
const centeredTextStyle = centered && styles.centeredText;
126+
const centeredTextStyle = centered && !showCharCounter && styles.centeredText;
127127
const _labelStyle = useMemo(() => [labelStyle, centeredTextStyle], [labelStyle, centeredTextStyle]);
128128
const _validationMessageStyle = useMemo(() => [validationMessageStyle, centeredTextStyle],
129129
[validationMessageStyle, centeredTextStyle]);
130-
const hasValue = fieldState.value !== undefined;
130+
const hasValue = fieldState.value !== undefined; // NOTE: not pressable if centered without a value (so can't center placeholder)
131131
const inputStyle = useMemo(() => [typographyStyle, colorStyle, others.style, hasValue && centeredTextStyle],
132132
[typographyStyle, colorStyle, others.style, centeredTextStyle, hasValue]);
133133
const dummyPlaceholderStyle = useMemo(() => [inputStyle, styles.dummyPlaceholder], [inputStyle]);
@@ -208,8 +208,8 @@ const TextField = (props: InternalTextFieldProps) => {
208208
{trailingAccessory}
209209
{/* </View> */}
210210
</View>
211-
<View row spread>
212-
<View flex>
211+
<View row spread center={centered}>
212+
<View flex={!centered} flexG={centered}>
213213
{validationMessagePosition === ValidationMessagePosition.BOTTOM && (
214214
<ValidationMessage
215215
enableErrors={enableErrors}

0 commit comments

Comments
 (0)