Skip to content

Commit a1aec1a

Browse files
ethansharM-i-k-e-l
andauthored
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 9425c85 commit a1aec1a

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
@@ -94,16 +94,22 @@ function NumberInput(props: NumberInputProps, ref: any) {
9494
processInput(data.userInput);
9595
}
9696
}, [options]);
97+
98+
const hasText = useMemo(() => {
99+
// Render (none or) both leading and trailing accessories together for flexness (especially when validation message is long)
100+
return !isEmpty(leadingText) || !isEmpty(trailingText);
101+
}, [leadingText, trailingText]);
102+
97103
const leadingAccessory = useMemo(() => {
98-
if (leadingText) {
99-
return <Text style={leadingTextStyle}>{leadingText}</Text>;
104+
if (hasText) {
105+
return <Text style={[styles.accessory, {textAlign: 'right'}, leadingTextStyle]}>{leadingText}</Text>;
100106
}
101-
}, [leadingText, leadingTextStyle]);
107+
}, [hasText, leadingText, leadingTextStyle]);
102108
const trailingAccessory = useMemo(() => {
103-
if (trailingText) {
104-
return <Text style={trailingTextStyle}>{trailingText}</Text>;
109+
if (hasText) {
110+
return <Text style={[styles.accessory, trailingTextStyle]}>{trailingText}</Text>;
105111
}
106-
}, [trailingText, trailingTextStyle]);
112+
}, [hasText, trailingText, trailingTextStyle]);
107113

108114
const _containerStyle = useMemo(() => {
109115
return [styles.containerStyle, containerStyle];
@@ -149,5 +155,8 @@ export default React.forwardRef<TextFieldProps, NumberInputProps>(NumberInput);
149155
const styles = StyleSheet.create({
150156
containerStyle: {
151157
overflow: 'hidden'
158+
},
159+
accessory: {
160+
flexGrow: 999
152161
}
153162
});

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();
@@ -135,11 +134,14 @@ const TextField = (props: InternalTextFieldProps) => {
135134
<View style={[paddings, fieldStyle]} row centerV centerH={centered}>
136135
{/* <View row centerV> */}
137136
{leadingAccessoryClone}
138-
{/* Note: We should avoid flexing this when the input is inlined or centered*/}
137+
138+
{/* Note: We're passing flexG to the View to support properly inline behavior - so the input will be rendered correctly in a row container.
139+
Known Issue: This slightly push the trailing accessory when entering a long text
140+
*/}
139141
{children || (
140-
<View flex={!centered && !inline}>
142+
<View flexG>
141143
{/* Note: Render dummy placeholder for Android center issues */}
142-
{Constants.isAndroid && (centered || inline) && (
144+
{Constants.isAndroid && centered && (
143145
<Text marginR-s1 style={styles.dummyPlaceholder}>
144146
{placeholder}
145147
</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)