index5.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. import { aC as Me, H as Ae, aD as Ce, aE as ie, aF as Ee, d as y, aG as he, i as Le, u as qe, aH as Ie, aI as Se, aJ as je, c as Ve, _ as le, M as $, O as z, Q as se, N as oe, f as _e, e as Q, P as ue, aK as Te, aL as Z } from "./index2.js";
  2. import { ref as ve, unref as _, reactive as Ne, shallowRef as Re, watch as U, toRaw as de, nextTick as xe, defineComponent as De, computed as P, createVNode as $e } from "vue";
  3. import { c as Be, g as Ge, a as ze, d as ee, v as We, e as Fe, _ as Ke, u as He, f as Je, h as ce, t as Y, i as fe, j as Qe } from "./FormItem.js";
  4. import { s as Ue } from "./index3.js";
  5. import { i as Ye, a as be, b as Xe, c as Pe, t as Ze } from "./hasIn.js";
  6. import { s as et, o as tt, f as rt } from "./_flatRest.js";
  7. import { d as nt } from "./debounce.js";
  8. import { i as at } from "./isPlainObject.js";
  9. import { u as it, a as re } from "./FormItemContext.js";
  10. function lt(e, t) {
  11. return et(tt(e, t, Ye), e + "");
  12. }
  13. function st(e, t, n) {
  14. var s = -1, a = e.length;
  15. t < 0 && (t = -t > a ? 0 : a + t), n = n > a ? a : n, n < 0 && (n += a), a = t > n ? 0 : n - t >>> 0, t >>>= 0;
  16. for (var o = Array(a); ++s < a; )
  17. o[s] = e[s + t];
  18. return o;
  19. }
  20. function ot(e) {
  21. return Me(e) && Ae(e);
  22. }
  23. function ut(e) {
  24. var t = e == null ? 0 : e.length;
  25. return t ? e[t - 1] : void 0;
  26. }
  27. var dt = Math.min;
  28. function ct(e, t, n) {
  29. for (var s = Ee, a = e[0].length, o = e.length, m = o, p = Array(o), F = 1 / 0, O = []; m--; ) {
  30. var k = e[m];
  31. F = dt(k.length, F), p[m] = a >= 120 && k.length >= 120 ? new Ce(m && k) : void 0;
  32. }
  33. k = e[0];
  34. var A = -1, C = p[0];
  35. e:
  36. for (; ++A < a && O.length < F; ) {
  37. var E = k[A], M = E;
  38. if (E = E !== 0 ? E : 0, !(C ? ie(C, M) : s(O, M))) {
  39. for (m = o; --m; ) {
  40. var q = p[m];
  41. if (!(q ? ie(q, M) : s(e[m], M)))
  42. continue e;
  43. }
  44. C && C.push(M), O.push(E);
  45. }
  46. }
  47. return O;
  48. }
  49. function ft(e) {
  50. return ot(e) ? e : [];
  51. }
  52. var mt = lt(function(e) {
  53. var t = be(e, ft);
  54. return t.length && t[0] === e[0] ? ct(t) : [];
  55. });
  56. function gt(e, t) {
  57. return t.length < 2 ? e : Xe(e, st(t, 0, -1));
  58. }
  59. function ht(e, t) {
  60. return t = Pe(t, e), e = gt(e, t), e == null || delete e[Ze(ut(t))];
  61. }
  62. function vt(e) {
  63. return at(e) ? void 0 : e;
  64. }
  65. var Ft = 1, bt = 2, Pt = 4, pt = rt(function(e, t) {
  66. var n = {};
  67. if (e == null)
  68. return n;
  69. var s = !1;
  70. t = be(t, function(o) {
  71. return o = Pe(o, e), s || (s = o.length > 1), o;
  72. }), Be(e, Ge(e), n), s && (n = ze(n, Ft | bt | Pt, vt));
  73. for (var a = t.length; a--; )
  74. ht(n, t[a]);
  75. return n;
  76. });
  77. function pe(e) {
  78. let t = !1, n = e.length;
  79. const s = [];
  80. return e.length ? new Promise((a, o) => {
  81. e.forEach((m, p) => {
  82. m.catch((F) => (t = !0, F)).then((F) => {
  83. n -= 1, s[p] = F, !(n > 0) && (t && o(s), a(s));
  84. });
  85. });
  86. }) : Promise.resolve([]);
  87. }
  88. function me(e) {
  89. let t = !1;
  90. return e && e.length && e.every((n) => n.required ? (t = !0, !1) : !0), t;
  91. }
  92. function ge(e) {
  93. return e == null ? [] : Array.isArray(e) ? e : [e];
  94. }
  95. function te(e, t, n) {
  96. let s = e;
  97. t = t.replace(/\[(\w+)\]/g, ".$1"), t = t.replace(/^\./, "");
  98. const a = t.split(".");
  99. let o = 0;
  100. for (let m = a.length; o < m - 1 && !(!s && !n); ++o) {
  101. const p = a[o];
  102. if (p in s)
  103. s = s[p];
  104. else {
  105. if (n)
  106. throw new Error("please transfer a valid name path to validate!");
  107. break;
  108. }
  109. }
  110. return {
  111. o: s,
  112. k: a[o],
  113. v: s ? s[a[o]] : null,
  114. isValid: s && a[o] in s
  115. };
  116. }
  117. function yt(e) {
  118. let t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : ve({}), n = arguments.length > 2 ? arguments[2] : void 0;
  119. const s = ee(_(e)), a = Ne({}), o = Re([]), m = (l) => {
  120. y(_(e), y(y({}, ee(s)), l)), xe(() => {
  121. Object.keys(a).forEach((i) => {
  122. a[i] = {
  123. autoLink: !1,
  124. required: me(_(t)[i])
  125. };
  126. });
  127. });
  128. }, p = function() {
  129. let l = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [], i = arguments.length > 1 ? arguments[1] : void 0;
  130. return i.length ? l.filter((c) => {
  131. const h = ge(c.trigger || "change");
  132. return mt(h, i).length;
  133. }) : l;
  134. };
  135. let F = null;
  136. const O = function(l) {
  137. let i = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {}, c = arguments.length > 2 ? arguments[2] : void 0;
  138. const h = [], g = {};
  139. for (let b = 0; b < l.length; b++) {
  140. const v = l[b], L = te(_(e), v, c);
  141. if (!L.isValid) continue;
  142. g[v] = L.v;
  143. const R = p(_(t)[v], ge(i && i.trigger));
  144. R.length && h.push(k(v, L.v, R, i || {}).then(() => ({
  145. name: v,
  146. errors: [],
  147. warnings: []
  148. })).catch((x) => {
  149. const D = [], B = [];
  150. return x.forEach((r) => {
  151. let {
  152. rule: {
  153. warningOnly: u
  154. },
  155. errors: f
  156. } = r;
  157. u ? B.push(...f) : D.push(...f);
  158. }), D.length ? Promise.reject({
  159. name: v,
  160. errors: D,
  161. warnings: B
  162. }) : {
  163. name: v,
  164. errors: D,
  165. warnings: B
  166. };
  167. }));
  168. }
  169. const d = pe(h);
  170. F = d;
  171. const I = d.then(() => F === d ? Promise.resolve(g) : Promise.reject([])).catch((b) => {
  172. const v = b.filter((L) => L && L.errors.length);
  173. return v.length ? Promise.reject({
  174. values: g,
  175. errorFields: v,
  176. outOfDate: F !== d
  177. }) : Promise.resolve(g);
  178. });
  179. return I.catch((b) => b), I;
  180. }, k = function(l, i, c) {
  181. let h = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : {};
  182. const g = We([l], i, c, y({
  183. validateMessages: Fe
  184. }, h), !!h.validateFirst);
  185. return a[l] ? (a[l].validateStatus = "validating", g.catch((d) => d).then(function() {
  186. let d = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : [];
  187. var I;
  188. if (a[l].validateStatus === "validating") {
  189. const b = d.filter((v) => v && v.errors.length);
  190. a[l].validateStatus = b.length ? "error" : "success", a[l].help = b.length ? b.map((v) => v.errors) : null, (I = n == null ? void 0 : n.onValidate) === null || I === void 0 || I.call(n, l, !b.length, b.length ? de(a[l].help[0]) : null);
  191. }
  192. }), g) : g.catch((d) => d);
  193. }, A = (l, i) => {
  194. let c = [], h = !0;
  195. l ? Array.isArray(l) ? c = l : c = [l] : (h = !1, c = o.value);
  196. const g = O(c, i || {}, h);
  197. return g.catch((d) => d), g;
  198. }, C = (l) => {
  199. let i = [];
  200. l ? Array.isArray(l) ? i = l : i = [l] : i = o.value, i.forEach((c) => {
  201. a[c] && y(a[c], {
  202. validateStatus: "",
  203. help: null
  204. });
  205. });
  206. }, E = (l) => {
  207. const i = {
  208. autoLink: !1
  209. }, c = [], h = Array.isArray(l) ? l : [l];
  210. for (let g = 0; g < h.length; g++) {
  211. const d = h[g];
  212. (d == null ? void 0 : d.validateStatus) === "error" && (i.validateStatus = "error", d.help && c.push(d.help)), i.required = i.required || (d == null ? void 0 : d.required);
  213. }
  214. return i.help = c, i;
  215. };
  216. let M = s, q = !0;
  217. const W = (l) => {
  218. const i = [];
  219. o.value.forEach((c) => {
  220. const h = te(l, c, !1), g = te(M, c, !1);
  221. (q && (n == null ? void 0 : n.immediate) && h.isValid || !he(h.v, g.v)) && i.push(c);
  222. }), A(i, {
  223. trigger: "change"
  224. }), q = !1, M = ee(de(l));
  225. }, N = n == null ? void 0 : n.debounce;
  226. let K = !0;
  227. return U(t, () => {
  228. o.value = t ? Object.keys(_(t)) : [], !K && n && n.validateOnRuleChange && A(), K = !1;
  229. }, {
  230. deep: !0,
  231. immediate: !0
  232. }), U(o, () => {
  233. const l = {};
  234. o.value.forEach((i) => {
  235. l[i] = y({}, a[i], {
  236. autoLink: !1,
  237. required: me(_(t)[i])
  238. }), delete a[i];
  239. });
  240. for (const i in a)
  241. Object.prototype.hasOwnProperty.call(a, i) && delete a[i];
  242. y(a, l);
  243. }, {
  244. immediate: !0
  245. }), U(e, N && N.wait ? nt(W, N.wait, pt(N, ["wait"])) : W, {
  246. immediate: n && !!n.immediate,
  247. deep: !0
  248. }), {
  249. modelRef: e,
  250. rulesRef: t,
  251. initialModel: s,
  252. validateInfos: a,
  253. resetFields: m,
  254. validate: A,
  255. validateField: k,
  256. mergeValidateInfo: E,
  257. clearValidate: C
  258. };
  259. }
  260. const wt = () => ({
  261. layout: ue.oneOf(Te("horizontal", "inline", "vertical")),
  262. labelCol: Q(),
  263. wrapperCol: Q(),
  264. colon: z(),
  265. labelAlign: se(),
  266. labelWrap: z(),
  267. prefixCls: String,
  268. requiredMark: oe([String, Boolean]),
  269. /** @deprecated Will warning in future branch. Pls use `requiredMark` instead. */
  270. hideRequiredMark: z(),
  271. model: ue.object,
  272. rules: Q(),
  273. validateMessages: Q(),
  274. validateOnRuleChange: z(),
  275. // 提交失败自动滚动到第一个错误字段
  276. scrollToFirstError: _e(),
  277. onSubmit: $(),
  278. name: String,
  279. validateTrigger: oe([String, Array]),
  280. size: se(),
  281. disabled: z(),
  282. onValuesChange: $(),
  283. onFieldsChange: $(),
  284. onFinish: $(),
  285. onFinishFailed: $(),
  286. onValidate: $()
  287. });
  288. function Ot(e, t) {
  289. return he(Y(e), Y(t));
  290. }
  291. const T = De({
  292. compatConfig: {
  293. MODE: 3
  294. },
  295. name: "AForm",
  296. inheritAttrs: !1,
  297. props: Le(wt(), {
  298. layout: "horizontal",
  299. hideRequiredMark: !1,
  300. colon: !0
  301. }),
  302. Item: Ke,
  303. useForm: yt,
  304. // emits: ['finishFailed', 'submit', 'finish', 'validate'],
  305. setup(e, t) {
  306. let {
  307. emit: n,
  308. slots: s,
  309. expose: a,
  310. attrs: o
  311. } = t;
  312. const {
  313. prefixCls: m,
  314. direction: p,
  315. form: F,
  316. size: O,
  317. disabled: k
  318. } = qe("form", e), A = P(() => e.requiredMark === "" || e.requiredMark), C = P(() => {
  319. var r;
  320. return A.value !== void 0 ? A.value : F && ((r = F.value) === null || r === void 0 ? void 0 : r.requiredMark) !== void 0 ? F.value.requiredMark : !e.hideRequiredMark;
  321. });
  322. Ie(O), Se(k);
  323. const E = P(() => {
  324. var r, u;
  325. return (r = e.colon) !== null && r !== void 0 ? r : (u = F.value) === null || u === void 0 ? void 0 : u.colon;
  326. }), {
  327. validateMessages: M
  328. } = je(), q = P(() => y(y(y({}, Fe), M.value), e.validateMessages)), [W, N] = He(m), K = P(() => Ve(m.value, {
  329. [`${m.value}-${e.layout}`]: !0,
  330. [`${m.value}-hide-required-mark`]: C.value === !1,
  331. [`${m.value}-rtl`]: p.value === "rtl",
  332. [`${m.value}-${O.value}`]: O.value
  333. }, N.value)), l = ve(), i = {}, c = (r, u) => {
  334. i[r] = u;
  335. }, h = (r) => {
  336. delete i[r];
  337. }, g = (r) => {
  338. const u = !!r, f = u ? Y(r).map(fe) : [];
  339. return u ? Object.values(i).filter((w) => f.findIndex((j) => Ot(j, w.fieldName.value)) > -1) : Object.values(i);
  340. }, d = (r) => {
  341. if (!e.model) {
  342. Z(!1, "Form", "model is required for resetFields to work.");
  343. return;
  344. }
  345. g(r).forEach((u) => {
  346. u.resetField();
  347. });
  348. }, I = (r) => {
  349. g(r).forEach((u) => {
  350. u.clearValidate();
  351. });
  352. }, b = (r) => {
  353. const {
  354. scrollToFirstError: u
  355. } = e;
  356. if (n("finishFailed", r), u && r.errorFields.length) {
  357. let f = {};
  358. typeof u == "object" && (f = u), L(r.errorFields[0].name, f);
  359. }
  360. }, v = function() {
  361. return D(...arguments);
  362. }, L = function(r) {
  363. let u = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
  364. const f = g(r ? [r] : void 0);
  365. if (f.length) {
  366. const w = f[0].fieldId.value, j = w ? document.getElementById(w) : null;
  367. j && Ue(j, y({
  368. scrollMode: "if-needed",
  369. block: "nearest"
  370. }, u));
  371. }
  372. }, R = function() {
  373. let r = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : !0;
  374. if (r === !0) {
  375. const u = [];
  376. return Object.values(i).forEach((f) => {
  377. let {
  378. namePath: w
  379. } = f;
  380. u.push(w.value);
  381. }), ce(e.model, u);
  382. } else
  383. return ce(e.model, r);
  384. }, x = (r, u) => {
  385. if (Z(!(r instanceof Function), "Form", "validateFields/validateField/validate not support callback, please use promise instead"), !e.model)
  386. return Z(!1, "Form", "model is required for validateFields to work."), Promise.reject("Form `model` is required for validateFields to work.");
  387. const f = !!r, w = f ? Y(r).map(fe) : [], j = [];
  388. Object.values(i).forEach((S) => {
  389. var G;
  390. if (f || w.push(S.namePath.value), !(!((G = S.rules) === null || G === void 0) && G.value.length))
  391. return;
  392. const V = S.namePath.value;
  393. if (!f || Qe(w, V)) {
  394. const ye = S.validateRules(y({
  395. validateMessages: q.value
  396. }, u));
  397. j.push(ye.then(() => ({
  398. name: V,
  399. errors: [],
  400. warnings: []
  401. })).catch((we) => {
  402. const J = [], X = [];
  403. return we.forEach((Oe) => {
  404. let {
  405. rule: {
  406. warningOnly: ke
  407. },
  408. errors: ae
  409. } = Oe;
  410. ke ? X.push(...ae) : J.push(...ae);
  411. }), J.length ? Promise.reject({
  412. name: V,
  413. errors: J,
  414. warnings: X
  415. }) : {
  416. name: V,
  417. errors: J,
  418. warnings: X
  419. };
  420. }));
  421. }
  422. });
  423. const H = pe(j);
  424. l.value = H;
  425. const ne = H.then(() => l.value === H ? Promise.resolve(R(w)) : Promise.reject([])).catch((S) => {
  426. const G = S.filter((V) => V && V.errors.length);
  427. return Promise.reject({
  428. values: R(w),
  429. errorFields: G,
  430. outOfDate: l.value !== H
  431. });
  432. });
  433. return ne.catch((S) => S), ne;
  434. }, D = function() {
  435. return x(...arguments);
  436. }, B = (r) => {
  437. r.preventDefault(), r.stopPropagation(), n("submit", r), e.model && x().then((f) => {
  438. n("finish", f);
  439. }).catch((f) => {
  440. b(f);
  441. });
  442. };
  443. return a({
  444. resetFields: d,
  445. clearValidate: I,
  446. validateFields: x,
  447. getFieldsValue: R,
  448. validate: v,
  449. scrollToField: L
  450. }), Je({
  451. model: P(() => e.model),
  452. name: P(() => e.name),
  453. labelAlign: P(() => e.labelAlign),
  454. labelCol: P(() => e.labelCol),
  455. labelWrap: P(() => e.labelWrap),
  456. wrapperCol: P(() => e.wrapperCol),
  457. vertical: P(() => e.layout === "vertical"),
  458. colon: E,
  459. requiredMark: C,
  460. validateTrigger: P(() => e.validateTrigger),
  461. rules: P(() => e.rules),
  462. addField: c,
  463. removeField: h,
  464. onValidate: (r, u, f) => {
  465. n("validate", r, u, f);
  466. },
  467. validateMessages: q
  468. }), U(() => e.rules, () => {
  469. e.validateOnRuleChange && x();
  470. }), () => {
  471. var r;
  472. return W($e("form", le(le({}, o), {}, {
  473. onSubmit: B,
  474. class: [K.value, o.class]
  475. }), [(r = s.default) === null || r === void 0 ? void 0 : r.call(s)]));
  476. };
  477. }
  478. });
  479. T.useInjectFormItemContext = it;
  480. T.ItemRest = re;
  481. T.install = function(e) {
  482. return e.component(T.name, T), e.component(T.Item.name, T.Item), e.component(re.name, re), e;
  483. };
  484. export {
  485. T as F
  486. };