chore: refactor code a little to use slotmixin
This commit is contained in:
parent
627490aedc
commit
55efd3c64b
3 changed files with 63 additions and 33 deletions
5
.changeset/nervous-lions-trade.md
Normal file
5
.changeset/nervous-lions-trade.md
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@lion/input-amount': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Empty or invalid currency now removes the currency label node, this is restored when the currency is valid again.
|
||||||
|
|
@ -30,6 +30,20 @@ export class LionInputAmount extends LocalizeMixin(LionInput) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get slots() {
|
||||||
|
return {
|
||||||
|
...super.slots,
|
||||||
|
after: () => {
|
||||||
|
const el = document.createElement('span');
|
||||||
|
// The data-label attribute will make sure that FormControl adds this to
|
||||||
|
// input[aria-labelledby]
|
||||||
|
el.setAttribute('data-label', '');
|
||||||
|
el.textContent = this.__currencyLabel;
|
||||||
|
return el;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
static get styles() {
|
static get styles() {
|
||||||
return [
|
return [
|
||||||
...super.styles,
|
...super.styles,
|
||||||
|
|
@ -49,6 +63,7 @@ export class LionInputAmount extends LocalizeMixin(LionInput) {
|
||||||
this.currency = undefined;
|
this.currency = undefined;
|
||||||
/** @type {string | undefined} */
|
/** @type {string | undefined} */
|
||||||
this.locale = undefined;
|
this.locale = undefined;
|
||||||
|
this.__currencyDisplayNodeIsConnected = true;
|
||||||
this.defaultValidators.push(new IsNumber());
|
this.defaultValidators.push(new IsNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -80,6 +95,20 @@ export class LionInputAmount extends LocalizeMixin(LionInput) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upon connecting slot mixin, we should check if
|
||||||
|
* the after slot was created by the slot mixin,
|
||||||
|
* and if so, we should execute the currency changed flow
|
||||||
|
* which evaluates whether the slot node should be
|
||||||
|
* removed for invalid currencies
|
||||||
|
*/
|
||||||
|
_connectSlotMixin() {
|
||||||
|
super._connectSlotMixin();
|
||||||
|
if (this._isPrivateSlot('after')) {
|
||||||
|
this._onCurrencyChanged({ currency: this.currency || null });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} newLocale
|
* @param {string} newLocale
|
||||||
* @param {string} oldLocale
|
* @param {string} oldLocale
|
||||||
|
|
@ -108,31 +137,34 @@ export class LionInputAmount extends LocalizeMixin(LionInput) {
|
||||||
* @protected
|
* @protected
|
||||||
*/
|
*/
|
||||||
_onCurrencyChanged({ currency }) {
|
_onCurrencyChanged({ currency }) {
|
||||||
this.formatOptions.currency = currency || undefined;
|
|
||||||
if (this.currency) {
|
|
||||||
if (!this.__currencyDisplayNode) {
|
if (!this.__currencyDisplayNode) {
|
||||||
this.__currencyDisplayNode = this._createCurrencyDisplayNode();
|
return;
|
||||||
}
|
|
||||||
this.__currencyDisplayNode.textContent = this.__currencyLabel;
|
|
||||||
this._calculateValues({ source: null });
|
|
||||||
} else {
|
|
||||||
this.__currencyDisplayNode = undefined;
|
|
||||||
}
|
|
||||||
this.__setCurrencyDisplayLabel();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
this.formatOptions.currency = currency || undefined;
|
||||||
* @returns a newly created node for displaying the currency
|
if (currency) {
|
||||||
* @protected
|
if (!this.__currencyDisplayNodeIsConnected) {
|
||||||
*/
|
this.appendChild(this.__currencyDisplayNode);
|
||||||
_createCurrencyDisplayNode() {
|
this.__currencyDisplayNodeIsConnected = true;
|
||||||
const el = document.createElement('span');
|
}
|
||||||
// The data-label attribute will make sure that FormControl adds this to
|
this.__currencyDisplayNode.textContent = this.__currencyLabel;
|
||||||
// input[aria-labelledby]
|
|
||||||
el.setAttribute('data-label', '');
|
try {
|
||||||
el.textContent = this.__currencyLabel;
|
this._calculateValues({ source: null });
|
||||||
el.slot = 'after';
|
} catch (e) {
|
||||||
return el;
|
// In case Intl.NumberFormat gives error for invalid currency
|
||||||
|
// we should catch, remove the node, and rethrow (since it's still a user error)
|
||||||
|
if (e instanceof RangeError) {
|
||||||
|
this.__currencyDisplayNode?.remove();
|
||||||
|
this.__currencyDisplayNodeIsConnected = false;
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
this.__setCurrencyDisplayLabel();
|
||||||
|
} else {
|
||||||
|
this.__currencyDisplayNode?.remove();
|
||||||
|
this.__currencyDisplayNodeIsConnected = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -140,18 +172,12 @@ export class LionInputAmount extends LocalizeMixin(LionInput) {
|
||||||
* @private
|
* @private
|
||||||
*/
|
*/
|
||||||
get __currencyDisplayNode() {
|
get __currencyDisplayNode() {
|
||||||
return Array.from(this.children).find(child => child.slot === 'after');
|
const node = Array.from(this.children).find(child => child.slot === 'after');
|
||||||
|
if (node) {
|
||||||
|
this.__storedCurrencyDisplayNode = node;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
return node || this.__storedCurrencyDisplayNode;
|
||||||
* @private
|
|
||||||
*/
|
|
||||||
set __currencyDisplayNode(node) {
|
|
||||||
if (node) {
|
|
||||||
this.appendChild(node);
|
|
||||||
} else {
|
|
||||||
this.__currencyDisplayNode?.remove();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @private */
|
/** @private */
|
||||||
|
|
|
||||||
|
|
@ -134,7 +134,6 @@ describe('<lion-input-amount>', () => {
|
||||||
const el = /** @type {LionInputAmount} */ (
|
const el = /** @type {LionInputAmount} */ (
|
||||||
await fixture(`<lion-input-amount currency="EUR"></lion-input-amount>`)
|
await fixture(`<lion-input-amount currency="EUR"></lion-input-amount>`)
|
||||||
);
|
);
|
||||||
|
|
||||||
expect(
|
expect(
|
||||||
/** @type {HTMLElement[]} */ (Array.from(el.children)).find(child => child.slot === 'after')
|
/** @type {HTMLElement[]} */ (Array.from(el.children)).find(child => child.slot === 'after')
|
||||||
?.innerText,
|
?.innerText,
|
||||||
|
|
@ -230,7 +229,7 @@ describe('<lion-input-amount>', () => {
|
||||||
expect(currLabel?.getAttribute('aria-label')).to.equal('euros');
|
expect(currLabel?.getAttribute('aria-label')).to.equal('euros');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sets currency label on the after after element', async () => {
|
it('sets currency label on the after element', async () => {
|
||||||
const el = /** @type {LionInputAmount} */ (
|
const el = /** @type {LionInputAmount} */ (
|
||||||
await fixture(`
|
await fixture(`
|
||||||
<lion-input-amount>
|
<lion-input-amount>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue