Skip to content

Fix flex issue of TextField when rendered #2391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ export default class TextFieldScreen extends Component {

<Text marginB-s1>Inline</Text>
<View row>
<TextField placeholder="hours" inline/>
<TextField placeholder="hours"/>
<Text marginT-s1> : </Text>
<TextField placeholder="minutes" inline/>
<TextField placeholder="minutes"/>
</View>
</View>
<KeyboardAwareInsetsView/>
Expand Down
21 changes: 15 additions & 6 deletions src/components/numberInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,22 @@ function NumberInput(props: NumberInputProps, ref: any) {
processInput(data.userInput);
}
}, [options]);

const hasText = useMemo(() => {
// Render (none or) both leading and trailing accessories together for flexness (especially when validation message is long)
return !isEmpty(leadingText) || !isEmpty(trailingText);
}, [leadingText, trailingText]);

const leadingAccessory = useMemo(() => {
if (leadingText) {
return <Text style={leadingTextStyle}>{leadingText}</Text>;
if (hasText) {
return <Text style={[styles.accessory, {textAlign: 'right'}, leadingTextStyle]}>{leadingText}</Text>;
}
}, [leadingText, leadingTextStyle]);
}, [hasText, leadingText, leadingTextStyle]);
const trailingAccessory = useMemo(() => {
if (trailingText) {
return <Text style={trailingTextStyle}>{trailingText}</Text>;
if (hasText) {
return <Text style={[styles.accessory, trailingTextStyle]}>{trailingText}</Text>;
}
}, [trailingText, trailingTextStyle]);
}, [hasText, trailingText, trailingTextStyle]);

const _containerStyle = useMemo(() => {
return [styles.containerStyle, containerStyle];
Expand Down Expand Up @@ -149,5 +155,8 @@ export default React.forwardRef<TextFieldProps, NumberInputProps>(NumberInput);
const styles = StyleSheet.create({
containerStyle: {
overflow: 'hidden'
},
accessory: {
flexGrow: 999
}
});
12 changes: 7 additions & 5 deletions src/incubator/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ const TextField = (props: InternalTextFieldProps) => {
// Input
placeholder,
children,
centered = false,
inline = false,
centered,
...others
} = usePreset(props);
const {ref: leadingAccessoryRef, measurements: leadingAccessoryMeasurements} = useMeasure();
Expand Down Expand Up @@ -135,11 +134,14 @@ const TextField = (props: InternalTextFieldProps) => {
<View style={[paddings, fieldStyle]} row centerV centerH={centered}>
{/* <View row centerV> */}
{leadingAccessoryClone}
{/* Note: We should avoid flexing this when the input is inlined or centered*/}

{/* Note: We're passing flexG to the View to support properly inline behavior - so the input will be rendered correctly in a row container.
Known Issue: This slightly push the trailing accessory when entering a long text
*/}
{children || (
<View flex={!centered && !inline}>
<View flexG>
{/* Note: Render dummy placeholder for Android center issues */}
{Constants.isAndroid && (centered || inline) && (
{Constants.isAndroid && centered && (
<Text marginR-s1 style={styles.dummyPlaceholder}>
{placeholder}
</Text>
Expand Down
5 changes: 0 additions & 5 deletions src/incubator/TextField/textField.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@
"type": "boolean",
"description": "Whether to center the TextField - container and label"
},
{
"name": "inline",
"type": "boolean",
"description": "Set an alignment for inline behavior (when rendered inside a row container)"
},
{
"name": "useGestureHandlerInput",
"type": "boolean",
Expand Down
1 change: 1 addition & 0 deletions src/incubator/TextField/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ export type TextFieldProps = MarginModifiers &
*/
centered?: boolean;
/**
* @deprecated
* Set an alignment fit for inline behavior (when rendered inside a row container)
*/
inline?: boolean;
Expand Down