UNPKG

zp-bee

Version:

zp-bee,是一款基于 Dumi,由 React + TypeScript 开发的组件库 🎉。

224 lines (194 loc) 5.86 kB
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; import mapValues from 'lodash/mapValues'; import { reaction } from 'mobx'; import FormStore, { ItemStore } from './store'; /** * 获取监听的key * @param keys * @param formStore * @returns */ export 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 */ export function getValues(formStore) { return mapValues(formStore.itemStores, function (itemStore) { return itemStore.source; }); } /** * 创建联合验证器 * @param autoValidator * @param formStore * @param autoValidatorId * @returns */ export 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(_defineProperty({}, autoValidatorId, { message: err, field: itemStore.key })); } else { itemStore.delUnionErr([autoValidatorId]); } } }; } /** * 创建自动处理器 */ export function createAutoHandle(autoHandle, formStore) { return reaction(getListenValues(autoHandle.listenKey, formStore), function () { autoHandle.action(getValues(formStore), formStore); }); } export function createUnionValidate(autoValidator, formStore, autoValidatorId) { // 去重 var listenKey = new Set(autoValidator.listenKey); listenKey.add(autoValidator.primaryKey); return 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(_defineProperty({}, autoValidatorId, { message: err, field: itemStore.key })); } else { itemStore.delUnionErr([autoValidatorId]); } } }); } export function configToItemStore(props) { return new ItemStore(props); } export 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 FormStore({ initInstances: initInstances }); return { formStore: formStore }; } export 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; } export function genFormId(id, replaceId) { if (replaceId) { return replaceId; } if (Array.isArray(id)) { return id[0]; } return id; } export 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 是否跳转成功 */ export 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; }; export 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; } export function getFirstError(errs) { var _a; if (errs) { return (_a = errs[Object.keys(errs)[0]]) === null || _a === void 0 ? void 0 : _a[0].message; } }