import { DragIndicator, Clear } from "@mui/icons-material";
import {
Card,
CardContent,
CardHeader,
IconButton,
Stack,
TextField,
} from "@mui/material";
import React, { useRef, useState } from "react";
import useForm from "../../hooks/useForm";
export default function FormBuilder({ formId }) {
return (
FormBuilder
);
}
const availableWidgets = {
label: {
name: "Label",
element: ({ id, name, setName, finalField }) =>
finalField ? (
{name}
) : (
setName(e.target.value)}
fullWidth
/>
),
},
number: {
name: "Number",
element: ({ id, value, setValue }) => (
{
if (setValue) {
setValue(e.target.value);
}
}}
fullWidth
/>
),
},
text: {
name: "Text",
element: ({ id, setValue, value }) => (
{
if (setValue) {
setValue(e.target.value);
}
}}
/>
),
},
multiline: {
name: "Multiline",
element: ({ id, setValue, value }) => (
{
if (setValue) {
setValue(e.target.value);
}
}}
/>
),
},
};
function WidgetsLibrary() {
return (
{Object.keys(availableWidgets).map((k) => {
const props = availableWidgets[k];
return ;
})}
);
}
function WidgetCard({ name, element, type, finalElement }) {
function onDragStart(e) {
e.dataTransfer.setData("application/formwidgettype", type);
e.dataTransfer.effectAllowed = "copy";
}
const disabledFieldOverlay = {
pointerEvents: "none",
background: "#7773",
zIndex: 10,
};
if (!element) {
return;
} else {
return (
{finalElement ? finalElement({}) : element({})}
);
}
}
function FormField({
formField,
setFormField,
deleteFormField,
editable,
setResult,
result,
}) {
const { id, type, name } = formField;
function setName(name) {
setFormField({ ...formField, name });
}
if (!type) {
return;
}
const { element: Element } = availableWidgets[type];
const dragHandleRef = useRef();
const [target, setTarget] = useState();
if (!editable) {
return (
);
} else {
return (
{
setTarget(e.target);
}}
onDragStart={(e) => {
if (dragHandleRef.current.contains(target)) {
e.dataTransfer.setData("application/formwidgetid", id);
} else {
e.preventDefault();
}
}}
>
);
}
}
function capture(e) {
e.stopPropagation();
e.preventDefault();
}
function DropZone({ insertField, index, moveField }) {
const [dragOver, setDragOver] = useState(false);
return (
capture(e) || setDragOver(true)}
onDragExit={() => setDragOver(false)}
onDrop={(e) => {
e.stopPropagation();
e.preventDefault();
setDragOver(false);
if ([...e.dataTransfer.types].includes("application/formwidgettype")) {
const type = e.dataTransfer.getData("application/formwidgettype");
insertField({ index, type });
} else if (
[...e.dataTransfer.types].includes("application/formwidgetid")
) {
const oldId = e.dataTransfer.getData("application/formwidgetid");
moveField(oldId, index);
}
}}
/>
);
}
export function Form({ editable, formId, setResults, results }) {
// const [form, setForm] = useState({ fields: [], ...initialForm });
const [form, setForm] = useForm(formId);
function updateFormField(newField) {
setForm({
...form,
fields: form.fields.map((field) =>
field?.id === newField.id ? newField : field,
),
});
}
function deleteFormField(id) {
const newForm = {
...form,
fields: form.fields.filter((field) => id !== field?.id),
};
setForm({ ...newForm });
}
function insertField({ index, type }) {
const id = crypto.randomUUID();
const newField = {
id,
type,
};
form?.fields?.splice(index + 1, 0, newField);
setForm({ ...form });
}
function moveField(oldId, newIndex) {
const oldIndex = form.fields.findIndex(({ id }) => id === oldId);
const [oldFormField] = form.fields.splice(oldIndex, 1);
if (oldIndex > newIndex) {
newIndex++;
}
form.fields.splice(newIndex, 0, oldFormField);
setForm({ ...form });
}
function setResult(fieldId, result) {
const fieldResult = {
fieldId,
result,
fieldName: form?.fields?.find(({ id }) => id === fieldId)?.name,
};
setResults({ ...results, [fieldId]: fieldResult });
}
return (
{editable && (
)}
{form?.fields?.map((formField, i) => (
updateFormField(field)}
deleteFormField={() => deleteFormField(formField.id)}
formField={formField}
editable={editable}
setResult={results && ((result) => setResult(formField.id, result))}
result={results && results[formField.id]}
/>
{editable && (
)}
))}
);
}
function FormHeader() {
return;
}
//function FormInitialPrompt() {
// return (
//
// Drag and drop and field here to get started
//
// );
//}
//
function Grouping({ children }) {
return <>{children}>;
}