Skip to content

Commit

Permalink
Allow to select date or range dates programatically with value attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoprietog committed Mar 6, 2024
1 parent 2e5076b commit d3603c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
6 changes: 5 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ <h2 id="demo" tabindex="-1">Demo</h2>
<td>
<input
aria-labelledby="value-label"
disabled
id="value"
type="text"
/>
Expand Down Expand Up @@ -1415,6 +1414,11 @@ <h2>About me</h2>
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();
Expand Down
17 changes: 14 additions & 3 deletions src/components/inclusive-dates/inclusive-dates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit d3603c2

Please sign in to comment.