/* eslint-env browser */ import React, { FC, useState } from 'react'; import dayjs from 'dayjs'; import AUCalendarComponent from './AUCalendarComponent'; type Props = { id: string; lang?: string; date?: Date; onDateChange: (date: Date, formatted: string) => void; onHide: (date: Date, dateFormatted: string) => void; format?: string; showTime?: boolean; showLabel?: boolean; labelInline?: boolean; labelText?: string; fullWidth?: boolean; sideBySideGrowth?: number; yearSpanMinus?: number; yearSpanPlus?: number; limitStart?: Date | null; limitEnd?: Date | null; disabled?: boolean; }; const AUDatepickerComponent: FC = ({ id, lang = 'da', date = new Date(), onDateChange, onHide, format = 'YYYY-MM-DD HH:mm', showTime = true, showLabel = false, labelInline = false, labelText = 'Vælg dato:', fullWidth = true, yearSpanMinus = 5, yearSpanPlus = 5, limitStart = null, limitEnd = null, disabled = false, sideBySideGrowth = 0, }) => { const [showCalendar, setShowCalendar] = useState(false); const [sDate, setDate] = useState(date as Date); const dateFormatted = dayjs(sDate).format(format); const hideDatePicker = () => { setShowCalendar(false); onHide(sDate, dateFormatted); }; const changeDate = (newDate: Date) => { setDate(newDate); onDateChange(newDate, dayjs(newDate).format(format)); }; const previousDate = dayjs(sDate).subtract(1, 'day').toDate(); const nextDate = dayjs(sDate).add(1, 'day').toDate(); const previousDisabled = disabled || ((limitStart && previousDate < limitStart) as boolean); const nextDisabled = disabled || ((limitEnd && nextDate > limitEnd) as boolean); let className = 'datepicker form__field'; if (fullWidth) { className = `${className} form__field--full-width`; } if (labelInline) { className = `${className} form__field--inline-label`; } if (disabled) { className = `${className} visually-disabled`; } if (typeof sideBySideGrowth !== 'undefined' && sideBySideGrowth > 0) { className = `${className} form__field--width-relative-${sideBySideGrowth}`; } return (
{showLabel && }
setShowCalendar(!showCalendar)} />
{showCalendar && (
{ const formatted = dayjs(selected).format(format); setDate(selected); onDateChange(selected, formatted); }} showTime={showTime} yearSpanMinus={yearSpanMinus} yearSpanPlus={yearSpanPlus} closeable onClose={hideDatePicker} controls={id} limitStart={limitStart} limitEnd={limitEnd} />
)}
); }; AUDatepickerComponent.displayName = 'AUDatepickerComponent'; export default AUDatepickerComponent;