@react-awesome-query-builder/antd
Version:
User-friendly query builder for React. AntDesign widgets
125 lines • 5.15 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
import React, { PureComponent } from "react";
import { Utils } from "@react-awesome-query-builder/ui";
import PropTypes from "prop-types";
import { DatePicker } from "../moment";
var RangePicker = DatePicker.RangePicker;
var moment = Utils.moment;
var DateWidget = /*#__PURE__*/function (_PureComponent) {
function DateWidget(props) {
var _this;
_classCallCheck(this, DateWidget);
_this = _callSuper(this, DateWidget, [props]);
_this.isValidSingleValue = function (value) {
var valueFormat = _this.props.valueFormat;
var v = value ? moment(value, valueFormat) : null;
return !v || v && v.isValid();
};
_this.isValidValue = function (value) {
var isSpecialRange = _this.props.isSpecialRange;
if (isSpecialRange) return value ? value.map(function (el) {
return _this.isValidSingleValue(el);
}).reduce(function (res, item) {
return res && item;
}, true) : true;else return _this.isValidSingleValue(value);
};
_this.getMomentSingleValue = function (value) {
var valueFormat = _this.props.valueFormat;
var v = value ? moment(value, valueFormat) : null;
if (v && !v.isValid()) v = null;
return v;
};
_this.getMomentValue = function (value) {
var isSpecialRange = _this.props.isSpecialRange;
if (isSpecialRange) return value ? value.map(function (el) {
return _this.getMomentSingleValue(el);
}) : [null, null];else return _this.getMomentSingleValue(value);
};
_this.formatSingleValue = function (value) {
var valueFormat = _this.props.valueFormat;
return value && value.isValid() ? value.format(valueFormat) : undefined;
};
_this.formatValue = function (value) {
var isSpecialRange = _this.props.isSpecialRange;
if (isSpecialRange) return value ? value.map(function (el) {
return _this.formatSingleValue(el);
}) : [undefined, undefined];else return _this.formatSingleValue(value);
};
_this.handleChange = function (value) {
var setValue = _this.props.setValue;
if (_this.isValidValue(value)) {
setValue(_this.formatValue(value));
}
};
var _value = props.value,
_setValue = props.setValue;
if (!_this.isValidValue(_value)) {
_setValue(_this.formatValue(_this.getMomentValue(_value)));
}
return _this;
}
_inherits(DateWidget, _PureComponent);
return _createClass(DateWidget, [{
key: "render",
value: function render() {
var _this$props = this.props,
placeholder = _this$props.placeholder,
placeholders = _this$props.placeholders,
customProps = _this$props.customProps,
value = _this$props.value,
dateFormat = _this$props.dateFormat,
config = _this$props.config,
readonly = _this$props.readonly,
isSpecialRange = _this$props.isSpecialRange;
var renderSize = config.settings.renderSize;
var dateValue = this.getMomentValue(value);
if (isSpecialRange) {
return /*#__PURE__*/React.createElement(RangePicker, _extends({
disabled: readonly,
key: "widget-date",
placeholder: placeholders,
size: renderSize,
format: dateFormat,
value: dateValue,
onChange: this.handleChange
}, customProps));
} else {
return /*#__PURE__*/React.createElement(DatePicker, _extends({
disabled: readonly,
key: "widget-date",
placeholder: placeholder,
size: renderSize,
format: dateFormat,
value: dateValue,
onChange: this.handleChange
}, customProps));
}
}
}]);
}(PureComponent);
DateWidget.propTypes = {
setValue: PropTypes.func.isRequired,
value: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
//in valueFormat
field: PropTypes.any,
config: PropTypes.object.isRequired,
placeholder: PropTypes.string,
placeholders: PropTypes.arrayOf(PropTypes.string),
customProps: PropTypes.object,
readonly: PropTypes.bool,
// from fieldSettings:
dateFormat: PropTypes.string,
valueFormat: PropTypes.string
};
DateWidget.defaultProps = {
dateFormat: "YYYY-MM-DD",
valueFormat: "YYYY-MM-DD"
};
export { DateWidget as default };