forked from rjsf-team/react-jsonschema-form
/
index.tsx
172 lines (157 loc) · 4.72 KB
/
index.tsx
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
166
167
168
169
170
171
172
import React from "react";
import classNames from "classnames";
import isObject from "lodash/isObject";
import isNumber from "lodash/isNumber";
import isString from "lodash/isString";
import {
canExpand,
getTemplate,
getUiOptions,
ObjectFieldTemplateProps,
ObjectFieldTemplatePropertyType,
RJSFSchema,
UiSchema,
GenericObjectType,
} from "@rjsf/utils";
import Col from "antd/lib/col";
import Row from "antd/lib/row";
import { withConfigConsumer } from "antd/lib/config-provider/context";
const DESCRIPTION_COL_STYLE = {
paddingBottom: "8px",
};
// Add in the `prefixCls` element needed by the `withConfigConsumer` HOC
export type AntdObjectFieldTemplateProps = ObjectFieldTemplateProps & {
prefixCls: string;
};
const ObjectFieldTemplate = ({
description,
disabled,
formContext,
formData,
idSchema,
onAddClick,
prefixCls,
properties,
readonly,
required,
registry,
schema,
title,
uiSchema,
}: AntdObjectFieldTemplateProps) => {
const uiOptions = getUiOptions(uiSchema);
const TitleFieldTemplate = getTemplate<"TitleFieldTemplate">(
"TitleFieldTemplate",
registry,
uiOptions
);
const DescriptionFieldTemplate = getTemplate<"DescriptionFieldTemplate">(
"DescriptionFieldTemplate",
registry,
uiOptions
);
// Button templates are not overridden in the uiSchema
const {
ButtonTemplates: { AddButton },
} = registry.templates;
const { colSpan = 24, labelAlign = "right", rowGutter = 24 } = formContext;
const labelClsBasic = `${prefixCls}-item-label`;
const labelColClassName = classNames(
labelClsBasic,
labelAlign === "left" && `${labelClsBasic}-left`
// labelCol.className,
);
const findSchema = (element: ObjectFieldTemplatePropertyType): RJSFSchema =>
element.content.props.schema;
const findSchemaType = (element: ObjectFieldTemplatePropertyType) =>
findSchema(element).type;
const findUiSchema = (
element: ObjectFieldTemplatePropertyType
): UiSchema | undefined => element.content.props.uiSchema;
const findUiSchemaField = (element: ObjectFieldTemplatePropertyType) =>
getUiOptions(findUiSchema(element)).field;
const findUiSchemaWidget = (element: ObjectFieldTemplatePropertyType) =>
getUiOptions(findUiSchema(element)).widget;
const calculateColSpan = (element: ObjectFieldTemplatePropertyType) => {
const type = findSchemaType(element);
const field = findUiSchemaField(element);
const widget = findUiSchemaWidget(element);
const defaultColSpan =
properties.length < 2 || // Single or no field in object.
type === "object" ||
type === "array" ||
widget === "textarea"
? 24
: 12;
if (isObject(colSpan)) {
const colSpanObj: GenericObjectType = colSpan;
if (isString(widget)) {
return colSpanObj[widget];
}
if (isString(field)) {
return colSpanObj[field];
}
if (isString(type)) {
return colSpanObj[type];
}
}
if (isNumber(colSpan)) {
return colSpan;
}
return defaultColSpan;
};
return (
<fieldset id={idSchema.$id}>
<Row gutter={rowGutter}>
{(uiOptions.title || title) && (
<Col className={labelColClassName} span={24}>
<TitleFieldTemplate
id={`${idSchema.$id}-title`}
required={required}
title={uiOptions.title || title}
schema={schema}
uiSchema={uiSchema}
registry={registry}
/>
</Col>
)}
{(uiOptions.description || description) && (
<Col span={24} style={DESCRIPTION_COL_STYLE}>
<DescriptionFieldTemplate
description={uiOptions.description || description!}
id={`${idSchema.$id}-description`}
schema={schema}
uiSchema={uiSchema}
registry={registry}
/>
</Col>
)}
{properties
.filter((e) => !e.hidden)
.map((element: ObjectFieldTemplatePropertyType) => (
<Col key={element.name} span={calculateColSpan(element)}>
{element.content}
</Col>
))}
</Row>
{canExpand(schema, uiSchema, formData) && (
<Col span={24}>
<Row gutter={rowGutter} justify="end">
<Col flex="192px">
<AddButton
className="object-property-expand"
disabled={disabled || readonly}
onClick={onAddClick(schema)}
uiSchema={uiSchema}
registry={registry}
/>
</Col>
</Row>
</Col>
)}
</fieldset>
);
};
export default withConfigConsumer<AntdObjectFieldTemplateProps>({
prefixCls: "form",
})(ObjectFieldTemplate);