let { DefaultButton, addDays, getDateRangeArray, Calendar, DayOfWeek, DateRangeType, Fabric } = window.Fabric; const DayPickerStrings = { months: [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ], shortMonths: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], shortDays: ['S', 'M', 'T', 'W', 'T', 'F', 'S'], goToToday: 'Go to today', weekNumberFormatString: 'Week number {0}' }; interface ICalendarInlineExampleState { selectedDate?: Date | null; selectedDateRange?: Date[] | null; } interface ICalendarInlineExampleProps { isMonthPickerVisible?: boolean; dateRangeType: DateRangeType; autoNavigateOnSelection: boolean; showGoToToday: boolean; showNavigateButtons?: boolean; highlightCurrentMonth?: boolean; highlightSelectedMonth?: boolean; isDayPickerVisible?: boolean; showMonthPickerAsOverlay?: boolean; showWeekNumbers?: boolean; minDate?: Date; maxDate?: Date; showSixWeeksByDefault?: boolean; workWeekDays?: DayOfWeek[]; firstDayOfWeek?: DayOfWeek; } class CalendarInlineExample extends React.Component { public constructor(props: ICalendarInlineExampleProps) { super(props); this.state = { selectedDate: null, selectedDateRange: null }; this._onDismiss = this._onDismiss.bind(this); this._onSelectDate = this._onSelectDate.bind(this); this._goNext = this._goNext.bind(this); this._goPrevious = this._goPrevious.bind(this); } public render(): JSX.Element { const divStyle: React.CSSProperties = { height: '340px' }; const buttonStyle: React.CSSProperties = { margin: '17px 10px 0 0' }; let dateRangeString: string | null = null; if (this.state.selectedDateRange) { const rangeStart = this.state.selectedDateRange[0]; const rangeEnd = this.state.selectedDateRange[this.state.selectedDateRange.length - 1]; dateRangeString = rangeStart.toLocaleDateString() + '-' + rangeEnd.toLocaleDateString(); } return (
{
Selected date(s):{' '} {!this.state.selectedDate ? 'Not set' : this.state.selectedDate.toLocaleString()}
}
Selected dates: {!dateRangeString ? 'Not set' : dateRangeString}
{(this.props.minDate || this.props.maxDate) && (
Date boundary: {' '} {this.props.minDate ? this.props.minDate.toLocaleDateString() : 'Not set'}- {this.props.maxDate ? this.props.maxDate.toLocaleDateString() : 'Not set'}
)} {this.props.showNavigateButtons && (
)}
); } private _onDismiss(): void { this.setState((prevState: ICalendarInlineExampleState) => { return prevState; }); } private _goPrevious(): void { this.setState((prevState: ICalendarInlineExampleState) => { const selectedDate = prevState.selectedDate || new Date(); const dateRangeArray = getDateRangeArray(selectedDate, this.props.dateRangeType, DayOfWeek.Sunday); let subtractFrom = dateRangeArray[0]; let daysToSubtract = dateRangeArray.length; if (this.props.dateRangeType === DateRangeType.Month) { subtractFrom = new Date(subtractFrom.getFullYear(), subtractFrom.getMonth(), 1); daysToSubtract = 1; } const newSelectedDate = addDays(subtractFrom, -daysToSubtract); return { selectedDate: newSelectedDate }; }); } private _goNext(): void { this.setState((prevState: ICalendarInlineExampleState) => { const selectedDate = prevState.selectedDate || new Date(); const dateRangeArray = getDateRangeArray(selectedDate, this.props.dateRangeType, DayOfWeek.Sunday); const newSelectedDate = addDays(dateRangeArray.pop()!, 1); return { selectedDate: newSelectedDate }; }); } private _onSelectDate(date: Date, dateRangeArray: Date[]): void { this.setState((prevState: ICalendarInlineExampleState) => { return { selectedDate: date, selectedDateRange: dateRangeArray }; }); } } ReactDOM.render(, document.getElementById('content'));