Skip to content

Commit

Permalink
Merge pull request #131 from uvarov-frontend/feature/enhancement_input
Browse files Browse the repository at this point in the history
Feature/enhancement input
  • Loading branch information
uvarov-frontend authored Oct 10, 2023
2 parents 8781ebb + 4111317 commit 2c52aac
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 29 deletions.
6 changes: 3 additions & 3 deletions docs/en/reference/additionally/actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,19 @@ The method is triggered after changing the time in any way. The parameters that

`Default: null`

`Options: changeToInput(e, HTMLInputElement, dates, time, hours, minutes, keeping) => void | null`
`Options: changeToInput(e, calendar, dates, time, hours, minutes, keeping) => void | null`

```js
new VanillaCalendar('#calendar', {
actions: {
changeToInput(e, HTMLInputElement, dates, time, hours, minutes, keeping) {},
changeToInput(e, calendar, dates, time, hours, minutes, keeping) {},
},
});
```
For this method to work, the parameter <a href="/docs/reference/main/main-settings#input">`input`</a> with the value `true` is required.

The method is triggered after clicking on a day in the calendar or changing the time in any way.
Parameters that can be received: `e` - event, `HTMLInputElement` - HTML element that is an input field, `dates` - array of selected dates, `time` - selected time, `hours` - selected hour, `minutes ` is the selected minutes, `keeping` is the selected AM/PM marker.
Parameters that can be received: `e` - event, `calendar` - an object containing `HTMLInputElement`, `HTMLElement` and methods `show()`, `hide()`, `dates` - array of selected dates, `time` - selected time, `hours` - selected hour, `minutes ` is the selected minutes, `keeping` is the selected AM/PM marker.

---

Expand Down
1 change: 0 additions & 1 deletion docs/en/reference/additionally/css-classes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ new VanillaCalendar('#calendar', {
calendarYear: 'vanilla-calendar_year',
calendarHidden: 'vanilla-calendar_hidden',
calendarToInput: 'vanilla-calendar_to-input',
calendarInputWrapper: 'vanilla-calendar-input-wrapper',
controls: 'vanilla-calendar-controls',
grid: 'vanilla-calendar-grid',
gridDisabled: 'vanilla-calendar-grid_disabled',
Expand Down
6 changes: 3 additions & 3 deletions docs/ru/reference/additionally/actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ new VanillaCalendar('#calendar', {

`Default: null`

`Options: changeToInput(e, HTMLInputElement, dates, time, hours, minutes, keeping) => void | null`
`Options: changeToInput(e, calendar, dates, time, hours, minutes, keeping) => void | null`

```js
new VanillaCalendar('#calendar', {
actions: {
changeToInput(e, HTMLInputElement, dates, time, hours, minutes, keeping) {},
changeToInput(e, calendar, dates, time, hours, minutes, keeping) {},
},
});
```
Для работы этого метода, необходим параметр <a href="/docs/reference/main/main-settings#input">`input`</a> со значением `true`.

Метод срабатывает после после нажатия на день в календаре или изменения времени любым способом.
Параметры, которые можно получить: `e` - событие, `HTMLInputElement` - HTML-элемент, который является полем ввода, `dates` - массив выбранных дат, `time` - выбранное время, `hours` - выбранный час, `minutes` - выбранные минуты,`keeping` - выбранный маркер AM/PM.
Параметры, которые можно получить: `e` - событие, `calendar` - объект, содержащий `HTMLInputElement`, `HTMLElement` и методы `show()`, `hide()`, `dates` - массив выбранных дат, `time` - выбранное время, `hours` - выбранный час, `minutes` - выбранные минуты,`keeping` - выбранный маркер AM/PM.

---

Expand Down
1 change: 0 additions & 1 deletion docs/ru/reference/additionally/css-classes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ new VanillaCalendar('#calendar', {
calendarYear: 'vanilla-calendar_year',
calendarHidden: 'vanilla-calendar_hidden',
calendarToInput: 'vanilla-calendar_to-input',
calendarInputWrapper: 'vanilla-calendar-input-wrapper',
controls: 'vanilla-calendar-controls',
grid: 'vanilla-calendar-grid',
gridDisabled: 'vanilla-calendar-grid_disabled',
Expand Down
1 change: 0 additions & 1 deletion package/src/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const classes = {
calendarYear: 'vanilla-calendar_year',
calendarHidden: 'vanilla-calendar_hidden',
calendarToInput: 'vanilla-calendar_to-input',
calendarInputWrapper: 'vanilla-calendar-input-wrapper',
controls: 'vanilla-calendar-controls',
grid: 'vanilla-calendar-grid',
gridDisabled: 'vanilla-calendar-grid_disabled',
Expand Down
2 changes: 1 addition & 1 deletion package/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ declare class VanillaCalendar<T extends (HTMLElement | string), R extends Partia

readonly HTMLElement: HTMLElement | null;

readonly HTMLInputElement?: HTMLElement;
readonly HTMLInputElement?: HTMLInputElement;

readonly currentType: string;

Expand Down
23 changes: 21 additions & 2 deletions package/src/scripts/methods/clickCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,27 @@ const clickCalendar = (self: IVanillaCalendar) => {

if (self.actions.clickDay) self.actions.clickDay(e, self.selectedDates);

if (self.input && self.HTMLInputElement && self.actions.changeToInput) {
self.actions.changeToInput(e, self.HTMLInputElement, self.selectedDates, self.selectedTime, self.selectedHours, self.selectedMinutes, self.selectedKeeping);
if (self.input && self.HTMLInputElement && self.HTMLElement && self.actions.changeToInput) {
const calendar = {
hide() {
(self.HTMLElement as HTMLElement).classList.add(self.CSSClasses.calendarHidden);
},
show() {
(self.HTMLElement as HTMLElement).classList.remove(self.CSSClasses.calendarHidden);
},
HTMLInputElement: self.HTMLInputElement,
HTMLElement: self.HTMLElement as HTMLDivElement,
};

self.actions.changeToInput(
e,
calendar,
self.selectedDates,
self.selectedTime,
self.selectedHours,
self.selectedMinutes,
self.selectedKeeping,
);
}

if (dayBtnPrevEl) {
Expand Down
21 changes: 20 additions & 1 deletion package/src/scripts/methods/controlTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,26 @@ const controlTime = (self: IVanillaCalendar, keepingTime: number | false) => {
}

if (self.input && self.HTMLInputElement && self.actions.changeToInput) {
self.actions.changeToInput(e, self.HTMLInputElement, self.selectedDates, self.selectedTime, self.selectedHours, self.selectedMinutes, self.selectedKeeping);
const calendar = {
hide() {
(self.HTMLElement as HTMLElement).classList.add(self.CSSClasses.calendarHidden);
},
show() {
(self.HTMLElement as HTMLElement).classList.remove(self.CSSClasses.calendarHidden);
},
HTMLInputElement: self.HTMLInputElement as HTMLInputElement,
HTMLElement: self.HTMLElement as HTMLDivElement,
};

self.actions.changeToInput(
e,
calendar,
self.selectedDates,
self.selectedTime,
self.selectedHours,
self.selectedMinutes,
self.selectedKeeping,
);
}
};

Expand Down
9 changes: 2 additions & 7 deletions package/src/scripts/methods/createCalendarToInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ import { IVanillaCalendar } from '../../types';

const createCalendarToInput = (self: IVanillaCalendar) => {
if (!self.input || !self.HTMLElement || !self.HTMLElement.parentNode) return;
self.HTMLInputElement = self.HTMLElement as HTMLElement;
self.HTMLInputElement = self.HTMLElement as HTMLInputElement;

const wrapper = document.createElement('div');
const calendar = document.createElement('div');
wrapper.className = self.CSSClasses.calendarInputWrapper;
calendar.className = `${self.CSSClasses.calendar} ${self.CSSClasses.calendarToInput} ${self.CSSClasses.calendarHidden}`;

self.HTMLElement.parentNode.insertBefore(wrapper, self.HTMLInputElement);
wrapper.append(self.HTMLInputElement);
self.HTMLElement = calendar;
wrapper.append(self.HTMLElement);
document.body.append(self.HTMLElement);
};

export default createCalendarToInput;
6 changes: 5 additions & 1 deletion package/src/scripts/methods/handlerInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const handlerInput = (self: IVanillaCalendar) => {
if (!self || !self.input) return;
currentSelf = self;
self.HTMLInputElement?.addEventListener('click', () => {
self.HTMLElement?.classList.remove(self.CSSClasses.calendarHidden);
if (self.HTMLElement && self.HTMLInputElement) {
self.HTMLElement.style.left = `${self.HTMLInputElement.offsetLeft}px`;
self.HTMLElement.style.top = `${self.HTMLInputElement.offsetTop + self.HTMLInputElement.clientHeight}px`;
self.HTMLElement.classList.remove(self.CSSClasses.calendarHidden);
}
document.addEventListener('click', documentClickEvent, { capture: true });
});
};
Expand Down
6 changes: 1 addition & 5 deletions package/src/styles/vanilla-calendar.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
}

.vanilla-calendar_to-input {
@apply absolute left-0 top-full mt-1
}

.vanilla-calendar-input-wrapper {
@apply relative;
@apply absolute left-0 bottom-0 mt-1
}

.vanilla-calendar-controls {
Expand Down
18 changes: 15 additions & 3 deletions package/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,20 @@ export interface IActions {
clickYear: ((e: MouseEvent, year: number) => void) | null;
clickArrow: ((e: MouseEvent, year: number, month: number) => void) | null;
changeTime: ((e: Event, time: string, hours: string, minutes: string, keeping: string) => void) | null;
changeToInput: ((e: Event, HTMLInputElement: HTMLElement, dates?: string[], time?: string, hours?: string, minutes?: string, keeping?: string) => void) | null;
changeToInput: ((
e: Event,
calendar: {
hide(): void;
show(): void;
HTMLInputElement: HTMLInputElement;
HTMLElement: HTMLDivElement;
},
dates?: string[],
time?: string,
hours?: string,
minutes?: string,
keeping?: string
) => void) | null;
getDays: ((day: number, date: string, HTMLElement: HTMLElement, HTMLButtonElement: HTMLButtonElement) => void) | null;
}

Expand All @@ -101,7 +114,6 @@ export interface ICSSClasses {
calendarYear: string;
calendarHidden: string;
calendarToInput: string;
calendarInputWrapper: string;
controls: string;
grid: string;
gridDisabled: string;
Expand Down Expand Up @@ -187,7 +199,7 @@ export interface IVariables extends IOptions {
}

export interface IVanillaCalendar extends IVariables {
HTMLInputElement?: HTMLElement;
HTMLInputElement?: HTMLInputElement;
rangeMin?: FormatDateString;
rangeMax?: FormatDateString;
rangeDisabled?: FormatDateString[];
Expand Down

0 comments on commit 2c52aac

Please sign in to comment.