Skip to content

Commit

Permalink
fix: textinput sizing for [email protected] (#4084)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomasz Janiczek <[email protected]>
  • Loading branch information
tjaniczek and Tomasz Janiczek authored Sep 18, 2023
1 parent 9d802f6 commit 2fa36a4
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 34 deletions.
Binary file added example/assets/fonts/Abel-Regular.ttf
Binary file not shown.
23 changes: 23 additions & 0 deletions example/src/Examples/TextInputExample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import {
View,
} from 'react-native';

import { useFonts } from 'expo-font';
import {
configureFonts,
HelperText,
List,
MD2Colors,
Expand Down Expand Up @@ -141,6 +143,10 @@ const TextInputExample = () => {
});
};

const [fontsLoaded] = useFonts({
Abel: require('../../assets/fonts/Abel-Regular.ttf'),
});

const [expandedId, setExpandedId] = React.useState<ExpandedId>('flat');

const onAccordionPress = (id: string | number) =>
Expand Down Expand Up @@ -674,6 +680,23 @@ const TextInputExample = () => {
placeholder="Custom colors"
/>
</View>
{fontsLoaded && theme.isV3 ? (
<View style={styles.inputContainerStyle}>
<TextInput
mode="outlined"
label="Text input with custom font"
placeholder="Custom font"
style={styles.fontSize}
theme={{
fonts: configureFonts({
config: {
fontFamily: 'Abel',
},
}),
}}
/>
</View>
) : null}
</List.Accordion>
</List.AccordionGroup>
</ScreenWrapper>
Expand Down
4 changes: 2 additions & 2 deletions example/src/index.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function PaperExample() {
useKeepAwake();

const [fontsLoaded] = useFonts({
NotoSans: require('../assets/fonts/NotoSans-Regular.ttf'),
Abel: require('../assets/fonts/Abel-Regular.ttf'),
});

const [isReady, setIsReady] = React.useState(false);
Expand Down Expand Up @@ -220,7 +220,7 @@ export default function PaperExample() {
...combinedTheme,
fonts: configureFonts({
config: {
fontFamily: 'NotoSans',
fontFamily: 'Abel',
},
}),
};
Expand Down
4 changes: 2 additions & 2 deletions example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function PaperExample() {
useKeepAwake();

const [fontsLoaded] = useFonts({
NotoSans: require('../assets/fonts/NotoSans-Regular.ttf'),
Abel: require('../assets/fonts/Abel-Regular.ttf'),
});

const [isReady, setIsReady] = React.useState(false);
Expand Down Expand Up @@ -177,7 +177,7 @@ export default function PaperExample() {
...combinedTheme,
fonts: configureFonts({
config: {
fontFamily: 'NotoSans',
fontFamily: 'Abel',
},
}),
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/TextInput/TextInputFlat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ const TextInputFlat = ({

const {
fontSize: fontSizeStyle,
lineHeight,
lineHeight: lineHeightStyle,
fontWeight,
height,
paddingHorizontal,
textAlign,
...viewStyle
} = (StyleSheet.flatten(style) || {}) as TextStyle;
const fontSize = fontSizeStyle || MAXIMIZED_LABEL_FONT_SIZE;
const lineHeight = lineHeightStyle || fontSize * 1.2;

const isPaddingHorizontalPassed =
paddingHorizontal !== undefined && typeof paddingHorizontal === 'number';
Expand Down
3 changes: 2 additions & 1 deletion src/components/TextInput/TextInputOutlined.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ const TextInputOutlined = ({
const {
fontSize: fontSizeStyle,
fontWeight,
lineHeight,
lineHeight: lineHeightStyle,
height,
backgroundColor = colors?.background,
textAlign,
...viewStyle
} = (StyleSheet.flatten(style) || {}) as TextStyle;
const fontSize = fontSizeStyle || MAXIMIZED_LABEL_FONT_SIZE;
const lineHeight = lineHeightStyle || fontSize * 1.2;

const {
inputTextColor,
Expand Down
72 changes: 71 additions & 1 deletion src/components/__tests__/TextInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable react-native/no-inline-styles */
import * as React from 'react';
import { StyleSheet, Text, Platform, I18nManager } from 'react-native';
import { StyleSheet, Text, Platform, I18nManager, View } from 'react-native';

import { fireEvent, render } from '@testing-library/react-native';
import color from 'color';
Expand Down Expand Up @@ -382,6 +382,76 @@ it("correctly applies theme background to label when input's background is trans
});
});

it('always applies line height, even if not specified', () => {
const { getByTestId } = render(
<View>
<TextInput
mode="outlined"
label="Default font outlined"
value="Some test value"
testID="default-font"
/>
<TextInput
mode="flat"
label="Default font outlined - flat"
value="Some test value"
testID="default-font-flat"
/>
<TextInput
mode="outlined"
label="Large font outlined"
value="Some test value"
testID="large-font"
style={{
fontSize: 30,
}}
/>
<TextInput
mode="outlined"
label="Large font outlined - flat"
value="Some test value"
testID="large-font-flat"
style={{
fontSize: 30,
}}
/>
<TextInput
mode="outlined"
label="Custom line height outlined"
value="Some test value"
testID="custom-line-height"
style={{
fontSize: 40,
lineHeight: 29,
}}
/>
<TextInput
mode="outlined"
label="Custom line height outlined - flat"
value="Some test value"
testID="custom-line-height-flat"
style={{
fontSize: 40,
lineHeight: 29,
}}
/>
</View>
);

expect(getByTestId('default-font')).toHaveStyle({ lineHeight: 16 * 1.2 });
expect(getByTestId('default-font-flat')).toHaveStyle({
lineHeight: 16 * 1.2,
});

expect(getByTestId('large-font')).toHaveStyle({ lineHeight: 30 * 1.2 });
expect(getByTestId('large-font-flat')).toHaveStyle({ lineHeight: 30 * 1.2 });

expect(getByTestId('custom-line-height')).toHaveStyle({ lineHeight: 29 });
expect(getByTestId('custom-line-height-flat')).toHaveStyle({
lineHeight: 29,
});
});

describe('maxFontSizeMultiplier', () => {
const createInput = (
type: Exclude<Props['mode'], undefined>,
Expand Down
Loading

0 comments on commit 2fa36a4

Please sign in to comment.