Skip to content

Commit

Permalink
feat: allow providing Date to min/max and selected dates
Browse files Browse the repository at this point in the history
- in theory it also allow epoch timestamp
  • Loading branch information
ghiscoding committed Apr 23, 2024
1 parent 1f718f5 commit 70ded97
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
34 changes: 19 additions & 15 deletions docs/en/reference/additionally/settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,19 @@ This parameter sets the start of the week in accordance with the international s

## settings.range.min

`Type: String`
`Type: String | Date`

`Default: '1970-01-01'`

`Options: 'YYYY-MM-DD'`
`Options: 'YYYY-MM-DD' | Date`

```js
new VanillaCalendar('#calendar', {
settings: {
range: {
min: '2022-07-01',
// or
min: new Date('2018-01-01'),
}
},
});
Expand All @@ -70,17 +72,19 @@ This parameter sets the minimum date that the user can choose. Dates earlier tha

## settings.range.max

`Type: String`
`Type: String | Date`

`Default: '2470-12-31'`

`Options: 'YYYY-MM-DD'`
`Options: 'YYYY-MM-DD' | Date`

```js
new VanillaCalendar('#calendar', {
settings: {
range: {
max: '2024-07-01',
// or
max: new Date('2018-01-01'),
}
},
});
Expand Down Expand Up @@ -185,17 +189,17 @@ This parameter allows you to disable specified weekdays. Specify an array with n

## settings.range.disabled

`Type: String[]`
`Type: String[] | Date[]`

`Default: null`

`Options: ['YYYY-MM-DD'] | null`
`Options: ['YYYY-MM-DD'] | [Date] | null`

```js
new VanillaCalendar('#calendar', {
settings: {
range: {
disabled: ['2022-08-10:2022-08-15', '2022-08-20'],
disabled: ['2022-08-10:2022-08-15', '2022-08-20', new Date()],
}
},
});
Expand All @@ -215,13 +219,13 @@ This parameter allows you to disable specific dates regardless of the specified

`Default: null`

`Options: ['YYYY-MM-DD'] | null`
`Options: ['YYYY-MM-DD'] | [Date] | null`

```js
new VanillaCalendar('#calendar', {
settings: {
range: {
enabled: ['2022-08-11:2022-08-16', '2022-08-20'],
enabled: ['2022-08-11:2022-08-16', '2022-08-20', new Date()],
}
},
});
Expand Down Expand Up @@ -413,17 +417,17 @@ This option allows you to enable/disable cancellation of the selected date by pr

## settings.selected.dates

`Type: String[]`
`Type: String[] | Date[]`

`Default: null`

`Options: ['YYYY-MM-DD'] | null`
`Options: ['YYYY-MM-DD'] | [Date] | null`

```js
new VanillaCalendar('#calendar', {
settings: {
selected: {
dates: ['2022-08-10:2022-08-15', '2022-08-20'],
dates: ['2022-08-10:2022-08-15', '2022-08-20', new Date()],
},
},
});
Expand Down Expand Up @@ -483,17 +487,17 @@ This parameter determines the year that will be displayed when the calendar is i

## settings.selected.holidays

`Type: String[]`
`Type: String[] | Date[]`

`Default: null`

`Options: ['YYYY-MM-DD'] | null`
`Options: ['YYYY-MM-DD'] | [Date] | null`

```js
new VanillaCalendar('#calendar', {
settings: {
selected: {
holidays: ['2022-08-10:2022-08-15', '2022-08-20'],
holidays: ['2022-08-10:2022-08-15', '2022-08-20', new Date()],
},
},
});
Expand Down
8 changes: 6 additions & 2 deletions docs/en/reference/main/main-settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ The `jumpMonths` parameter controls the number of months to jump.

## date.min

`Type: String`
`Type: String | Date`

`Default: '1970-01-01'`

Expand All @@ -86,6 +86,8 @@ The `jumpMonths` parameter controls the number of months to jump.
new VanillaCalendar('#calendar', {
date: {
min: '1970-01-01',
// or
min: new Date('2018-01-01'),
},
});
```
Expand All @@ -96,7 +98,7 @@ The `date.min` parameter sets the minimum allowable date that the calendar will

## date.max

`Type: String`
`Type: String | Date`

`Default: '2470-12-31'`

Expand All @@ -106,6 +108,8 @@ The `date.min` parameter sets the minimum allowable date that the calendar will
new VanillaCalendar('#calendar', {
date: {
max: '2470-12-31',
// or
max: new Date('2018-01-01'),
},
});
```
Expand Down
7 changes: 5 additions & 2 deletions package/src/scripts/helpers/parseDates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import { FormatDateString } from '@package/types';
import getDateString from '@scripts/helpers/getDateString';
import getDate from '@scripts/helpers/getDate';

const parseDates = (dates: string[]): FormatDateString[] => dates.reduce((accumulator: FormatDateString[], date) => {
if (date.match(/^(\d{4}-\d{2}-\d{2})$/g)) {
const parseDates = (dates: Array<number | string | Date>): FormatDateString[] => dates.reduce((accumulator: FormatDateString[], date) => {
if (date instanceof Date || typeof date === 'number') {
const d = date instanceof Date ? date : new Date(date);
accumulator.push(d.toISOString().substring(0, 10) as FormatDateString);
} else if (date.match(/^(\d{4}-\d{2}-\d{2})$/g)) {
accumulator.push(date as FormatDateString);
} else {
date.replace(/(\d{4}-\d{2}-\d{2}).*?(\d{4}-\d{2}-\d{2})/g, (_, startDateStr, endDateStr) => {
Expand Down
2 changes: 1 addition & 1 deletion package/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface ISelection {
}

export interface ISelected {
dates?: string[];
dates?: Array<Date | number | string>;
month?: number;
year?: number;
holidays?: string[];
Expand Down

0 comments on commit 70ded97

Please sign in to comment.