Skip to content

Commit

Permalink
refactor: removed weekIndex dayIndex props from cell
Browse files Browse the repository at this point in the history
  • Loading branch information
anuraghazra committed Sep 22, 2020
1 parent 10d28d0 commit 2df03fa
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 44 deletions.
12 changes: 3 additions & 9 deletions src/calendar-v1/CalendarCell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ import { CalendarStateReturn, getCellOptionsReturn } from "./CalendarState";
export type CalendarCellOptions = BoxOptions &
Pick<CalendarStateReturn, "dateValue" | "isDisabled"> &
Partial<Pick<RangeCalendarStateReturn, "highlightDate">> & {
weekIndex: number;
dayIndex: number;
date: Date;
getCellOptions: (
weekIndex: number,
dayIndex: number,
date: Date,
) => Partial<getRangeCellOptionsReturn & getCellOptionsReturn>;
};

Expand All @@ -39,11 +36,8 @@ export const useCalendarCell = createHook<
compose: useBox,
keys: CALENDAR_CELL_KEYS,

useProps(
{ date, getCellOptions, weekIndex, dayIndex, highlightDate, isDisabled },
htmlProps,
) {
const cellProps = getCellOptions?.(weekIndex, dayIndex);
useProps({ date, getCellOptions, highlightDate, isDisabled }, htmlProps) {
const cellProps = getCellOptions?.(date);

const onMouseEnter = () => {
highlightDate && highlightDate(date);
Expand Down
13 changes: 4 additions & 9 deletions src/calendar-v1/CalendarRangeState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,8 @@ export function useRangeCalendarState(props: RangeCalendarProps) {
calendar.setFocusedDate(date);
}
},
getCellOptions(
weekIndex: number,
dayIndex: number,
): getRangeCellOptionsReturn {
const opts = calendar.getCellOptions(weekIndex, dayIndex);
getCellOptions(cellDate: Date): getRangeCellOptionsReturn {
const opts = calendar.getCellOptions(cellDate);
const isSelected =
highlightedRange &&
opts.cellDate >= highlightedRange.start &&
Expand All @@ -101,12 +98,10 @@ export function useRangeCalendarState(props: RangeCalendarProps) {
...opts,
isRangeSelection: isSelected,
isSelected,
isRangeStart:
isSelected && (dayIndex === 0 || opts.cellDate.getDate() === 1),
isRangeStart: isSelected && opts.cellDate.getDate() === 1,
isRangeEnd:
isSelected &&
(dayIndex === 6 ||
opts.cellDate.getDate() === getDaysInMonth(calendar.currentMonth)),
opts.cellDate.getDate() === getDaysInMonth(calendar.currentMonth),
isSelectionStart:
highlightedRange && isSameDay(opts.cellDate, highlightedRange.start),
isSelectionEnd:
Expand Down
4 changes: 1 addition & 3 deletions src/calendar-v1/CalendarState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,7 @@ export function useCalendarState(props: IUseCalendarProps = {}) {
selectDate(date: Date) {
setValue(date);
},
getCellOptions(weekIndex: number, dayIndex: number): getCellOptionsReturn {
const day = weekIndex * 7 + dayIndex - monthStartsAt + 1;
const cellDate = new Date(year, month, day);
getCellOptions(cellDate: Date): getCellOptionsReturn {
const isCurrentMonth = cellDate.getMonth() === month;

return {
Expand Down
9 changes: 2 additions & 7 deletions src/calendar-v1/__keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,8 @@ export const CALENDAR_BUTTON_KEYS = [
"goto",
"getAriaLabel",
] as const;
export const CALENDAR_CELL_KEYS = [
...CALENDAR_KEYS,
"weekIndex",
"dayIndex",
"date",
] as const;
export const CALENDAR_CELL_BUTTON_KEYS = [...CALENDAR_KEYS, "date"] as const;
export const CALENDAR_CELL_KEYS = [...CALENDAR_KEYS, "date"] as const;
export const CALENDAR_CELL_BUTTON_KEYS = CALENDAR_CELL_KEYS;
export const CALENDAR_GRID_KEYS = CALENDAR_KEYS;
export const CALENDAR_HEADER_KEYS = [...CALENDAR_GRID_KEYS, "format"] as const;
export const CALENDAR_WEEK_TITLE_KEYS = [
Expand Down
9 changes: 1 addition & 8 deletions src/calendar-v1/stories/Calendar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,7 @@ const CalendarComp: React.FC<IUseCalendarProps> = props => {
{state.daysInMonth.map((week, weekIndex) => (
<tr key={weekIndex}>
{week.map((day, dayIndex) => (
<CalendarCell
{...state}
as="td"
key={dayIndex}
date={day}
dayIndex={dayIndex}
weekIndex={weekIndex}
>
<CalendarCell {...state} as="td" key={dayIndex} date={day}>
<CalendarCellButton {...state} date={day} />
</CalendarCell>
))}
Expand Down
9 changes: 1 addition & 8 deletions src/calendar-v1/stories/RangeCalendar.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,7 @@ const RangeCalendarComp: React.FC<RangeCalendarProps> = props => {
{state.daysInMonth.map((week, weekIndex) => (
<tr key={weekIndex}>
{week.map((day, dayIndex) => (
<CalendarCell
dayIndex={dayIndex}
weekIndex={weekIndex}
{...state}
as="td"
key={dayIndex}
date={day}
>
<CalendarCell {...state} as="td" key={dayIndex} date={day}>
<CalendarCellButton {...state} date={day} />
</CalendarCell>
))}
Expand Down

0 comments on commit 2df03fa

Please sign in to comment.