Skip to content

Commit 8853435

Browse files
mario-aleoMikhail Bashkirov
authored andcommitted
fix(textarea): calculate max-height based on empty state (fix #184)
1 parent 80b816e commit 8853435

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packages/textarea/src/LionTextarea.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,20 @@ export class LionTextarea extends ObserverMixin(LionInput) {
6565
* To support maxRows we need to set max-height of the textarea
6666
*/
6767
setTextareaMaxHeight() {
68+
const { value } = this.inputElement;
69+
this.inputElement.value = '';
70+
this.resizeTextarea();
71+
6872
const cs = window.getComputedStyle(this.inputElement, null);
6973
const lineHeight = parseFloat(cs.lineHeight) || parseFloat(cs.height) / this.rows;
7074
const paddingOffset = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);
7175
const borderOffset = parseFloat(cs.borderTopWidth) + parseFloat(cs.borderBottomWidth);
7276
const offset = cs.boxSizing === 'border-box' ? paddingOffset + borderOffset : 0;
7377

7478
this.inputElement.style.maxHeight = `${lineHeight * this.maxRows + offset}px`;
79+
80+
this.inputElement.value = value;
81+
this.resizeTextarea();
7582
}
7683

7784
static get styles() {

packages/textarea/test/lion-textarea.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,31 @@ describe('<lion-textarea>', () => {
6161
}, Promise.resolve(0));
6262
});
6363

64+
it('stops growing after property "maxRows" is reached when there was an initial value', async () => {
65+
const el = await fixture(
66+
html`
67+
<lion-textarea .modelValue="${'1\n2\n3'}"></lion-textarea>
68+
`,
69+
);
70+
71+
return [4, 5, 6, 7, 8].reduce(async (heightPromise, i) => {
72+
const oldHeight = await heightPromise;
73+
el.modelValue += `\n`;
74+
await el.updateComplete;
75+
const newHeight = el.offsetHeight;
76+
77+
if (i > el.maxRows) {
78+
// stop growing
79+
expect(newHeight).to.equal(oldHeight);
80+
} else if (i > el.rows) {
81+
// growing normally
82+
expect(newHeight >= oldHeight).to.equal(true);
83+
}
84+
85+
return Promise.resolve(newHeight);
86+
}, Promise.resolve(0));
87+
});
88+
6489
it('stops shrinking after property "rows" is reached', async () => {
6590
const el = await fixture(
6691
html`

0 commit comments

Comments
 (0)