zp-bee
Version:
zp-bee,是一款基于 Dumi,由 React + TypeScript 开发的组件库 🎉。
720 lines (555 loc) • 20.9 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = exports.ItemStore = void 0;
var _createForOfIteratorHelper2 = _interopRequireDefault(require("@babel/runtime/helpers/createForOfIteratorHelper"));
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
var _mobx = require("mobx");
var _asyncValidator = _interopRequireDefault(require("async-validator"));
var _isEmpty = _interopRequireDefault(require("lodash/isEmpty"));
var _omit = _interopRequireDefault(require("lodash/omit"));
var __decorate = void 0 && (void 0).__decorate || function (decorators, target, key, desc) {
var c = arguments.length,
r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
d;
if ((typeof Reflect === "undefined" ? "undefined" : (0, _typeof2.default)(Reflect)) === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) {
if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
}
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = void 0 && (void 0).__awaiter || function (thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function (resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __rest = void 0 && (void 0).__rest || function (s, e) {
var t = {};
for (var p in s) {
if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
}
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
}
return t;
};
var ItemStore = /*#__PURE__*/function () {
function ItemStore(props) {
var _this = this;
(0, _classCallCheck2.default)(this, ItemStore);
this.defaultValue = undefined;
this.onChangeContext = {};
this.isChange = false;
this.source = undefined;
this.verifying = false;
this.unionErr = {};
this.setProps = function (props) {
var defaultValue = props.defaultValue,
rules = props.rules,
parse = props.parse,
format = props.format;
_this.setDefaultValue(defaultValue);
_this.setRules(rules);
_this.setParse(parse);
_this.setFormat(format);
};
this.setVerifying = function (Verifying) {
_this.verifying = Verifying;
};
this.setUnionErr = function (props) {
if (_this.isChange) {
_this.unionErr = Object.assign(Object.assign({}, _this.unionErr), props);
}
};
this.delUnionErr = function (keys) {
_this.unionErr = _omit.default.apply(void 0, [_this.unionErr].concat((0, _toConsumableArray2.default)(keys)));
};
this.setOnChangeContext = function (context) {
_this.onChangeContext = context || {};
};
this.clearOnChangeContext = function () {
_this.onChangeContext = {};
};
this.onChange = function (value) {
var _a, _b, _c;
var parseValue;
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
if (_this.parse && !_this.onChangeContext.noParse) {
parseValue = _this.parse.apply(_this, [value].concat(args));
} else if (!(0, _isEmpty.default)(value)) {
if (value instanceof Event) {
parseValue = value.target.value;
} else if (value.nativeEvent instanceof Event) {
parseValue = (_a = value.target.value) !== null && _a !== void 0 ? _a : value.target.checked;
} else {
parseValue = value;
}
} else {
parseValue = value;
}
if (!_this.isChange) {
_this.isChange = true;
_this.formStore.setChangeState(true);
}
_this.source = parseValue; // 在值改变后在调用 方便实现自动保存等功能
(_c = (_b = _this.formStore).onChange) === null || _c === void 0 ? void 0 : _c.call(_b, _this.key, parseValue, value, args);
_this.validate();
_this.clearOnChangeContext();
};
this.setDefaultValue = function (value) {
if (!_this.isChange) _this.source = value;
_this.defaultValue = value;
};
this.reset = function () {
// 没有变更不需要reset
if (_this.isChange) {
_this.isChange = false;
_this.source = _this.defaultValue;
_this.err = undefined;
_this.unionErr = {};
}
};
this.validate = (0, _mobx.flow)( /*#__PURE__*/_regenerator.default.mark(function _callee(rootId) {
var err, e;
return _regenerator.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!this.isChange) {
this.isChange = true;
this.formStore.setChangeState(true);
}
if (!this.schema) {
_context.next = 9;
break;
}
this.setVerifying(true);
_context.next = 5;
return this.schema.validate((0, _defineProperty2.default)({}, this.key, this.source)).catch(function (errs) {
return errs;
});
case 5:
e = _context.sent;
if (this.isChange) {
_context.next = 8;
break;
}
return _context.abrupt("return", err);
case 8:
if (e) {
err = e.errors.map(function (item) {
return {
message: item.message,
field: "".concat(rootId || '').concat(rootId ? '.' : '').concat(item.field)
};
});
this.err = err;
} else {
this.err = undefined;
}
case 9:
this.setVerifying(false);
return _context.abrupt("return", err);
case 11:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
this.setRules = function (rules) {
_this.rules = rules;
if (rules) _this.schema = new _asyncValidator.default((0, _defineProperty2.default)({}, _this.key, rules));else _this.schema = undefined;
if (_this.isChange) {
_this.validate();
}
};
this.setParse = function (parse) {
_this.parse = parse;
};
this.setFormat = function (format) {
_this.format = format;
};
var key = props.key,
formStore = props.formStore,
rest = __rest(props, ["key", "formStore"]);
this.key = key;
this.formStore = formStore;
this.setProps(rest);
(0, _mobx.makeObservable)(this);
}
(0, _createClass2.default)(ItemStore, [{
key: "errors",
get: function get() {
var errors = (this.err || []).concat(Object.values(this.unionErr));
if ((0, _isEmpty.default)(errors)) return undefined;
return errors;
}
}, {
key: "value",
get: function get() {
if (this.format) {
return this.format(this.source);
}
return this.source;
}
}, {
key: "checkResult",
get: function get() {
if (this.verifying) return 'loading';
if (this.errors) return 'error';
if (this.isChange) return 'success';
return 'default';
}
}]);
return ItemStore;
}();
exports.ItemStore = ItemStore;
__decorate([_mobx.observable], ItemStore.prototype, "isChange", void 0);
__decorate([_mobx.observable], ItemStore.prototype, "source", void 0);
__decorate([_mobx.observable], ItemStore.prototype, "err", void 0);
__decorate([_mobx.observable], ItemStore.prototype, "verifying", void 0);
__decorate([_mobx.observable], ItemStore.prototype, "unionErr", void 0);
__decorate([_mobx.computed], ItemStore.prototype, "errors", null);
__decorate([_mobx.computed], ItemStore.prototype, "value", null);
__decorate([_mobx.computed], ItemStore.prototype, "checkResult", null);
__decorate([_mobx.observable], ItemStore.prototype, "rules", void 0);
__decorate([_mobx.observable], ItemStore.prototype, "format", void 0);
__decorate([_mobx.action], ItemStore.prototype, "setVerifying", void 0);
__decorate([_mobx.action], ItemStore.prototype, "setUnionErr", void 0);
__decorate([_mobx.action], ItemStore.prototype, "delUnionErr", void 0);
__decorate([_mobx.action], ItemStore.prototype, "onChange", void 0);
__decorate([_mobx.action], ItemStore.prototype, "setDefaultValue", void 0);
__decorate([_mobx.action], ItemStore.prototype, "reset", void 0);
var FormStore = /*#__PURE__*/function () {
function FormStore(props) {
var _this2 = this;
(0, _classCallCheck2.default)(this, FormStore);
this.disabled = false;
this.isChange = false;
this.validateOnlyFirst = false;
this.setDisabled = function (disabled) {
_this2.disabled = disabled;
};
this.unionValidatorDict = {};
this.setChangeState = function (isChange) {
_this2.isChange = isChange;
};
this.addItemStore = function (component) {
if (_this2.itemStores[component.key]) return false;else {
_this2.itemStores[component.key] = component;
return true;
}
};
this.removeItemStore = function (component) {
if (_this2.itemStores[component.key]) {
delete _this2.itemStores[component.key];
return true;
} else {
return false;
}
};
this.submit = function (callback) {
var values = _this2.getValues();
_this2.validate().then(function (errs) {
callback({
values: values,
errs: errs
});
});
};
this.reset = function () {
for (var key in _this2.itemStores) {
if (Reflect.has(_this2.itemStores, key)) {
_this2.itemStores[key].reset();
}
}
_this2.setChangeState(false);
};
this.getValue = function (key) {
if (_this2.itemStores[key]) {
return _this2.itemStores[key].source;
}
};
this.getValues = function (keys) {
var values = {};
if (!Array.isArray(keys)) {
Object.keys(_this2.itemStores).forEach(function (key) {
values[key] = _this2.getValue(key);
});
} else {
var _iterator = (0, _createForOfIteratorHelper2.default)(keys),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var key = _step.value;
values[key] = _this2.getValue(key);
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
return values;
};
this.setValue = function (key, value, onChangeContext) {
var instance = _this2.itemStores[key];
if (instance) {
instance.setOnChangeContext(onChangeContext || {
noParse: true
});
instance.onChange(value);
}
};
this.setValues = function (props, onChangeContext) {
Object.keys(props).forEach(function (key) {
_this2.setValue(key, props[key], onChangeContext);
});
};
this.setAllValues = function (props, onChangeContext) {
for (var key in _this2.itemStores) {
if (Reflect.has(_this2.itemStores, key)) {
if (props[key]) {
_this2.setValue(key, props[key], onChangeContext);
} else {
_this2.setValue(key, undefined, onChangeContext);
}
}
}
};
this.validate = function (rootId) {
return __awaiter(_this2, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee2() {
var _this3 = this;
var errs, key, itemStore, errors, unionValidators, _iterator2, _step2, unionValidator, keys, errList;
return _regenerator.default.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
errs = {};
if (!this.validateOnlyFirst) {
_context2.next = 38;
break;
}
_context2.t0 = _regenerator.default.keys(this.itemStores);
case 3:
if ((_context2.t1 = _context2.t0()).done) {
_context2.next = 36;
break;
}
key = _context2.t1.value;
if (!Reflect.has(this.itemStores, key)) {
_context2.next = 34;
break;
}
itemStore = this.itemStores[key]; // eslint-disable-next-line
_context2.next = 9;
return itemStore.validate(rootId);
case 9:
errors = _context2.sent;
if (!errors) {
_context2.next = 13;
break;
}
errs[itemStore.key] = itemStore.errors;
return _context2.abrupt("return", errs);
case 13:
unionValidators = this.unionValidatorDict[key];
if (!Array.isArray(unionValidators)) {
_context2.next = 34;
break;
}
_iterator2 = (0, _createForOfIteratorHelper2.default)(unionValidators);
_context2.prev = 16;
_iterator2.s();
case 18:
if ((_step2 = _iterator2.n()).done) {
_context2.next = 26;
break;
}
unionValidator = _step2.value;
unionValidator();
if (!itemStore.errors) {
_context2.next = 24;
break;
}
errs[itemStore.key] = itemStore.errors;
return _context2.abrupt("return", errs);
case 24:
_context2.next = 18;
break;
case 26:
_context2.next = 31;
break;
case 28:
_context2.prev = 28;
_context2.t2 = _context2["catch"](16);
_iterator2.e(_context2.t2);
case 31:
_context2.prev = 31;
_iterator2.f();
return _context2.finish(31);
case 34:
_context2.next = 3;
break;
case 36:
_context2.next = 43;
break;
case 38:
keys = Object.keys(this.itemStores);
_context2.next = 41;
return Promise.all(keys.map(function (key) {
return _this3.validateValue(key, rootId);
}));
case 41:
errList = _context2.sent;
keys.forEach(function (key, i) {
var errMessages = errList[i];
if (errMessages) {
Object.assign(errs, (0, _defineProperty2.default)({}, key, errMessages));
}
});
case 43:
if (!(0, _isEmpty.default)(errs)) {
_context2.next = 45;
break;
}
return _context2.abrupt("return", undefined);
case 45:
return _context2.abrupt("return", errs);
case 46:
case "end":
return _context2.stop();
}
}
}, _callee2, this, [[16, 28, 31, 34]]);
}));
};
this.validateValue = function (key, rootId) {
return __awaiter(_this2, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee3() {
var unionValidFuncs, _iterator3, _step3, unionValidator;
return _regenerator.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
if (this.itemStores[key]) {
_context3.next = 2;
break;
}
return _context3.abrupt("return", undefined);
case 2:
_context3.next = 4;
return this.itemStores[key].validate(rootId);
case 4:
unionValidFuncs = this.unionValidatorDict[key];
if (Array.isArray(unionValidFuncs)) {
_iterator3 = (0, _createForOfIteratorHelper2.default)(unionValidFuncs);
try {
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
unionValidator = _step3.value;
unionValidator();
}
} catch (err) {
_iterator3.e(err);
} finally {
_iterator3.f();
}
}
return _context3.abrupt("return", this.itemStores[key].errors);
case 7:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
};
this.validateValues = function (keys, rootId) {
return __awaiter(_this2, void 0, void 0, /*#__PURE__*/_regenerator.default.mark(function _callee4() {
var _this4 = this;
var errs, promiseErrors, errors;
return _regenerator.default.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
errs = {};
promiseErrors = keys.map(function (errMessages) {
return _this4.validateValue(errMessages, rootId);
});
_context4.next = 4;
return Promise.all(promiseErrors);
case 4:
errors = _context4.sent;
keys.forEach(function (key, i) {
errs[key] = errors[i];
});
return _context4.abrupt("return", errs);
case 7:
case "end":
return _context4.stop();
}
}
}, _callee4);
}));
};
this.itemStores = props.initInstances(this);
(0, _mobx.makeObservable)(this);
}
(0, _createClass2.default)(FormStore, [{
key: "errors",
get: function get() {
var _this5 = this;
var errs = Object.keys(this.itemStores).reduce(function (list, key) {
var _a;
var errors = (_a = _this5.itemStores[key]) === null || _a === void 0 ? void 0 : _a.errors;
if (!errors) {
return list;
}
return list.concat(errors);
}, []);
if ((0, _isEmpty.default)(errs)) return undefined;
return errs;
}
}]);
return FormStore;
}();
__decorate([_mobx.observable], FormStore.prototype, "itemStores", void 0);
__decorate([_mobx.observable], FormStore.prototype, "disabled", void 0);
__decorate([_mobx.observable], FormStore.prototype, "isChange", void 0);
__decorate([_mobx.action], FormStore.prototype, "setDisabled", void 0);
__decorate([_mobx.computed], FormStore.prototype, "errors", null);
__decorate([_mobx.action], FormStore.prototype, "setChangeState", void 0);
__decorate([_mobx.action], FormStore.prototype, "addItemStore", void 0);
__decorate([_mobx.action], FormStore.prototype, "removeItemStore", void 0);
var _default = FormStore;
exports.default = _default;