Skip to content

Commit 5752f6a

Browse files
fix(input-amount-dropdown): update _isEmpty method to only check amount property for emptiness
fix(input-amount-dropdown): update _isEmpty method to only check amount property for emptiness Co-authored-by: Radu Adascalitei <adascalitei.radu@live.com>
1 parent 89cb70d commit 5752f6a

4 files changed

Lines changed: 55 additions & 5 deletions

File tree

.changeset/neat-mangos-like.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@lion/ui': patch
3+
---
4+
5+
fix: [input-amount-dropdown] lion input amount dropdown not working with validators due to \_isEmpty implementation

packages/ui/components/input-amount-dropdown/src/LionInputAmountDropdown.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,16 @@ export class LionInputAmountDropdown extends LionInputAmount {
398398

399399
/**
400400
* Used for Required validation and computation of interaction states.
401-
* We need to override this, because we prefill the input with the currency code, but for proper UX,
402-
* we don't consider this as having interaction state `prefilled`
403-
* @param {string} modelValue
401+
* We only consider the amount part for emptiness checks.
402+
* Missing, null, empty-string or NaN amount is considered empty.
404403
* @return {boolean}
405404
* @protected
406405
*/
407406
_isEmpty(modelValue = this.modelValue) {
408-
return super._isEmpty(modelValue) || this.currency === this.__initializedCurrencyCode;
407+
const isNumberValue =
408+
typeof modelValue?.amount === 'number' &&
409+
(modelValue?.amount === 0 || Number.isNaN(modelValue?.amount));
410+
return !modelValue?.amount && !isNumberValue;
409411
}
410412

411413
/**

packages/ui/components/input-amount-dropdown/test-suites/LionInputAmountDropdown.suite.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,29 @@ export function runInputAmountDropdownSuite({ klass } = { klass: LionInputAmount
317317
// @ts-ignore
318318
expect(el._isEmpty(), 'empty').to.be.true;
319319
});
320+
321+
it('only checks modelValue.amount', async () => {
322+
const el = await fixture(html` <${tag}></${tag}> `);
323+
324+
el.modelValue = { currency: 'GBP', amount: 1 };
325+
await el.updateComplete;
326+
// @ts-ignore
327+
expect(el._isEmpty(), 'filled amount').to.be.false;
328+
329+
el.modelValue = { currency: 'EUR', amount: '' };
330+
await el.updateComplete;
331+
// @ts-ignore
332+
expect(el._isEmpty(), 'empty amount').to.be.true;
333+
334+
el.modelValue = { currency: 'EUR' };
335+
await el.updateComplete;
336+
// @ts-ignore
337+
expect(el._isEmpty(), 'missing amount property').to.be.true;
338+
339+
el.modelValue = null;
340+
await el.updateComplete;
341+
// @ts-ignore
342+
expect(el._isEmpty(), 'null modelValue').to.be.true;
343+
});
320344
});
321345
}

packages/ui/components/input-amount-dropdown/test/LionInputAmountDropdown.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { mimicUserChangingDropdown } from '@lion/ui/input-amount-dropdown-test-helpers.js';
22
import { runInputAmountDropdownSuite } from '@lion/ui/input-amount-dropdown-test-suites.js';
33
import { LionInputAmountDropdown } from '@lion/ui/input-amount-dropdown.js';
4+
import { Required } from '@lion/ui/form-core.js';
45
import { aTimeout, expect, fixture } from '@open-wc/testing';
56
import { LionSelectRich } from '@lion/ui/select-rich.js';
67
import { repeat } from 'lit/directives/repeat.js';
@@ -194,7 +195,25 @@ describe('WithFormControlInputAmountDropdown', () => {
194195
el.modelValue = { currency: 'EUR' };
195196
await aTimeout(0);
196197
el.modelValue = { currency: 'EUR', amount: '' };
197-
expect(el.hasFeedbackFor).to.deep.equal(['error']);
198+
expect(el.hasFeedbackFor).to.deep.equal([]);
198199
});
199200
});
201+
202+
it('includes error in showsFeedbackFor when required, empty, and touched & dirty', async () => {
203+
const el = /** @type {LionInputAmountDropdown} */ (
204+
await fixture(html`
205+
<lion-input-amount-dropdown
206+
.allowedCurrencies="${['GBP', 'EUR']}"
207+
.validators="${[new Required()]}"
208+
></lion-input-amount-dropdown>
209+
`)
210+
);
211+
await el.updateComplete;
212+
el.touched = true;
213+
el.dirty = true;
214+
await el.updateComplete;
215+
await el.feedbackComplete;
216+
expect(el.hasFeedbackFor).to.include('error', 'hasFeedbackFor');
217+
expect(el.showsFeedbackFor).to.include('error', 'showsFeedbackFor');
218+
});
200219
});

0 commit comments

Comments
 (0)