Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix date time when selecting date in calendar to prevent timezone issues #1617

Merged
merged 13 commits into from
Sep 30, 2024
Merged
5 changes: 5 additions & 0 deletions .changeset/chilly-rockets-breathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sebgroup/green-core': patch
---

**Calendar:** Setting the time to 12.00 when selecting a date in the calendar
5 changes: 5 additions & 0 deletions .changeset/lazy-cheetahs-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sebgroup/green-core': patch
---

**Datepicker:** Set hours to 12 when setting date though spinners in the field. Fixes #1284
5 changes: 5 additions & 0 deletions .changeset/tall-hounds-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sebgroup/green-core': patch
---

**Calendar:** Only disable dates that fall fully before or after the min/max setting
22 changes: 16 additions & 6 deletions libs/core/src/components/calendar/calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
subMonths,
addMonths,
lastDayOfMonth,
setHours,
} from 'date-fns'

import { GdsElement } from '../../gds-element'
Expand Down Expand Up @@ -246,17 +247,23 @@ export class GdsCalendar extends GdsElement {
...customization,
}

const isOutsideCurrentMonth =
!isSameMonth(this.focusedDate, day) ||
day < this.min ||
day > this.max
const isOutsideCurrentMonth = !isSameMonth(
this.focusedDate,
day,
)

const isOutsideMinMax =
(day < this.min || day > this.max) &&
!isSameDay(day, this.min) &&
!isSameDay(day, this.max)

const isWeekend = day.getDay() === 0 || day.getDay() === 6

// Establish final disabled state
const isDisabled =
displayOptions.disabled ||
isOutsideCurrentMonth ||
isOutsideMinMax ||
(this.disabledWeekends && isWeekend)

const shouldRenderBlank =
Expand Down Expand Up @@ -316,11 +323,14 @@ export class GdsCalendar extends GdsElement {
}

#setSelectedDate(date: Date) {
this.value = date
// Set the time to midday to avoid timezone issues
const dateOnMidDay = setHours(date, 12)

this.value = dateOnMidDay

this.dispatchEvent(
new CustomEvent('change', {
detail: date,
detail: dateOnMidDay,
bubbles: false,
composed: false,
}),
Expand Down
1 change: 1 addition & 0 deletions libs/core/src/components/datepicker/datepicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ export class GdsDatepicker extends GdsFormControlElement<Date> {
newDate.setFullYear(parseInt(this.#spinnerState.year))
newDate.setMonth(parseInt(this.#spinnerState.month) - 1)
newDate.setDate(parseInt(this.#spinnerState.day))
newDate.setHours(12, 0, 0, 0)

if (newDate.toString() === 'Invalid Date') return

Expand Down
Loading