UNPKG

@zohodesk/hooks

Version:

Unified Component Library - Hooks

102 lines (94 loc) 1.97 kB
import { dateDecode, dateEncode } from "../utils/dateConverter"; import getAllMonthDaysCount from "../utils/getAllMonthDaysCount"; function dateValidation(date, month, year) { let newDate = date; let monthDaysCount = getAllMonthDaysCount(year); if (monthDaysCount[month - 1] < date) { newDate = monthDaysCount[month - 1]; } return newDate; } export function handleChange(_ref) { let { date, month, year } = _ref; let newDate = dateValidation(date, month, year); let newValue = dateEncode({ date: newDate, month, year }); return newValue; } export function handlePrevMonth(value) { const { date, month, year } = dateDecode(value); let newMonth = month - 1; let newYear = year; if (newMonth == 0) { newMonth = 12; newYear = year - 1; } let newDate = dateValidation(date, newMonth, newYear); let newValue = dateEncode({ date: newDate, month: newMonth, year: newYear }); return newValue; } export function handleNextMonth(value) { const { date, month, year } = dateDecode(value); let newMonth = month + 1; let newYear = year; if (newMonth == 13) { newMonth = 1; newYear = year + 1; } let newDate = dateValidation(date, newMonth, newYear); let newValue = dateEncode({ date: newDate, month: newMonth, year: newYear }); return newValue; } export function handlePrevYear(value) { const { date, month, year } = dateDecode(value); let newYear = year - 1; let newDate = dateValidation(date, month, newYear); let newValue = dateEncode({ date: newDate, month, year: newYear }); return newValue; } export function handleNextYear(value) { const { date, month, year } = dateDecode(value); let newYear = year + 1; let newDate = dateValidation(date, month, newYear); let newValue = dateEncode({ date: newDate, month, year: newYear }); return newValue; }