-
Notifications
You must be signed in to change notification settings - Fork 583
/
Fields.js
143 lines (132 loc) · 4.51 KB
/
Fields.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
import React from "react";
import { useFormEditor } from "@webiny/app-forms/admin/components/FormEditor/Context";
import { getPlugins } from "@webiny/plugins";
import styled from "react-emotion";
import { css } from "emotion";
import { Icon } from "@webiny/ui/Icon";
import { Accordion, AccordionItem } from "@webiny/ui/Accordion";
import { ReactComponent as HandleIcon } from "./icons/round-drag_indicator-24px.svg";
import Draggable from "./Draggable";
const FieldContainer = styled("div")({
padding: "10px 15px",
marginBottom: 20,
display: "flex",
width: "100%",
backgroundColor: "var(--mdc-theme-on-background)",
borderRadius: 15,
boxSizing: "border-box",
cursor: "grab",
opacity: 1,
transition: "opacity 225ms",
"&:hover": {
opacity: 0.8
},
"&:last-child": {
marginBottom: 0
}
});
const FieldLabel = styled("div")({
textTransform: "uppercase",
lineHeight: "145%",
color: "var(--mdc-theme-on-surface)"
});
const FieldHandle = styled("div")({
marginRight: 15,
color: "var(--mdc-theme-on-surface)"
});
const FormAccordionContent = styled("div")({
marginLeft: -40
});
const accordionItem = css({
"&.webiny-ui-accordion-item": {
".webiny-ui-accordion-item__list-item": {
height: "14px",
borderRadius: "15px !important",
padding: "15px 20px 14px 20px",
textTransform: "uppercase",
backgroundColor: "var(--mdc-theme-on-background)",
marginBottom: 20,
".webiny-ui-accordion-item__title": {
">div": {
fontWeight: 400
}
}
},
".webiny-ui-accordion-item__content": {
border: "1px solid var(--mdc-theme-on-background)",
borderRadius: 15,
paddingTop: 60,
marginTop: -65,
marginBottom: 20
}
}
});
const Field = ({ onFieldDragStart, fieldType: { name, label } }) => {
return (
<Draggable beginDrag={{ ui: "field", name }}>
{({ connectDragSource }) =>
connectDragSource(
<div style={{ marginBottom: 10 }} onDragStart={onFieldDragStart}>
<FieldContainer>
<FieldHandle>
<Icon icon={<HandleIcon />} />
</FieldHandle>
<FieldLabel>{label}</FieldLabel>
</FieldContainer>
</div>
)
}
</Draggable>
);
};
export const Fields = ({ onFieldDragStart }) => {
const { getField } = useFormEditor();
function getGroups() {
const presetFieldPlugins = getPlugins("form-editor-field-type")
.filter(pl => pl.field.group)
.filter(pl => {
if (pl.field.unique) {
return !getField({ name: pl.field.name });
}
return true;
});
return getPlugins("form-editor-field-group").map(pl => ({
...pl.group,
name: pl.name,
fields: presetFieldPlugins.filter(f => f.field.group === pl.name).map(pl => pl.field)
}));
}
return (
<React.Fragment>
<Field
fieldType={{ name: "custom", label: "Custom field" }}
onFieldDragStart={onFieldDragStart}
/>
<Accordion elevation={0}>
{getGroups().map(group => (
<AccordionItem
key={group.name}
title={group.title}
icon={null}
className={accordionItem}
>
<FormAccordionContent>
{!group.fields.length && (
<span>No fields are available at the moment!</span>
)}
{group.fields.map(fieldType => {
return (
<Field
key={fieldType.name}
fieldType={fieldType}
onFieldDragStart={onFieldDragStart}
/>
);
})}
</FormAccordionContent>
</AccordionItem>
))}
</Accordion>
</React.Fragment>
);
};