Skip to content

Commit 92d241b

Browse files
ethansharM-i-k-e-l
andcommitted
Fix flex issue of TextField when rendered (#2391)
* Fix flex issue of TextField when rendered * Fix NumberInput with TextField's new flexness Co-authored-by: M-i-k-e-l <[email protected]>
1 parent 48d861f commit 92d241b

File tree

5 files changed

+25
-18
lines changed

5 files changed

+25
-18
lines changed

demo/src/screens/incubatorScreens/IncubatorTextFieldScreen.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ export default class TextFieldScreen extends Component {
282282

283283
<Text marginB-s1>Inline</Text>
284284
<View row>
285-
<TextField placeholder="hours" inline/>
285+
<TextField placeholder="hours"/>
286286
<Text marginT-s1> : </Text>
287-
<TextField placeholder="minutes" inline/>
287+
<TextField placeholder="minutes"/>
288288
</View>
289289
</View>
290290
<KeyboardAwareInsetsView/>

src/components/numberInput/index.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,22 @@ function NumberInput(props: NumberInputProps, ref: any) {
9393
processInput(data.userInput);
9494
}
9595
}, [options]);
96+
97+
const hasText = useMemo(() => {
98+
// Render (none or) both leading and trailing accessories together for flexness (especially when validation message is long)
99+
return !isEmpty(leadingText) || !isEmpty(trailingText);
100+
}, [leadingText, trailingText]);
101+
96102
const leadingAccessory = useMemo(() => {
97-
if (leadingText) {
98-
return <Text style={leadingTextStyle}>{leadingText}</Text>;
103+
if (hasText) {
104+
return <Text style={[styles.accessory, {textAlign: 'right'}, leadingTextStyle]}>{leadingText}</Text>;
99105
}
100-
}, [leadingText, leadingTextStyle]);
106+
}, [hasText, leadingText, leadingTextStyle]);
101107
const trailingAccessory = useMemo(() => {
102-
if (trailingText) {
103-
return <Text style={trailingTextStyle}>{trailingText}</Text>;
108+
if (hasText) {
109+
return <Text style={[styles.accessory, trailingTextStyle]}>{trailingText}</Text>;
104110
}
105-
}, [trailingText, trailingTextStyle]);
111+
}, [hasText, trailingText, trailingTextStyle]);
106112

107113
const _containerStyle = useMemo(() => {
108114
return [styles.containerStyle, containerStyle];
@@ -148,5 +154,8 @@ export default React.forwardRef<TextFieldProps, NumberInputProps>(NumberInput);
148154
const styles = StyleSheet.create({
149155
containerStyle: {
150156
overflow: 'hidden'
157+
},
158+
accessory: {
159+
flexGrow: 999
151160
}
152161
});

src/incubator/TextField/index.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ const TextField = (props: InternalTextFieldProps) => {
7272
// Input
7373
placeholder,
7474
children,
75-
centered = false,
76-
inline = false,
75+
centered,
7776
...others
7877
} = usePreset(props);
7978
const {ref: leadingAccessoryRef, measurements: leadingAccessoryMeasurements} = useMeasure();
@@ -132,11 +131,14 @@ const TextField = (props: InternalTextFieldProps) => {
132131
<View style={[paddings, fieldStyle]} row centerV centerH={centered}>
133132
{/* <View row centerV> */}
134133
{leadingAccessoryClone}
135-
{/* Note: We should avoid flexing this when the input is inlined or centered*/}
134+
135+
{/* Note: We're passing flexG to the View to support properly inline behavior - so the input will be rendered correctly in a row container.
136+
Known Issue: This slightly push the trailing accessory when entering a long text
137+
*/}
136138
{children || (
137-
<View flex={!centered && !inline}>
139+
<View flexG>
138140
{/* Note: Render dummy placeholder for Android center issues */}
139-
{Constants.isAndroid && (centered || inline) && (
141+
{Constants.isAndroid && centered && (
140142
<Text marginR-s1 style={styles.dummyPlaceholder}>
141143
{placeholder}
142144
</Text>

src/incubator/TextField/textField.api.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,6 @@
9393
"type": "boolean",
9494
"description": "Whether to center the TextField - container and label"
9595
},
96-
{
97-
"name": "inline",
98-
"type": "boolean",
99-
"description": "Set an alignment for inline behavior (when rendered inside a row container)"
100-
},
10196
{
10297
"name": "useGestureHandlerInput",
10398
"type": "boolean",

src/incubator/TextField/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ export type TextFieldProps = MarginModifiers &
222222
*/
223223
centered?: boolean;
224224
/**
225+
* @deprecated
225226
* Set an alignment fit for inline behavior (when rendered inside a row container)
226227
*/
227228
inline?: boolean;

0 commit comments

Comments
 (0)