@bigfishtv/cockpit
Version:
238 lines (206 loc) • 7.8 kB
JavaScript
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _class, _temp;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import DatePicker from 'react-datepicker';
import moment from 'moment';
import Icon from '../Icon';
var valueDateTimeFormat = 'YYYY-MM-DDTHH:mm:ssZ';
var dateFormat = 'DD/MM/YY';
var timeFormat = 'h:mm A';
var dateFormats = [
// day/month/year
'DD/MM/YYYY', 'D/MM/YYYY', 'DD/M/YYYY', 'D/M/YYYY', 'DD/MM/YY', 'D/M/YY', 'D/MM/YY', 'DD/M/YY',
// year-month-day
'YYYY-MM-DD', 'YYYY-MM-D', 'YYYY-M-DD', 'YYYY-M-D', 'YY-MM-DD', 'YY-MM-D', 'YY-M-DD', 'YY-M-D'];
var timeFormats = ['', 'ha', 'h:mm', 'h:mma', 'h', 'hh'];
var DateTimeInput = (_temp = _class = function (_Component) {
_inherits(DateTimeInput, _Component);
function DateTimeInput(props) {
_classCallCheck(this, DateTimeInput);
var _this = _possibleConstructorReturn(this, _Component.call(this));
_this.handleChangeDate = function (event) {
// if parsed + formatted date is the same as the
// input value, then just send an onChange event
// TODO: also support this when defaultTime is provided
var date = moment(event.target.value, dateFormats);
if (date.format(dateFormat) === event.target.value && !_this.props.defaultTime) {
_this.props.onChange(date.format(valueDateTimeFormat));
return;
}
_this.setState({
dirtyDate: true,
dateValue: event.target.value
});
};
_this.handleChangeDatepicker = function (dateTime) {
_this.setState({
dateValue: dateTime.format(dateFormat),
dirtyDate: true,
timeValue: _this.state.timeValue || _this.props.defaultTime,
currentDate: dateTime
}, function () {
return _this.handleBlur();
});
setTimeout(function () {
return _this.dateInputRef.focus();
}, 1);
};
_this.handleChangeTime = function (event) {
// if parsed + formatted time is the same as the
// input value, then just send an onChange event
var time = moment(event.target.value, timeFormats);
if (time.format(timeFormat) === event.target.value) {
var date = moment(_this.state.dateValue, dateFormats);
date.hours(time.hours());
date.minutes(time.minutes());
date.seconds(time.seconds());
_this.props.onChange(date.format(valueDateTimeFormat));
return;
}
_this.setState({
dirtyTime: true,
timeValue: event.target.value
});
};
_this.handleBlur = function (event) {
var _this$state = _this.state,
dateValue = _this$state.dateValue,
timeValue = _this$state.timeValue,
dirtyDate = _this$state.dirtyDate,
dirtyTime = _this$state.dirtyTime;
// if no values have changed then donna worry bout ittt
if (!dirtyTime && !dirtyDate) return;
// attempt to parse date and time from input values
var date = moment(dateValue, dateFormats);
var time = moment(timeValue, timeFormats);
// update state, including if values are valid
_this.setState({ dirtyDate: false, dirtyTime: false, dateValid: date.isValid(), timeValid: time.isValid() }, function () {
// return blank date if invalid
if (!_this.state.dateValid) {
_this.props.onChange(null);
return;
}
// copy time value into date object
if (_this.state.timeValid) {
date.hours(time.hours());
date.minutes(time.minutes());
date.seconds(time.seconds());
}
_this.props.onChange(date.format(valueDateTimeFormat));
});
};
_this.handleKeyDown = function (event) {
if (event.key === 'Enter') {
_this.handleBlur();
}
};
var state = _this.getStateForValue(props);
_this.state = _extends({
dirtyTime: false,
dirtyDate: false,
dateValid: state.dateValue !== '',
timeValid: state.timeValue !== ''
}, state);
return _this;
}
DateTimeInput.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
if (nextProps.value != this.props.value && !this.state.dirty) {
this.setState(_extends({}, this.getStateForValue(nextProps)));
}
};
DateTimeInput.prototype.getStateForValue = function getStateForValue(nextProps) {
var dateTimeString = nextProps.value;
if (!dateTimeString) {
return {
dateValue: '',
timeValue: nextProps.defaultTime,
currentDate: null
};
}
var dateTime = moment(dateTimeString, valueDateTimeFormat);
return {
dateValue: dateTime.format(dateFormat),
timeValue: dateTime.hour() == 0 && dateTime.minute() == 0 ? '' : dateTime.format(timeFormat),
currentDate: dateTime
};
};
DateTimeInput.prototype.render = function render() {
var _this2 = this;
var _state = this.state,
timeValue = _state.timeValue,
dateValue = _state.dateValue;
var _props = this.props,
value = _props.value,
minDate = _props.minDate,
maxDate = _props.maxDate,
displayTime = _props.displayTime;
return React.createElement(
'div',
{ className: 'inputs-inline' },
React.createElement(
'div',
{ className: 'input-group' },
React.createElement(
'div',
{ className: 'input-group-icon' },
!this.props.readOnly && !this.props.disabled && React.createElement(DatePicker, {
showYearDropdown: true,
selected: this.state.currentDate,
onChange: this.handleChangeDatepicker,
minDate: minDate ? moment(minDate) : null,
maxDate: maxDate ? moment(maxDate) : null,
tabIndex: -1
}),
React.createElement(Icon, { name: 'calendar', size: '18' })
),
React.createElement('input', {
ref: function ref(el) {
return _this2.dateInputRef = el;
},
type: 'text',
placeholder: dateFormat,
value: dateValue,
onChange: this.handleChangeDate,
onBlur: this.handleBlur,
onKeyDown: this.handleKeyDown,
readOnly: this.props.readOnly,
autoFocus: this.props.autoFocus
})
),
displayTime && value && React.createElement(
'div',
{ className: 'input-group' },
React.createElement(
'div',
{ className: 'input-group-icon' },
React.createElement(Icon, { name: 'clock', size: '18' })
),
React.createElement('input', {
type: 'text',
placeholder: value ? '12:00 AM' : '',
value: timeValue,
onChange: this.handleChangeTime,
onBlur: this.handleBlur,
onKeyDown: this.handleKeyDown,
readOnly: this.props.readOnly
})
)
);
};
return DateTimeInput;
}(Component), _class.propTypes = {
value: PropTypes.string,
defaultTime: PropTypes.string
}, _class.defaultProps = {
displayTime: true,
defaultTime: '',
onChange: function onChange(value) {
return console.warn('No onChange prop set for DateTime');
},
readOnly: false
}, _temp);
export { DateTimeInput as default };