Calendar

import { Calendar } from '@megafon/ui-core';

Особенности использования

Колбек onChange вызывается только при ручном выборе дат в календаре, при этом, если новые даты были прокинуты через пропсы, но противоречат логике компонента (endDate является более ранней датой, чем startDate), они будут выведены согласно логике календаря

Базовое использование

май 2024
пн
вт
ср
чт
пт
сб
вс

Стилизованная обертка

май 2024
пн
вт
ср
чт
пт
сб
вс

Выбор одной даты

май 2024
пн
вт
ср
чт
пт
сб
вс

Определение недоступного диапазона дат

Недоступен выбор до определенной даты (включительно)

май 2024
пн
вт
ср
чт
пт
сб
вс

Недоступен выбор после определенной даты (включительно)

май 2024
пн
вт
ср
чт
пт
сб
вс
Демо-данные
const date = new Date();
const currentMonth = date.getMonth();
const currentYear = date.getFullYear();
const minBookingDate = new Date(currentYear, currentMonth, 7);
const maxBookingDate = new Date(currentYear, currentMonth + 1, 20);
const maxBookingDateInCurrentMonth = new Date(currentYear, currentMonth, 20);

Предвыбранный диапазон дат

Выбран диапазон дат с 07.02.2020 до 19.02.2020

февраль 2020
пн
вт
ср
чт
пт
сб
вс
Код DemoCalendarWithHandleChange
export const DemoCalendarWithHandleChange = ({ children }) => {
const [from, setFrom] = React.useState<string | null>(formatDate(startDate, 'dd.MM.yyyy'));
const [to, setTo] = React.useState<string | null>(formatDate(endDate, 'dd.MM.yyyy'));
const onChange = (currentStartDate, currentEndDate) => {
setFrom(formatDate(currentStartDate, 'dd.MM.yyyy'));
setTo(currentEndDate ? formatDate(currentEndDate, 'dd.MM.yyyy') : currentEndDate);
};
return (
<div style={{ textAlign: 'center' }}>
<h3 style={{ marginBottom: '32px' }}>
{to ? `Выбран диапазон дат с ${from} до ${to}` : `Выбрана начальная дата ${from}`}
</h3>
{children({ onChange, startDate, endDate })}
</div>
);
};

Определение выбранных дат вне календаря

февраль 2020
пн
вт
ср
чт
пт
сб
вс
Код DemoCalendarWithDatesChange
export const DemoCalendarWithDatesChange = ({ children }) => {
const startDate = new Date(2020, 1, 7);
const endDate = new Date(2020, 1, 19);
const [period, setPeriod] = React.useState({ periodStart: startDate, periodEnd: endDate });
const onChange = (firstDate, secondDate) => () =>
setPeriod({ periodStart: firstDate, periodEnd: secondDate });
const { periodEnd, periodStart } = period;
return (
<>
{children({ startDate: periodStart, endDate: periodEnd })}
<div>
<div style={{ display: 'inline-block', marginRight: '12px' }}>
<Button onClick={onChange(new Date(2020, 0, 1), new Date(2020, 0, 31))}>
Январь
</Button>
</div>
<Button onClick={onChange(new Date(2020, 1, 1), new Date(2020, 1, 29))}>
Февраль
</Button>
</div>
</>
);
};