react-modular-datepicker 0.0.3 → 0.0.4
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/README.md +132 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -17,6 +17,20 @@ A modular, lightweight, and type-safe React datepicker library. Whether you need
|
|
|
17
17
|
|
|
18
18
|
---
|
|
19
19
|
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install react-modular-datepicker dayjs
|
|
24
|
+
# or
|
|
25
|
+
pnpm add react-modular-datepicker dayjs
|
|
26
|
+
# or
|
|
27
|
+
yarn add react-modular-datepicker dayjs
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
> **Note on Peer Dependencies**: `dayjs` is configured as an optional peer dependency. By default, `react-modular-datepicker` uses the built-in `DayjsAdapter`, so `dayjs` must be installed in your project. However, if you bring your own date management library (such as `date-fns` or Luxon), you can implement a custom `DateAdapter` and pass it to the `adapter` prop without needing `dayjs`.
|
|
31
|
+
|
|
32
|
+
---
|
|
33
|
+
|
|
20
34
|
## API Reference
|
|
21
35
|
|
|
22
36
|
### Imports
|
|
@@ -90,6 +104,29 @@ Represents a month grid.
|
|
|
90
104
|
- `year`: `number`
|
|
91
105
|
- `weeks`: `(DateObj | null)[][]`
|
|
92
106
|
|
|
107
|
+
#### `DateAdapter`
|
|
108
|
+
Pluggable interface for date operations:
|
|
109
|
+
```typescript
|
|
110
|
+
export interface DateAdapter<T = any> {
|
|
111
|
+
date(value?: any): T;
|
|
112
|
+
add(date: T, amount: number, unit: 'day' | 'month' | 'year'): T;
|
|
113
|
+
subtract(date: T, amount: number, unit: 'day' | 'month' | 'year'): T;
|
|
114
|
+
startOf(date: T, unit: 'day' | 'month' | 'year'): T;
|
|
115
|
+
endOf(date: T, unit: 'day' | 'month' | 'year'): T;
|
|
116
|
+
isBefore(date: T, comparison: T, unit?: 'day' | 'month' | 'year'): boolean;
|
|
117
|
+
isAfter(date: T, comparison: T, unit?: 'day' | 'month' | 'year'): boolean;
|
|
118
|
+
isSame(date: T, comparison: T, unit?: 'day' | 'month' | 'year'): boolean;
|
|
119
|
+
set(date: T, unit: 'day' | 'month' | 'year', value: number): T;
|
|
120
|
+
get(date: T, unit: 'day' | 'month' | 'year'): number;
|
|
121
|
+
format(date: T, formatStr: string, locale?: string): string;
|
|
122
|
+
getDaysInMonth(date: T): number;
|
|
123
|
+
toDate(date: T): Date;
|
|
124
|
+
diff(date: T, comparison: T, unit: 'month' | 'year'): number;
|
|
125
|
+
getMonths(locale?: string): string[];
|
|
126
|
+
getWeekdays(locale?: string): string[];
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
93
130
|
---
|
|
94
131
|
|
|
95
132
|
## Recipes
|
|
@@ -363,6 +400,101 @@ const { calendars, getBackProps, getForwardProps, getDateProps } = useDates({
|
|
|
363
400
|
|
|
364
401
|
</details>
|
|
365
402
|
|
|
403
|
+
<details>
|
|
404
|
+
<summary><b>Custom Date Adapter (e.g. date-fns)</b></summary>
|
|
405
|
+
Replace the default `DayjsAdapter` with a custom adapter using `date-fns` (or any date library).
|
|
406
|
+
|
|
407
|
+
```tsx
|
|
408
|
+
import { Calendar, DateAdapter } from 'react-modular-datepicker';
|
|
409
|
+
import {
|
|
410
|
+
addDays, addMonths, addYears,
|
|
411
|
+
subDays, subMonths, subYears,
|
|
412
|
+
startOfDay, startOfMonth, startOfYear,
|
|
413
|
+
endOfDay, endOfMonth, endOfYear,
|
|
414
|
+
isBefore, isAfter, isSameDay, isSameMonth, isSameYear,
|
|
415
|
+
setDate, setMonth, setYear,
|
|
416
|
+
getDate, getMonth, getYear,
|
|
417
|
+
format, getDaysInMonth,
|
|
418
|
+
differenceInCalendarMonths, differenceInCalendarYears
|
|
419
|
+
} from 'date-fns';
|
|
420
|
+
|
|
421
|
+
class DateFnsAdapter implements DateAdapter<Date> {
|
|
422
|
+
date(value?: any): Date { return value ? new Date(value) : new Date(); }
|
|
423
|
+
add(date: Date, amount: number, unit: 'day' | 'month' | 'year'): Date {
|
|
424
|
+
if (unit === 'day') return addDays(date, amount);
|
|
425
|
+
if (unit === 'month') return addMonths(date, amount);
|
|
426
|
+
return addYears(date, amount);
|
|
427
|
+
}
|
|
428
|
+
subtract(date: Date, amount: number, unit: 'day' | 'month' | 'year'): Date {
|
|
429
|
+
if (unit === 'day') return subDays(date, amount);
|
|
430
|
+
if (unit === 'month') return subMonths(date, amount);
|
|
431
|
+
return subYears(date, amount);
|
|
432
|
+
}
|
|
433
|
+
startOf(date: Date, unit: 'day' | 'month' | 'year'): Date {
|
|
434
|
+
if (unit === 'day') return startOfDay(date);
|
|
435
|
+
if (unit === 'month') return startOfMonth(date);
|
|
436
|
+
return startOfYear(date);
|
|
437
|
+
}
|
|
438
|
+
endOf(date: Date, unit: 'day' | 'month' | 'year'): Date {
|
|
439
|
+
if (unit === 'day') return endOfDay(date);
|
|
440
|
+
if (unit === 'month') return endOfMonth(date);
|
|
441
|
+
return endOfYear(date);
|
|
442
|
+
}
|
|
443
|
+
isBefore(date: Date, comparison: Date, unit?: 'day' | 'month' | 'year'): boolean {
|
|
444
|
+
if (unit === 'day') return startOfDay(date) < startOfDay(comparison);
|
|
445
|
+
if (unit === 'month') return startOfMonth(date) < startOfMonth(comparison);
|
|
446
|
+
if (unit === 'year') return startOfYear(date) < startOfYear(comparison);
|
|
447
|
+
return date < comparison;
|
|
448
|
+
}
|
|
449
|
+
isAfter(date: Date, comparison: Date, unit?: 'day' | 'month' | 'year'): boolean {
|
|
450
|
+
if (unit === 'day') return startOfDay(date) > startOfDay(comparison);
|
|
451
|
+
if (unit === 'month') return startOfMonth(date) > startOfMonth(comparison);
|
|
452
|
+
if (unit === 'year') return startOfYear(date) > startOfYear(comparison);
|
|
453
|
+
return date > comparison;
|
|
454
|
+
}
|
|
455
|
+
isSame(date: Date, comparison: Date, unit?: 'day' | 'month' | 'year'): boolean {
|
|
456
|
+
if (unit === 'day') return isSameDay(date, comparison);
|
|
457
|
+
if (unit === 'month') return isSameMonth(date, comparison);
|
|
458
|
+
if (unit === 'year') return isSameYear(date, comparison);
|
|
459
|
+
return date.getTime() === comparison.getTime();
|
|
460
|
+
}
|
|
461
|
+
set(date: Date, unit: 'day' | 'month' | 'year', value: number): Date {
|
|
462
|
+
if (unit === 'day') return setDate(date, value);
|
|
463
|
+
if (unit === 'month') return setMonth(date, value);
|
|
464
|
+
return setYear(date, value);
|
|
465
|
+
}
|
|
466
|
+
get(date: Date, unit: 'day' | 'month' | 'year'): number {
|
|
467
|
+
if (unit === 'day') return getDate(date);
|
|
468
|
+
if (unit === 'month') return getMonth(date);
|
|
469
|
+
return getYear(date);
|
|
470
|
+
}
|
|
471
|
+
format(date: Date, formatStr: string): string {
|
|
472
|
+
const f = formatStr.replace(/YYYY/g, 'yyyy').replace(/YY/g, 'yy').replace(/D/g, 'd');
|
|
473
|
+
return format(date, f);
|
|
474
|
+
}
|
|
475
|
+
getDaysInMonth(date: Date): number { return getDaysInMonth(date); }
|
|
476
|
+
toDate(date: Date): Date { return date; }
|
|
477
|
+
diff(date: Date, comparison: Date, unit: 'month' | 'year'): number {
|
|
478
|
+
if (unit === 'month') return differenceInCalendarMonths(date, comparison);
|
|
479
|
+
return differenceInCalendarYears(date, comparison);
|
|
480
|
+
}
|
|
481
|
+
getMonths(): string[] {
|
|
482
|
+
return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
|
|
483
|
+
}
|
|
484
|
+
getWeekdays(): string[] {
|
|
485
|
+
return ['SUN', 'MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT'];
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const customAdapter = new DateFnsAdapter();
|
|
490
|
+
|
|
491
|
+
function CustomAdapterExample() {
|
|
492
|
+
return <Calendar adapter={customAdapter} onChange={(date) => console.log(date)} />;
|
|
493
|
+
}
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
</details>
|
|
497
|
+
|
|
366
498
|
---
|
|
367
499
|
|
|
368
500
|
## Contributors
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-modular-datepicker",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.4",
|
|
4
|
+
"description": "A modular, lightweight, and type-safe React datepicker and calendar component with headless hooks for single, multiple, and range selection.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react",
|
|
7
7
|
"react-component",
|