UNPKG

maycur-business

Version:

maycur business react components of web

184 lines (147 loc) 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.setOriginalRule = void 0; var _reactDom = _interopRequireDefault(require("react-dom")); var _domScrollIntoView = _interopRequireDefault(require("dom-scroll-into-view")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function computedStyle(el, prop) { var getComputedStyle = window.getComputedStyle; var style = // If we have getComputedStyle getComputedStyle ? // Query it // TODO: From CSS-Query notes, we might need (node, null) for FF getComputedStyle(el) : // Otherwise, we are in IE and use currentStyle el.currentStyle; if (style) { // Switch to camelCase for CSSOM // DEV: Grabbed from jQuery // https://github.com/jquery/jquery/blob/1.9-stable/src/css.js#L191-L194 // https://github.com/jquery/jquery/blob/1.9-stable/src/core.js#L593-L597 return style[prop.replace(/-(\w)/gi, function (word, letter) { return letter.toUpperCase(); })]; } return undefined; } function getScrollableContainer(n) { var node = n; var nodeName; /* eslint no-cond-assign:0 */ while ((nodeName = node.nodeName.toLowerCase()) !== 'body') { var overflowY = computedStyle(node, 'overflowY'); // https://stackoverflow.com/a/36900407/3040605 if (node !== n && (overflowY === 'auto' || overflowY === 'scroll') && node.scrollHeight > node.clientHeight) { return node; } node = node.parentNode; } return nodeName === 'body' ? node.ownerDocument : node; } var originRules = { DateTimeInput: function DateTimeInput() { var field = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var rv; var withDefault = false; var valueFormat = field.valueFormat, valueDefault = field.valueDefault; var now = new Date(); if (valueDefault === 'NOW') { rv = { currentTime: undefined, timeRange: undefined }; var defaultValue; withDefault = true; switch (field.selectionModule) { case 'SINGLE': defaultValue = valueFormat === 'YYYY-MM' ? new Date(now.getFullYear(), now.getMonth(), 1) : now; rv.currentTime = defaultValue.valueOf(); break; case 'PAIR': defaultValue = now; rv.timeRange = { startDate: defaultValue.valueOf() }; break; default: break; } } return { hasDefault: withDefault, value: rv }; } }; var setOriginalRule = function setOriginalRule(name, fn) { if (name && typeof fn === 'function' && !originRules[name]) { originRules[name] = fn; } }; exports.setOriginalRule = setOriginalRule; var Field = function Field() { var _this = this; var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; _classCallCheck(this, Field); this.setDefaultHandle = function (fn) { if (typeof fn === 'function') _this.getDefaultFn = fn; }; this.setWatch = function (fn) { if (typeof fn === 'function') _this.watch.push(fn); }; this.getDefault = function () { return _this.getDefaultFn ? _this.getDefaultFn(_this.config) : undefined; }; this.setValue = function (val) { _this.value = val; }; this.setValidator = function (v) { _this.validator = v; }; this.scroll = function () { if (_this.componentInstance) { var node = _reactDom["default"].findDOMNode(_this.componentInstance); if (node) { var parentNode = getScrollableContainer(node); (0, _domScrollIntoView["default"])(node, parentNode, { onlyScrollIfNeeded: true }); } } }; this.validate = function () { var fieldName = _this.name; return new Promise(function (resolve, reject) { _this.validator.validate(_defineProperty({}, fieldName, _this.value), { formFields: [_this.config] }, function (errors, fields) { if (errors) { _this.validateError = fields[fieldName] && fields[fieldName].message; } else { _this.validateError = undefined; } resolve({ errors: errors, fields: fields }); }); }); }; var config = props.config, defaultHandle = props.defaultHandle; this.config = config; this.watch = []; this.value = undefined; this.name = config.identifier; this.validateError = undefined; this.componentInstance = undefined; // 默认值函数 if (typeof defaultHandle === 'function') { this.getDefaultFn = defaultHandle; } else { if (originRules[config.type]) this.getDefaultFn = originRules[config.type]; } }; var _default = Field; exports["default"] = _default;