-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
61 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 5 additions & 18 deletions
23
...lient/packages/design-system/widgets/src/components/Calendar/src/CalendarYearDropdown.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
app/client/packages/design-system/widgets/src/components/Calendar/utils/calendar.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { useDateFormatter } from "@react-aria/i18n"; | ||
import type { CalendarState } from "@react-stately/calendar"; | ||
|
||
export function useYearOptions(state: CalendarState) { | ||
const formatter = useDateFormatter({ | ||
year: "numeric", | ||
timeZone: state.timeZone, | ||
}); | ||
|
||
const years: { value: CalendarState["focusedDate"]; formatted: string }[] = | ||
[]; | ||
|
||
const YEAR_RANGE = 20; | ||
|
||
for (let i = -YEAR_RANGE; i <= YEAR_RANGE; i++) { | ||
const date = state.focusedDate.add({ years: i }); | ||
|
||
years.push({ | ||
value: date, | ||
formatted: formatter.format(date.toDate(state.timeZone)), | ||
}); | ||
} | ||
|
||
return years; | ||
} | ||
|
||
export function useValidMonths( | ||
state: CalendarState, | ||
formatter: Intl.DateTimeFormat, | ||
) { | ||
const months = []; | ||
const numMonths = state.focusedDate.calendar.getMonthsInYear( | ||
state.focusedDate, | ||
); | ||
|
||
for (let i = 1; i <= numMonths; i++) { | ||
const date = state.focusedDate.set({ month: i }); | ||
|
||
// Skip months outside valid range | ||
if (state.minValue && date.compare(state.minValue) < 0) { | ||
continue; | ||
} | ||
|
||
if (state.maxValue && date.compare(state.maxValue) > 0) { | ||
continue; | ||
} | ||
|
||
months.push(formatter.format(date.toDate(state.timeZone))); | ||
} | ||
|
||
return months; | ||
} |