UNPKG

ng-layout-form

Version:

layout form

183 lines (181 loc) 6.66 kB
var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/formset/index.tsx var formset_exports = {}; __export(formset_exports, { FormFieldSet: () => import_FormFieldSet2.default, FormSetComp: () => FormSetComp, default: () => formset_default }); module.exports = __toCommonJS(formset_exports); var import_ng_lib_tsx = require("ng-lib-tsx"); var import_react = require("react"); var import__ = require("../"); var import_util = require("../util/util"); var import_FormFieldSet = require("./FormFieldSet"); var import_index = require("./index.less"); var import_FormFieldSet2 = __toESM(require("./FormFieldSet")); function FormSetComp(props) { const { formConf = [], opt, defaultValue: dv, value, initSc = {}, outRef, dataId, onValuesChange, style: formSetStyle, className: formSetClassName, rules, collapsible, collapse: defaultCollapse //展开 } = props; const defaultValue = (0, import_react.useMemo)(() => { return value || dv || {}; }, [value, dv]); const s = Array.isArray(formConf) ? formConf : formConf.children; const { query } = import_ng_lib_tsx.history.location; const optType = opt || query.opt; const defaultCollapsible = collapsible ?? formConf.collapsible ?? true; const collapse = defaultCollapse !== void 0 ? defaultCollapse : formConf.collapse; function getFormatValues() { const id = dataId || "PhId"; const opt2 = optType || (defaultValue == null ? void 0 : defaultValue[id]) ? "modifiedRow" : "newRow"; let g = (0, import__.fomatValues)("COMMON_KEY", "form", opt2, id)(defaultValue); s.forEach((item) => { g = g(getData(item.name || item.itemId, "getValues", false)); }); return g()["COMMON_KEY"]; } function getValues() { let v = {}; s.forEach((item) => { v = { ...v, ...getData(item.name || item.itemId, "getValues", false) }; }); return { ...defaultValue, ...v }; } function mergeHandler(v) { s.forEach((item) => { const key = item.name || item.itemId; const t = import_ng_lib_tsx.NG.getCmp(key); t && t.getApi().mergeHandler(v[key]); }); } (0, import_ng_lib_tsx.useExtendRef)(outRef, { mergeHandler, getValues, getFormatValues, validateForm, isValid, getItem: (name) => { var _a, _b; for (let i = 0; i < s.length; i++) { const t = (_b = (_a = import_ng_lib_tsx.NG.getCmpApi(s[i].name || s[i].itemId)) == null ? void 0 : _a.getItem) == null ? void 0 : _b.call(_a, name); if (t) return t; } }, isChanged() { return s.some((item) => { var _a; return (_a = import_ng_lib_tsx.NG.getCmpApi(item.name || item.itemId)) == null ? void 0 : _a.isChanged(); }); }, getConfig: () => formConf, getForm: (key) => { var _a; return (_a = import_ng_lib_tsx.NG.getCmp(key)) == null ? void 0 : _a.getApi(); }, subscribe: (0, import__.getSubscribeFn)(outRef) }); function validateForm() { return Promise.all(s.map((item) => getData(item.name || item.itemId, "validateForm"))); } function isValid() { return Promise.all(s.map((item) => getData(item.name || item.itemId, "isValid"))).then((result) => { return result.every((r) => r === true); }); } const defaultConfig = (0, import_react.useMemo)(() => { const cfg = { ...Array.isArray(formConf) ? {} : formConf }; delete cfg.children; return cfg; }, [formConf]); const containerId = `${props.id || props["data-cid"] || defaultConfig.name || "formset"}`; return /* @__PURE__ */ React.createElement( "div", { id: props.id || props["data-cid"], className: import_ng_lib_tsx.NG.classNames("formSetContainer", formSetClassName), style: formSetStyle }, s && s.map((item) => { const name = item.name || item.itemId; return item.hidden ? null : /* @__PURE__ */ React.createElement( ItemRender, { defaultConfig, item, title: item.title || item.label, initSc: initSc[name], id: name, cid: containerId, key: name, formSetRef: outRef, layout: "horizontal", labelPosition: "left", opt: optType, rules, disabled: props.disabled, collapsible: item.collapsible ?? (defaultCollapsible && (item.title || item.title)), collapse: typeof collapse === "boolean" ? collapse : collapse == null ? void 0 : collapse[name], value: defaultValue, onValuesChange } ); }) ); } function ItemRender({ item, defaultConfig, ...others }) { const cfg = (0, import_react.useMemo)(() => ({ ...defaultConfig, ...item }), [defaultConfig, item]); return /* @__PURE__ */ React.createElement(import_FormFieldSet.FormFieldSet, { formConf: cfg, ...others }); } function getData(id, key, ...args) { const t = import_ng_lib_tsx.NG.getCmp(id).getApi(); return t && t[key](...args) || {}; } var FormSet = (0, import_ng_lib_tsx.compHoc)((0, import_util.LayConfWrap)(FormSetComp), "NGFormSet"); var formset_default = FormSet; (0, import_ng_lib_tsx.registerComponent)({ FormSet }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FormFieldSet, FormSetComp });