zp-bee
Version:
zp-bee,是一款基于 Dumi,由 React + TypeScript 开发的组件库 🎉。
264 lines (214 loc) • 6.77 kB
JavaScript
;
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getListenValues = getListenValues;
exports.getValues = getValues;
exports.createUnionValidator = createUnionValidator;
exports.createAutoHandle = createAutoHandle;
exports.createUnionValidate = createUnionValidate;
exports.configToItemStore = configToItemStore;
exports.parseFormConfigs = parseFormConfigs;
exports.parseAddFormConfigs = parseAddFormConfigs;
exports.genFormId = genFormId;
exports.searchRequired = searchRequired;
exports.scrollByFormErrors = scrollByFormErrors;
exports.getFirstError = getFirstError;
exports.scrollToField = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mapValues = _interopRequireDefault(require("lodash/mapValues"));
var _mobx = require("mobx");
var _store = _interopRequireWildcard(require("./store"));
/**
* 获取监听的key
* @param keys
* @param formStore
* @returns
*/
function getListenValues(keys, formStore) {
return function () {
return keys.filter(function (key) {
return formStore.itemStores[key];
}).map(function (key) {
return formStore.itemStores[key].source;
});
};
}
/**
* 获取所有值
* @param formStore
* @returns
*/
function getValues(formStore) {
return (0, _mapValues.default)(formStore.itemStores, function (itemStore) {
return itemStore.source;
});
}
/**
* 创建联合验证器
* @param autoValidator
* @param formStore
* @param autoValidatorId
* @returns
*/
function createUnionValidator(autoValidator, formStore, autoValidatorId) {
return function () {
var err = autoValidator.validator(getValues(formStore), formStore);
var itemStore = formStore.itemStores[autoValidator.primaryKey];
if (itemStore) {
if (err) {
itemStore.setUnionErr((0, _defineProperty2.default)({}, autoValidatorId, {
message: err,
field: itemStore.key
}));
} else {
itemStore.delUnionErr([autoValidatorId]);
}
}
};
}
/**
* 创建自动处理器
*/
function createAutoHandle(autoHandle, formStore) {
return (0, _mobx.reaction)(getListenValues(autoHandle.listenKey, formStore), function () {
autoHandle.action(getValues(formStore), formStore);
});
}
function createUnionValidate(autoValidator, formStore, autoValidatorId) {
// 去重
var listenKey = new Set(autoValidator.listenKey);
listenKey.add(autoValidator.primaryKey);
return (0, _mobx.reaction)(getListenValues(Array.from(listenKey), formStore), function () {
var err = autoValidator.validator(getValues(formStore), formStore);
var itemStore = formStore.itemStores[autoValidator.primaryKey];
if (itemStore) {
if (err) {
itemStore.setUnionErr((0, _defineProperty2.default)({}, autoValidatorId, {
message: err,
field: itemStore.key
}));
} else {
itemStore.delUnionErr([autoValidatorId]);
}
}
});
}
function configToItemStore(props) {
return new _store.ItemStore(props);
}
function parseFormConfigs(formConfigs) {
function initInstances(formStore) {
var itemStores = {};
for (var key in formConfigs) {
if (Reflect.has(formConfigs, key)) {
itemStores[key] = configToItemStore(Object.assign({
key: key,
formStore: formStore
}, formConfigs[key]));
}
}
return itemStores;
}
var formStore = new _store.default({
initInstances: initInstances
});
return {
formStore: formStore
};
}
function parseAddFormConfigs(formStore, formConfigs) {
var itemStores = {};
for (var key in formConfigs) {
if (Reflect.has(formConfigs, key)) {
itemStores[key] = configToItemStore(Object.assign({
key: key,
formStore: formStore
}, formConfigs[key]));
formStore.addItemStore(itemStores[key]);
}
}
return itemStores;
}
function genFormId(id, replaceId) {
if (replaceId) {
return replaceId;
}
if (Array.isArray(id)) {
return id[0];
}
return id;
}
function searchRequired(id, store) {
var _a;
if (Array.isArray(id)) {
return id.some(function (i) {
var _a;
var rules = (_a = store === null || store === void 0 ? void 0 : store.itemStores[i]) === null || _a === void 0 ? void 0 : _a.rules;
if (Array.isArray(rules)) return rules.some(function (item) {
return item.required;
});
return false;
});
}
var rules = (_a = store === null || store === void 0 ? void 0 : store.itemStores[id]) === null || _a === void 0 ? void 0 : _a.rules;
if (Array.isArray(rules)) return rules.some(function (item) {
return item.required;
});
return (rules === null || rules === void 0 ? void 0 : rules.required) || false;
}
/**
* 跳转到表单字段处,如果可以focus则自动focus
* @param fieldKey 字典id
* @param options scrollIntoView的配置透传
* @returns 是否跳转成功
*/
var scrollToField = function scrollToField(fieldKey) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
block: 'center'
};
var _a, _b, _c, _d;
try {
var inputNode = document.querySelector("#".concat(fieldKey === null || fieldKey === void 0 ? void 0 : fieldKey.replace(/\.|\[|\]/g, '-')));
if (inputNode) {
(_b = (_a = inputNode).focus) === null || _b === void 0 ? void 0 : _b.call(_a);
inputNode.scrollIntoView(options);
return true;
}
inputNode = document.getElementById("".concat(fieldKey));
if (inputNode) {
(_d = (_c = inputNode).focus) === null || _d === void 0 ? void 0 : _d.call(_c);
inputNode.scrollIntoView(options);
return true;
}
var labelNode = document.querySelector("label[for=\"".concat(fieldKey, "\"]"));
if (labelNode) {
labelNode.scrollIntoView(options);
return true;
}
labelNode = document.querySelector("label[for=\"".concat(fieldKey === null || fieldKey === void 0 ? void 0 : fieldKey.replace(/\.|\[|\]/g, '-'), "\"]"));
if (labelNode) {
labelNode.scrollIntoView(options);
return true;
}
} catch (err) {
console.error(err);
}
return false;
};
exports.scrollToField = scrollToField;
function scrollByFormErrors(errs) {
var _a;
if (errs) {
return scrollToField((_a = errs[Object.keys(errs)[0]]) === null || _a === void 0 ? void 0 : _a[0].field);
}
return true;
}
function getFirstError(errs) {
var _a;
if (errs) {
return (_a = errs[Object.keys(errs)[0]]) === null || _a === void 0 ? void 0 : _a[0].message;
}
}