/
ArrayFieldItemTemplate.tsx
100 lines (97 loc) · 3.13 KB
/
ArrayFieldItemTemplate.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
import React from "react";
import {
ArrayFieldTemplateItemType,
FormContextType,
GenericObjectType,
RJSFSchema,
StrictRJSFSchema,
getUiOptions,
} from "@rjsf/utils";
import { Button, Grid, Segment } from "semantic-ui-react";
import { MaybeWrap } from "../util";
const gridStyle = (vertical: boolean) => ({
display: "grid",
gridTemplateColumns: `1fr ${vertical ? 65 : 110}px`,
});
/** 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,
hasToolbar,
hasMoveDown,
hasMoveUp,
hasRemove,
index,
onDropIndexClick,
onReorderClick,
readonly,
uiSchema,
registry,
} = props;
const { MoveDownButton, MoveUpButton, RemoveButton } =
registry.templates.ButtonTemplates;
const uiOptions = getUiOptions<T, S, F>(uiSchema);
// Pull the semantic props out of the uiOptions that were put in via the ArrayFieldTemplate
const { horizontalButtons = false, wrapItem = false } =
uiOptions.semantic as GenericObjectType;
return (
<div className="array-item">
<MaybeWrap wrap={wrapItem} component={Segment}>
<Grid
style={
index !== 0
? { ...gridStyle(!horizontalButtons), alignItems: "center" }
: gridStyle(!horizontalButtons)
}
>
<Grid.Column width={16} verticalAlign="middle">
{children}
</Grid.Column>
{hasToolbar && (
<Grid.Column>
{(hasMoveUp || hasMoveDown || hasRemove) && (
<Button.Group size="mini" vertical={!horizontalButtons}>
{(hasMoveUp || hasMoveDown) && (
<MoveUpButton
className="array-item-move-up"
disabled={disabled || readonly || !hasMoveUp}
onClick={onReorderClick(index, index - 1)}
uiSchema={uiSchema}
registry={registry}
/>
)}
{(hasMoveUp || hasMoveDown) && (
<MoveDownButton
className="array-item-move-down"
disabled={disabled || readonly || !hasMoveDown}
onClick={onReorderClick(index, index + 1)}
uiSchema={uiSchema}
registry={registry}
/>
)}
{hasRemove && (
<RemoveButton
className="array-item-remove"
disabled={disabled || readonly}
onClick={onDropIndexClick(index)}
uiSchema={uiSchema}
registry={registry}
/>
)}
</Button.Group>
)}
</Grid.Column>
)}
</Grid>
</MaybeWrap>
</div>
);
}