From d3603c2ccc643fff35124d987d71fbfd1dcea8bf Mon Sep 17 00:00:00 2001 From: Bruno Prieto Date: Wed, 6 Mar 2024 00:47:52 -0300 Subject: [PATCH] Allow to select date or range dates programatically with value attribute --- docs/index.html | 6 +++++- .../inclusive-dates/inclusive-dates.tsx | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/docs/index.html b/docs/index.html index f147027..7cc5282 100644 --- a/docs/index.html +++ b/docs/index.html @@ -192,7 +192,6 @@

Demo

@@ -1415,6 +1414,11 @@

About me

valueInput.value = event.detail ? JSON.stringify(event.detail) : ""; }); + valueInput.addEventListener("change", (event) => { + datepicker.setAttribute("value", event.target.value); + updateDemoCode(); + }); + idInput.addEventListener("change", (event) => { datepicker.setAttribute("id", event.target.value); updateDemoCode(); diff --git a/src/components/inclusive-dates/inclusive-dates.tsx b/src/components/inclusive-dates/inclusive-dates.tsx index 061358d..c1ba936 100644 --- a/src/components/inclusive-dates/inclusive-dates.tsx +++ b/src/components/inclusive-dates/inclusive-dates.tsx @@ -490,9 +490,20 @@ export class InclusiveDates { } @Watch("value") - watchValue() { - if (Boolean(this.value) && !this.isRangeValue(this.value)) { - this.internalValue = this.value as string; + watchValue(newValue) { + if (this.range) { + const parsedValue = JSON.parse(newValue.replace(/'/g, '"')); + this.internalValue = parsedValue; + this.pickerRef.value = parsedValue.map((date) => + removeTimezoneOffset(new Date(date as string)) + ); + } else { + this.internalValue = newValue; + this.pickerRef.value = removeTimezoneOffset(new Date(newValue)); + } + this.errorState = false; + if (document.activeElement !== this.inputRef) { + this.formatInput(true, false); } }