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: add jumpToSelectedDate option #251

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 26 additions & 0 deletions docs/en/reference/main/main-settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ The `jumpMonths` parameter controls the number of months to jump.

- - -

## jumpToSelectedDate

`Type: Boolean`

`Default: false`

`Options: true | false`

```js
new VanillaCalendar('#calendar', {
jumpToSelectedDate: true,
settings: {
selected: {
dates: ['2018-05-02'],
}
},
});
```

When the option is enabled and 1 or more selected date(s) are provided but without providing `selected.month` and `selected.year`, it will make the calendar jump to the first selected date. If set to `false`, the calendar will always open to the current month and year.
<Info>
This option has no effect when `selected.month` and `selected.year` are provided.
</Info>

- - -

## date.min

`Type: String`
Expand Down
26 changes: 26 additions & 0 deletions docs/ru/reference/main/main-settings.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ new VanillaCalendar('#calendar', {

- - -

## jumpToSelectedDate

`Type: Boolean`

`Default: false`

`Options: true | false`

```js
new VanillaCalendar('#calendar', {
jumpToSelectedDate: true,
settings: {
selected: {
dates: ['2018-05-02'],
}
},
});
```

Если эта опция включена и указаны одна или несколько выбранных дат, но без указания `selected.month` и `selected.year`, календарь перейдет к первой выбранной дате. Если установлено значение `false`, календарь всегда будет открываться для текущего месяца и года.
<Info>
Эта опция не имеет эффекта, если указаны `selected.month` и `selected.year`.
</Info>

- - -

## date.min

`Type: String`
Expand Down
1 change: 1 addition & 0 deletions package/src/scripts/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class DefaultOptionsCalendar {
type: T.TypesCalendar = 'default';
months = 2;
jumpMonths = 1;
jumpToSelectedDate = false;
date: T.IDates = {
min: '1970-01-01',
max: '2470-12-31',
Expand Down
5 changes: 5 additions & 0 deletions package/src/scripts/helpers/setVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import getDate from '@scripts/helpers/getDate';
import messages from './getMessages';

const initSelectedMonthYear = (self: VanillaCalendar) => {
if (self.jumpToSelectedDate && self.settings.selected.dates?.length && (self.settings.selected.month === undefined && self.settings.selected.year === undefined)) {
const selectedDate = getDate(parseDates(self.settings.selected.dates)[0]);
self.settings.selected.month = selectedDate.getMonth();
self.settings.selected.year = selectedDate.getFullYear();
}
const isValidMonth = self.settings.selected.month !== undefined && Number(self.settings.selected.month) >= 0 && Number(self.settings.selected.month) < 12;
const isValidYear = self.settings.selected.year !== undefined && Number(self.settings.selected.year) >= 0 && Number(self.settings.selected.year) <= 9999;

Expand Down
1 change: 1 addition & 0 deletions package/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export interface IOptions {
type?: TypesCalendar;
months?: number;
jumpMonths?: number;
jumpToSelectedDate?: boolean;
date?: Partial<IDates>;
settings?: Partial<{
lang: string;
Expand Down