From 8a4a58e8154716b802e2c5f14804f61144d02000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Schubert?= Date: Thu, 28 Oct 2021 12:05:47 +0200 Subject: [PATCH] fix(ld-select): form validation / event timing --- src/liquid/components/ld-select/ld-select.tsx | 66 ++++++----- src/liquid/components/ld-select/readme.md | 2 +- .../test/__snapshots__/ld-select.spec.ts.snap | 106 ++++++++++++++++-- 3 files changed, 135 insertions(+), 39 deletions(-) diff --git a/src/liquid/components/ld-select/ld-select.tsx b/src/liquid/components/ld-select/ld-select.tsx index e195f432be..31d93f9681 100644 --- a/src/liquid/components/ld-select/ld-select.tsx +++ b/src/liquid/components/ld-select/ld-select.tsx @@ -106,7 +106,10 @@ export class LdSelect { @State() renderHiddenInput = false @Watch('selected') - emitEvents(newSelection: SelectOption[], oldSelection: SelectOption[]) { + emitEventsAndUpdateHidden( + newSelection: SelectOption[], + oldSelection: SelectOption[] + ) { if (!this.initialized) return const newValues = newSelection.map((option) => option.value) @@ -115,6 +118,10 @@ export class LdSelect { this.updateTriggerMoreIndicator(true) + if (this.renderHiddenInput) { + this.updateHiddenInput(newSelection) + } + this.input.emit(newValues) this.change.emit(newValues) } @@ -356,31 +363,25 @@ export class LdSelect { ) } - setTimeout(() => { - if (!initialized) { - this.internalOptionsHTML = this.el.innerHTML['replaceAll']( - /(.|\n|\r)*<\/ld-icon>/g, + '' ) - .replaceAll(/<\/ld-option/g, '(.|\n|\r)*<\/ld-icon>/g, - '' - ) - } - this.selected = selectedChildren.map((child) => ({ - value: child.getAttribute('value'), - text: child.innerText, - })) - - if (this.renderHiddenInput) { - this.updateHiddenInput(this.selected) - } + } + this.selected = selectedChildren.map((child) => ({ + value: child.getAttribute('value'), + text: child.innerText, + })) - this.updateTriggerMoreIndicator(true) - }) + this.updateTriggerMoreIndicator(true) } private updateHiddenInput = (selected: SelectOption[]) => { @@ -392,6 +393,7 @@ export class LdSelect { if (index >= 0) { selectedValues.splice(index, 1) } else { + console.log('here') hiddenInput.remove() } }) @@ -425,11 +427,9 @@ export class LdSelect { const oldValues = [...this.selected] this.initOptions() - setTimeout(() => { - this.initialized = true - const newValues = [...this.selected] - this.emitEvents(newValues, oldValues) - }) + this.initialized = true + const newValues = [...this.selected] + this.emitEventsAndUpdateHidden(newValues, oldValues) } private initObserver() { @@ -837,11 +837,15 @@ export class LdSelect { if (this.el.getAttribute('aria-disabled') === 'true') { this.ariaDisabled = true } - } - componentDidLoad() { this.initOptions() + if (this.renderHiddenInput) { + this.updateHiddenInput(this.selected) + } + } + + componentDidLoad() { setTimeout(() => { this.initObserver() this.initialized = true diff --git a/src/liquid/components/ld-select/readme.md b/src/liquid/components/ld-select/readme.md index 7ca72278ec..e661dbbac9 100644 --- a/src/liquid/components/ld-select/readme.md +++ b/src/liquid/components/ld-select/readme.md @@ -1191,7 +1191,7 @@ The `ld-select` Web Component provides a low level API for integrating it with t const submitButton = document.querySelector('#example-form ld-button') let selectDirty = false function validateInput() { - if (selectDirty && (!form.fruits. || form.fruits.length < 3)) { + if (selectDirty && (!form.fruits || !form.fruits.length || form.fruits.length < 3)) { select.setAttribute('invalid', 'true') errorMessage.style.visibility = 'inherit' return false diff --git a/src/liquid/components/ld-select/test/__snapshots__/ld-select.spec.ts.snap b/src/liquid/components/ld-select/test/__snapshots__/ld-select.spec.ts.snap index 995945e68a..e96e6e3992 100644 --- a/src/liquid/components/ld-select/test/__snapshots__/ld-select.spec.ts.snap +++ b/src/liquid/components/ld-select/test/__snapshots__/ld-select.spec.ts.snap @@ -30,7 +30,34 @@ exports[`ld-select creates hidden input field, if inside a form 1`] = ` -
+
+ + +
+ + + + + + +
+
+ Apple +
+ + +
+ + + + + + +
+
+ Banana +
+
@@ -60,11 +87,49 @@ exports[`ld-select fills hidden input fields with initially selected option valu
-
+
+ + +
+ + + + +
+
+ Apple +
+ + +
+ + + + +
+
+ Banana +
+