Skip to content

Commit 4e2543e

Browse files
fix: textinput multiline with numberoflines issue
1 parent ff0df54 commit 4e2543e

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

src/components/TextInput/TextInput.tsx

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,21 @@ const TextInput = forwardRef<TextInputHandles, Props>(
482482

483483
const scaledLabel = !!(value || focused);
484484

485+
const defaultLineHeight = 24;
486+
487+
const lineHeight =
488+
// MD3 fonts use `bodyLarge`
489+
(theme.fonts as any)?.bodyLarge?.lineHeight ??
490+
// MD2 fonts use `regular` (no lineHeight, so rely on size * 1.3)
491+
((theme.fonts as any)?.regular?.lineHeight ||
492+
(theme.fonts as any)?.regular?.fontSize * 1.3) ??
493+
defaultLineHeight;
494+
495+
const autoHeight =
496+
multiline && rest.numberOfLines
497+
? rest.numberOfLines * lineHeight + 8 // + padding
498+
: undefined;
499+
485500
if (mode === 'outlined') {
486501
return (
487502
<TextInputOutlined
@@ -519,7 +534,10 @@ const TextInput = forwardRef<TextInputHandles, Props>(
519534
onLeftAffixLayoutChange={onLeftAffixLayoutChange}
520535
onRightAffixLayoutChange={onRightAffixLayoutChange}
521536
maxFontSizeMultiplier={maxFontSizeMultiplier}
522-
contentStyle={contentStyle}
537+
contentStyle={[
538+
contentStyle,
539+
autoHeight ? { minHeight: autoHeight } : undefined,
540+
].filter(Boolean)}
523541
scaledLabel={scaledLabel}
524542
/>
525543
);
@@ -561,7 +579,10 @@ const TextInput = forwardRef<TextInputHandles, Props>(
561579
onLeftAffixLayoutChange={onLeftAffixLayoutChange}
562580
onRightAffixLayoutChange={onRightAffixLayoutChange}
563581
maxFontSizeMultiplier={maxFontSizeMultiplier}
564-
contentStyle={contentStyle}
582+
contentStyle={[
583+
contentStyle,
584+
autoHeight ? { minHeight: autoHeight } : undefined,
585+
].filter(Boolean)}
565586
scaledLabel={scaledLabel}
566587
/>
567588
);

src/components/__tests__/__snapshots__/TextInput.test.tsx.snap

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ exports[`call onPress when affix adornment pressed 1`] = `
224224
"marginLeft": 0,
225225
"paddingLeft": 40,
226226
},
227-
undefined,
227+
[],
228228
]
229229
}
230230
testID="text-input-flat"
@@ -545,7 +545,7 @@ exports[`correctly applies a component as the text label 1`] = `
545545
[
546546
{},
547547
],
548-
undefined,
548+
[],
549549
]
550550
}
551551
testID="text-input-flat"
@@ -779,7 +779,7 @@ exports[`correctly applies cursorColor prop 1`] = `
779779
[
780780
{},
781781
],
782-
undefined,
782+
[],
783783
]
784784
}
785785
testID="text-input-flat"
@@ -1013,7 +1013,7 @@ exports[`correctly applies default textAlign based on default RTL 1`] = `
10131013
[
10141014
{},
10151015
],
1016-
undefined,
1016+
[],
10171017
]
10181018
}
10191019
testID="text-input-flat"
@@ -1272,7 +1272,7 @@ exports[`correctly applies height to multiline Outline TextInput 1`] = `
12721272
[
12731273
{},
12741274
],
1275-
undefined,
1275+
[],
12761276
]
12771277
}
12781278
testID="text-input-outlined"
@@ -1506,9 +1506,11 @@ exports[`correctly applies paddingLeft from contentStyleProp 1`] = `
15061506
[
15071507
{},
15081508
],
1509-
{
1510-
"paddingLeft": 20,
1511-
},
1509+
[
1510+
{
1511+
"paddingLeft": 20,
1512+
},
1513+
],
15121514
]
15131515
}
15141516
testID="text-input-flat"
@@ -1742,7 +1744,7 @@ exports[`correctly applies textAlign center 1`] = `
17421744
[
17431745
{},
17441746
],
1745-
undefined,
1747+
[],
17461748
]
17471749
}
17481750
testID="text-input-flat"
@@ -1979,7 +1981,7 @@ exports[`correctly renders left-side affix adornment, and right-side icon adornm
19791981
"paddingLeft": 40,
19801982
"paddingRight": 16,
19811983
},
1982-
undefined,
1984+
[],
19831985
]
19841986
}
19851987
testID="text-input-flat"
@@ -2406,7 +2408,7 @@ exports[`correctly renders left-side icon adornment, and right-side affix adornm
24062408
"paddingLeft": 16,
24072409
"paddingRight": 40,
24082410
},
2409-
undefined,
2411+
[],
24102412
]
24112413
}
24122414
testID="text-input-flat"

0 commit comments

Comments
 (0)