d2-ui
Version:
106 lines (88 loc) • 4.93 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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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 React from 'react';
import PropTypes from 'prop-types';
import { DatePicker as MuiDatePicker } from 'material-ui';
var DatePicker = function (_React$Component) {
_inherits(DatePicker, _React$Component);
function DatePicker(props) {
_classCallCheck(this, DatePicker);
var _this = _possibleConstructorReturn(this, (DatePicker.__proto__ || Object.getPrototypeOf(DatePicker)).call(this, props));
_this.onDateSelect = _this.onDateSelect.bind(_this);
_this.formatDate = _this.formatDate.bind(_this);
_this.state = { value: _this.props.value };
return _this;
}
_createClass(DatePicker, [{
key: 'onDateSelect',
value: function onDateSelect(event, date) {
this.setState({ value: date });
this.props.onChange({
target: {
value: date
}
});
}
}, {
key: 'formatDate',
value: function formatDate(date) {
var dd = date.getDate();
var mm = date.getMonth() + 1;
var yyyy = date.getFullYear();
if (dd < 10) {
dd = '0' + dd;
}
if (mm < 10) {
mm = '0' + mm;
}
switch (this.dateFormat) {
case 'dd-MM-yyyy':
return dd + '-' + mm + '-' + yyyy;
case 'yyyy-MM-dd':
return yyyy + '-' + mm + '-' + dd;
default:
return dd + '-' + mm + '-' + yyyy;
}
}
}, {
key: 'render',
value: function render() {
var _props = this.props,
allowFuture = _props.allowFuture,
dateFormat = _props.dateFormat,
other = _objectWithoutProperties(_props, ['allowFuture', 'dateFormat']);
return React.createElement(
'div',
null,
React.createElement(MuiDatePicker, _extends({}, other, {
value: this.state.value,
floatingLabelText: this.props.floatingLabelText,
maxDate: allowFuture ? undefined : new Date(),
errorText: this.props.errorText,
formatDate: this.formatDate,
onChange: this.onDateSelect
}))
);
}
}]);
return DatePicker;
}(React.Component);
DatePicker.propTypes = {
floatingLabelText: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
errorText: PropTypes.string,
dateFormat: PropTypes.oneOf(['dd-MM-yyyy', 'yyyy-MM-dd']),
allowFuture: PropTypes.bool,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.object])
};
DatePicker.defaultProps = {
errorText: '',
value: PropTypes.oneOfType([PropTypes.string, PropTypes.object]),
dateFormat: 'dd-MM-yyyy',
allowFuture: false
};
export default DatePicker;