Skip to content

Commit d4e69bd

Browse files
authored
TextField - fix centered with new topTrailingAccessory (#3132)
* TextField - fix centered with new topTrailingAccessory * fix for center inputStyle * clean styles * remove input fix * removing topTrailingAccessory marginL-s2
1 parent 4befb01 commit d4e69bd

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

demo/src/screens/componentScreens/TextFieldScreen.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,16 @@ export default class TextFieldScreen extends Component {
422422
</Text>
423423

424424
<Text marginB-s1 $textPrimary>Centered:</Text>
425-
<TextField label="PIN" placeholder="XXXX" centered/>
425+
<TextField
426+
label="PIN"
427+
placeholder="XXXX"
428+
centered
429+
topTrailingAccessory={<Icon source={Assets.icons.demo.info} size={16}/>}
430+
validate={'required'}
431+
validationMessage={'This field is required'}
432+
validateOnBlur
433+
validationMessagePosition={this.state.errorPosition}
434+
/>
426435

427436
<Text marginB-s1 $textPrimary>Inline:</Text>
428437
<View row>

src/components/textField/index.tsx

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* 3. Passing typography preset that includes lineHeight usually cause alignment issues with
66
* other elements (leading/trailing accessories). It usually best to set lineHeight with undefined
77
*/
8+
import {isEmpty, trim, omit} from 'lodash';
89
import React, {useMemo} from 'react';
910
import {StyleSheet} from 'react-native';
10-
import {isEmpty, trim, omit} from 'lodash';
1111
import {asBaseComponent, Constants, forwardRef} from '../../commons/new';
1212
import View from '../view';
1313
import Text from '../text';
@@ -92,6 +92,7 @@ const TextField = (props: InternalTextFieldProps) => {
9292
showMandatoryIndication,
9393
...others
9494
} = usePreset(props);
95+
9596
const {ref: leadingAccessoryRef, measurements: leadingAccessoryMeasurements} = useMeasure();
9697
const {onFocus, onBlur, onChangeText, fieldState, validateField, checkValidity} = useFieldState(others);
9798

@@ -118,28 +119,23 @@ const TextField = (props: InternalTextFieldProps) => {
118119
const colorStyle = useMemo(() => color && {color}, [color]);
119120
const _floatingPlaceholderStyle = useMemo(() => [typographyStyle, floatingPlaceholderStyle],
120121
[typographyStyle, floatingPlaceholderStyle]);
121-
122122
const fieldStyle = [fieldStyleProp, dynamicFieldStyle?.(context, {preset: props.preset})];
123123
const hidePlaceholder = shouldHidePlaceholder(props, fieldState.isFocused);
124124
const retainTopMessageSpace = !floatingPlaceholder && isEmpty(trim(label));
125125
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]);
131130
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]);
138134

139135
return (
140136
<FieldContext.Provider value={context}>
141137
<View {...containerProps} style={[margins, positionStyle, containerStyle, centeredContainerStyle]}>
142-
<View row spread>
138+
<View row spread centerV={Constants.isAndroid} style={centeredContainerStyle}>
143139
<Label
144140
label={label}
145141
labelColor={labelColor}
@@ -161,7 +157,7 @@ const TextField = (props: InternalTextFieldProps) => {
161157
testID={`${props.testID}.validationMessage`}
162158
/>
163159
)}
164-
{topTrailingAccessory}
160+
{topTrailingAccessory && <View>{topTrailingAccessory}</View>}
165161
</View>
166162
<View style={[paddings, fieldStyle]} row centerV centerH={centered}>
167163
{/* <View row centerV> */}
@@ -259,6 +255,7 @@ export {
259255
MandatoryIndication as TextFieldMandatoryIndication,
260256
TextFieldValidators
261257
};
258+
262259
export default asBaseComponent<TextFieldProps, StaticMembers, TextFieldRef>(forwardRef(TextField as any), {
263260
modifiersOptions: {
264261
margins: true,
@@ -273,13 +270,7 @@ const styles = StyleSheet.create({
273270
centeredContainer: {
274271
alignSelf: 'center'
275272
},
276-
centeredLabel: {
277-
textAlign: 'center'
278-
},
279-
centeredInput: {
280-
textAlign: 'center'
281-
},
282-
centeredValidationMessage: {
273+
centeredText: {
283274
textAlign: 'center'
284275
},
285276
dummyPlaceholder: {

0 commit comments

Comments
 (0)