Skip to content

Commit 2fa36a4

Browse files
tjaniczekTomasz Janiczek
andauthored
fix: textinput sizing for [email protected] (#4084)
Co-authored-by: Tomasz Janiczek <[email protected]>
1 parent 9d802f6 commit 2fa36a4

File tree

8 files changed

+129
-34
lines changed

8 files changed

+129
-34
lines changed

example/assets/fonts/Abel-Regular.ttf

32.4 KB
Binary file not shown.

example/src/Examples/TextInputExample.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {
77
View,
88
} from 'react-native';
99

10+
import { useFonts } from 'expo-font';
1011
import {
12+
configureFonts,
1113
HelperText,
1214
List,
1315
MD2Colors,
@@ -141,6 +143,10 @@ const TextInputExample = () => {
141143
});
142144
};
143145

146+
const [fontsLoaded] = useFonts({
147+
Abel: require('../../assets/fonts/Abel-Regular.ttf'),
148+
});
149+
144150
const [expandedId, setExpandedId] = React.useState<ExpandedId>('flat');
145151

146152
const onAccordionPress = (id: string | number) =>
@@ -674,6 +680,23 @@ const TextInputExample = () => {
674680
placeholder="Custom colors"
675681
/>
676682
</View>
683+
{fontsLoaded && theme.isV3 ? (
684+
<View style={styles.inputContainerStyle}>
685+
<TextInput
686+
mode="outlined"
687+
label="Text input with custom font"
688+
placeholder="Custom font"
689+
style={styles.fontSize}
690+
theme={{
691+
fonts: configureFonts({
692+
config: {
693+
fontFamily: 'Abel',
694+
},
695+
}),
696+
}}
697+
/>
698+
</View>
699+
) : null}
677700
</List.Accordion>
678701
</List.AccordionGroup>
679702
</ScreenWrapper>

example/src/index.native.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default function PaperExample() {
5959
useKeepAwake();
6060

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

6565
const [isReady, setIsReady] = React.useState(false);
@@ -220,7 +220,7 @@ export default function PaperExample() {
220220
...combinedTheme,
221221
fonts: configureFonts({
222222
config: {
223-
fontFamily: 'NotoSans',
223+
fontFamily: 'Abel',
224224
},
225225
}),
226226
};

example/src/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export default function PaperExample() {
5050
useKeepAwake();
5151

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

5656
const [isReady, setIsReady] = React.useState(false);
@@ -177,7 +177,7 @@ export default function PaperExample() {
177177
...combinedTheme,
178178
fonts: configureFonts({
179179
config: {
180-
fontFamily: 'NotoSans',
180+
fontFamily: 'Abel',
181181
},
182182
}),
183183
};

src/components/TextInput/TextInputFlat.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,15 @@ const TextInputFlat = ({
8383

8484
const {
8585
fontSize: fontSizeStyle,
86-
lineHeight,
86+
lineHeight: lineHeightStyle,
8787
fontWeight,
8888
height,
8989
paddingHorizontal,
9090
textAlign,
9191
...viewStyle
9292
} = (StyleSheet.flatten(style) || {}) as TextStyle;
9393
const fontSize = fontSizeStyle || MAXIMIZED_LABEL_FONT_SIZE;
94+
const lineHeight = lineHeightStyle || fontSize * 1.2;
9495

9596
const isPaddingHorizontalPassed =
9697
paddingHorizontal !== undefined && typeof paddingHorizontal === 'number';

src/components/TextInput/TextInputOutlined.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,14 @@ const TextInputOutlined = ({
8686
const {
8787
fontSize: fontSizeStyle,
8888
fontWeight,
89-
lineHeight,
89+
lineHeight: lineHeightStyle,
9090
height,
9191
backgroundColor = colors?.background,
9292
textAlign,
9393
...viewStyle
9494
} = (StyleSheet.flatten(style) || {}) as TextStyle;
9595
const fontSize = fontSizeStyle || MAXIMIZED_LABEL_FONT_SIZE;
96+
const lineHeight = lineHeightStyle || fontSize * 1.2;
9697

9798
const {
9899
inputTextColor,

src/components/__tests__/TextInput.test.tsx

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable react-native/no-inline-styles */
22
import * as React from 'react';
3-
import { StyleSheet, Text, Platform, I18nManager } from 'react-native';
3+
import { StyleSheet, Text, Platform, I18nManager, View } from 'react-native';
44

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

385+
it('always applies line height, even if not specified', () => {
386+
const { getByTestId } = render(
387+
<View>
388+
<TextInput
389+
mode="outlined"
390+
label="Default font outlined"
391+
value="Some test value"
392+
testID="default-font"
393+
/>
394+
<TextInput
395+
mode="flat"
396+
label="Default font outlined - flat"
397+
value="Some test value"
398+
testID="default-font-flat"
399+
/>
400+
<TextInput
401+
mode="outlined"
402+
label="Large font outlined"
403+
value="Some test value"
404+
testID="large-font"
405+
style={{
406+
fontSize: 30,
407+
}}
408+
/>
409+
<TextInput
410+
mode="outlined"
411+
label="Large font outlined - flat"
412+
value="Some test value"
413+
testID="large-font-flat"
414+
style={{
415+
fontSize: 30,
416+
}}
417+
/>
418+
<TextInput
419+
mode="outlined"
420+
label="Custom line height outlined"
421+
value="Some test value"
422+
testID="custom-line-height"
423+
style={{
424+
fontSize: 40,
425+
lineHeight: 29,
426+
}}
427+
/>
428+
<TextInput
429+
mode="outlined"
430+
label="Custom line height outlined - flat"
431+
value="Some test value"
432+
testID="custom-line-height-flat"
433+
style={{
434+
fontSize: 40,
435+
lineHeight: 29,
436+
}}
437+
/>
438+
</View>
439+
);
440+
441+
expect(getByTestId('default-font')).toHaveStyle({ lineHeight: 16 * 1.2 });
442+
expect(getByTestId('default-font-flat')).toHaveStyle({
443+
lineHeight: 16 * 1.2,
444+
});
445+
446+
expect(getByTestId('large-font')).toHaveStyle({ lineHeight: 30 * 1.2 });
447+
expect(getByTestId('large-font-flat')).toHaveStyle({ lineHeight: 30 * 1.2 });
448+
449+
expect(getByTestId('custom-line-height')).toHaveStyle({ lineHeight: 29 });
450+
expect(getByTestId('custom-line-height-flat')).toHaveStyle({
451+
lineHeight: 29,
452+
});
453+
});
454+
385455
describe('maxFontSizeMultiplier', () => {
386456
const createInput = (
387457
type: Exclude<Props['mode'], undefined>,

0 commit comments

Comments
 (0)