Skip to content

Commit 7ac9d88

Browse files
daKmoRJoren Broekema
authored andcommitted
fix(textarea): sync rows property instead of delegating
1 parent b3a1f91 commit 7ac9d88

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

packages/textarea/src/LionTextarea.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,24 @@
11
import autosize from 'autosize/src/autosize.js';
22
import { LionInput } from '@lion/input';
33
import { css } from '@lion/core';
4-
import { ObserverMixin } from '@lion/core/src/ObserverMixin.js';
54

65
/**
76
* LionTextarea: extension of lion-field with native input element in place and user friendly API
87
*
98
* @customElement
109
* @extends LionInput
1110
*/
12-
export class LionTextarea extends ObserverMixin(LionInput) {
11+
export class LionTextarea extends LionInput {
1312
static get properties() {
1413
return {
1514
maxRows: {
1615
type: Number,
1716
attribute: 'max-rows',
1817
},
19-
};
20-
}
21-
22-
get delegations() {
23-
return {
24-
...super.delegations,
25-
target: () => this.inputElement,
26-
properties: [...super.delegations.properties, 'rows'],
27-
attributes: [...super.delegations.attributes, 'rows'],
28-
};
29-
}
30-
31-
static get asyncObservers() {
32-
return {
33-
...super.asyncObservers,
34-
resizeTextarea: ['maxRows', 'modelValue'],
35-
setTextareaMaxHeight: ['maxRows', 'rows'],
18+
rows: {
19+
type: Number,
20+
reflect: true,
21+
},
3622
};
3723
}
3824

@@ -68,6 +54,24 @@ export class LionTextarea extends ObserverMixin(LionInput) {
6854
autosize.destroy(this.inputElement);
6955
}
7056

57+
updated(changedProperties) {
58+
super.updated(changedProperties);
59+
if (changedProperties.has('rows')) {
60+
const native = this.inputElement;
61+
if (native) {
62+
native.rows = this.rows;
63+
}
64+
}
65+
66+
if (changedProperties.has('modelValue')) {
67+
this.resizeTextarea();
68+
}
69+
70+
if (changedProperties.has('maxRows') || changedProperties.has('rows')) {
71+
this.setTextareaMaxHeight();
72+
}
73+
}
74+
7175
/**
7276
* To support maxRows we need to set max-height of the textarea
7377
*/

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,28 @@ describe('<lion-textarea>', () => {
1616
expect(el.querySelector('textarea').nodeName).to.equal('TEXTAREA');
1717
});
1818

19-
it('has default minRows and maxRows', async () => {
19+
it('has .rows=2 and .maxRows=6', async () => {
2020
const el = await fixture(`<lion-textarea></lion-textarea>`);
2121
expect(el.rows).to.equal(2);
2222
expect(el.maxRows).to.equal(6);
2323
});
2424

25+
it('has .rows=2 and rows="2" by default', async () => {
26+
const el = await fixture(`<lion-textarea>foo</lion-textarea>`);
27+
expect(el.rows).to.equal(2);
28+
expect(el.getAttribute('rows')).to.be.equal('2');
29+
expect(el.inputElement.rows).to.equal(2);
30+
expect(el.inputElement.getAttribute('rows')).to.be.equal('2');
31+
});
32+
33+
it('sync rows down to the native textarea', async () => {
34+
const el = await fixture(`<lion-textarea rows="8">foo</lion-textarea>`);
35+
expect(el.rows).to.equal(8);
36+
expect(el.getAttribute('rows')).to.be.equal('8');
37+
expect(el.inputElement.rows).to.equal(8);
38+
expect(el.inputElement.getAttribute('rows')).to.be.equal('8');
39+
});
40+
2541
it('disables user resize behavior', async () => {
2642
if (!hasBrowserResizeSupport()) {
2743
return;
@@ -102,12 +118,9 @@ describe('<lion-textarea>', () => {
102118
});
103119

104120
it('stops shrinking after property "rows" is reached', async () => {
105-
const el = await fixture(
106-
html`
107-
<lion-textarea rows="1" max-rows="3"></lion-textarea>
108-
`,
109-
);
110-
121+
const el = await fixture(html`
122+
<lion-textarea rows="1" max-rows="3"></lion-textarea>
123+
`);
111124
expect(el.scrollHeight).to.be.equal(el.clientHeight);
112125
const oneRowHeight = el.clientHeight;
113126

0 commit comments

Comments
 (0)