SForm.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { useAttrs as g, ref as d, computed as f, watchEffect as v, openBlock as S, createElementBlock as w, createVNode as V, mergeProps as _, withCtx as C, renderSlot as x } from "vue";
  2. import { n as O } from "./index2.js";
  3. import { F as B } from "./index5.js";
  4. const A = { class: "s-form" }, j = {
  5. __name: "SForm",
  6. props: {
  7. // 表单数据对象
  8. model: {
  9. type: Object,
  10. default() {
  11. return {};
  12. }
  13. },
  14. // 校验规则
  15. rules: {
  16. type: Object,
  17. default() {
  18. return {};
  19. }
  20. },
  21. // 排列方向:horizontal / vertical
  22. layout: {
  23. type: String,
  24. default() {
  25. return "vertical";
  26. }
  27. },
  28. // 每行列数
  29. column: {
  30. type: [Number, String],
  31. default() {
  32. return 1;
  33. }
  34. },
  35. // 标签宽度,单位 px
  36. labelWidth: {
  37. type: [Number, String],
  38. default() {
  39. return 80;
  40. }
  41. }
  42. },
  43. setup(u, { expose: m }) {
  44. const i = g(), o = u, n = d(), p = d(o.column >= 5 ? 4 : o.column), h = /* @__PURE__ */ new Set([
  45. "disabled",
  46. "allowClear",
  47. "clearable",
  48. "showSearch",
  49. "multiple",
  50. "readonly",
  51. "autofocus",
  52. "bordered",
  53. "checked",
  54. "open",
  55. "autoClearSearchValue",
  56. "noDrag",
  57. "isManual",
  58. "expandAll",
  59. "recursionChildren",
  60. "treeCheckable",
  61. "showOnlyOrganization",
  62. "isDataScopeFilter"
  63. ]), y = (e) => typeof e == "boolean" ? e : e === "" || e === null || e === void 0 ? e === "" ? !0 : e : typeof e == "string" ? !(e === "false" || e === "0") : !!e, F = f(() => {
  64. const e = {};
  65. return Object.keys(i || {}).forEach((t) => {
  66. const l = i[t];
  67. e[t] = h.has(t) ? y(l) : l;
  68. }), e;
  69. }), b = f(() => {
  70. const e = o.layout, t = (o == null ? void 0 : o.labelWidth) + "";
  71. return e === "horizontal" ? {
  72. style: {
  73. width: t.includes("px") ? t : t + "px"
  74. }
  75. } : {};
  76. });
  77. return v(() => {
  78. o.column > 4 && console.warn(`当前设置表单列column【${o.column}】最多显示为4列,已默认为最多4列`);
  79. }), m({
  80. validate: (e, t) => {
  81. const l = typeof e == "function" ? e : typeof t == "function" ? t : null, r = typeof e == "function" ? void 0 : e;
  82. return new Promise((s, a) => {
  83. (r && r.length ? n.value.validateFields(r) : n.value.validate()).then((c) => {
  84. l && l(!0), s(c);
  85. }).catch((c) => {
  86. if (l) {
  87. l(!1, c), s(void 0);
  88. return;
  89. }
  90. a(c);
  91. });
  92. });
  93. },
  94. validateField: (e, t) => {
  95. const l = typeof t == "function" ? t : null;
  96. return new Promise((r, s) => {
  97. if (!n.value) {
  98. r(void 0);
  99. return;
  100. }
  101. n.value.validateFields(e).then((a) => {
  102. l && l(!0), r(a);
  103. }).catch((a) => {
  104. if (l) {
  105. l(!1, a), r(void 0);
  106. return;
  107. }
  108. s(a);
  109. });
  110. });
  111. },
  112. clearValidate: (e) => {
  113. n.value && n.value.clearValidate(e);
  114. },
  115. resetFields: (e) => {
  116. n.value && n.value.resetFields(e);
  117. },
  118. getFieldsValue: (e) => n.value ? n.value.getFieldsValue(e) : {},
  119. setFieldsValue: (e) => {
  120. n.value && n.value.setFieldsValue(e);
  121. }
  122. }), (e, t) => {
  123. const l = B;
  124. return S(), w("div", A, [
  125. V(l, _({
  126. labelCol: b.value.labelCol,
  127. model: u.model,
  128. rules: u.rules,
  129. layout: u.layout
  130. }, F.value, {
  131. ref_key: "form",
  132. ref: n,
  133. style: {
  134. display: "grid",
  135. gridTemplateColumns: `repeat(${p.value}, 1fr)`,
  136. gap: "20px"
  137. }
  138. }), {
  139. default: C(() => [
  140. x(e.$slots, "default", {}, void 0, !0)
  141. ]),
  142. _: 3
  143. }, 16, ["labelCol", "model", "rules", "layout", "style"])
  144. ]);
  145. };
  146. }
  147. }, M = /* @__PURE__ */ O(j, [["__scopeId", "data-v-678f5a2b"]]);
  148. export {
  149. M as default
  150. };