Skip to content

Commit

Permalink
fix(date-textbox): SSR issue & dates past year 9999 (#2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuLaValva committed Mar 13, 2024
1 parent 9c9771b commit 9732d1e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/components/ebay-calendar/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ class Calendar extends Marko.Component<Input, State> {
};
}

onMount() {
// recalculate on the browser in case firstDayOfWeek is not supported
const { firstDayOfWeek } = getWeekdayInfo(
localeOverride(this.input.locale),
);
this.state.firstDayOfWeek = firstDayOfWeek;
}

onInput(input: Input) {
if (input.selected) {
// If no selected times are visible, snap the view to the first one
Expand Down
3 changes: 2 additions & 1 deletion src/components/ebay-calendar/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export function getWeekdayInfo(localeName: string) {
export function dateArgToISO(arg: DateConstructor["arguments"]) {
if (!arg) return undefined;
if (/^\d\d\d\d-\d\d-\d\d$/g.test(arg)) return arg;
return toISO(new Date(arg));
const date = toISO(new Date(arg));
return /^\d\d\d\d-\d\d-\d\d$/g.test(date) ? date : undefined;
}

export function toISO(date: Date): DayISO {
Expand Down
4 changes: 2 additions & 2 deletions src/components/ebay-date-textbox/component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Expander from "makeup-expander";
import { toISO, type DayISO, dateArgToISO } from "../ebay-calendar/date-utils";
import { type DayISO, dateArgToISO } from "../ebay-calendar/date-utils";
import type { WithNormalizedProps } from "../../global";
import type { AttrString } from "marko/tags-html";

Expand Down Expand Up @@ -89,7 +89,7 @@ class DateTextbox extends Marko.Component<Input, State> {

handleInputChange(index: number, { value }: { value: string }) {
const valueDate = new Date(value);
const iso = isNaN(valueDate.getTime()) ? null : toISO(valueDate);
const iso = isNaN(valueDate.getTime()) ? null : dateArgToISO(valueDate);
if (index === 0) {
this.state.firstSelected = iso;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MenuButtonEvent } from "../component";
import type { MenuButtonEvent } from "../component";
class {
declare state: {
labelText?: string | null;
Expand Down

0 comments on commit 9732d1e

Please sign in to comment.