forked from alibaba/x-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·165 lines (154 loc) · 4.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React from 'react';
import PropTypes from 'prop-types';
import { isDeepEqual, combineSchema } from './base/utils';
import { asField, DefaultFieldUI } from './base/as-field';
import parse from './base/parser';
import resolve from './base/resolve';
import { getValidateList } from './base/validate';
import '../atom.css';
import '../index.css';
function renderField(schema, fields, events) {
const { Field, props } = parse(schema, fields);
if (!Field) {
return null;
}
return (
<Field
isRoot
{...props}
value={schema.data}
{...events}
formData={schema.formData}
/>
);
}
// 在顶层将 propsSchema 和 uiSchema 合并,便于后续处理。 也可直接传入合并的 schema
const Wrapper = ({ schema, propsSchema = {}, uiSchema = {}, ...rest }) => {
const _schema = schema ? schema : combineSchema(propsSchema, uiSchema);
return <FormRender {...rest} schema={_schema} />;
};
class FormRender extends React.Component {
static propTypes = {
name: PropTypes.string,
column: PropTypes.number,
schema: PropTypes.object,
formData: PropTypes.object,
widgets: PropTypes.objectOf(PropTypes.func),
FieldUI: PropTypes.elementType,
fields: PropTypes.objectOf(PropTypes.element),
mapping: PropTypes.object,
showDescIcon: PropTypes.bool,
showValidate: PropTypes.bool,
displayType: PropTypes.string,
onChange: PropTypes.func,
onValidate: PropTypes.func,
};
static defaultProps = {
name: '$form',
column: 1,
schema: {},
formData: {},
widgets: {},
FieldUI: DefaultFieldUI,
fields: {},
mapping: {},
showDescIcon: false,
showValidate: true,
displayType: 'column',
onChange: () => {},
onValidate: () => {},
};
constructor() {
super();
this.originWidgets = null;
this.generatedFields = {};
}
componentDidMount() {
this.needUpdateForm();
}
componentWillReceiveProps(nextProps) {
const { schema, formData } = this.props;
// 遇到有更新 schema 情况,假如项数增多了,发现增多项不会更新
if (
!isDeepEqual(nextProps.schema, schema) ||
!isDeepEqual(Object.keys(nextProps.formData), Object.keys(formData))
) {
setTimeout(() => {
this.needUpdateForm();
}, 0);
}
}
needUpdateForm = () => {
const { schema, formData, onChange, onValidate } = this.props;
const data = resolve(schema, formData);
onChange(data);
onValidate(getValidateList(data, schema));
};
render() {
const {
name,
column,
className,
schema,
formData,
widgets,
FieldUI,
fields: customized,
mapping,
showDescIcon,
showValidate,
displayType,
onChange,
onValidate,
} = this.props;
const generated = {};
const list = widgets;
if (!this.originWidgets) {
this.originWidgets = list;
}
Object.keys(list).forEach(key => {
const oWidget = this.originWidgets[key];
const nWidget = list[key];
let gField = this.generatedFields[key];
if (!gField || oWidget !== nWidget) {
if (oWidget !== nWidget) {
this.originWidgets[key] = nWidget;
}
gField = asField({ FieldUI, Widget: nWidget });
this.generatedFields[key] = gField;
}
generated[key] = gField;
});
return (
<div className={className}>
{renderField(
{
schema,
data: formData,
name,
column,
showDescIcon,
showValidate,
displayType,
formData,
},
{
// 根据 Widget 生成的 Field
generated,
// 自定义的 Field
customized,
// 字段 type 与 widgetName 的映射关系
mapping,
},
{
onChange(key, val) {
onChange(val);
onValidate(getValidateList(val, schema));
},
}
)}
</div>
);
}
}
export default Wrapper;