Skip to content

Commit a9b5f54

Browse files
Merge pull request #275 from ghiscoding/feat/edges-only
feat: add new `edgesOnly` to return only start/end dates in selection range
2 parents 501f311 + 147045c commit a9b5f54

File tree

6 files changed

+85
-2
lines changed

6 files changed

+85
-2
lines changed

docs/en/reference/additionally/settings.mdx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ new VanillaCalendar('#calendar', {
9797
// or
9898
max: new Date('2018-01-01'),
9999
// or
100-
ma: 'today',
100+
max: 'today',
101101
}
102102
},
103103
});
@@ -115,6 +115,32 @@ This parameter sets the maximum date that the user can choose. Dates later than
115115

116116
---
117117

118+
## settings.range.edgesOnly
119+
120+
`Type: Boolean`
121+
122+
`Default: false`
123+
124+
`Options: true | false`
125+
126+
```js
127+
new VanillaCalendar('#calendar', {
128+
settings: {
129+
range: {
130+
edgesOnly: true,
131+
}
132+
},
133+
});
134+
```
135+
136+
This parameter will keep references of the date range edges only, which are the Start and End dates in `settings.selected.dates` but nothing in between the range. It only works when <a href="/docs/reference/additionally/settings#selection-day">`settings.selection.day`</a> is set to `'multiple-ranged'`.
137+
138+
<Info>
139+
It's important to note that with this setting, disabling dates within the date range will have no effect. So make sure to use this parameter when you are interested with only the start and end date but nothing in-between.
140+
</Info>
141+
142+
---
143+
118144
## settings.range.disablePast
119145

120146
`Type: Boolean`

docs/ru/reference/additionally/settings.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,32 @@ new VanillaCalendar('#calendar', {
115115

116116
---
117117

118+
## settings.range.edgesOnly
119+
120+
`Type: Boolean`
121+
122+
`Default: false`
123+
124+
`Options: true | false`
125+
126+
```js
127+
new VanillaCalendar('#calendar', {
128+
settings: {
129+
range: {
130+
edgesOnly: true,
131+
}
132+
},
133+
});
134+
```
135+
136+
Этот параметр позволяет получить только начальную и конечную выбранную пользователем дату, игнорируя промежуточные даты. Этот параметр работает только в том случае, если для <a href="/docs/reference/additionally/settings#selection-day">`settings.selection.day`</a> установлено значение `'multiple-ranged'`.
137+
138+
<Info>
139+
Важно отметить, что при использовании этой настройки отключение дат в пределах диапазона дат не будет иметь никакого эффекта. Поэтому обязательно используйте этот параметр, если вас интересуют только начальная и конечная выбранная пользователем дата.
140+
</Info>
141+
142+
---
143+
118144
## settings.range.disablePast
119145

120146
`Type: Boolean`

package/src/scripts/default.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default class DefaultOptionsCalendar {
2626
max: this.date.max,
2727
disablePast: false,
2828
disableGaps: false,
29+
edgesOnly: false,
2930
disableAllDays: false,
3031
disableWeekday: undefined,
3132
disabled: undefined,

package/src/scripts/handles/handleDayRangedSelection.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ const handleDayRangedSelection = (self: VanillaCalendar, formattedDate?: FormatD
128128
reset: () => {
129129
const [startDate, endDate] = [self.selectedDates[0], self.selectedDates[self.selectedDates.length - 1]];
130130
self.selectedDates = self.selectedDates[0] !== self.selectedDates[self.selectedDates.length - 1]
131-
? parseDates([`${startDate as string}:${endDate as string}`])
131+
? self.settings.range.edgesOnly
132+
? [startDate, endDate]
133+
: parseDates([`${startDate as string}:${endDate as string}`])
132134
: [self.selectedDates[0], self.selectedDates[0]];
133135
self.HTMLElement.removeEventListener('mousemove', handleHoverDaysEvent);
134136
document.removeEventListener('keydown', handleCancelSelectionDays);

package/src/scripts/methods/createDays.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ const setDayModifier = (
6565
}
6666
}
6767
}
68+
69+
// when using multiple-ranged with range edges only (only includes start/end selected dates)
70+
if (self.settings.range.edgesOnly && self.selectedDates.length > 1 && self.settings.selection.day === 'multiple-ranged') {
71+
const firstDate = +new Date(self.selectedDates[0]);
72+
const lastDate = +new Date(self.selectedDates[self.selectedDates.length - 1]);
73+
const nextDate = +new Date(date);
74+
75+
if (nextDate > firstDate && nextDate < lastDate) {
76+
dayBtnEl.classList.add(self.CSSClasses.dayBtnSelected);
77+
dayEl.classList.add(self.CSSClasses.daySelectedIntermediate);
78+
}
79+
}
6880
};
6981

7082
const createDay = (

package/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,29 @@ export interface IDates {
1616
}
1717

1818
export interface IRange {
19+
/** This parameter sets the minimum date that the user can choose */
1920
min: FormatDateString | 'today';
21+
/** This parameter sets the maximum date that the user can choose */
2022
max: FormatDateString | 'today';
23+
/** This parameter disables all past days. */
2124
disablePast: boolean;
25+
/**
26+
* This parameter disables the selection of days within a range with disabled dates.
27+
* Only works when `settings.selection.day` is set to `'multiple-ranged'`.
28+
*/
2229
disableGaps: boolean;
30+
/**
31+
* This parameter will only keep references of the date range edges (start/end dates) in the `settings.selected.dates` array.
32+
* Only works when `settings.selection.day` is set to `'multiple-ranged'`.
33+
*/
34+
edgesOnly?: boolean;
35+
/** This parameter disables all days and can be useful when using `settings.range.enabled` */
2336
disableAllDays: boolean;
37+
/** This parameter allows you to disable specified weekdays. */
2438
disableWeekday?: number[];
39+
/** This parameter allows you to disable specific dates regardless of the specified range. */
2540
disabled?: string[];
41+
/** This parameter allows you to enable specific dates regardless of the range and disabled dates. */
2642
enabled?: string[];
2743
}
2844

0 commit comments

Comments
 (0)