Commit 20a256f7f052f82bc710266500812afc928aab91

Authored by liuhanchen
1 parent 469b0191

feat: 表格编辑器支持动态校验规则

Showing 1 changed file with 13 additions and 4 deletions   Show diff stats
packages/table/editor.vue
@@ -16,7 +16,11 @@ function isItemRequired(context, config) { @@ -16,7 +16,11 @@ function isItemRequired(context, config) {
16 let rules = editorRules[config.prop] || []; 16 let rules = editorRules[config.prop] || [];
17 const match = editorProps.items.find(i => i.prop === config.prop); 17 const match = editorProps.items.find(i => i.prop === config.prop);
18 if (match && match.rules) { 18 if (match && match.rules) {
19 - rules = match.rules; 19 + let matchRules = match.rules;
  20 + if (typeof match.rules === 'function') {
  21 + matchRules = match.rules({});
  22 + }
  23 + rules = matchRules;
20 } 24 }
21 if (rules.length === 0) { 25 if (rules.length === 0) {
22 return false; 26 return false;
@@ -74,7 +78,7 @@ function cellRender(h, context, item) { @@ -74,7 +78,7 @@ function cellRender(h, context, item) {
74 } 78 }
75 79
76 // 表单项渲染 80 // 表单项渲染
77 -function formItemRender(h, context, item, scope, children) { 81 +function formItemRender(h, context, item, value, scope, children) {
78 // 渲染函数配置 82 // 渲染函数配置
79 const contentProps = context.props || {}; 83 const contentProps = context.props || {};
80 // 编辑器统一配置 84 // 编辑器统一配置
@@ -84,10 +88,15 @@ function formItemRender(h, context, item, scope, children) { @@ -84,10 +88,15 @@ function formItemRender(h, context, item, scope, children) {
84 formItemProp.push(scope.$index); 88 formItemProp.push(scope.$index);
85 formItemProp.push(item.prop); 89 formItemProp.push(item.prop);
86 formItemProp = formItemProp.join('.'); 90 formItemProp = formItemProp.join('.');
  91 + // 处理校验规则
  92 + let itemRules = item.rules;
  93 + if (typeof item.rules === 'function') {
  94 + itemRules = item.rules({ item, value, index: scope.$index, ...scope });
  95 + }
87 return h( 96 return h(
88 editorProps.formItem || 'el-form-item', 97 editorProps.formItem || 'el-form-item',
89 { 98 {
90 - props: { prop: formItemProp, rules: item.rules || editorRules[item.prop], inlineMessage: true }, 99 + props: { prop: formItemProp, rules: itemRules || editorRules[item.prop], inlineMessage: true },
91 }, 100 },
92 children, 101 children,
93 ); 102 );
@@ -166,7 +175,7 @@ function editorRender(h, context, item) { @@ -166,7 +175,7 @@ function editorRender(h, context, item) {
166 } 175 }
167 // 需要校验时外层嵌套校验组件 176 // 需要校验时外层嵌套校验组件
168 if (editorProps.validate) { 177 if (editorProps.validate) {
169 - return formItemRender(h, context, item, scope, [vnode]); 178 + return formItemRender(h, context, item, value, scope, [vnode]);
170 } 179 }
171 return vnode; 180 return vnode;
172 }; 181 };