Merge pull request #189 from mario-aleo/fix/textareaMaxRow
Calculate max-height based on empty state
This commit is contained in:
commit
cd276aa787
2 changed files with 41 additions and 8 deletions
|
|
@ -65,6 +65,10 @@ export class LionTextarea extends ObserverMixin(LionInput) {
|
||||||
* To support maxRows we need to set max-height of the textarea
|
* To support maxRows we need to set max-height of the textarea
|
||||||
*/
|
*/
|
||||||
setTextareaMaxHeight() {
|
setTextareaMaxHeight() {
|
||||||
|
const { value } = this.inputElement;
|
||||||
|
this.inputElement.value = '';
|
||||||
|
this.resizeTextarea();
|
||||||
|
|
||||||
const cs = window.getComputedStyle(this.inputElement, null);
|
const cs = window.getComputedStyle(this.inputElement, null);
|
||||||
const lineHeight = parseFloat(cs.lineHeight) || parseFloat(cs.height) / this.rows;
|
const lineHeight = parseFloat(cs.lineHeight) || parseFloat(cs.height) / this.rows;
|
||||||
const paddingOffset = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);
|
const paddingOffset = parseFloat(cs.paddingTop) + parseFloat(cs.paddingBottom);
|
||||||
|
|
@ -72,6 +76,9 @@ export class LionTextarea extends ObserverMixin(LionInput) {
|
||||||
const offset = cs.boxSizing === 'border-box' ? paddingOffset + borderOffset : 0;
|
const offset = cs.boxSizing === 'border-box' ? paddingOffset + borderOffset : 0;
|
||||||
|
|
||||||
this.inputElement.style.maxHeight = `${lineHeight * this.maxRows + offset}px`;
|
this.inputElement.style.maxHeight = `${lineHeight * this.maxRows + offset}px`;
|
||||||
|
|
||||||
|
this.inputElement.value = value;
|
||||||
|
this.resizeTextarea();
|
||||||
}
|
}
|
||||||
|
|
||||||
static get styles() {
|
static get styles() {
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ describe('<lion-textarea>', () => {
|
||||||
expect(el.querySelector('textarea').nodeName).to.equal('TEXTAREA');
|
expect(el.querySelector('textarea').nodeName).to.equal('TEXTAREA');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('has a default minRows of 2 and maxRows of 10', async () => {
|
it('has default minRows and maxRows', async () => {
|
||||||
const el = await fixture(`<lion-textarea></lion-textarea>`);
|
const el = await fixture(`<lion-textarea></lion-textarea>`);
|
||||||
expect(el.rows).to.equal(2);
|
expect(el.rows).to.equal(2);
|
||||||
expect(el.maxRows).to.equal(6);
|
expect(el.maxRows).to.equal(6);
|
||||||
|
|
@ -31,13 +31,13 @@ describe('<lion-textarea>', () => {
|
||||||
const initialHeight = el.offsetHeight;
|
const initialHeight = el.offsetHeight;
|
||||||
el.modelValue = 'batman\nand\nrobin\nand\ncatwoman';
|
el.modelValue = 'batman\nand\nrobin\nand\ncatwoman';
|
||||||
await el.updateComplete;
|
await el.updateComplete;
|
||||||
const hightWith4TextLines = el.offsetHeight;
|
const hightWith5TextLines = el.offsetHeight;
|
||||||
expect(hightWith4TextLines > initialHeight).to.equal(true);
|
expect(hightWith5TextLines > initialHeight).to.equal(true);
|
||||||
|
|
||||||
el.modelValue = 'batman';
|
el.modelValue = 'batman';
|
||||||
await el.updateComplete;
|
await el.updateComplete;
|
||||||
const hightWith1Line = el.offsetHeight;
|
const hightWith1Line = el.offsetHeight;
|
||||||
expect(hightWith1Line < hightWith4TextLines).to.equal(true);
|
expect(hightWith1Line < hightWith5TextLines).to.equal(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it(`starts growing when content is bigger than "rows"
|
it(`starts growing when content is bigger than "rows"
|
||||||
|
|
@ -61,11 +61,37 @@ describe('<lion-textarea>', () => {
|
||||||
}, Promise.resolve(0));
|
}, Promise.resolve(0));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('stops growing after property "maxRows" is reached when there was an initial value', async () => {
|
||||||
|
const el = await fixture(
|
||||||
|
html`
|
||||||
|
<lion-textarea .modelValue="${'1\n2\n3'}"></lion-textarea>
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
return [4, 5, 6, 7, 8].reduce(async (heightPromise, i) => {
|
||||||
|
const oldHeight = await heightPromise;
|
||||||
|
el.modelValue += `\n`;
|
||||||
|
await el.updateComplete;
|
||||||
|
const newHeight = el.offsetHeight;
|
||||||
|
|
||||||
|
if (i > el.maxRows) {
|
||||||
|
// stop growing
|
||||||
|
expect(newHeight).to.equal(oldHeight);
|
||||||
|
} else if (i > el.rows) {
|
||||||
|
// growing normally
|
||||||
|
expect(newHeight >= oldHeight).to.equal(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve(newHeight);
|
||||||
|
}, Promise.resolve(0));
|
||||||
|
});
|
||||||
|
|
||||||
it('stops shrinking after property "rows" is reached', async () => {
|
it('stops shrinking after property "rows" is reached', async () => {
|
||||||
const el = await fixture(`<lion-textarea></lion-textarea>`);
|
const el = await fixture(
|
||||||
el.rows = 1;
|
html`
|
||||||
el.maxRows = 3;
|
<lion-textarea rows="1" max-rows="3"></lion-textarea>
|
||||||
await el.updateComplete;
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
expect(el.scrollHeight).to.be.equal(el.clientHeight);
|
expect(el.scrollHeight).to.be.equal(el.clientHeight);
|
||||||
const oneRowHeight = el.clientHeight;
|
const oneRowHeight = el.clientHeight;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue