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: calendar state keys #52

Merged
merged 1 commit into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/calendar-v1/CalendarButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { callAllHandlers } from "@chakra-ui/utils";
import { createComponent, createHook } from "reakit-system";
import { ButtonHTMLProps, ButtonOptions, useButton } from "reakit";

import { CalendarState } from "./CalendarState";
import { CalendarStateReturn } from "./CalendarState";
import { CALENDAR_BUTTON_KEYS } from "./__keys";

export type TGoto = "nextMonth" | "previousMonth" | "nextYear" | "previousYear";

export type CalendarButtonOptions = ButtonOptions &
CalendarState & {
CalendarStateReturn & {
goto: TGoto;
getAriaLabel?: (goto: TGoto) => string;
};
Expand Down
4 changes: 2 additions & 2 deletions src/calendar-v1/CalendarCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { BoxHTMLProps, BoxOptions, useBox } from "reakit";
import { createComponent, createHook } from "reakit-system";

import { CALENDAR_CELL_KEYS } from "./__keys";
import { CalendarState } from "./CalendarState";
import { CalendarStateReturn } from "./CalendarState";
import { ariaAttr } from "@chakra-ui/utils";

export type CalendarCellOptions = BoxOptions &
CalendarState & {
CalendarStateReturn & {
weekIndex: number;
dayIndex: number;
};
Expand Down
4 changes: 2 additions & 2 deletions src/calendar-v1/CalendarCellButton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { useDateFormatter } from "@react-aria/i18n";
import { createComponent, createHook } from "reakit-system";
import { ButtonHTMLProps, ButtonOptions, useButton } from "reakit";

import { CalendarState } from "./CalendarState";
import { CalendarStateReturn } from "./CalendarState";
import { CALENDAR_CELL_BUTTON_KEYS } from "./__keys";

export type CalendarCellButtonOptions = ButtonOptions &
CalendarState & {
CalendarStateReturn & {
weekIndex: number;
dayIndex: number;
};
Expand Down
4 changes: 2 additions & 2 deletions src/calendar-v1/CalendarGrid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { createComponent, createHook } from "reakit-system";
import { ariaAttr, callAllHandlers } from "@chakra-ui/utils";

import { CALENDAR_GRID_KEYS } from "./__keys";
import { CalendarState } from "./CalendarState";
import { CalendarStateReturn } from "./CalendarState";

export type CalendarGridOptions = BoxOptions & CalendarState;
export type CalendarGridOptions = BoxOptions & CalendarStateReturn;

export type CalendarGridHTMLProps = BoxHTMLProps;

Expand Down
34 changes: 4 additions & 30 deletions src/calendar-v1/CalendarState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface CalendarProps {
onChange?: (value: DateValue) => void;
}

export function useCalendarState(props: CalendarProps = {}): CalendarState {
export function useCalendarState(props: CalendarProps = {}) {
const {
minValue: initialMinValue,
maxValue: initialMaxValue,
Expand Down Expand Up @@ -140,7 +140,7 @@ export function useCalendarState(props: CalendarProps = {}): CalendarState {
selectFocusedDate() {
setValue(focusedDate);
},
selectDate(date) {
selectDate(date: Date) {
setValue(date);
},
isDisabled,
Expand All @@ -149,7 +149,7 @@ export function useCalendarState(props: CalendarProps = {}): CalendarState {
setFocused,
weeksInMonth,
weekStart,
getCellOptions(weekIndex, dayIndex) {
getCellOptions(weekIndex: number, dayIndex: number) {
const day = weekIndex * 7 + dayIndex - monthStartsAt + 1;
const cellDate = new Date(year, month, day);
const isCurrentMonth = cellDate.getMonth() === month;
Expand All @@ -175,33 +175,7 @@ function isInvalid(date: Date, minDate: Date | null, maxDate: Date | null) {
);
}

export interface CalendarState {
dateValue: Date | null;
setDateValue(value: Date): void;
currentMonth: Date;
setCurrentMonth(value: Date): void;
focusedDate: Date;
setFocusedDate(value: Date): void;
focusNextDay(): void;
focusPreviousDay(): void;
focusNextWeek(): void;
focusPreviousWeek(): void;
focusNextMonth(): void;
focusPreviousMonth(): void;
focusStartOfMonth(): void;
focusEndOfMonth(): void;
focusNextYear(): void;
focusPreviousYear(): void;
selectFocusedDate(): void;
selectDate(date: Date): void;
isDisabled: boolean;
isFocused: boolean;
isReadOnly: boolean;
setFocused(value: boolean): void;
weeksInMonth: number;
weekStart: number;
getCellOptions(weekIndex: number, dayIndex: number): CalendarCellOptions;
}
export type CalendarStateReturn = ReturnType<typeof useCalendarState>;

export interface CalendarCellOptions {
cellDate: Date;
Expand Down
18 changes: 8 additions & 10 deletions src/calendar-v1/__keys.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Automatically generated
export const CALENDAR_STATE_KEYS = [
const CALENDAR_STATE_KEYS = [
"dateValue",
"setDateValue",
"currentMonth",
Expand All @@ -26,17 +26,15 @@ export const CALENDAR_STATE_KEYS = [
"weekStart",
"getCellOptions",
] as const;

export const CALENDAR_CELL_BUTTON_KEYS = [
export const CALENDAR_BUTTON_KEYS = [
...CALENDAR_STATE_KEYS,
"goto",
"getAriaLabel",
] as const;
export const CALENDAR_CELL_KEYS = [
...CALENDAR_STATE_KEYS,
"weekIndex",
"dayIndex",
] as const;

export const CALENDAR_CELL_KEYS = CALENDAR_CELL_BUTTON_KEYS;
export const CALENDAR_CELL_BUTTON_KEYS = CALENDAR_CELL_KEYS;
export const CALENDAR_GRID_KEYS = CALENDAR_STATE_KEYS;
export const CALENDAR_BUTTON_KEYS = [
...CALENDAR_STATE_KEYS,
"goto",
"getAriaLabel",
];