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: date picker core #11

Draft
wants to merge 96 commits into
base: v0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 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
dc3bcbf
refactor: update CalendarCoreOptions interface to use default generic…
bart-krakowski Jun 20, 2024
1465e85
refactor: Update CalendarCoreOptions interface in calendar.ts
bart-krakowski Jun 20, 2024
55751f8
feat: date picker core
bart-krakowski Jun 20, 2024
3f4ef79
test: imports
bart-krakowski Jun 20, 2024
ce4fa6b
feat: date picker core
bart-krakowski Jun 20, 2024
bff5f34
docs: typo
bart-krakowski Jun 20, 2024
1c10f14
refactor: DatePickerCore
bart-krakowski Jun 21, 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
a1812ee
chore: resolve conflicts
bart-krakowski Jun 25, 2024
4ee4be4
chore: resolve conflicts
bart-krakowski Jun 25, 2024
bd1d3e3
docs: jsdocs
bart-krakowski Jun 25, 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
230 changes: 230 additions & 0 deletions docs/framework/react/reference/useCalendar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
---
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

- `weekStartsOn?: number`
- This parameter is an optional number that specifies the day of the week that the calendar should start on. It defaults to 0, which is Sunday.
- `events: Event[]`
- This parameter is an array of events that the calendar should display.
- `viewMode: 'month' | 'week' | number`
- This parameter is a string that specifies the initial view mode of the calendar. It can be either 'month', 'week', or a number representing the number of days in a custom view mode.
- `locale?: string`
- This parameter is an optional string that specifies the locale to use for formatting dates and times. It defaults to the system locale.
- `onChangeViewMode?: ({ value: number; unit: "month" | "week" | "day"; }) => void`
- This parameter is an optional callback function that is called when the view mode of the calendar changes. It receives the new view mode as an argument.
- `onChangeViewMode?: (viewMode: value: number; unit: "month" | "week" | "day";) => void`
- This parameter is an optional callback function that is called when the view mode of the calendar changes. It receives the new view mode as an argument.
- `reducer?: (state: CalendarState, action: CalendarAction) => CalendarState`
- This parameter is an optional custom reducer function that can be used to manage the state of the calendar.


#### 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: MouseEventHandler<HTMLButtonElement>`
- This function is a click event handler that navigates to the previous period.
- `goToNextPeriod: MouseEventHandler<HTMLButtonElement>`
- This function is a click event handler that navigates to the next period.
- `goToCurrentPeriod: MouseEventHandler<HTMLButtonElement>`
- This function is a click event handler that navigates to the current period.
- `goToSpecificPeriod: (date: Temporal.PlainDate) => void`
- This function is a callback function that is called when a date is selected in the calendar. It receives the selected date as an argument.
- `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: 'month' | 'week' | number`
- This value represents the current view mode of the calendar.
- `changeViewMode: (newViewMode: 'month' | 'week' | number) => 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.
- `getEventProps: (id: string) => { style: CSSProperties } | null`
- This function is used to retrieve the style properties for a specific event based on its ID.
- `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: Temporal.PlainTime }`
- 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: ({ days: Day[], unit: 'week' | 'month', fillMissingDays?: boolean }) => Day[][]`
- 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({
weekStartsOn: 1,
viewMode: { value: 1, unit: 'month' },
locale: 'en-US',
onChangeViewMode: (newViewMode) => console.log('View mode changed:', newViewMode),
});

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.

113 changes: 113 additions & 0 deletions docs/reference/calendar-core.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
---
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.


#### 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: 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>({
weekStartsOn: 1,
viewMode: { value: 1, unit: 'month' },
events,
locale: 'en-US',
});

// 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);
```
Loading