@gobrand/calendar-core 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +10 -0
- package/dist/index.d.cts +107 -49
- package/dist/index.d.ts +107 -49
- package/dist/index.js +10 -0
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -661,6 +661,16 @@ function createCalendar(options) {
|
|
|
661
661
|
setOptions(updater) {
|
|
662
662
|
_options = functionalUpdate(updater, _options);
|
|
663
663
|
},
|
|
664
|
+
// Type predicates for runtime view checking
|
|
665
|
+
hasMonthView() {
|
|
666
|
+
return "month" in _options.views && _options.views.month !== void 0;
|
|
667
|
+
},
|
|
668
|
+
hasWeekView() {
|
|
669
|
+
return "week" in _options.views && _options.views.week !== void 0;
|
|
670
|
+
},
|
|
671
|
+
hasDayView() {
|
|
672
|
+
return "day" in _options.views && _options.views.day !== void 0;
|
|
673
|
+
},
|
|
664
674
|
store
|
|
665
675
|
};
|
|
666
676
|
return calendar;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
1
|
import * as _tanstack_store from '@tanstack/store';
|
|
2
2
|
import { Temporal } from '@js-temporal/polyfill';
|
|
3
3
|
|
|
4
|
-
type CalendarAccessor<
|
|
5
|
-
getDate: (item:
|
|
6
|
-
getStart?: (item:
|
|
7
|
-
getEnd?: (item:
|
|
4
|
+
type CalendarAccessor<TItem> = {
|
|
5
|
+
getDate: (item: TItem) => Temporal.PlainDate;
|
|
6
|
+
getStart?: (item: TItem) => Temporal.ZonedDateTime;
|
|
7
|
+
getEnd?: (item: TItem) => Temporal.ZonedDateTime;
|
|
8
8
|
};
|
|
9
|
-
type CalendarDay<
|
|
9
|
+
type CalendarDay<TItem = unknown> = {
|
|
10
10
|
date: Temporal.PlainDate;
|
|
11
11
|
isCurrentMonth: boolean;
|
|
12
12
|
isToday: boolean;
|
|
13
|
-
items:
|
|
13
|
+
items: TItem[];
|
|
14
14
|
};
|
|
15
|
-
type CalendarWeek<
|
|
16
|
-
type CalendarMonth<
|
|
17
|
-
weeks: CalendarWeek<
|
|
15
|
+
type CalendarWeek<TItem = unknown> = CalendarDay<TItem>[];
|
|
16
|
+
type CalendarMonth<TItem = unknown> = {
|
|
17
|
+
weeks: CalendarWeek<TItem>[];
|
|
18
18
|
month: Temporal.PlainYearMonth;
|
|
19
19
|
};
|
|
20
|
-
type WeekDay<
|
|
20
|
+
type WeekDay<TItem = unknown> = {
|
|
21
21
|
date: Temporal.PlainDate;
|
|
22
22
|
isToday: boolean;
|
|
23
|
-
items:
|
|
24
|
-
timeSlots?: TimeSlot<
|
|
23
|
+
items: TItem[];
|
|
24
|
+
timeSlots?: TimeSlot<TItem>[];
|
|
25
25
|
};
|
|
26
|
-
type CalendarWeekView<
|
|
27
|
-
days: WeekDay<
|
|
26
|
+
type CalendarWeekView<TItem = unknown> = {
|
|
27
|
+
days: WeekDay<TItem>[];
|
|
28
28
|
weekStart: Temporal.PlainDate;
|
|
29
29
|
weekEnd: Temporal.PlainDate;
|
|
30
30
|
};
|
|
31
|
-
type TimeSlot<
|
|
31
|
+
type TimeSlot<TItem = unknown> = {
|
|
32
32
|
hour: number;
|
|
33
33
|
minute: number;
|
|
34
34
|
time: Temporal.PlainTime;
|
|
35
|
-
items:
|
|
35
|
+
items: TItem[];
|
|
36
36
|
};
|
|
37
|
-
type CalendarDayView<
|
|
37
|
+
type CalendarDayView<TItem = unknown> = {
|
|
38
38
|
date: Temporal.PlainDate;
|
|
39
39
|
isToday: boolean;
|
|
40
|
-
timeSlots: TimeSlot<
|
|
41
|
-
items:
|
|
40
|
+
timeSlots: TimeSlot<TItem>[];
|
|
41
|
+
items: TItem[];
|
|
42
42
|
};
|
|
43
43
|
type DateRange = {
|
|
44
44
|
start: Temporal.ZonedDateTime;
|
|
@@ -50,31 +50,31 @@ type CalendarState = {
|
|
|
50
50
|
currentView?: string;
|
|
51
51
|
dateRange: DateRange;
|
|
52
52
|
};
|
|
53
|
-
type MonthViewOptions<
|
|
54
|
-
accessor: CalendarAccessor<
|
|
53
|
+
type MonthViewOptions<TItem> = {
|
|
54
|
+
accessor: CalendarAccessor<TItem>;
|
|
55
55
|
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
56
56
|
};
|
|
57
|
-
type WeekViewOptions<
|
|
58
|
-
accessor: CalendarAccessor<
|
|
57
|
+
type WeekViewOptions<TItem> = {
|
|
58
|
+
accessor: CalendarAccessor<TItem>;
|
|
59
59
|
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
60
60
|
startHour?: number;
|
|
61
61
|
endHour?: number;
|
|
62
62
|
slotDuration?: number;
|
|
63
63
|
};
|
|
64
|
-
type DayViewOptions<
|
|
65
|
-
accessor: CalendarAccessor<
|
|
64
|
+
type DayViewOptions<TItem> = {
|
|
65
|
+
accessor: CalendarAccessor<TItem>;
|
|
66
66
|
startHour?: number;
|
|
67
67
|
endHour?: number;
|
|
68
68
|
slotDuration?: number;
|
|
69
69
|
};
|
|
70
|
-
type CalendarViewOptions<
|
|
71
|
-
month?: MonthViewOptions<
|
|
72
|
-
week?: WeekViewOptions<
|
|
73
|
-
day?: DayViewOptions<
|
|
70
|
+
type CalendarViewOptions<TItem> = {
|
|
71
|
+
month?: MonthViewOptions<TItem>;
|
|
72
|
+
week?: WeekViewOptions<TItem>;
|
|
73
|
+
day?: DayViewOptions<TItem>;
|
|
74
74
|
};
|
|
75
|
-
type CalendarOptions<
|
|
76
|
-
data:
|
|
77
|
-
views: CalendarViewOptions<
|
|
75
|
+
type CalendarOptions<TItem> = {
|
|
76
|
+
data: TItem[];
|
|
77
|
+
views: CalendarViewOptions<TItem>;
|
|
78
78
|
timeZone?: 'UTC' | string;
|
|
79
79
|
state?: Partial<CalendarState>;
|
|
80
80
|
onStateChange?: (updater: Updater<CalendarState>) => void;
|
|
@@ -82,8 +82,8 @@ type CalendarOptions<T> = {
|
|
|
82
82
|
type HasView<Options, View extends 'month' | 'week' | 'day'> = Options extends {
|
|
83
83
|
views: infer V;
|
|
84
84
|
} ? V extends Record<View, unknown> ? V[View] extends undefined ? false : true : false : false;
|
|
85
|
-
type MonthMethods<
|
|
86
|
-
getMonth(): CalendarMonth<
|
|
85
|
+
type MonthMethods<TItem, TOptions> = HasView<TOptions, 'month'> extends true ? {
|
|
86
|
+
getMonth(): CalendarMonth<TItem>;
|
|
87
87
|
nextMonth(): void;
|
|
88
88
|
previousMonth(): void;
|
|
89
89
|
goToMonth(year: number, month: number): void;
|
|
@@ -93,8 +93,8 @@ type MonthMethods<T, Options> = HasView<Options, 'month'> extends true ? {
|
|
|
93
93
|
previousMonth?: never;
|
|
94
94
|
goToMonth?: never;
|
|
95
95
|
};
|
|
96
|
-
type WeekMethods<
|
|
97
|
-
getWeek(): CalendarWeekView<
|
|
96
|
+
type WeekMethods<TItem, TOptions> = HasView<TOptions, 'week'> extends true ? {
|
|
97
|
+
getWeek(): CalendarWeekView<TItem>;
|
|
98
98
|
nextWeek(): void;
|
|
99
99
|
previousWeek(): void;
|
|
100
100
|
} : {
|
|
@@ -102,8 +102,8 @@ type WeekMethods<T, Options> = HasView<Options, 'week'> extends true ? {
|
|
|
102
102
|
nextWeek?: never;
|
|
103
103
|
previousWeek?: never;
|
|
104
104
|
};
|
|
105
|
-
type DayMethods<
|
|
106
|
-
getDay(): CalendarDayView<
|
|
105
|
+
type DayMethods<TItem, TOptions> = HasView<TOptions, 'day'> extends true ? {
|
|
106
|
+
getDay(): CalendarDayView<TItem>;
|
|
107
107
|
nextDay(): void;
|
|
108
108
|
previousDay(): void;
|
|
109
109
|
} : {
|
|
@@ -129,17 +129,75 @@ type BaseCalendarMethods<Options> = {
|
|
|
129
129
|
options: Options;
|
|
130
130
|
setOptions(updater: (old: Options) => Options): void;
|
|
131
131
|
store: _tanstack_store.Store<CalendarState>;
|
|
132
|
+
hasMonthView(): boolean;
|
|
133
|
+
hasWeekView(): boolean;
|
|
134
|
+
hasDayView(): boolean;
|
|
132
135
|
};
|
|
133
|
-
type ViewsToOptions<
|
|
134
|
-
data:
|
|
135
|
-
views:
|
|
136
|
+
type ViewsToOptions<TItem, TViews extends CalendarViewOptions<TItem>> = {
|
|
137
|
+
data: TItem[];
|
|
138
|
+
views: TViews;
|
|
136
139
|
};
|
|
137
|
-
type Calendar<
|
|
140
|
+
type Calendar<TItem, TOptionsOrViews = CalendarOptions<TItem>> = TOptionsOrViews extends CalendarOptions<TItem> ? BaseCalendarMethods<TOptionsOrViews> & MonthMethods<TItem, TOptionsOrViews> & WeekMethods<TItem, TOptionsOrViews> & DayMethods<TItem, TOptionsOrViews> : TOptionsOrViews extends CalendarViewOptions<TItem> ? BaseCalendarMethods<ViewsToOptions<TItem, TOptionsOrViews>> & MonthMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> & WeekMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> & DayMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> : BaseCalendarMethods<CalendarOptions<TItem>> & MonthMethods<TItem, CalendarOptions<TItem>> & WeekMethods<TItem, CalendarOptions<TItem>> & DayMethods<TItem, CalendarOptions<TItem>>;
|
|
138
141
|
type InferViewNames<V extends CalendarViewOptions<unknown>> = keyof V & string;
|
|
139
142
|
type InferViewConfig<V extends CalendarViewOptions<unknown>, Name extends keyof V> = V[Name] extends infer Config ? {
|
|
140
143
|
name: Name;
|
|
141
144
|
config: Config;
|
|
142
145
|
} : never;
|
|
146
|
+
/**
|
|
147
|
+
* Extract the item type from a Calendar type
|
|
148
|
+
* @example type Item = CalendarItemType<typeof calendar>; // Post
|
|
149
|
+
*/
|
|
150
|
+
type CalendarItemType<C extends Calendar<any, any>> = C extends Calendar<infer TItem, any> ? TItem : never;
|
|
151
|
+
/**
|
|
152
|
+
* Extract the views configuration from a Calendar type
|
|
153
|
+
* @example type Views = CalendarViewsConfig<typeof calendar>;
|
|
154
|
+
*/
|
|
155
|
+
type CalendarViewsConfig<C extends Calendar<any, any>> = C extends Calendar<any, infer TOptions> ? TOptions extends {
|
|
156
|
+
views: infer V;
|
|
157
|
+
} ? V : TOptions extends CalendarViewOptions<any> ? TOptions : never : never;
|
|
158
|
+
/**
|
|
159
|
+
* Check if a Calendar has a specific view configured
|
|
160
|
+
* @example type HasMonth = HasViewType<typeof calendar, 'month'>; // true | false
|
|
161
|
+
*/
|
|
162
|
+
type HasViewType<C extends Calendar<any, any>, V extends 'month' | 'week' | 'day'> = C extends Calendar<any, infer TOptions> ? HasView<TOptions, V> : false;
|
|
163
|
+
/**
|
|
164
|
+
* Extract valid view names as a union from a Calendar
|
|
165
|
+
* @example type Views = ValidViewNames<typeof calendar>; // 'month' | 'week'
|
|
166
|
+
*/
|
|
167
|
+
type ValidViewNames<C extends Calendar<any, any>> = C extends Calendar<any, infer TOptions> ? ValidViews<TOptions> : never;
|
|
168
|
+
/**
|
|
169
|
+
* Extract the item type from CalendarOptions before creating a calendar
|
|
170
|
+
* @example type Item = ItemTypeFromOptions<typeof options>; // Post
|
|
171
|
+
*/
|
|
172
|
+
type ItemTypeFromOptions<O extends CalendarOptions<any>> = O extends CalendarOptions<infer TItem> ? TItem : never;
|
|
173
|
+
/**
|
|
174
|
+
* Base props for components that receive a Calendar
|
|
175
|
+
* @example
|
|
176
|
+
* function MyComponent<C extends Calendar<any, any>>(
|
|
177
|
+
* props: CalendarComponentProps<C>
|
|
178
|
+
* ) { ... }
|
|
179
|
+
*/
|
|
180
|
+
type CalendarComponentProps<C extends Calendar<any, any>> = {
|
|
181
|
+
calendar: C;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Require that a Calendar has month view configured
|
|
185
|
+
* @example
|
|
186
|
+
* function MonthView<C extends Calendar<any, any>>(
|
|
187
|
+
* props: { calendar: RequireMonthView<C> }
|
|
188
|
+
* ) {
|
|
189
|
+
* // calendar.getMonth() is guaranteed to exist
|
|
190
|
+
* }
|
|
191
|
+
*/
|
|
192
|
+
type RequireMonthView<C extends Calendar<any, any>> = HasViewType<C, 'month'> extends true ? C : never;
|
|
193
|
+
/**
|
|
194
|
+
* Require that a Calendar has week view configured
|
|
195
|
+
*/
|
|
196
|
+
type RequireWeekView<C extends Calendar<any, any>> = HasViewType<C, 'week'> extends true ? C : never;
|
|
197
|
+
/**
|
|
198
|
+
* Require that a Calendar has day view configured
|
|
199
|
+
*/
|
|
200
|
+
type RequireDayView<C extends Calendar<any, any>> = HasViewType<C, 'day'> extends true ? C : never;
|
|
143
201
|
|
|
144
202
|
declare function functionalUpdate<T>(updater: T | ((old: T) => T), input: T): T;
|
|
145
203
|
declare function createCalendarAccessor<T, A extends CalendarAccessor<T> = CalendarAccessor<T>>(accessor: A): A;
|
|
@@ -234,12 +292,12 @@ declare function getWeekDateRange(date: Temporal.PlainDate, timeZone: string, op
|
|
|
234
292
|
*/
|
|
235
293
|
declare function getDayDateRange(date: Temporal.PlainDate, timeZone: string): DateRange;
|
|
236
294
|
|
|
237
|
-
declare function createCalendar<
|
|
295
|
+
declare function createCalendar<TItem, TOptions extends CalendarOptions<TItem>>(options: TOptions): Calendar<TItem, TOptions>;
|
|
238
296
|
|
|
239
|
-
declare function createCalendarViews<
|
|
240
|
-
month?: MonthViewOptions<
|
|
241
|
-
week?: WeekViewOptions<
|
|
242
|
-
day?: DayViewOptions<
|
|
243
|
-
}>(views:
|
|
297
|
+
declare function createCalendarViews<TItem>(): <const TViews extends {
|
|
298
|
+
month?: MonthViewOptions<TItem>;
|
|
299
|
+
week?: WeekViewOptions<TItem>;
|
|
300
|
+
day?: DayViewOptions<TItem>;
|
|
301
|
+
}>(views: TViews) => TViews;
|
|
244
302
|
|
|
245
|
-
export { type Calendar, type CalendarAccessor, type CalendarDay, type CalendarDayView, type CalendarMonth, type CalendarOptions, type CalendarState, type CalendarViewOptions, type CalendarWeek, type CalendarWeekView, type DateRange, type DateRangeBounds, type DayViewOptions, type InferViewConfig, type InferViewNames, type MonthViewOptions, type TimeSlot, type Updater, type WeekDay, type WeekViewOptions, buildDay, buildMonth, buildWeek, convertToTimezone, createCalendar, createCalendarAccessor, createCalendarViews, createZonedDateTime, formatTime, functionalUpdate, getCurrentTimeZone, getDayDateRange, getDayRange, getEventPosition, getMonthDateRange, getMonthName, getMonthRange, getTimeSlotHeight, getTimezoneOffset, getWeekDateRange, getWeekRange, getWeekdays, goToToday, nextDay, nextMonth, nextWeek, previousDay, previousMonth, previousWeek };
|
|
303
|
+
export { type Calendar, type CalendarAccessor, type CalendarComponentProps, type CalendarDay, type CalendarDayView, type CalendarItemType, type CalendarMonth, type CalendarOptions, type CalendarState, type CalendarViewOptions, type CalendarViewsConfig, type CalendarWeek, type CalendarWeekView, type DateRange, type DateRangeBounds, type DayViewOptions, type HasViewType, type InferViewConfig, type InferViewNames, type ItemTypeFromOptions, type MonthViewOptions, type RequireDayView, type RequireMonthView, type RequireWeekView, type TimeSlot, type Updater, type ValidViewNames, type WeekDay, type WeekViewOptions, buildDay, buildMonth, buildWeek, convertToTimezone, createCalendar, createCalendarAccessor, createCalendarViews, createZonedDateTime, formatTime, functionalUpdate, getCurrentTimeZone, getDayDateRange, getDayRange, getEventPosition, getMonthDateRange, getMonthName, getMonthRange, getTimeSlotHeight, getTimezoneOffset, getWeekDateRange, getWeekRange, getWeekdays, goToToday, nextDay, nextMonth, nextWeek, previousDay, previousMonth, previousWeek };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,44 +1,44 @@
|
|
|
1
1
|
import * as _tanstack_store from '@tanstack/store';
|
|
2
2
|
import { Temporal } from '@js-temporal/polyfill';
|
|
3
3
|
|
|
4
|
-
type CalendarAccessor<
|
|
5
|
-
getDate: (item:
|
|
6
|
-
getStart?: (item:
|
|
7
|
-
getEnd?: (item:
|
|
4
|
+
type CalendarAccessor<TItem> = {
|
|
5
|
+
getDate: (item: TItem) => Temporal.PlainDate;
|
|
6
|
+
getStart?: (item: TItem) => Temporal.ZonedDateTime;
|
|
7
|
+
getEnd?: (item: TItem) => Temporal.ZonedDateTime;
|
|
8
8
|
};
|
|
9
|
-
type CalendarDay<
|
|
9
|
+
type CalendarDay<TItem = unknown> = {
|
|
10
10
|
date: Temporal.PlainDate;
|
|
11
11
|
isCurrentMonth: boolean;
|
|
12
12
|
isToday: boolean;
|
|
13
|
-
items:
|
|
13
|
+
items: TItem[];
|
|
14
14
|
};
|
|
15
|
-
type CalendarWeek<
|
|
16
|
-
type CalendarMonth<
|
|
17
|
-
weeks: CalendarWeek<
|
|
15
|
+
type CalendarWeek<TItem = unknown> = CalendarDay<TItem>[];
|
|
16
|
+
type CalendarMonth<TItem = unknown> = {
|
|
17
|
+
weeks: CalendarWeek<TItem>[];
|
|
18
18
|
month: Temporal.PlainYearMonth;
|
|
19
19
|
};
|
|
20
|
-
type WeekDay<
|
|
20
|
+
type WeekDay<TItem = unknown> = {
|
|
21
21
|
date: Temporal.PlainDate;
|
|
22
22
|
isToday: boolean;
|
|
23
|
-
items:
|
|
24
|
-
timeSlots?: TimeSlot<
|
|
23
|
+
items: TItem[];
|
|
24
|
+
timeSlots?: TimeSlot<TItem>[];
|
|
25
25
|
};
|
|
26
|
-
type CalendarWeekView<
|
|
27
|
-
days: WeekDay<
|
|
26
|
+
type CalendarWeekView<TItem = unknown> = {
|
|
27
|
+
days: WeekDay<TItem>[];
|
|
28
28
|
weekStart: Temporal.PlainDate;
|
|
29
29
|
weekEnd: Temporal.PlainDate;
|
|
30
30
|
};
|
|
31
|
-
type TimeSlot<
|
|
31
|
+
type TimeSlot<TItem = unknown> = {
|
|
32
32
|
hour: number;
|
|
33
33
|
minute: number;
|
|
34
34
|
time: Temporal.PlainTime;
|
|
35
|
-
items:
|
|
35
|
+
items: TItem[];
|
|
36
36
|
};
|
|
37
|
-
type CalendarDayView<
|
|
37
|
+
type CalendarDayView<TItem = unknown> = {
|
|
38
38
|
date: Temporal.PlainDate;
|
|
39
39
|
isToday: boolean;
|
|
40
|
-
timeSlots: TimeSlot<
|
|
41
|
-
items:
|
|
40
|
+
timeSlots: TimeSlot<TItem>[];
|
|
41
|
+
items: TItem[];
|
|
42
42
|
};
|
|
43
43
|
type DateRange = {
|
|
44
44
|
start: Temporal.ZonedDateTime;
|
|
@@ -50,31 +50,31 @@ type CalendarState = {
|
|
|
50
50
|
currentView?: string;
|
|
51
51
|
dateRange: DateRange;
|
|
52
52
|
};
|
|
53
|
-
type MonthViewOptions<
|
|
54
|
-
accessor: CalendarAccessor<
|
|
53
|
+
type MonthViewOptions<TItem> = {
|
|
54
|
+
accessor: CalendarAccessor<TItem>;
|
|
55
55
|
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
56
56
|
};
|
|
57
|
-
type WeekViewOptions<
|
|
58
|
-
accessor: CalendarAccessor<
|
|
57
|
+
type WeekViewOptions<TItem> = {
|
|
58
|
+
accessor: CalendarAccessor<TItem>;
|
|
59
59
|
weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6;
|
|
60
60
|
startHour?: number;
|
|
61
61
|
endHour?: number;
|
|
62
62
|
slotDuration?: number;
|
|
63
63
|
};
|
|
64
|
-
type DayViewOptions<
|
|
65
|
-
accessor: CalendarAccessor<
|
|
64
|
+
type DayViewOptions<TItem> = {
|
|
65
|
+
accessor: CalendarAccessor<TItem>;
|
|
66
66
|
startHour?: number;
|
|
67
67
|
endHour?: number;
|
|
68
68
|
slotDuration?: number;
|
|
69
69
|
};
|
|
70
|
-
type CalendarViewOptions<
|
|
71
|
-
month?: MonthViewOptions<
|
|
72
|
-
week?: WeekViewOptions<
|
|
73
|
-
day?: DayViewOptions<
|
|
70
|
+
type CalendarViewOptions<TItem> = {
|
|
71
|
+
month?: MonthViewOptions<TItem>;
|
|
72
|
+
week?: WeekViewOptions<TItem>;
|
|
73
|
+
day?: DayViewOptions<TItem>;
|
|
74
74
|
};
|
|
75
|
-
type CalendarOptions<
|
|
76
|
-
data:
|
|
77
|
-
views: CalendarViewOptions<
|
|
75
|
+
type CalendarOptions<TItem> = {
|
|
76
|
+
data: TItem[];
|
|
77
|
+
views: CalendarViewOptions<TItem>;
|
|
78
78
|
timeZone?: 'UTC' | string;
|
|
79
79
|
state?: Partial<CalendarState>;
|
|
80
80
|
onStateChange?: (updater: Updater<CalendarState>) => void;
|
|
@@ -82,8 +82,8 @@ type CalendarOptions<T> = {
|
|
|
82
82
|
type HasView<Options, View extends 'month' | 'week' | 'day'> = Options extends {
|
|
83
83
|
views: infer V;
|
|
84
84
|
} ? V extends Record<View, unknown> ? V[View] extends undefined ? false : true : false : false;
|
|
85
|
-
type MonthMethods<
|
|
86
|
-
getMonth(): CalendarMonth<
|
|
85
|
+
type MonthMethods<TItem, TOptions> = HasView<TOptions, 'month'> extends true ? {
|
|
86
|
+
getMonth(): CalendarMonth<TItem>;
|
|
87
87
|
nextMonth(): void;
|
|
88
88
|
previousMonth(): void;
|
|
89
89
|
goToMonth(year: number, month: number): void;
|
|
@@ -93,8 +93,8 @@ type MonthMethods<T, Options> = HasView<Options, 'month'> extends true ? {
|
|
|
93
93
|
previousMonth?: never;
|
|
94
94
|
goToMonth?: never;
|
|
95
95
|
};
|
|
96
|
-
type WeekMethods<
|
|
97
|
-
getWeek(): CalendarWeekView<
|
|
96
|
+
type WeekMethods<TItem, TOptions> = HasView<TOptions, 'week'> extends true ? {
|
|
97
|
+
getWeek(): CalendarWeekView<TItem>;
|
|
98
98
|
nextWeek(): void;
|
|
99
99
|
previousWeek(): void;
|
|
100
100
|
} : {
|
|
@@ -102,8 +102,8 @@ type WeekMethods<T, Options> = HasView<Options, 'week'> extends true ? {
|
|
|
102
102
|
nextWeek?: never;
|
|
103
103
|
previousWeek?: never;
|
|
104
104
|
};
|
|
105
|
-
type DayMethods<
|
|
106
|
-
getDay(): CalendarDayView<
|
|
105
|
+
type DayMethods<TItem, TOptions> = HasView<TOptions, 'day'> extends true ? {
|
|
106
|
+
getDay(): CalendarDayView<TItem>;
|
|
107
107
|
nextDay(): void;
|
|
108
108
|
previousDay(): void;
|
|
109
109
|
} : {
|
|
@@ -129,17 +129,75 @@ type BaseCalendarMethods<Options> = {
|
|
|
129
129
|
options: Options;
|
|
130
130
|
setOptions(updater: (old: Options) => Options): void;
|
|
131
131
|
store: _tanstack_store.Store<CalendarState>;
|
|
132
|
+
hasMonthView(): boolean;
|
|
133
|
+
hasWeekView(): boolean;
|
|
134
|
+
hasDayView(): boolean;
|
|
132
135
|
};
|
|
133
|
-
type ViewsToOptions<
|
|
134
|
-
data:
|
|
135
|
-
views:
|
|
136
|
+
type ViewsToOptions<TItem, TViews extends CalendarViewOptions<TItem>> = {
|
|
137
|
+
data: TItem[];
|
|
138
|
+
views: TViews;
|
|
136
139
|
};
|
|
137
|
-
type Calendar<
|
|
140
|
+
type Calendar<TItem, TOptionsOrViews = CalendarOptions<TItem>> = TOptionsOrViews extends CalendarOptions<TItem> ? BaseCalendarMethods<TOptionsOrViews> & MonthMethods<TItem, TOptionsOrViews> & WeekMethods<TItem, TOptionsOrViews> & DayMethods<TItem, TOptionsOrViews> : TOptionsOrViews extends CalendarViewOptions<TItem> ? BaseCalendarMethods<ViewsToOptions<TItem, TOptionsOrViews>> & MonthMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> & WeekMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> & DayMethods<TItem, ViewsToOptions<TItem, TOptionsOrViews>> : BaseCalendarMethods<CalendarOptions<TItem>> & MonthMethods<TItem, CalendarOptions<TItem>> & WeekMethods<TItem, CalendarOptions<TItem>> & DayMethods<TItem, CalendarOptions<TItem>>;
|
|
138
141
|
type InferViewNames<V extends CalendarViewOptions<unknown>> = keyof V & string;
|
|
139
142
|
type InferViewConfig<V extends CalendarViewOptions<unknown>, Name extends keyof V> = V[Name] extends infer Config ? {
|
|
140
143
|
name: Name;
|
|
141
144
|
config: Config;
|
|
142
145
|
} : never;
|
|
146
|
+
/**
|
|
147
|
+
* Extract the item type from a Calendar type
|
|
148
|
+
* @example type Item = CalendarItemType<typeof calendar>; // Post
|
|
149
|
+
*/
|
|
150
|
+
type CalendarItemType<C extends Calendar<any, any>> = C extends Calendar<infer TItem, any> ? TItem : never;
|
|
151
|
+
/**
|
|
152
|
+
* Extract the views configuration from a Calendar type
|
|
153
|
+
* @example type Views = CalendarViewsConfig<typeof calendar>;
|
|
154
|
+
*/
|
|
155
|
+
type CalendarViewsConfig<C extends Calendar<any, any>> = C extends Calendar<any, infer TOptions> ? TOptions extends {
|
|
156
|
+
views: infer V;
|
|
157
|
+
} ? V : TOptions extends CalendarViewOptions<any> ? TOptions : never : never;
|
|
158
|
+
/**
|
|
159
|
+
* Check if a Calendar has a specific view configured
|
|
160
|
+
* @example type HasMonth = HasViewType<typeof calendar, 'month'>; // true | false
|
|
161
|
+
*/
|
|
162
|
+
type HasViewType<C extends Calendar<any, any>, V extends 'month' | 'week' | 'day'> = C extends Calendar<any, infer TOptions> ? HasView<TOptions, V> : false;
|
|
163
|
+
/**
|
|
164
|
+
* Extract valid view names as a union from a Calendar
|
|
165
|
+
* @example type Views = ValidViewNames<typeof calendar>; // 'month' | 'week'
|
|
166
|
+
*/
|
|
167
|
+
type ValidViewNames<C extends Calendar<any, any>> = C extends Calendar<any, infer TOptions> ? ValidViews<TOptions> : never;
|
|
168
|
+
/**
|
|
169
|
+
* Extract the item type from CalendarOptions before creating a calendar
|
|
170
|
+
* @example type Item = ItemTypeFromOptions<typeof options>; // Post
|
|
171
|
+
*/
|
|
172
|
+
type ItemTypeFromOptions<O extends CalendarOptions<any>> = O extends CalendarOptions<infer TItem> ? TItem : never;
|
|
173
|
+
/**
|
|
174
|
+
* Base props for components that receive a Calendar
|
|
175
|
+
* @example
|
|
176
|
+
* function MyComponent<C extends Calendar<any, any>>(
|
|
177
|
+
* props: CalendarComponentProps<C>
|
|
178
|
+
* ) { ... }
|
|
179
|
+
*/
|
|
180
|
+
type CalendarComponentProps<C extends Calendar<any, any>> = {
|
|
181
|
+
calendar: C;
|
|
182
|
+
};
|
|
183
|
+
/**
|
|
184
|
+
* Require that a Calendar has month view configured
|
|
185
|
+
* @example
|
|
186
|
+
* function MonthView<C extends Calendar<any, any>>(
|
|
187
|
+
* props: { calendar: RequireMonthView<C> }
|
|
188
|
+
* ) {
|
|
189
|
+
* // calendar.getMonth() is guaranteed to exist
|
|
190
|
+
* }
|
|
191
|
+
*/
|
|
192
|
+
type RequireMonthView<C extends Calendar<any, any>> = HasViewType<C, 'month'> extends true ? C : never;
|
|
193
|
+
/**
|
|
194
|
+
* Require that a Calendar has week view configured
|
|
195
|
+
*/
|
|
196
|
+
type RequireWeekView<C extends Calendar<any, any>> = HasViewType<C, 'week'> extends true ? C : never;
|
|
197
|
+
/**
|
|
198
|
+
* Require that a Calendar has day view configured
|
|
199
|
+
*/
|
|
200
|
+
type RequireDayView<C extends Calendar<any, any>> = HasViewType<C, 'day'> extends true ? C : never;
|
|
143
201
|
|
|
144
202
|
declare function functionalUpdate<T>(updater: T | ((old: T) => T), input: T): T;
|
|
145
203
|
declare function createCalendarAccessor<T, A extends CalendarAccessor<T> = CalendarAccessor<T>>(accessor: A): A;
|
|
@@ -234,12 +292,12 @@ declare function getWeekDateRange(date: Temporal.PlainDate, timeZone: string, op
|
|
|
234
292
|
*/
|
|
235
293
|
declare function getDayDateRange(date: Temporal.PlainDate, timeZone: string): DateRange;
|
|
236
294
|
|
|
237
|
-
declare function createCalendar<
|
|
295
|
+
declare function createCalendar<TItem, TOptions extends CalendarOptions<TItem>>(options: TOptions): Calendar<TItem, TOptions>;
|
|
238
296
|
|
|
239
|
-
declare function createCalendarViews<
|
|
240
|
-
month?: MonthViewOptions<
|
|
241
|
-
week?: WeekViewOptions<
|
|
242
|
-
day?: DayViewOptions<
|
|
243
|
-
}>(views:
|
|
297
|
+
declare function createCalendarViews<TItem>(): <const TViews extends {
|
|
298
|
+
month?: MonthViewOptions<TItem>;
|
|
299
|
+
week?: WeekViewOptions<TItem>;
|
|
300
|
+
day?: DayViewOptions<TItem>;
|
|
301
|
+
}>(views: TViews) => TViews;
|
|
244
302
|
|
|
245
|
-
export { type Calendar, type CalendarAccessor, type CalendarDay, type CalendarDayView, type CalendarMonth, type CalendarOptions, type CalendarState, type CalendarViewOptions, type CalendarWeek, type CalendarWeekView, type DateRange, type DateRangeBounds, type DayViewOptions, type InferViewConfig, type InferViewNames, type MonthViewOptions, type TimeSlot, type Updater, type WeekDay, type WeekViewOptions, buildDay, buildMonth, buildWeek, convertToTimezone, createCalendar, createCalendarAccessor, createCalendarViews, createZonedDateTime, formatTime, functionalUpdate, getCurrentTimeZone, getDayDateRange, getDayRange, getEventPosition, getMonthDateRange, getMonthName, getMonthRange, getTimeSlotHeight, getTimezoneOffset, getWeekDateRange, getWeekRange, getWeekdays, goToToday, nextDay, nextMonth, nextWeek, previousDay, previousMonth, previousWeek };
|
|
303
|
+
export { type Calendar, type CalendarAccessor, type CalendarComponentProps, type CalendarDay, type CalendarDayView, type CalendarItemType, type CalendarMonth, type CalendarOptions, type CalendarState, type CalendarViewOptions, type CalendarViewsConfig, type CalendarWeek, type CalendarWeekView, type DateRange, type DateRangeBounds, type DayViewOptions, type HasViewType, type InferViewConfig, type InferViewNames, type ItemTypeFromOptions, type MonthViewOptions, type RequireDayView, type RequireMonthView, type RequireWeekView, type TimeSlot, type Updater, type ValidViewNames, type WeekDay, type WeekViewOptions, buildDay, buildMonth, buildWeek, convertToTimezone, createCalendar, createCalendarAccessor, createCalendarViews, createZonedDateTime, formatTime, functionalUpdate, getCurrentTimeZone, getDayDateRange, getDayRange, getEventPosition, getMonthDateRange, getMonthName, getMonthRange, getTimeSlotHeight, getTimezoneOffset, getWeekDateRange, getWeekRange, getWeekdays, goToToday, nextDay, nextMonth, nextWeek, previousDay, previousMonth, previousWeek };
|
package/dist/index.js
CHANGED
|
@@ -607,6 +607,16 @@ function createCalendar(options) {
|
|
|
607
607
|
setOptions(updater) {
|
|
608
608
|
_options = functionalUpdate(updater, _options);
|
|
609
609
|
},
|
|
610
|
+
// Type predicates for runtime view checking
|
|
611
|
+
hasMonthView() {
|
|
612
|
+
return "month" in _options.views && _options.views.month !== void 0;
|
|
613
|
+
},
|
|
614
|
+
hasWeekView() {
|
|
615
|
+
return "week" in _options.views && _options.views.week !== void 0;
|
|
616
|
+
},
|
|
617
|
+
hasDayView() {
|
|
618
|
+
return "day" in _options.views && _options.views.day !== void 0;
|
|
619
|
+
},
|
|
610
620
|
store
|
|
611
621
|
};
|
|
612
622
|
return calendar;
|