Commit 20a256f7f052f82bc710266500812afc928aab91
1 parent
469b0191
Exists in
master
and in
1 other branch
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 | 16 | let rules = editorRules[config.prop] || []; |
| 17 | 17 | const match = editorProps.items.find(i => i.prop === config.prop); |
| 18 | 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 | 25 | if (rules.length === 0) { |
| 22 | 26 | return false; |
| ... | ... | @@ -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 | 83 | const contentProps = context.props || {}; |
| 80 | 84 | // 编辑器统一配置 |
| ... | ... | @@ -84,10 +88,15 @@ function formItemRender(h, context, item, scope, children) { |
| 84 | 88 | formItemProp.push(scope.$index); |
| 85 | 89 | formItemProp.push(item.prop); |
| 86 | 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 | 96 | return h( |
| 88 | 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 | 101 | children, |
| 93 | 102 | ); |
| ... | ... | @@ -166,7 +175,7 @@ function editorRender(h, context, item) { |
| 166 | 175 | } |
| 167 | 176 | // 需要校验时外层嵌套校验组件 |
| 168 | 177 | if (editorProps.validate) { |
| 169 | - return formItemRender(h, context, item, scope, [vnode]); | |
| 178 | + return formItemRender(h, context, item, value, scope, [vnode]); | |
| 170 | 179 | } |
| 171 | 180 | return vnode; |
| 172 | 181 | }; | ... | ... |