From 1298b7b0500d305074bdc075b6eaefd21226d4fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20Garc=C3=ADa?= Date: Sun, 13 Oct 2019 17:05:33 +0200 Subject: [PATCH] fix(select-rich): sync selected el after add item on selected index --- packages/select-rich/src/LionSelectRich.js | 4 +- .../select-rich/test/lion-select-rich.test.js | 85 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) diff --git a/packages/select-rich/src/LionSelectRich.js b/packages/select-rich/src/LionSelectRich.js index e79f5cc36..bb211223f 100644 --- a/packages/select-rich/src/LionSelectRich.js +++ b/packages/select-rich/src/LionSelectRich.js @@ -360,8 +360,10 @@ export class LionSelectRich extends OverlayMixin( const foundChecked = this.modelValue.find(subModelValue => subModelValue.checked); if (foundChecked && foundChecked.value !== this.checkedValue) { this.checkedValue = foundChecked.value; + } - // sync to invoker + // sync to invoker + if (this._invokerNode) { this._invokerNode.selectedElement = this.formElements[this.checkedIndex]; } diff --git a/packages/select-rich/test/lion-select-rich.test.js b/packages/select-rich/test/lion-select-rich.test.js index ec1869cdb..6d770d438 100644 --- a/packages/select-rich/test/lion-select-rich.test.js +++ b/packages/select-rich/test/lion-select-rich.test.js @@ -7,6 +7,7 @@ import { unsafeStatic, nextFrame, } from '@open-wc/testing'; +import { LitElement } from '@lion/core'; import '@lion/option/lion-option.js'; import { OverlayController } from '@lion/overlays'; @@ -435,6 +436,90 @@ describe('lion-select-rich', () => { active: false, }); }); + + it('keeps showing the selected item after a new item has been added in the selectedIndex position', async () => { + const mySelectContainerTagString = defineCE( + class extends LitElement { + static get properties() { + return { + colorList: Array, + }; + } + + constructor() { + super(); + this.colorList = []; + } + + render() { + return html` + + + ${this.colorList.map( + colorObj => html` + ${colorObj.label} + `, + )} + + + `; + } + }, + ); + const mySelectContainerTag = unsafeStatic(mySelectContainerTagString); + const el = await fixture(html` + <${mySelectContainerTag}> + `); + + const colorList = [ + { + label: 'Red', + value: 'red', + checked: false, + }, + { + label: 'Hotpink', + value: 'hotpink', + checked: true, + }, + { + label: 'Teal', + value: 'teal', + checked: false, + }, + ]; + + el.colorList = colorList; + el.requestUpdate(); + await el.updateComplete; + + const selectRich = el.shadowRoot.querySelector('lion-select-rich'); + const invoker = selectRich._invokerNode; + + // needed to properly set the checkedIndex and checkedValue + selectRich.requestUpdate(); + await selectRich.updateComplete; + + expect(selectRich.checkedIndex).to.equal(1); + expect(selectRich.checkedValue).to.equal('hotpink'); + expect(invoker.selectedElement.value).to.equal('hotpink'); + + colorList.splice(1, 0, { + label: 'Blue', + value: 'blue', + checked: false, + }); + + el.requestUpdate(); + await el.updateComplete; + + expect(selectRich.checkedIndex).to.equal(2); + expect(selectRich.checkedValue).to.equal('hotpink'); + expect(invoker.selectedElement.value).to.equal('hotpink'); + }); }); describe('Subclassers', () => {