Skip to content

Commit

Permalink
update setDateModifier.ts, handleHoverDatesEvent.ts, toggleHoverEffec…
Browse files Browse the repository at this point in the history
…t.ts and layout.css
  • Loading branch information
uvarov-frontend committed Dec 17, 2024
1 parent 5ea7001 commit 7b3c8b4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
10 changes: 8 additions & 2 deletions package/src/scripts/creators/createDates/setDateModifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ const setDateModifier = (
dateEl.setAttribute('data-vc-date-selected', '');
if (dateBtnEl) dateBtnEl.setAttribute('aria-selected', 'true');
if (self.context.selectedDates.length > 1 && self.selectionDatesMode === 'multiple-ranged') {
if (self.context.selectedDates[0] === dateStr) dateEl.setAttribute('data-vc-date-selected', 'first');
if (self.context.selectedDates[self.context.selectedDates.length - 1] === dateStr) dateEl.setAttribute('data-vc-date-selected', 'last');
if (self.context.selectedDates[0] === dateStr && self.context.selectedDates[self.context.selectedDates.length - 1] === dateStr) {
dateEl.setAttribute('data-vc-date-selected', 'first-and-last');
} else if (self.context.selectedDates[0] === dateStr) {
dateEl.setAttribute('data-vc-date-selected', 'first');
} else if (self.context.selectedDates[self.context.selectedDates.length - 1] === dateStr) {
dateEl.setAttribute('data-vc-date-selected', 'last');
}

if (self.context.selectedDates[0] !== dateStr && self.context.selectedDates[self.context.selectedDates.length - 1] !== dateStr)
dateEl.setAttribute('data-vc-date-selected', 'middle');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const handleHoverDatesEvent = (e: MouseEvent) => {
const startDate = getDate(state.self.context.selectedDates[0]);
const endDate = getDate(lastDateString);

const firstDateEl = state.self.context.mainElement.querySelector<HTMLElement>(`[data-vc-date="${state.self.context.selectedDates[0]}"]`);
const lastDateEl = state.self.context.mainElement.querySelector<HTMLElement>(`[data-vc-date="${lastDateString}"]`);
const firstDateEls = state.self.context.mainElement.querySelectorAll<HTMLElement>(`[data-vc-date="${state.self.context.selectedDates[0]}"]`);
const lastDateEls = state.self.context.mainElement.querySelectorAll<HTMLElement>(`[data-vc-date="${lastDateString}"]`);

const [firstDateElCorrect, lastDateElCorrect] = startDate < endDate ? [firstDateEl, lastDateEl] : [lastDateEl, firstDateEl];
const [firstDateElCorrect, lastDateElCorrect] = startDate < endDate ? [firstDateEls, lastDateEls] : [lastDateEls, firstDateEls];
const [start, end] = startDate < endDate ? [startDate, endDate] : [endDate, startDate];

for (let i = new Date(start); i <= end; i.setDate(i.getDate() + 1)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import state from '@scripts/handles/handleSelectDateRange/state';
import getDateString from '@scripts/utils/getDateString';

export const addHoverEffect = (date: Date, firstDateEl: HTMLElement | null, lastDateEl: HTMLElement | null) => {
export const addHoverEffect = (date: Date, firstDateEls: NodeListOf<HTMLElement>, lastDateEls: NodeListOf<HTMLElement>) => {
if (!state.self?.context?.selectedDates[0]) return;

const formattedDate = getDateString(date);
if (state.self.context.disableDates?.includes(formattedDate)) return;

state.self.context.mainElement.querySelectorAll<HTMLElement>(`[data-vc-date="${formattedDate}"]`).forEach((d) => (d.dataset.vcDateHover = ''));
if (firstDateEl) firstDateEl.dataset.vcDateHoverFirst = '';
if (lastDateEl) lastDateEl.dataset.vcDateHoverLast = '';
firstDateEls.forEach((d) => (d.dataset.vcDateHover = 'first'));
lastDateEls.forEach((d) => {
if (d.dataset.vcDateHover === 'first') {
d.dataset.vcDateHover = 'first-and-last';
} else {
d.dataset.vcDateHover = 'last';
}
});
};

export const removeHoverEffect = () => {
if (!state.self?.context?.mainElement) return;
const dateEls = state.self.context.mainElement.querySelectorAll<HTMLElement>('[data-vc-date-hover], [data-vc-date-hover-first], [data-vc-date-hover-last]');
dateEls.forEach((d) => {
d.removeAttribute('data-vc-date-hover');
d.removeAttribute('data-vc-date-hover-first');
d.removeAttribute('data-vc-date-hover-last');
});
const dateEls = state.self.context.mainElement.querySelectorAll<HTMLElement>('[data-vc-date-hover]');
dateEls.forEach((d) => d.removeAttribute('data-vc-date-hover'));
};
12 changes: 6 additions & 6 deletions package/src/styles/layout.css
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,23 @@ button[data-vc-week-day] {
@apply rounded-none;
}

[data-vc-date][data-vc-date-hover-first] [data-vc-date-btn] {
[data-vc-date][data-vc-date-hover='first'] [data-vc-date-btn] {
@apply rounded-r-none rounded-l-lg;
}

[data-vc-date][data-vc-date-hover-last] [data-vc-date-btn] {
[data-vc-date][data-vc-date-hover='last'] [data-vc-date-btn] {
@apply rounded-l-none rounded-r-lg;
}

[data-vc-date][data-vc-date-hover-first][data-vc-date-hover-last] [data-vc-date-btn] {
[data-vc-date][data-vc-date-hover='first-and-last'] [data-vc-date-btn] {
@apply rounded-lg;
}

[data-vc-date][data-vc-date-hover-first][data-vc-date-selected] [data-vc-date-btn] {
[data-vc-date][data-vc-date-hover='first'][data-vc-date-selected] [data-vc-date-btn] {
@apply rounded-l-lg;
}

[data-vc-date][data-vc-date-hover-last][data-vc-date-selected] [data-vc-date-btn] {
[data-vc-date][data-vc-date-hover='last'][data-vc-date-selected] [data-vc-date-btn] {
@apply rounded-r-lg;
}

Expand All @@ -174,7 +174,7 @@ button[data-vc-week-day] {
@apply rounded-l-none rounded-r-lg;
}

[data-vc-date][data-vc-date-selected='first'][data-vc-date-selected='last'] [data-vc-date-btn] {
[data-vc-date][data-vc-date-selected='first-and-last'] [data-vc-date-btn] {
@apply rounded-l-lg rounded-r-lg;
}

Expand Down

0 comments on commit 7b3c8b4

Please sign in to comment.