fix(select-rich): clicking on the label should focus the invoker button

This commit is contained in:
Thomas Allmer 2020-08-03 14:25:04 +02:00
parent d94a83a92d
commit 7742e2eaf4
4 changed files with 43 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/select-rich': patch
---
Clicking on a label of a rich select should focus the invoker button. This behavior is to mimic the native select.

View file

@ -2,7 +2,11 @@
module.exports = { module.exports = {
'*': ['eclint fix', 'git add'], '*': ['eclint fix', 'git add'],
'*.js': ['eslint --fix', 'prettier --write', 'git add'], '*.js': ['eslint --fix', 'prettier --write', 'git add'],
'{*!(.changeset)/**/*.md,*.md}': ['prettier --write', 'markdownlint', 'git add'], '*.md': [
'prettier --write',
"markdownlint --ignore '{.changeset/*.md,**/CHANGELOG.md}'",
'git add',
],
'yarn.lock': ['node ./scripts/yarn-lock-scan.js'], 'yarn.lock': ['node ./scripts/yarn-lock-scan.js'],
'*package.json': absolutePaths => { '*package.json': absolutePaths => {
const sortPackages = []; const sortPackages = [];

View file

@ -190,6 +190,8 @@ export class LionSelectRich extends ScopedElementsMixin(
this.__hasInitialSelectedFormElement = false; this.__hasInitialSelectedFormElement = false;
this.hasNoDefaultSelected = false; this.hasNoDefaultSelected = false;
this._repropagationRole = 'choice-group'; // configures FormControlMixin this._repropagationRole = 'choice-group'; // configures FormControlMixin
this.__focusInvokerOnLabelClick = this.__focusInvokerOnLabelClick.bind(this);
} }
connectedCallback() { connectedCallback() {
@ -204,6 +206,10 @@ export class LionSelectRich extends ScopedElementsMixin(
this.__toggleInvokerDisabled(); this.__toggleInvokerDisabled();
if (this._labelNode) {
this._labelNode.addEventListener('click', this.__focusInvokerOnLabelClick);
}
this.registrationComplete.then(() => { this.registrationComplete.then(() => {
this.__initInteractionStates(); this.__initInteractionStates();
}); });
@ -213,6 +219,15 @@ export class LionSelectRich extends ScopedElementsMixin(
}); });
} }
disconnectedCallback() {
if (super.disconnectedCallback) {
super.disconnectedCallback();
}
if (this._labelNode) {
this._labelNode.removeEventListener('click', this.__toggleChecked);
}
}
_requestUpdate(name, oldValue) { _requestUpdate(name, oldValue) {
super._requestUpdate(name, oldValue); super._requestUpdate(name, oldValue);
if (name === 'interactionMode') { if (name === 'interactionMode') {
@ -694,6 +709,10 @@ export class LionSelectRich extends ScopedElementsMixin(
} }
} }
__focusInvokerOnLabelClick() {
this._invokerNode.focus();
}
/** /**
* @override Configures OverlayMixin * @override Configures OverlayMixin
*/ */

View file

@ -109,13 +109,24 @@ describe('lion-select-rich', () => {
expect(el1.fieldName).to.equal(el1._labelNode.textContent); expect(el1.fieldName).to.equal(el1._labelNode.textContent);
const el2 = await fixture(html` const el2 = await fixture(html`
<lion-select-rich <lion-select-rich>
><label slot="label">bar</label><lion-options slot="input"></lion-options <label slot="label">bar</label><lion-options slot="input"></lion-options>
></lion-select-rich> </lion-select-rich>
`); `);
expect(el2.fieldName).to.equal(el2._labelNode.textContent); expect(el2.fieldName).to.equal(el2._labelNode.textContent);
}); });
it('clicking the label should focus the invoker', async () => {
const el = await fixture(html`
<lion-select-rich label="foo">
<lion-options slot="input"></lion-options>
</lion-select-rich>
`);
expect(document.activeElement === document.body).to.be.true;
el._labelNode.click();
expect(document.activeElement === el._invokerNode).to.be.true;
});
it(`has a fieldName based on the name if no label exists`, async () => { it(`has a fieldName based on the name if no label exists`, async () => {
const el = await fixture(html` const el = await fixture(html`
<lion-select-rich name="foo"><lion-options slot="input"></lion-options></lion-select-rich> <lion-select-rich name="foo"><lion-options slot="input"></lion-options></lion-select-rich>