UNPKG

@bigfishtv/cockpit

Version:

196 lines (166 loc) 6.33 kB
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 valueDateFormat = 'YYYY-MM-DD'; var dateFormat = 'DD/MM/YY'; 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 DateInput = (_temp = _class = function (_Component) { _inherits(DateInput, _Component); function DateInput(props) { _classCallCheck(this, DateInput); 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 var date = moment(event.target.value, dateFormats); if (date.format(dateFormat) === event.target.value) { _this.props.onChange(date.format(valueDateFormat)); return; } _this.setState({ dirtyDate: true, dateValue: event.target.value }); }; _this.handleChangeDatepicker = function (dateTime) { _this.setState({ dateValue: dateTime.format(dateFormat), dirtyDate: true }, function () { return _this.handleBlur(); }); setTimeout(function () { return _this.setState({ currentDate: dateTime }); }, 1); }; _this.handleFocus = function (event) { if (_this.props.openOnFocus) { _this.datePicker.setOpen(true); } }; _this.handleBlur = function (event) { var _this$state = _this.state, dateValue = _this$state.dateValue, dirtyDate = _this$state.dirtyDate; // close datepicker on blur _this.datePicker.setOpen(false); // if no values have changed then donna worry bout ittt if (!dirtyDate) return; // attempt to parse date and time from input values var date = moment(dateValue, dateFormats); // update state, including if values are valid _this.setState({ initState: false, dirtyDate: false, dateValid: date.isValid() }, function () { // return blank date if invalid if (!_this.state.dateValid) { _this.props.onChange(null); return; } // this just triggers onChange prop var dateValue = date.format(valueDateFormat); _this.props.onChange(dateValue); }); }; _this.handleKeyDown = function (event) { if (event.key === 'Enter') { _this.handleBlur(); } }; var state = _this.getStateForValue(props.value); _this.state = _extends({ dirtyDate: false, dateValid: state.dateValue !== '', initState: state.dateValue === '', currentDate: null }, state); return _this; } DateInput.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) { if (nextProps.value != this.props.value && !this.state.dirty) { this.setState(_extends({}, this.getStateForValue(nextProps.value))); } }; DateInput.prototype.getStateForValue = function getStateForValue(dateString) { if (!dateString) { return { dateValue: '' }; } var dateTime = moment(dateString, valueDateFormat); return { dateValue: dateTime.format(dateFormat), currentDate: dateTime }; }; DateInput.prototype.render = function render() { var _this2 = this; var _state = this.state, initState = _state.initState, dateValid = _state.dateValid, dateValue = _state.dateValue, currentDate = _state.currentDate; var _props = this.props, minDate = _props.minDate, maxDate = _props.maxDate; 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, { ref: function ref(el) { return _this2.datePicker = el; }, showYearDropdown: true, dateFormat: 'YYYY-MM-DD', selected: currentDate, onChange: this.handleChangeDatepicker, minDate: minDate ? moment(minDate, valueDateFormat) : null, maxDate: maxDate ? moment(maxDate, valueDateFormat) : null, tabIndex: -1 }), React.createElement(Icon, { name: 'calendar', size: '18' }) ), React.createElement('input', { type: 'text', placeholder: dateFormat, value: dateValue, onFocus: this.handleFocus, onChange: this.handleChangeDate, onBlur: this.handleBlur, onKeyDown: this.handleKeyDown, className: !dateValid && !initState && dateValue != '' ? 'warning' : undefined, readOnly: this.props.readOnly, autoFocus: this.props.autoFocus }) ) ); }; return DateInput; }(Component), _class.propTypes = { value: PropTypes.string, onChange: PropTypes.func, readOnly: PropTypes.bool, openOnFocus: PropTypes.bool }, _class.defaultProps = { onChange: function onChange(value) { return console.warn('No onChange prop set for DateInput'); }, readOnly: false, openOnFocus: false }, _temp); export { DateInput as default };