Skip to content

Commit 48d861f

Browse files
ethansharM-i-k-e-l
authored andcommitted
Add inline prop to TextField (#2386)
* Add inline prop to TextField * Fix example fix * Add dummy placeholder for Android center/inline issues
1 parent e553ec7 commit 48d861f

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

demo/src/screens/incubatorScreens/IncubatorTextFieldScreen.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,18 @@ export default class TextFieldScreen extends Component {
274274
/>
275275

276276
<Text h3 blue50 marginV-s4>
277-
Centered
277+
Custom Alignments
278278
</Text>
279279

280+
<Text marginB-s1>Centered</Text>
280281
<TextField label="PIN" placeholder="XXXX" centered/>
282+
283+
<Text marginB-s1>Inline</Text>
284+
<View row>
285+
<TextField placeholder="hours" inline/>
286+
<Text marginT-s1> : </Text>
287+
<TextField placeholder="minutes" inline/>
288+
</View>
281289
</View>
282290
<KeyboardAwareInsetsView/>
283291
</ScrollView>

src/incubator/TextField/index.tsx

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import React, {useMemo} from 'react';
99
import {StyleSheet} from 'react-native';
1010
import {isEmpty, trim, omit} from 'lodash';
11-
import {asBaseComponent, forwardRef} from '../../commons/new';
11+
import {asBaseComponent, Constants, forwardRef} from '../../commons/new';
1212
import View from '../../components/view';
13+
import Text from '../../components/text';
1314
import {useMeasure} from '../../hooks';
1415
import {
1516
TextFieldProps,
@@ -72,6 +73,7 @@ const TextField = (props: InternalTextFieldProps) => {
7273
placeholder,
7374
children,
7475
centered = false,
76+
inline = false,
7577
...others
7678
} = usePreset(props);
7779
const {ref: leadingAccessoryRef, measurements: leadingAccessoryMeasurements} = useMeasure();
@@ -130,30 +132,39 @@ const TextField = (props: InternalTextFieldProps) => {
130132
<View style={[paddings, fieldStyle]} row centerV centerH={centered}>
131133
{/* <View row centerV> */}
132134
{leadingAccessoryClone}
133-
{children || <View flex={!centered} flexG={centered} /* flex row */>
134-
{floatingPlaceholder && (
135-
<FloatingPlaceholder
136-
defaultValue={others.defaultValue}
135+
{/* Note: We should avoid flexing this when the input is inlined or centered*/}
136+
{children || (
137+
<View flex={!centered && !inline}>
138+
{/* Note: Render dummy placeholder for Android center issues */}
139+
{Constants.isAndroid && (centered || inline) && (
140+
<Text marginR-s1 style={styles.dummyPlaceholder}>
141+
{placeholder}
142+
</Text>
143+
)}
144+
{floatingPlaceholder && (
145+
<FloatingPlaceholder
146+
defaultValue={others.defaultValue}
147+
placeholder={placeholder}
148+
floatingPlaceholderStyle={_floatingPlaceholderStyle}
149+
floatingPlaceholderColor={floatingPlaceholderColor}
150+
floatOnFocus={floatOnFocus}
151+
validationMessagePosition={validationMessagePosition}
152+
extraOffset={leadingAccessoryMeasurements?.width}
153+
testID={`${props.testID}.floatingPlaceholder`}
154+
/>
155+
)}
156+
<Input
157+
placeholderTextColor={hidePlaceholder ? 'transparent' : placeholderTextColor}
158+
{...others}
159+
style={[typographyStyle, colorStyle, others.style]}
160+
onFocus={onFocus}
161+
onBlur={onBlur}
162+
onChangeText={onChangeText}
137163
placeholder={placeholder}
138-
floatingPlaceholderStyle={_floatingPlaceholderStyle}
139-
floatingPlaceholderColor={floatingPlaceholderColor}
140-
floatOnFocus={floatOnFocus}
141-
validationMessagePosition={validationMessagePosition}
142-
extraOffset={leadingAccessoryMeasurements?.width}
143-
testID={`${props.testID}.floatingPlaceholder`}
164+
hint={hint}
144165
/>
145-
)}
146-
<Input
147-
placeholderTextColor={hidePlaceholder ? 'transparent' : placeholderTextColor}
148-
{...others}
149-
style={[typographyStyle, colorStyle, others.style]}
150-
onFocus={onFocus}
151-
onBlur={onBlur}
152-
onChangeText={onChangeText}
153-
placeholder={placeholder}
154-
hint={hint}
155-
/>
156-
</View>}
166+
</View>
167+
)}
157168
{trailingAccessory}
158169
{/* </View> */}
159170
</View>
@@ -206,5 +217,8 @@ const styles = StyleSheet.create({
206217
centeredValidationMessage: {
207218
flexGrow: 1,
208219
textAlign: 'center'
220+
},
221+
dummyPlaceholder: {
222+
height: 0
209223
}
210224
});

src/incubator/TextField/textField.api.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@
8888
"type": "(value) => string | undefined",
8989
"description": "Custom formatter for the input value (used only when input if not focused)"
9090
},
91+
{
92+
"name": "centered",
93+
"type": "boolean",
94+
"description": "Whether to center the TextField - container and label"
95+
},
96+
{
97+
"name": "inline",
98+
"type": "boolean",
99+
"description": "Set an alignment for inline behavior (when rendered inside a row container)"
100+
},
91101
{
92102
"name": "useGestureHandlerInput",
93103
"type": "boolean",

src/incubator/TextField/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ export type TextFieldProps = MarginModifiers &
221221
* Whether to center the TextField - container and label
222222
*/
223223
centered?: boolean;
224+
/**
225+
* Set an alignment fit for inline behavior (when rendered inside a row container)
226+
*/
227+
inline?: boolean;
224228
};
225229

226230
export type InternalTextFieldProps = PropsWithChildren<

0 commit comments

Comments
 (0)