Skip to content

Commit f18d3a4

Browse files
authored
Fix flex issues in TextFiled Incubator (#2345)
1 parent 53c22ee commit f18d3a4

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

demo/src/screens/incubatorScreens/IncubatorTextFieldScreen.tsx

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,7 @@ export default class TextFieldScreen extends Component {
277277
Centered
278278
</Text>
279279

280-
<TextField
281-
label="PIN"
282-
placeholder="XXXX"
283-
labelStyle={{alignSelf: 'center'}}
284-
containerStyle={{alignSelf: 'center'}}
285-
/>
280+
<TextField label="PIN" placeholder="XXXX" centered/>
286281
</View>
287282
<KeyboardAwareInsetsView/>
288283
</ScrollView>

src/incubator/TextField/index.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* other elements (leading/trailing accessories). It usually best to set lineHeight with undefined
77
*/
88
import React, {useMemo} from 'react';
9+
import {StyleSheet} from 'react-native';
910
import {isEmpty, trim, omit} from 'lodash';
1011
import {asBaseComponent, forwardRef} from '../../commons/new';
1112
import View from '../../components/view';
@@ -70,6 +71,7 @@ const TextField = (props: InternalTextFieldProps) => {
7071
// Input
7172
placeholder,
7273
children,
74+
centered = false,
7375
...others
7476
} = usePreset(props);
7577
const {ref: leadingAccessoryRef, measurements: leadingAccessoryMeasurements} = useMeasure();
@@ -96,14 +98,16 @@ const TextField = (props: InternalTextFieldProps) => {
9698
const fieldStyle = [fieldStyleProp, dynamicFieldStyle?.(context, {preset: props.preset})];
9799
const hidePlaceholder = shouldHidePlaceholder(props, fieldState.isFocused);
98100
const retainTopMessageSpace = !floatingPlaceholder && isEmpty(trim(label));
101+
const centeredContainerStyle = centered && styles.centeredContainer;
102+
const centeredLabelStyle = centered && styles.centeredLabel;
99103

100104
return (
101105
<FieldContext.Provider value={context}>
102-
<View style={[margins, positionStyle, containerStyle]}>
106+
<View style={[margins, positionStyle, containerStyle, centeredContainerStyle]}>
103107
<Label
104108
label={label}
105109
labelColor={labelColor}
106-
labelStyle={labelStyle}
110+
labelStyle={[labelStyle, centeredLabelStyle]}
107111
labelProps={labelProps}
108112
floatingPlaceholder={floatingPlaceholder}
109113
validationMessagePosition={validationMessagePosition}
@@ -122,7 +126,7 @@ const TextField = (props: InternalTextFieldProps) => {
122126
<View style={[paddings, fieldStyle]} row centerV>
123127
{/* <View row centerV> */}
124128
{leadingAccessoryClone}
125-
<View flexG /* flex row */>
129+
<View flex={!centered} flexG={centered} /* flex row */>
126130
{floatingPlaceholder && (
127131
<FloatingPlaceholder
128132
placeholder={placeholder}
@@ -188,3 +192,12 @@ export default asBaseComponent<TextFieldProps, StaticMembers>(forwardRef(TextFie
188192
color: true
189193
}
190194
});
195+
196+
const styles = StyleSheet.create({
197+
centeredContainer: {
198+
alignSelf: 'center'
199+
},
200+
centeredLabel: {
201+
textAlign: 'center'
202+
}
203+
});

src/incubator/TextField/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ export type TextFieldProps = MarginModifiers &
216216
* Predefined preset to use for styling the field
217217
*/
218218
preset?: 'default' | null | string;
219+
/**
220+
* Whether to center the TextField - container and label
221+
*/
222+
centered?: boolean;
219223
};
220224

221225
export type InternalTextFieldProps = PropsWithChildren<

0 commit comments

Comments
 (0)