Skip to content

Commit 5bc285c

Browse files
committed
refactor: 优化代码,不需要this的组件使用 functional
1.不需要this的组件使用 functional 2.优化formUtils代码 3.更正补充文档 docs: 补充未完善文档
1 parent b85b69f commit 5bc285c

File tree

17 files changed

+106
-112
lines changed

17 files changed

+106
-112
lines changed

packages/demo/src/index/views/Demo/schemaTypes/1.Simple/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export default {
7373
'ui:options': {
7474
placeholder: '请输入你的签名',
7575
type: 'textarea',
76-
rows: 6
76+
attrs: {
77+
rows: 6
78+
}
7779
}
7880
}
7981
},

packages/demo/src/index/views/Demo/schemaTypes/5.Widgets/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ export default {
104104
textarea: {
105105
'ui:options': {
106106
type: 'textarea',
107-
rows: 6,
107+
attrs: {
108+
rows: 6,
109+
},
108110
autosize: {
109111
minRows: 2,
110112
maxRows: 4

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ uiSchema = {
139139

140140
// 其它所有参数会通过 props 传递给 widget 组件
141141
type: 'textarea',
142-
rows: '6',
143142
placeholder: '请输入你的内容'
144143
}
145144
}
@@ -196,7 +195,8 @@ export default {
196195
'ui:emptyValue': '',
197196
'ui:options': {
198197
attrs: {
199-
'autofocus': true
198+
'autofocus': true,
199+
rows: '6'
200200
},
201201
style: {
202202
boxShadow: '0 0 6px 2px #2b9939'
@@ -205,7 +205,6 @@ export default {
205205
className_hei: true
206206
},
207207
type: 'textarea',
208-
rows: '6',
209208
placeholder: '请输入你的内容'
210209
}
211210
},

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export default {
9999
* 默认值:`{}`
100100

101101
>* `0.0.16` 之后版本支持配置 `uiSchema``schema` 参数中 [点击查看](#uischema配置在schema中)
102-
>* `0.0.17` 之后版本支持配置 [errorSchema](#errorschema)[uiSchema](#uischema) 中。(`uiSchema``errorSchema` 格式完全相同,且同属ui显示,一份可方便配置)
102+
>* `0.1.0` 之后版本支持配置 [errorSchema](#errorschema)[uiSchema](#uischema) 中。(`uiSchema``errorSchema` 格式完全相同,且同属ui显示,一份可方便配置)
103103
104104
用于配置表单展示样式,普通json数据,非 `JSON Schema` 规范
105105

@@ -114,7 +114,7 @@ export default {
114114
* `ui:widget` `ui:field` `ui:fieldProps` 不支持配置在 `ui:options`
115115
:::
116116

117-
参数格式如下
117+
通用参数格式如下
118118
```js
119119
uiSchema = {
120120
'ui:title': '覆盖schema title', // 覆盖schema title
@@ -128,21 +128,35 @@ uiSchema = {
128128
showTitle: '是否显示标题', // 只对 type为`object`、`array` 类型有效
129129
showDescription: '是否显示描述信息', // 只对type为 `object`、`array` 类型有效
130130
attrs: {
131-
// 通过 vue render函数 attrs 传递给 widget 组件
131+
// 通过 vue render函数 attrs 传递给 Widget 组件,只能配置在叶子节点
132132
autofocus: true
133133
},
134134
style: {
135-
// 通过 vue render函数 style 传递给 widget 组件
135+
// 通过 vue render函数 style 传递给 Widget 组件,只能配置在叶子节点
136136
boxShadow: '0 0 6px 2px #2b9939'
137137
},
138138
class: {
139-
// 通过 vue render函数 class 传递给 widget 组件
139+
// 0.1.0 版本添加
140+
// 通过 vue render函数 class 传递给 Widget 组件,只能配置在叶子节点
140141
className_hei: true
141142
},
143+
fieldStyle: {
144+
// 0.1.0 版本添加
145+
// 通过 vue render函数 style 传递给 Field 组件,支持所有field节点
146+
background: 'red'
147+
},
148+
fieldClass: {
149+
// 0.1.0 版本添加
150+
// 通过 vue render函数 class 传递给 Field 组件,支持所有field节点
151+
fieldClass: true
152+
},
153+
fieldAttrs: {
154+
// 通过 vue render函数 attrs 传递给 Field 组件,支持所有field节点
155+
'attr-x': 'xxx'
156+
},
142157

143-
// 其它所有参数会通过 props 传递给 widget 组件
158+
// 其它所有参数会通过 props 传递给 Widget 组件
144159
type: 'textarea',
145-
rows: '6',
146160
placeholder: '请输入你的内容'
147161
}
148162
}
@@ -343,7 +357,7 @@ uiSchema = {
343357
* 默认值:`{}`
344358

345359
>* `0.0.16` 之后版本支持配置 `errorSchema``schema` 参数中 [点击查看](#errorschema配置在schema中)
346-
>* `0.0.17` 之后版本支持配置 [errorSchema](#errorschema)[uiSchema](#uischema) 中。(`uiSchema``errorSchema` 格式完全相同,且同属ui显示,一份可方便配置)
360+
>* `0.1.0` 之后版本支持配置 [errorSchema](#errorschema)[uiSchema](#uischema) 中。(`uiSchema``errorSchema` 格式完全相同,且同属ui显示,一份可方便配置)
347361
348362
用于配置表单校验错误文案信息,普通json数据,非JSON Schema规范
349363

packages/docs/docs/zh/guide/todo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
- [x] ui:widget 支持在array级别传入
2020
- [x] customRule 参数 支持
2121
- [x] 逐步开源发布
22+
- [x 优化源码 不需要this的组件调整为 functional
2223

2324
- [ ] 添加代码测试
2425
- [ ] Ui配置,支持函数表达式配置,hidden title description placeholder等
25-
- [ ] 优化源码 不需要this的组件调整为 functional
2626
- [ ] 对照react schema from适配更多规则支持
2727
- [ ] 解耦elementUi 重新开发form 和formItem组件,通过配置化实现适配elementUi iView 等常用ui组件

packages/docs/docs/zh/rules/array.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,18 @@
253253
```
254254
:::
255255

256+
## 其它配置
257+
* 支持通过配置 `uiSchema` `ui:addable``ui:sortable``ui:removable` 配置数组是否可添加/排序/移除
258+
* 参见 [uiSchema 配置](https://form.lljj.me/#/demo?type=Arrays)
259+
260+
比如:
261+
```js
262+
// 支持 * 匹配其它值
263+
uiSchema = {
264+
'ui:options': {
265+
addable: false,
266+
sortable: false,
267+
removable: false
268+
}
269+
}
270+
```

packages/lib/src/JsonSchemaForm/common/formUtils.js

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,12 @@ import retrieveSchema from './schema/retriev';
44

55
import { isObject, getSchemaType } from './utils';
66

7-
// 解析当前节点 ui widget
8-
export function getUiWidget({
9-
schema = {},
10-
uiSchema = {}
11-
}, fallback) {
12-
// uiSchema 配置了widget 直接使用
13-
const uiWidget = uiSchema['ui:widget'] || schema['ui:widget'];
14-
15-
if (uiWidget) {
16-
return {
17-
widget: uiWidget
18-
};
19-
}
20-
21-
if (fallback) {
22-
// 没配置widget 回退到具体field方案配置
23-
return fallback({ schema, uiSchema });
24-
}
25-
26-
return {};
27-
}
28-
297
// 是否为 hidden Widget
308
export function isHiddenWidget({
319
schema = {},
3210
uiSchema = {}
3311
}) {
34-
const { widget } = getUiWidget({
35-
schema,
36-
uiSchema,
37-
});
38-
12+
const widget = uiSchema['ui:widget'] || schema['ui:widget'];
3913
return widget === 'HiddenWidget' || widget === 'hidden';
4014
}
4115

@@ -90,7 +64,6 @@ export function getUserUiOptions({
9064
}
9165
return { ...options, [key.substring(3)]: value };
9266
}, {})));
93-
9467
}
9568

9669
// 解析当前节点的ui options参数
@@ -126,10 +99,6 @@ export function getUiOptions({
12699
return {
127100
title: schema.title, // 默认使用 schema 的配置
128101
description: schema.description,
129-
hidden: isHiddenWidget({
130-
schema,
131-
uiSchema
132-
}),
133102

134103
// 特殊处理部分
135104
...spec,
@@ -147,17 +116,19 @@ export function getUiOptions({
147116
export function getWidgetConfig({
148117
schema = {},
149118
uiSchema = {}
150-
}, fallback = () => {}) {
151-
const widgetConfig = {
152-
...getUiOptions({
153-
schema,
154-
uiSchema
155-
}),
156-
...getUiWidget({
119+
}, fallback = null) {
120+
const uiOptions = getUiOptions({
121+
schema,
122+
uiSchema
123+
});
124+
125+
// 没有配置 Widget ,各个Field组件根据类型判断
126+
if (!uiOptions.widget && fallback) {
127+
Object.assign(uiOptions, fallback({
157128
schema,
158129
uiSchema
159-
}, fallback)
160-
};
130+
}));
131+
}
161132

162133
const {
163134
widget,
@@ -172,7 +143,7 @@ export function getWidgetConfig({
172143
fieldClass,
173144
emptyValue,
174145
...uiProps
175-
} = widgetConfig;
146+
} = uiOptions;
176147

177148
return {
178149
widget,

packages/lib/src/JsonSchemaForm/fields/ArrayField/arrayTypes/ArrayFieldDateRange.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default {
2424
return h(
2525
Widget,
2626
{
27+
...context.data,
2728
props: {
2829
...context.props,
2930
...widgetConfig

packages/lib/src/JsonSchemaForm/fields/ArrayField/arrayTypes/ArrayFieldMultiSelect.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@ import Widget from '../../../fieldComponents/Widget';
1414

1515
export default {
1616
name: 'ArrayFieldMultiSelect',
17+
functional: true,
1718
props: {
18-
...vueProps,
19-
itemsFormData: {
20-
type: Array,
21-
default: () => []
22-
}
19+
...vueProps
2320
},
24-
render(h) {
21+
render(h, context) {
22+
const {
23+
schema, rootSchema, uiSchema
24+
} = context.props;
25+
2526
// 这里需要索引当前节点,通过到schemaField组件的会统一处理
26-
const itemsSchema = retrieveSchema(this.schema.items, this.rootSchema, this.itemsFormData);
27-
const enumOptions = optionsList(itemsSchema, this.uiSchema);
27+
const itemsSchema = retrieveSchema(schema.items, rootSchema);
28+
29+
const enumOptions = optionsList(itemsSchema, uiSchema);
2830

2931
const widgetConfig = getWidgetConfig({
30-
schema: this.schema,
31-
uiSchema: this.uiSchema
32+
schema,
33+
uiSchema
3234
}, () => ({
3335
widget: WIDGET_MAP.common.checkboxGroup
3436
}));
@@ -41,8 +43,9 @@ export default {
4143
return h(
4244
Widget,
4345
{
46+
...context.data,
4447
props: {
45-
...this.$props,
48+
...context.props,
4649
...widgetConfig
4750
}
4851
}

packages/lib/src/JsonSchemaForm/fields/ArrayField/arrayTypes/ArrayFieldNormal.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@ import vueProps from '../../props';
1313

1414
export default {
1515
name: 'ArrayFieldNormal',
16+
functional: true,
1617
props: {
1718
...vueProps,
1819
itemsFormData: {
1920
type: Array,
2021
// default: () => []
2122
}
2223
},
23-
render(h) {
24+
render(h, context) {
2425
const {
25-
schema, uiSchema, errorSchema
26-
} = this.$props;
26+
schema, uiSchema, curNodePath, itemsFormData, errorSchema
27+
} = context.props;
2728

2829
const {
2930
title,
@@ -41,19 +42,19 @@ export default {
4142
uiSchema
4243
});
4344

44-
const arrayItemsVNodeList = this.itemsFormData.map((item, index) => ({
45+
const arrayItemsVNodeList = itemsFormData.map((item, index) => ({
4546
key: item.key,
4647
vNode: h(
4748
SchemaField,
4849
{
4950
key: item.key,
5051
props: {
51-
...this.$props,
52+
...context.props,
5253
schema: schema.items,
5354
required: !([].concat(schema.items.type).includes('null')),
5455
uiSchema: uiSchema.items,
5556
errorSchema: errorSchema.items,
56-
curNodePath: computedCurPath(this.curNodePath, index)
57+
curNodePath: computedCurPath(curNodePath, index)
5758
}
5859
}
5960
)
@@ -68,7 +69,10 @@ export default {
6869
showTitle,
6970
showDescription
7071
},
71-
class: fieldClass,
72+
class: {
73+
...context.data.class,
74+
...fieldClass
75+
},
7276
attrs: fieldAttrs,
7377
style: fieldStyle,
7478
},
@@ -84,7 +88,7 @@ export default {
8488
maxItems: schema.maxItems,
8589
minItems: schema.minItems,
8690
},
87-
on: this.$listeners
91+
on: context.listeners
8892
}
8993
)
9094
]

0 commit comments

Comments
 (0)