forked from rjsf-team/react-jsonschema-form
/
index.tsx
93 lines (88 loc) · 2.41 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
import React from "react";
import Button from "antd/lib/button";
import Col from "antd/lib/col";
import Row from "antd/lib/row";
import {
ArrayFieldTemplateItemType,
FormContextType,
RJSFSchema,
StrictRJSFSchema,
} from "@rjsf/utils";
const BTN_GRP_STYLE = {
width: "100%",
};
const BTN_STYLE = {
width: "calc(100% / 3)",
};
/** The `ArrayFieldItemTemplate` component is the template used to render an items of an array.
*
* @param props - The `ArrayFieldTemplateItemType` props for the component
*/
export default function ArrayFieldItemTemplate<
T = any,
S extends StrictRJSFSchema = RJSFSchema,
F extends FormContextType = any
>(props: ArrayFieldTemplateItemType<T, S, F>) {
const {
children,
disabled,
hasMoveDown,
hasMoveUp,
hasRemove,
hasToolbar,
index,
onDropIndexClick,
onReorderClick,
readonly,
registry,
uiSchema,
} = props;
const { MoveDownButton, MoveUpButton, RemoveButton } =
registry.templates.ButtonTemplates;
const { rowGutter = 24, toolbarAlign = "top" } = registry.formContext;
return (
<Row
align={toolbarAlign}
key={`array-item-${index}`}
gutter={rowGutter}
className={`array-item-row`}
>
<Col flex="1" className="array-item-content">
{children}
</Col>
{hasToolbar && (
<Col flex="192px" className="array-item-toolbar">
<Button.Group style={BTN_GRP_STYLE}>
{(hasMoveUp || hasMoveDown) && (
<MoveUpButton
disabled={disabled || readonly || !hasMoveUp}
onClick={onReorderClick(index, index - 1)}
style={BTN_STYLE}
uiSchema={uiSchema}
registry={registry}
/>
)}
{(hasMoveUp || hasMoveDown) && (
<MoveDownButton
disabled={disabled || readonly || !hasMoveDown}
onClick={onReorderClick(index, index + 1)}
style={BTN_STYLE}
uiSchema={uiSchema}
registry={registry}
/>
)}
{hasRemove && (
<RemoveButton
disabled={disabled || readonly}
onClick={onDropIndexClick(index)}
style={BTN_STYLE}
uiSchema={uiSchema}
registry={registry}
/>
)}
</Button.Group>
</Col>
)}
</Row>
);
}