Skip to content

Commit cd2d8c3

Browse files
committed
feat(lib): 添加 fallback-label 参数
支持不配置title 默认使用属性名做为表单label re #45
1 parent 9b6166e commit cd2d8c3

File tree

35 files changed

+1232
-672
lines changed

35 files changed

+1232
-672
lines changed

packages/demo/demo-common/schemaTypes/15.Widgets/index.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,6 @@ export default {
112112
}
113113
},
114114
},
115-
color: {
116-
'ui:options': {
117-
size: 'small'
118-
}
119-
},
120115
},
121116
secret: {
122117
'ui:widget': 'HiddenWidget',

packages/demo/demo-v2/src/pages/index/views/Demo/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@
178178
:custom-formats="customFormats"
179179
:form-footer="trueFormFooter"
180180
:form-props="trueFormProps"
181+
:fallback-label="true"
181182
@on-form-mounted="handleFormMounted"
182183
@on-change="handleDataChange"
183184
@on-cancel="handleCancel"

packages/demo/demo-v3/src/pages/index/views/Demo/index.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
:custom-formats="customFormats"
203203
:form-footer="trueFormFooter"
204204
:form-props="trueFormProps"
205+
:fallback-label="true"
205206
@form-mounted="handleFormMounted"
206207
@change="handleDataChange"
207208
@cancel="handleCancel"

packages/docs/docs/zh/guide/basic-config.md

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ export default {
601601
* 自定义校验规则,实现类似 el-form rules validator 的方式校验表单数据
602602
* [详细查看这里](/zh/guide/validate.html#custom-rule-自定义校验)
603603

604+
### value / v-model
605+
* 类型:`object`
606+
* 默认值:`{}`
607+
608+
表单绑定值,`对于不需要双向绑定的值,可以使用 value props`
609+
604610

605611
### form-footer
606612
* 类型:`object`
@@ -618,11 +624,22 @@ formFooter = {
618624
}
619625
```
620626

621-
### value / v-model
622-
* 类型:`object`
623-
* 默认值:`{}`
627+
### fallback-label
628+
* 类型:`boolean`
629+
* default:`false`
624630

625-
表单绑定值,`对于不需要双向绑定的值,可以使用 value props`
631+
`schema` 没有配置 `title`,是否使用当前属性名做为表单 `label`
632+
633+
如:`street_address` 表单项是否显示 `label``street_address`
634+
```js
635+
schema = {
636+
properties: {
637+
street_address: {
638+
type: 'string'
639+
}
640+
}
641+
}
642+
```
626643

627644
### form-props
628645
* 类型:`object`

packages/lib/vue3/vue3-core/src/components/FieldGroupWrap.vue renamed to packages/lib/utils/components/FieldGroupWrap.vue

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
22
<div class="fieldGroupWrap">
33
<h3
4-
v-if="showTitle && title"
4+
v-if="showTitle && trueTitle"
55
class="fieldGroupWrap_title"
66
>
7-
{{ title }}
7+
{{ trueTitle }}
88
</h3>
99
<p
1010
v-if="showDescription && description"
@@ -21,7 +21,13 @@
2121
<script>
2222
export default {
2323
name: 'FieldGroupWrap',
24+
inject: ['genFormProvide'],
2425
props: {
26+
// 当前节点路径
27+
curNodePath: {
28+
type: String,
29+
default: ''
30+
},
2531
showTitle: {
2632
type: Boolean,
2733
default: true
@@ -38,6 +44,21 @@ export default {
3844
type: String,
3945
default: ''
4046
}
47+
},
48+
computed: {
49+
trueTitle() {
50+
const title = this.title;
51+
if (title) {
52+
return title;
53+
}
54+
55+
const genFormProvide = this.genFormProvide.value || this.genFormProvide;
56+
57+
const backTitle = genFormProvide.fallbackLabel && this.curNodePath.split('.').pop();
58+
if (backTitle !== `${Number(backTitle)}`) return backTitle;
59+
60+
return '';
61+
}
4162
}
4263
};
4364
</script>

packages/lib/utils/formUtils.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export function getUiOptions({
174174

175175
// 计算ui配置
176176
return {
177-
title: schema.title, // 默认使用 schema 的配置
177+
title: schema.title /* || curNodePath.split('.').pop() */, // 默认使用 schema 的配置
178178
description: schema.description,
179179

180180
// 特殊处理部分
@@ -407,3 +407,15 @@ export function optionsList(schema, uiSchema, curNodePath, rootFormData) {
407407
});
408408

409409
}
410+
411+
export function fallbackLabel(oriLabel, isFallback, curNodePath) {
412+
if (oriLabel) return oriLabel;
413+
if (isFallback) {
414+
const backLabel = curNodePath.split('.').pop();
415+
416+
// 过滤纯数字字符串
417+
if (backLabel && (backLabel !== `${Number(backLabel)}`)) return backLabel;
418+
}
419+
420+
return '';
421+
}

packages/lib/vue2/vue2-core/src/components/Widget.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import {
88

99
import { validateFormDataAndTransformMsg } from '@lljj/vjsf-utils/schema/validate';
1010
import { IconQuestion } from '@lljj/vjsf-utils/icons';
11+
import { fallbackLabel } from '@lljj/vjsf-utils/formUtils';
1112

1213
export default {
1314
name: 'Widget',
15+
inject: ['genFormProvide'],
1416
props: {
1517
// 是否同步formData的值,默认表单元素都需要
1618
// oneOf anyOf 中的select属于formData之外的数据
@@ -165,8 +167,10 @@ export default {
165167
render(h) {
166168
const self = this;
167169

170+
const { curNodePath } = this.$props;
171+
168172
// 判断是否为根节点
169-
const isRootNode = isRootNodePath(this.curNodePath);
173+
const isRootNode = isRootNodePath(curNodePath);
170174

171175
const miniDesModel = self.globalOptions.HELPERS.isMiniDes(self.formProps);
172176

@@ -212,6 +216,9 @@ export default {
212216
} : {})
213217
};
214218

219+
// 运行配置回退到 属性名
220+
const label = fallbackLabel(self.label, (self.widget && this.genFormProvide.fallbackLabel), curNodePath);
221+
215222
return h(
216223
COMPONENT_MAP.formItem,
217224
{
@@ -225,7 +232,7 @@ export default {
225232
...self.labelWidth ? { labelWidth: self.labelWidth } : {},
226233
...this.isFormData ? {
227234
// 这里对根节点打特殊标志,绕过elementUi无prop属性不校验
228-
prop: isRootNode ? '__$$root' : path2prop(self.curNodePath),
235+
prop: isRootNode ? '__$$root' : path2prop(curNodePath),
229236
rules: [
230237
{
231238
validator(rule, value, callback) {
@@ -239,15 +246,15 @@ export default {
239246
customFormats: self.$props.customFormats,
240247
errorSchema: self.errorSchema,
241248
required: self.required,
242-
propPath: path2prop(self.curNodePath)
249+
propPath: path2prop(curNodePath)
243250
});
244251
if (errors.length > 0) return callback(errors[0].message);
245252

246253
// customRule 如果存在自定义校验
247254
const curCustomRule = self.$props.customRule;
248255
if (curCustomRule && (typeof curCustomRule === 'function')) {
249256
return curCustomRule({
250-
field: self.curNodePath,
257+
field: curNodePath,
251258
value,
252259
rootFormData: self.rootFormData,
253260
callback
@@ -274,14 +281,14 @@ export default {
274281
},
275282
},
276283
[
277-
self.label ? h('span', {
284+
label ? h('span', {
278285
slot: 'label',
279286
class: {
280287
genFormLabel: true,
281288
genFormItemRequired: self.required,
282289
},
283290
}, [
284-
`${self.label}`,
291+
`${label}`,
285292
miniDescriptionVNode,
286293
`${(self.formProps && self.formProps.labelSuffix) || ''}`
287294
]) : null,

packages/lib/vue2/vue2-core/src/fields/ArrayField/arrayTypes/ArrayFieldNormal.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import { computedCurPath } from '@lljj/vjsf-utils/vueUtils';
66
import { getUiOptions, replaceArrayIndex } from '@lljj/vjsf-utils/formUtils';
77

8+
import FieldGroupWrap from '@lljj/vjsf-utils/components/FieldGroupWrap';
89
import SchemaField from '../../SchemaField';
9-
import FieldGroupWrap from '../../../components/FieldGroupWrap';
1010
import ArrayOrderList from '../components/ArrayOrderList';
1111

1212
import vueProps from '../../props';
@@ -80,7 +80,8 @@ export default {
8080
title,
8181
description,
8282
showTitle,
83-
showDescription
83+
showDescription,
84+
curNodePath
8485
},
8586
class: {
8687
...context.data.class,

packages/lib/vue2/vue2-core/src/fields/ArrayField/arrayTypes/ArrayFieldTuple.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { allowAdditionalItems, getUiOptions, replaceArrayIndex } from '@lljj/vjs
55
import getDefaultFormState from '@lljj/vjsf-utils/schema/getDefaultFormState';
66
import { computedCurPath } from '@lljj/vjsf-utils/vueUtils';
77
import { cutOff } from '@lljj/vjsf-utils/arrayUtils';
8+
import FieldGroupWrap from '@lljj/vjsf-utils/components/FieldGroupWrap';
89
import vueProps from '../../props';
910

1011
import SchemaField from '../../SchemaField';
11-
import FieldGroupWrap from '../../../components/FieldGroupWrap';
1212
import ArrayOrderList from '../components/ArrayOrderList';
1313

1414
export default {
@@ -117,7 +117,7 @@ export default {
117117
...tempUiSchema
118118
},
119119
errorSchema: errorSchema.additionalItems,
120-
curNodePath: computedCurPath(this.curNodePath, index + schema.items.length)
120+
curNodePath: computedCurPath(curNodePath, index + schema.items.length)
121121
}
122122
}
123123
)
@@ -135,7 +135,8 @@ export default {
135135
title,
136136
description,
137137
showTitle,
138-
showDescription
138+
showDescription,
139+
curNodePath
139140
},
140141
class: fieldClass,
141142
attrs: fieldAttrs,

packages/lib/vue2/vue2-core/src/fields/ObjectField/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import { orderProperties, getUiOptions } from '@lljj/vjsf-utils/formUtils';
66
import { computedCurPath, getPathVal } from '@lljj/vjsf-utils/vueUtils';
77
import { isObject } from '@lljj/vjsf-utils/utils';
8+
import FieldGroupWrap from '@lljj/vjsf-utils/components/FieldGroupWrap';
89
import vueProps from '../props';
9-
import FieldGroupWrap from '../../components/FieldGroupWrap';
1010
import Widget from '../../components/Widget';
1111

1212
// eslint-disable-next-line import/no-cycle
@@ -98,7 +98,8 @@ export default {
9898
title,
9999
description,
100100
showTitle,
101-
showDescription
101+
showDescription,
102+
curNodePath
102103
},
103104
class: { ...context.data.class, ...fieldClass },
104105
attrs: fieldAttrs,

0 commit comments

Comments
 (0)