fix(textarea): delegate readonly to the native element

Co-authored-by: Nithya Lakshmi <nithyalakshmicse97@gmail.com>
This commit is contained in:
Joren Broekema 2019-10-25 10:52:26 +02:00
parent 88e75a271f
commit d78f60054b
2 changed files with 22 additions and 1 deletions

View file

@ -19,6 +19,11 @@ export class LionTextarea extends LionField {
type: Number, type: Number,
reflect: true, reflect: true,
}, },
readOnly: {
type: Boolean,
attribute: 'readonly',
reflect: true,
},
}; };
} }
@ -42,6 +47,7 @@ export class LionTextarea extends LionField {
super(); super();
this.rows = 2; this.rows = 2;
this.maxRows = 6; this.maxRows = 6;
this.readOnly = false;
} }
connectedCallback() { connectedCallback() {
@ -63,6 +69,13 @@ export class LionTextarea extends LionField {
} }
} }
if (changedProperties.has('readOnly')) {
const native = this.inputElement;
if (native) {
native.readOnly = this.readOnly;
}
}
if (changedProperties.has('modelValue')) { if (changedProperties.has('modelValue')) {
this.resizeTextarea(); this.resizeTextarea();
} }

View file

@ -22,12 +22,14 @@ describe('<lion-textarea>', () => {
expect(el.maxRows).to.equal(6); expect(el.maxRows).to.equal(6);
}); });
it('has .rows=2 and rows="2" by default', async () => { it('has .readOnly=false .rows=2 and rows="2" by default', async () => {
const el = await fixture(`<lion-textarea>foo</lion-textarea>`); const el = await fixture(`<lion-textarea>foo</lion-textarea>`);
expect(el.rows).to.equal(2); expect(el.rows).to.equal(2);
expect(el.getAttribute('rows')).to.be.equal('2'); expect(el.getAttribute('rows')).to.be.equal('2');
expect(el.inputElement.rows).to.equal(2); expect(el.inputElement.rows).to.equal(2);
expect(el.inputElement.getAttribute('rows')).to.be.equal('2'); expect(el.inputElement.getAttribute('rows')).to.be.equal('2');
expect(el.readOnly).to.be.false;
expect(el.inputElement.hasAttribute('readonly')).to.be.false;
}); });
it('sync rows down to the native textarea', async () => { it('sync rows down to the native textarea', async () => {
@ -38,6 +40,12 @@ describe('<lion-textarea>', () => {
expect(el.inputElement.getAttribute('rows')).to.be.equal('8'); expect(el.inputElement.getAttribute('rows')).to.be.equal('8');
}); });
it('sync readOnly to the native textarea', async () => {
const el = await fixture(`<lion-textarea readonly>foo</lion-textarea>`);
expect(el.readOnly).to.be.true;
expect(el.querySelector('textarea').readOnly).to.be.true;
});
it('disables user resize behavior', async () => { it('disables user resize behavior', async () => {
if (!hasBrowserResizeSupport()) { if (!hasBrowserResizeSupport()) {
return; return;