1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | import styles from './style.postcss'; import React, { PureComponent } from 'react'; import DayPicker, { DateUtils } from 'react-day-picker'; import moment from 'domain/moment'; import is from 'is_js'; import classnames from 'classnames'; import Icon from 'components/Icon'; import PropTypes from 'prop-types'; import 'react-day-picker/lib/style.css'; import './datePicker.postcss'; const overlayStyle = { position: 'absolute', background: 'white', boxShadow: '0 2px 5px rgba(0, 0, 0, .15)', marginBottom: '20px', zIndex: '1000', }; const weekdaysLong = { en: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'], }; const weekdaysShort = { en: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'], }; const months = { en: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], }; const firstDayOfWeek = { en: 1, }; const localeUtils = { formatDay: (d, locale = 'en') => `${weekdaysLong[locale][d.getDay()]}, ${d.getDate()} ${months[locale][d.getMonth()]} ${d.getFullYear()}`, formatWeekdayShort: (index, locale = 'en') => weekdaysShort[locale][index], formatWeekdayLong: (index, locale = 'en') => weekdaysLong[locale][index], getFirstDayOfWeek: (locale) => firstDayOfWeek[locale], getMonths: (locale) => months[locale], formatMonthTitle: (d, locale) => `${months[locale][d.getMonth()]} ${d.getFullYear()}`, }; export default class CalendarPicker extends PureComponent { constructor(props) { super(props); this.handleDayClick = this.handleDayClick.bind(this); this.handleInputFocus = this.handleInputFocus.bind(this); this.handleInputBlur = this.handleInputBlur.bind(this); this.handleContainerMouseDown = this.handleContainerMouseDown.bind(this); this.state = { showOverlay: false, }; } componentWillUnmount() { clearTimeout(this.clickTimeout); } handleContainerMouseDown() { this.clickedInside = true; this.clickTimeout = setTimeout(() => { this.clickedInside = false; }, 0); } handleInputFocus() { this.setState({ showOverlay: true, }); } handleInputBlur() { const showOverlay = this.clickedInside; this.setState({ showOverlay, }); if (showOverlay) { this.input.focus(); } } handleCalendarClick() { this.input.focus(); } handleDayClick(e, day, { disabled }) { if (disabled) { return; } this.setState({ showOverlay: false, }); this.input.blur(); this.props.onDateChanged(day); } _initMonth() { const now = new Date(); return this.props.day === null ? now : this.props.day; } _displayedValue() { return this.props.day ? moment(this.props.day).format('YYYY-MM-DD') : ''; } _getDate(datetime) { return new Date(moment(datetime).format('YYYY-MM-DD')); } _disabledDay(day) { let disabled = false; if (this.props.isDisablePastDay) { disabled = DateUtils.isPastDay(day); } const calendarDate = this._getDate(day); const { disabledDayBefore, disabledDayAfter } = this.props; const disabledDateBefore = disabledDayBefore && this._getDate(disabledDayBefore); const disabledDateAfter = disabledDayAfter && this._getDate(disabledDayAfter); if (is.not.undefined(disabledDateBefore) && is.undefined(disabledDateAfter)) { return disabled || calendarDate < disabledDateBefore; } else if (is.undefined(disabledDateBefore) && is.not.undefined(disabledDateAfter)) { return disabled || calendarDate > disabledDateAfter; } else if (is.not.undefined(disabledDateBefore) && is.not.undefined(disabledDateAfter)) { return disabled || (calendarDate < disabledDateBefore) || (calendarDate > disabledDateAfter); } return disabled; } render() { const inputBorderStyle = classnames(styles.CalendarPicker, this.props.isValid ? '' : styles.CalendarPicker_error); return ( <div onMouseDown={this.handleContainerMouseDown} className={inputBorderStyle}> <div className={styles.CalendarPicker_clickArea} onClick={() => this.handleCalendarClick()}> <input className={styles.CalendarPicker_input} type="text" ref={(el) => { this.input = el; }} value={this._displayedValue()} onFocus={this.handleInputFocus} onBlur={this.handleInputBlur} /> <Icon id="calendar" className={styles.CalendarPicker_calendarIcon} /> </div> { this.state.showOverlay && <div className={styles.CalendarPicker_calendar} style={{ position: 'absolute' }}> <div style={overlayStyle}> {this.props.day === null ? <DayPicker ref={(el) => { this.daypicker = el; }} enableOutsideDays localeUtils={localeUtils} onDayClick={this.handleDayClick} selectedDays={(day) => DateUtils.isSameDay(this.props.day, day)} disabledDays={(day) => this._disabledDay(day)} /> : <DayPicker ref={(el) => { this.daypicker = el; }} enableOutsideDays initialMonth={this.props.day} localeUtils={localeUtils} onDayClick={this.handleDayClick} selectedDays={(day) => DateUtils.isSameDay(this.props.day, day)} disabledDays={(day) => this._disabledDay(day)} /> } </div> </div> } </div> ); } } CalendarPicker.propTypes = { onDateChanged: PropTypes.func, day: PropTypes.object, isValid: PropTypes.bool, disabledDayBefore: PropTypes.object, disabledDayAfter: PropTypes.object, isDisablePastDay: PropTypes.bool, }; |