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

feat: calendar core #10

Open
wants to merge 128 commits into
base: v0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
128 commits
Select commit Hold shift + click to select a range
169e06d
refactor: update isValidDate return type for improved type safety
bart-krakowski May 29, 2024
75cb7b3
refactor: update isValidDate return type for improved type safety
bart-krakowski May 29, 2024
5dca8d5
refactor: update isValidDate return type for improved type safety
bart-krakowski May 30, 2024
e9e4be9
revert: isValidDate util
bart-krakowski May 30, 2024
d64d04a
feat: useCalendar hook
bart-krakowski Jun 1, 2024
28b4304
test: useCalendar tests
bart-krakowski Jun 2, 2024
28f491c
docs: useCalendar hook
bart-krakowski Jun 2, 2024
468dc26
docs: useCalendar hook
bart-krakowski Jun 2, 2024
0b9bdeb
test: useCalendar tests
bart-krakowski Jun 2, 2024
5b6533e
test: useCalendar tests
bart-krakowski Jun 2, 2024
37effd4
test: overlaping events
bart-krakowski Jun 2, 2024
096f95d
feat(react-time/useCalendar): getCurrentTimeMarkerProps
bart-krakowski Jun 3, 2024
26b0e1e
feat(react-time/useCalendar): getCurrentTimeMarkerProps
bart-krakowski Jun 3, 2024
3cf89c3
feat(react-time/useCalendar): getCurrentTimeMarkerProps
bart-krakowski Jun 3, 2024
4ae3c45
refactor: reducer
bart-krakowski Jun 3, 2024
4009dae
refactor: reducer
bart-krakowski Jun 3, 2024
fa3716a
refactor(useCalendar): types
bart-krakowski Jun 4, 2024
131984b
refactor(useCalendar): types
bart-krakowski Jun 4, 2024
e542934
refactor: useCalendar reducer
bart-krakowski Jun 4, 2024
a149778
refactor: useCalendar reducer
bart-krakowski Jun 5, 2024
28ac64f
refactor: useCalendar reducer
bart-krakowski Jun 5, 2024
dfa353a
feat: add the isToday field
bart-krakowski Jun 5, 2024
8742087
feat: add the isToday field
bart-krakowski Jun 5, 2024
3d6f74a
feat: add the isToday field
bart-krakowski Jun 5, 2024
d93b29d
docs: useCalendar
bart-krakowski Jun 5, 2024
6432973
feat: useCalendar hook
bart-krakowski Jun 5, 2024
75a445a
cd
bart-krakowski Jun 5, 2024
7536efc
feat: useCalendar hook
bart-krakowski Jun 5, 2024
29d20ae
feat: useCalendar hook
bart-krakowski Jun 5, 2024
e37dbf7
feat: useCalendar hook
bart-krakowski Jun 5, 2024
d76ddc1
feat: useCalendar hook
bart-krakowski Jun 5, 2024
f6b6f29
test: useCalendar
bart-krakowski Jun 5, 2024
d9ed5e7
test: useCalendar
bart-krakowski Jun 5, 2024
7b8662b
test: useCalendar
bart-krakowski Jun 5, 2024
be2e360
feat: custom reducer
bart-krakowski Jun 6, 2024
4ccd56b
docs: tsdocs
bart-krakowski Jun 6, 2024
cc59409
docs: tsdocs
bart-krakowski Jun 6, 2024
8d51b58
docs: tsdocs
bart-krakowski Jun 6, 2024
1e71684
refactor: useCalendar
bart-krakowski Jun 6, 2024
e134065
refactor: useCalendar
bart-krakowski Jun 6, 2024
f3f47ed
refactor: useCalendar
bart-krakowski Jun 6, 2024
1ee814a
refactor: change viewMode type
bart-krakowski Jun 9, 2024
c2419de
refactor: change viewMode type
bart-krakowski Jun 9, 2024
215f722
fix: missing days in view
bart-krakowski Jun 9, 2024
0f05c00
refactor: api
bart-krakowski Jun 11, 2024
def6499
refactor: api
bart-krakowski Jun 11, 2024
110e8be
fix: types
bart-krakowski Jun 11, 2024
3333fcb
fix: types
bart-krakowski Jun 11, 2024
492405b
fix: types
bart-krakowski Jun 11, 2024
7bee6b4
fix: types
bart-krakowski Jun 11, 2024
83f0724
docs: useCalendar
bart-krakowski Jun 11, 2024
e4fb70a
docs: useCalendar
bart-krakowski Jun 11, 2024
e598c20
fix: types
bart-krakowski Jun 11, 2024
a9665a7
refactor: add the fillMissingDays argument
bart-krakowski Jun 11, 2024
d9e9b88
refactor: add the fillMissingDays argument
bart-krakowski Jun 11, 2024
fe13c2c
refactor: add the fillMissingDays argument
bart-krakowski Jun 11, 2024
9f99846
refactor: move utilities to the core package
bart-krakowski Jun 11, 2024
93b519a
fix: getCurrentTimeMarkerProps
bart-krakowski Jun 12, 2024
87544e2
docs: update jsdocs
bart-krakowski Jun 12, 2024
b5ae21e
test: useCalendar
bart-krakowski Jun 12, 2024
f5bd2b7
refactor: getCurrentTimeMarkerProps
bart-krakowski Jun 12, 2024
c03128c
feat: calendar core
bart-krakowski Jun 13, 2024
3c48a5e
feat: calendar core
bart-krakowski Jun 13, 2024
6499475
feat: calendar core
bart-krakowski Jun 13, 2024
2ac6065
docs: calendar core
bart-krakowski Jun 13, 2024
a71f5a8
refactor: useCalendar
bart-krakowski Jun 13, 2024
965de14
test: calendar core
bart-krakowski Jun 17, 2024
301d4b8
refactor: useCalendar
bart-krakowski Jun 17, 2024
1324311
refactor: useCalendar
bart-krakowski Jun 17, 2024
042bb13
refactor: useCalendar
bart-krakowski Jun 17, 2024
82c1c87
refactor(useCalendar): types
bart-krakowski Jun 17, 2024
f61a99d
refactor: move calendar-related files to the core directory
bart-krakowski Jun 20, 2024
1465e85
refactor: Update CalendarCoreOptions interface in calendar.ts
bart-krakowski Jun 20, 2024
3f4ef79
test: imports
bart-krakowski Jun 20, 2024
bff5f34
docs: typo
bart-krakowski Jun 20, 2024
57c547e
refactor: DatePickerCore
bart-krakowski Jun 21, 2024
3dc223d
refactor: weekInfoPolyfill
bart-krakowski Jun 23, 2024
2eabee4
refactor: weekInfoPolyfill
bart-krakowski Jun 23, 2024
564c4e7
refactor: weekInfoPolyfill
bart-krakowski Jun 23, 2024
1a5f193
refactor: weekInfoPolyfill
bart-krakowski Jun 24, 2024
3331c17
refactor: date defaults
bart-krakowski Jun 24, 2024
e1f9c12
refactor: date defaults
bart-krakowski Jun 24, 2024
1dfecfb
refactor: date defaults
bart-krakowski Jun 24, 2024
277f416
refactor: types
bart-krakowski Jun 25, 2024
4131b8e
docs: jsdocs
bart-krakowski Jun 25, 2024
b7f67f0
refactor: types
bart-krakowski Jun 25, 2024
4165b20
refactor: types
bart-krakowski Jun 25, 2024
1b71c7e
test: useCalendar
bart-krakowski Jun 25, 2024
604c8bb
refactor: useIsomorphicLayoutEffect
bart-krakowski Jun 25, 2024
6311195
docs: calendar
bart-krakowski Jun 26, 2024
6627c0c
docs: calendar
bart-krakowski Jun 26, 2024
df925d4
refactor: zonedDateTime
bart-krakowski Jun 28, 2024
c65b760
refactor: rename startDate to start and endDate to end
bart-krakowski Jun 28, 2024
18ad00c
refactor: remove the getCurrentTimeMarkerProps method
bart-krakowski Jun 28, 2024
403df99
refactor: weekInfoPolyfill
bart-krakowski Jun 30, 2024
a805131
refactor: add a resources field to event
bart-krakowski Jun 30, 2024
e17f0cd
refactor: add a resources field to event
bart-krakowski Jun 30, 2024
396d629
refactor: add a resources field to event
bart-krakowski Jun 30, 2024
adc9998
refactor: add startOf and endOf helpers
bart-krakowski Jun 30, 2024
31470b4
refactor: remove all the layout specific code
bart-krakowski Jun 30, 2024
16867e8
refactor: update getDaysNames method to accept weekday parameter
bart-krakowski Jul 1, 2024
be68798
refactor: update useCalendar test to mock current date and time
bart-krakowski Jul 1, 2024
c3ba0c9
refactor: update useCalendar test to mock current date and time
bart-krakowski Jul 1, 2024
5afea3e
test: update useCalendar tests
bart-krakowski Jul 1, 2024
15aced5
test: update useCalendar tests
bart-krakowski Jul 1, 2024
eb8b7b7
test: useCalendar
bart-krakowski Jul 1, 2024
4d71196
refactor: update CalendarCore tests to mock current date and time
bart-krakowski Jul 1, 2024
fbbae15
refactor: Update CalendarCore to use destructuring assignment for dat…
bart-krakowski Jul 2, 2024
ce9b924
feat: add the activeDate property
bart-krakowski Jul 2, 2024
66bfe53
refactor: use compare method
bart-krakowski Jul 2, 2024
06f633d
feat: add workWeek unit
bart-krakowski Jul 2, 2024
0c8c868
feat: add workWeek unit
bart-krakowski Jul 2, 2024
4b7ea40
test: tests reorganisation
bart-krakowski Jul 2, 2024
d544348
chore: add the get-week-info-polyfill
bart-krakowski Jul 3, 2024
d090483
feat: getWeekInfo polyfill
bart-krakowski Jul 3, 2024
178aa34
refactor: getEventProps
bart-krakowski Jul 3, 2024
79fdfdd
refactor: getEventProps
bart-krakowski Jul 3, 2024
20fb06d
refactor: Update generateDateRange to accept string inputs
bart-krakowski Jul 3, 2024
8ae1bcd
chore: upgrade polyfill
bart-krakowski Jul 4, 2024
0e974ac
fix: getEventProps
bart-krakowski Jul 4, 2024
85c6a77
fix: getEventProps
bart-krakowski Jul 4, 2024
72d43d4
refactor: update date inputs to accept string format
bart-krakowski Jul 4, 2024
cbc2186
refactor: convert activeDate, currentPeriod to string in useCalendar
bart-krakowski Jul 4, 2024
1f6048e
refactor: startOf and endOf helpers
bart-krakowski Jul 18, 2024
291cfb1
test: calendar core
bart-krakowski Jul 18, 2024
3aa0488
refactor: update useCalendar to accept string inputs for activeDate a…
bart-krakowski Jul 20, 2024
8850d2a
refactor: Update get-week-info-polyfill dependency to version 1.0.6
bart-krakowski Jul 20, 2024
fe67772
refactor: Update startOf and endOf helpers to accept string inputs
bart-krakowski Jul 20, 2024
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
Empty file.
223 changes: 223 additions & 0 deletions docs/framework/react/reference/useCalendar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
---
title: Use Calendar
id: useCalendar
---

### `useCalendar`

```tsx
export function useCalendar({
weekStartsOn,
events,
viewMode,
locale,
onChangeViewMode,
}: UseCalendarProps)
```

`useCalendar` is a hook that provides a comprehensive set of functionalities for managing calendar events, view modes, and period navigation.


#### Parameters

- `events?: TEvent[] | null`
An optional array of events to be handled by the calendar.
- `viewMode: CalendarStore['viewMode']`
The initial view mode configuration of the calendar.
- `locale?: Intl.UnicodeBCP47LocaleIdentifier`
Optional locale for date formatting. Uses a BCP 47 language tag.
- `timeZone?: Temporal.TimeZoneLike`
Optional time zone specification for the calendar.
- `calendar?: Temporal.CalendarLike`
Optional calendar system to be used.


#### Returns

`firstDayOfPeriod: Temporal.PlainDate`
This value represents the first day of the current period displayed by the calendar.
`currentPeriod: string`
This value represents a string that describes the current period displayed by the calendar.
`goToPreviousPeriod: () => void`
This function navigates to the previous period.
`goToNextPeriod: () => void`
This function navigates to the next period.
- `goToCurrentPeriod: () => void`
This function navigates to the current period.
- `goToSpecificPeriod: (date: Temporal.PlainDate) => void`
This function navigates to a specific period based on the provided date.
- `days: Day[]`
This value represents an array of days in the current period displayed by the calendar.
- `daysNames: string[]`
This value represents an array of strings that contain the names of the days of the week.
- `viewMode: CalendarStore['viewMode']`
This value represents the current view mode of the calendar.
- `changeViewMode: (newViewMode: CalendarStore['viewMode']) => void`
This function is used to change the view mode of the calendar.
- `getEventProps: (id: string) => { style: CSSProperties } | null`
This function is used to retrieve the style properties for a specific event based on its ID.
- `getCurrentTimeMarkerProps: () => { style: CSSProperties, currentTime: string | undefined }`
This function is used to retrieve the style properties and current time for the current time marker.
- `isPending: boolean`
This value represents whether the calendar is in a pending state.
- `groupDaysBy: (props: Omit<GroupDaysByProps<TEvent>, 'weekStartsOn'>) => (Day<TEvent> | null)[][]`
This function is used to group the days in the current period by a specified unit. The fillMissingDays parameter can be used to fill in missing days with previous or next month's days.

#### Example Usage

```tsx
const CalendarComponent = ({ events }) => {
const {
firstDayOfPeriod,
currentPeriod,
goToPreviousPeriod,
goToNextPeriod,
goToCurrentPeriod,
goToSpecificPeriod,
changeViewMode,
days,
daysNames,
viewMode,
getEventProps,
getCurrentTimeMarkerProps,
groupDaysBy,
} = useCalendar({
viewMode: { value: 1, unit: 'month' },
events,
locale: 'en-US',
timeZone: 'America/New_York',
calendar: 'gregory',
});

return (
<div className="calendar-container">
<div className="calendar-header">
<button onClick={goToPreviousPeriod}>Previous</button>
<button onClick={goToCurrentPeriod}>Today</button>
<button onClick={goToNextPeriod}>Next</button>
</div>
<div className="calendar-view-mode">
<button onClick={() => changeViewMode({ value: 1, unit: 'month' })}>Month View</button>
<button onClick={() => changeViewMode({ value: 1, unit: 'week' })}>Week View</button>
<button onClick={() => changeViewMode({ value: 3, unit: 'day' })}>3-Day View</button>
<button onClick={() => changeViewMode({ value: 1, unit: 'day' })}>1-Day View</button>
</div>
<table className="calendar-table">
{viewMode.unit === 'month' && (
groupDaysBy(days, 'months').map((month, monthIndex) => (
<tbody key={monthIndex} className="calendar-month">
<tr>
<th colSpan={7} className="calendar-month-name">
{month[0]?.date.toLocaleString('default', { month: 'long' })}{' '}
{month[0]?.date.year}
</th>
</tr>
<tr>
{daysNames.map((dayName, index) => (
<th key={index} className="calendar-day-name">
{dayName}
</th>
))}
</tr>
{groupDaysBy(month, 'weeks').map((week, weekIndex) => (
<tr key={weekIndex} className="calendar-week">
{week.map((day) => (
<td
key={day.date.toString()}
className={`calendar-day ${day.isToday ? 'today' : ''
} ${day.isInCurrentPeriod ? 'current' : 'not-current'}`}
>
<div className="calendar-date">{day.date.day}</div>
<div className="calendar-events">
{day.events.map((event) => (
<div
key={event.id}
className="calendar-event"
style={getEventProps(event.id)?.style}
>
{event.title}
</div>
))}
</div>
</td>
))}
</tr>
))}
</tbody>
))
)}

{viewMode.unit === 'week' && (
<tbody className="calendar-week">
<tr>
{daysNames.map((dayName, index) => (
<th key={index} className="calendar-day-name">
{dayName}
</th>
))}
</tr>
{groupDaysBy(days, 'weeks').map((week, weekIndex) => (
<tr key={weekIndex}>
{week.map((day) => (
<td
key={day.date.toString()}
className={`calendar-day ${day.isToday ? 'today' : ''
} ${day.isInCurrentPeriod ? 'current' : 'not-current'}`}
>
<div className="calendar-date">{day.date.day}</div>
<div className="calendar-events">
{day.events.map((event) => (
<div
key={event.id}
className="calendar-event"
style={getEventProps(event.id)?.style}
>
{event.title}
</div>
))}
</div>
</td>
))}
</tr>
))}
</tbody>
)}

{viewMode.unit === 'day' && (
<tbody className="calendar-day">
<tr>
{daysNames.map((dayName, index) => (
<th key={index} className="calendar-day-name">
{dayName}
</th>
))}
</tr>
<tr>
{days.map((day) => (
<td
key={day.date.toString()}
className={`calendar-day ${day.isToday ? 'today' : ''
} ${day.isInCurrentPeriod ? 'current' : 'not-current'}`}
>
<div className="calendar-date">{day.date.day}</div>
<div className="calendar-events">
{day.events.map((event) => (
<div
key={event.id}
className="calendar-event"
style={getEventProps(event.id)?.style}
>
{event.title}
</div>
))}
</div>
</td>
))}
</tr>
</tbody>
)}
</table>
</div>
);
};
```
28 changes: 0 additions & 28 deletions docs/framework/react/reference/useStore.md

This file was deleted.

118 changes: 118 additions & 0 deletions docs/reference/calendar-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
title: Calendar Core
id: calendar-core
---

### `CalendarCore`

```tsx
export class CalendarCore<TEvent extends Event> {
constructor(options: CalendarCoreOptions<TEvent>);
}
```

The `CalendarCore` class provides a set of functionalities for managing calendar events, view modes, and period navigation. This class is designed to be used in various calendar applications where precise date management and event handling are required.


#### Parameters

- `weekStartsOn?: number`
An optional number that specifies the day of the week that the calendar should start on. It defaults to 1 (Monday).
- `events?: TEvent[]`
An optional array of events that the calendar should display.
- `viewMode: ViewMode`
An object that specifies the initial view mode of the calendar.
- `locale?: Parameters<Temporal.PlainDate['toLocaleString']>['0']`
An optional string that specifies the locale to use for formatting dates and times.
- `timeZone?: Temporal.TimeZoneLike`
Optional time zone specification for the calendar.
- `calendar?: Temporal.CalendarLike`
Optional calendar system to be used.


#### Returns

- `getDaysWithEvents(): Array<Day<TEvent>>`
Returns an array of days in the current period with their associated events.
- `getDaysNames(): string[]`
Returns an array of strings representing the names of the days of the week based on the locale and week start day.
- `changeViewMode(newViewMode: CalendarStore['viewMode']): void`
Changes the view mode of the calendar.
- `goToPreviousPeriod(): void`
Navigates to the previous period based on the current view mode.
- `goToNextPeriod(): void`
Navigates to the next period based on the current view mode.
- `goToCurrentPeriod(): void`
Navigates to the current period.
- `goToSpecificPeriod(date: Temporal.PlainDate): void`
Navigates to a specific period based on the provided date.
- `updateCurrentTime(): void`
Updates the current time.
- `getEventProps(id: Event['id']): { style: CSSProperties } | null`
Retrieves the style properties for a specific event based on its ID.
- `getCurrentTimeMarkerProps(): { style: CSSProperties; currentTime: string | undefined }`
Retrieves the style properties and current time for the current time marker.
- `groupDaysBy(props: Omit<GroupDaysByProps<TEvent>, 'weekStartsOn'>): (Day<TEvent> | null)[][]`
Groups the days in the current period by a specified unit. The fillMissingDays parameter can be used to fill in missing days with previous or next month's days.


#### Example Usage

```ts
import { CalendarCore, Event } from '@tanstack/time';
import { Temporal } from '@js-temporal/polyfill';

interface MyEvent extends Event {
location: string;
}

const events: MyEvent[] = [
{
id: '1',
startDate: Temporal.PlainDateTime.from('2024-06-10T09:00'),
endDate: Temporal.PlainDateTime.from('2024-06-10T10:00'),
title: 'Event 1',
location: 'Room 101',
},
{
id: '2',
startDate: Temporal.PlainDateTime.from('2024-06-12T11:00'),
endDate: Temporal.PlainDateTime.from('2024-06-12T12:00'),
title: 'Event 2',
location: 'Room 202',
},
];

const calendarCore = new CalendarCore<MyEvent>({
viewMode: { value: 1, unit: 'month' },
events,
locale: 'en-US',
timeZone: 'America/New_York',
calendar: 'gregory',
});

// Get days with events
const daysWithEvents = calendarCore.getDaysWithEvents();
console.log(daysWithEvents);

// Change view mode to week
calendarCore.changeViewMode({ value: 1, unit: 'week' });

// Navigate to the next period
calendarCore.goToNextPeriod();

// Update current time
calendarCore.updateCurrentTime();

// Get event properties
const eventProps = calendarCore.getEventProps('1');
console.log(eventProps);

// Get current time marker properties
const currentTimeMarkerProps = calendarCore.getCurrentTimeMarkerProps();
console.log(currentTimeMarkerProps);

// Group days by week
const groupedDays = calendarCore.groupDaysBy({ days: daysWithEvents, unit: 'week' });
console.log(groupedDays);
```
3 changes: 3 additions & 0 deletions packages/react-time/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@
"react-dom": "^17.0.0 || ^18.0.0"
},
"dependencies": {
"@js-temporal/polyfill": "^0.4.4",
"@tanstack/react-store": "^0.4.1",
"@tanstack/time": "workspace:*",
"typesafe-actions": "^5.1.0",
"use-sync-external-store": "^1.2.0"
},
"devDependencies": {
Expand Down
4 changes: 1 addition & 3 deletions packages/react-time/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
/**
* TanStack Time
*/
export { useCalendar } from './useCalendar';
Loading