import moment from 'moment'; // @ts-ignore import DaysView from 'react-datetime/src/DaysView'; import React from 'react'; import {LocaleProps, localeable} from '../../locale'; import {ClassNamesFn} from '../../theme'; interface CustomDaysViewProps extends LocaleProps { classPrefix?: string; prevIcon?: string; nextIcon?: string; viewDate: moment.Moment; selectedDate: moment.Moment; timeFormat: string; requiredConfirm?: boolean; isEndDate?: boolean; renderDay?: Function; onClose?: () => void; onChange: (value: moment.Moment) => void; setDateTimeState: (state: any) => void; setTime: (type: string, amount: number) => void; subtractTime: ( amount: number, type: string, toSelected?: moment.Moment ) => () => void; addTime: ( amount: number, type: string, toSelected?: moment.Moment ) => () => void; isValidDate?: ( currentDate: moment.Moment, selected?: moment.Moment ) => boolean; showView: (view: string) => () => void; updateSelectedDate: (event: React.MouseEvent, close?: boolean) => void; handleClickOutside: () => void; classnames: ClassNamesFn; } export class CustomDaysView extends DaysView { props: CustomDaysViewProps; getDaysOfWeek: (locale: any) => any; renderDays: () => JSX.Element; updateSelectedDate = (event: React.MouseEvent) => { // need confirm if (this.props.requiredConfirm) { const viewDate = this.props.viewDate.clone(); const currentDate = this.props.selectedDate || viewDate; const target = event.target as HTMLElement; let modifier = 0; if (~target.className.indexOf('rdtNew')) { modifier = 1; } if (~target.className.indexOf('rdtOld')) { modifier = -1; } viewDate .month(viewDate.month() + modifier) .date(parseInt(target.getAttribute('data-value') as string, 10)) .hours(currentDate.hours()) .minutes(currentDate.minutes()) .seconds(currentDate.seconds()) .milliseconds(currentDate.milliseconds()); this.props.setDateTimeState({ viewDate, selectedDate: viewDate.clone() }); return; } this.props.updateSelectedDate(event, true); }; setTime = ( type: 'hours' | 'minutes' | 'seconds' | 'milliseconds', value: number ) => { const date = (this.props.selectedDate || this.props.viewDate).clone(); date[type](value); this.props.setDateTimeState({ viewDate: date.clone(), selectedDate: date.clone() }); if (!this.props.requiredConfirm) { this.props.onChange(date); } }; confirm = () => { const date = (this.props.selectedDate || this.props.viewDate).clone(); this.props.setDateTimeState({ selectedDate: date }); this.props.onChange(date); this.props.onClose && this.props.onClose(); }; cancel = () => { this.props.onClose && this.props.onClose(); }; renderDay = (props: any, currentDate: moment.Moment) => { return {currentDate.date()}; }; renderTimes = () => { const { timeFormat, selectedDate, viewDate, isEndDate, classnames: cx } = this.props; const date = selectedDate || (isEndDate ? viewDate.endOf('day') : viewDate); const inputs: Array = []; timeFormat.split(':').forEach((format, i) => { const type = /h/i.test(format) ? 'hours' : /m/i.test(format) ? 'minutes' : 'seconds'; const min = 0; const max = type === 'hours' ? 23 : 59; inputs.push( this.setTime( type, Math.max( min, Math.min( parseInt(e.currentTarget.value.replace(/\D/g, ''), 10) || 0, max ) ) ) } /> ); inputs.push(:); }); inputs.length && inputs.pop(); return
{inputs}
; }; renderFooter = () => { if (!this.props.timeFormat && !this.props.requiredConfirm) { return null; } const {translate: __, classnames: cx} = this.props; return ( {this.props.timeFormat ? this.renderTimes() : null} {this.props.requiredConfirm ? (
{__('cancel')} {__('confirm')}
) : null} ); }; render() { const footer = this.renderFooter(); const date = this.props.viewDate; const locale = date.localeData(); const __ = this.props.translate; const tableChildren = [
«
{date.format(__('dateformat.year'))} {date.format(__('MMM'))}
»
{this.getDaysOfWeek(locale).map((day: number, index: number) => ( {day} ))} , {this.renderDays()} ]; footer && tableChildren.push(footer); return (
{tableChildren}
); } } export default localeable( (CustomDaysView as any) as React.ComponentClass );