/
githubmirror
/
wp-calypso
Обзор
Документация
Войти
/
githubmirror
/
wp-calypso
Код
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
trunk
client/components/date-range/utils.ts
33 строки
894 B
Nikhil
Date range: double-click a day to select it as both start and end (#112409)
09 июл 2026, 09:04
Не верифицирован
09 июл 2026, 09:04
e5671d1
Код
Авторство
О чём код?
import { Moment } from 'moment'; interface DateRange { from: Moment | null; to: Moment | null; } function addDayToRange( day: Moment | null, range: DateRange ): DateRange { if ( ! day || ! day.isValid() ) { return range; } const from = range.from?.clone().startOf( 'day' ) ?? null; const to = range.to?.clone().startOf( 'day' ) ?? null; const selectedDay = day.clone().startOf( 'day' ); // A complete range exists: start a new selection from the clicked day. if ( from && to ) { return { ...range, from: selectedDay, to: null }; } // Only one endpoint is set: fill the other, keeping the two days ordered. const anchor = from ?? to; if ( ! anchor ) { return { ...range, from: selectedDay, to: null }; } return selectedDay.isBefore( anchor ) ? { ...range, from: selectedDay, to: anchor } : { ...range, from: anchor, to: selectedDay }; } export { addDayToRange };