From 99d894efb3926b4c7894854d0bc35231c2a2685b Mon Sep 17 00:00:00 2001 From: Felix Mueller Date: Thu, 29 Jul 2021 07:26:05 +0200 Subject: [PATCH] Make Currency Display Node Private --- packages/input-amount/src/LionInputAmount.js | 50 +++++++++---------- .../test/lion-input-amount.test.js | 14 ++++-- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/packages/input-amount/src/LionInputAmount.js b/packages/input-amount/src/LionInputAmount.js index ebe130de8..a2a62efca 100644 --- a/packages/input-amount/src/LionInputAmount.js +++ b/packages/input-amount/src/LionInputAmount.js @@ -110,36 +110,17 @@ export class LionInputAmount extends LocalizeMixin(LionInput) { _onCurrencyChanged({ currency }) { this.formatOptions.currency = currency || undefined; if (this.currency) { - if (!this._currencyDisplayNode) { - this._currencyDisplayNode = this._createCurrencyDisplayNode(); + if (!this.__currencyDisplayNode) { + this.__currencyDisplayNode = this._createCurrencyDisplayNode(); } - this._currencyDisplayNode.textContent = this.__currencyLabel; + this.__currencyDisplayNode.textContent = this.__currencyLabel; this._calculateValues({ source: null }); } else { - this._currencyDisplayNode = undefined; + this.__currencyDisplayNode = undefined; } this.__setCurrencyDisplayLabel(); } - /** - * @returns the current currency display node - * @protected - */ - get _currencyDisplayNode() { - return Array.from(this.children).find(child => child.slot === 'after'); - } - - /** - * @protected - */ - set _currencyDisplayNode(node) { - if (node) { - this.appendChild(node); - } else { - this._currencyDisplayNode?.remove(); - } - } - /** * @returns a newly created node for displaying the currency * @protected @@ -154,13 +135,32 @@ export class LionInputAmount extends LocalizeMixin(LionInput) { return el; } + /** + * @returns the current currency display node + * @private + */ + get __currencyDisplayNode() { + return Array.from(this.children).find(child => child.slot === 'after'); + } + + /** + * @private + */ + set __currencyDisplayNode(node) { + if (node) { + this.appendChild(node); + } else { + this.__currencyDisplayNode?.remove(); + } + } + /** @private */ __setCurrencyDisplayLabel() { // TODO: (@erikkroes) for optimal a11y, abbreviations should be part of aria-label // example, for a language switch with text 'en', an aria-label of 'english' is not // sufficient, it should also contain the abbreviation. - if (this._currencyDisplayNode) { - this._currencyDisplayNode.setAttribute( + if (this.__currencyDisplayNode) { + this.__currencyDisplayNode.setAttribute( 'aria-label', this.currency ? getCurrencyName(this.currency, {}) : '', ); diff --git a/packages/input-amount/test/lion-input-amount.test.js b/packages/input-amount/test/lion-input-amount.test.js index ebfd3c7b3..27cae4065 100644 --- a/packages/input-amount/test/lion-input-amount.test.js +++ b/packages/input-amount/test/lion-input-amount.test.js @@ -256,19 +256,25 @@ describe('', () => { const el = /** @type {LionInputAmount} */ ( await fixture(``) ); - expect(el._currencyDisplayNode?.getAttribute('data-label')).to.be.not.null; + const label = /** @type {HTMLElement[]} */ (Array.from(el.children)).find( + child => child.slot === 'after', + ); + expect(label?.getAttribute('data-label')).to.be.not.null; const { _inputNode } = getInputMembers(/** @type {* & LionInput} */ (el)); - expect(_inputNode.getAttribute('aria-labelledby')).to.contain(el._currencyDisplayNode?.id); + expect(_inputNode.getAttribute('aria-labelledby')).to.contain(label?.id); }); it('adds an aria-label to currency slot', async () => { const el = /** @type {LionInputAmount} */ ( await fixture(``) ); - expect(el._currencyDisplayNode?.getAttribute('aria-label')).to.equal('euros'); + const label = /** @type {HTMLElement[]} */ (Array.from(el.children)).find( + child => child.slot === 'after', + ); + expect(label?.getAttribute('aria-label')).to.equal('euros'); el.currency = 'USD'; await el.updateComplete; - expect(el._currencyDisplayNode?.getAttribute('aria-label')).to.equal('US dollars'); + expect(label?.getAttribute('aria-label')).to.equal('US dollars'); el.currency = 'PHP'; await el.updateComplete; // TODO: Chrome Intl now thinks this should be pesos instead of pisos. They're probably right.