2023-02-16 22:39:57 +00:00
|
|
|
import type { App_RoutingForms_Form } from "@prisma/client";
|
|
|
|
import type { Dispatch, SetStateAction } from "react";
|
2022-11-10 12:58:07 +00:00
|
|
|
|
2023-01-07 15:50:28 +00:00
|
|
|
import { getQueryBuilderConfig } from "../lib/getQueryBuilderConfig";
|
2023-01-04 13:30:46 +00:00
|
|
|
import isRouterLinkedField from "../lib/isRouterLinkedField";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { SerializableForm, Response } from "../types/types";
|
2022-11-10 12:58:07 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
form: SerializableForm<App_RoutingForms_Form>;
|
|
|
|
response: Response;
|
|
|
|
setResponse: Dispatch<SetStateAction<Response>>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function FormInputFields(props: Props) {
|
|
|
|
const { form, response, setResponse } = props;
|
|
|
|
|
|
|
|
const queryBuilderConfig = getQueryBuilderConfig(form);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{form.fields?.map((field) => {
|
2023-01-04 13:30:46 +00:00
|
|
|
if (isRouterLinkedField(field)) {
|
2023-01-27 01:50:56 +00:00
|
|
|
// @ts-expect-error FIXME @hariombalhara
|
2023-01-04 13:30:46 +00:00
|
|
|
field = field.routerField;
|
|
|
|
}
|
2022-11-10 12:58:07 +00:00
|
|
|
const widget = queryBuilderConfig.widgets[field.type];
|
|
|
|
if (!("factory" in widget)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const Component = widget.factory;
|
|
|
|
|
|
|
|
const optionValues = field.selectText?.trim().split("\n");
|
|
|
|
const options = optionValues?.map((value) => {
|
|
|
|
const title = value;
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
title,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return (
|
|
|
|
<div key={field.id} className="mb-4 block flex-col sm:flex ">
|
|
|
|
<div className="min-w-48 mb-2 flex-grow">
|
2023-04-05 18:14:46 +00:00
|
|
|
<label id="slug-label" htmlFor="slug" className="text-default flex text-sm font-medium">
|
2022-11-10 12:58:07 +00:00
|
|
|
{field.label}
|
|
|
|
</label>
|
|
|
|
</div>
|
2023-04-12 02:09:04 +00:00
|
|
|
<Component
|
|
|
|
value={response[field.id]?.value}
|
|
|
|
placeholder={field.placeholder ?? ""}
|
|
|
|
// required property isn't accepted by query-builder types
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
/* @ts-ignore */
|
|
|
|
required={!!field.required}
|
|
|
|
listValues={options}
|
|
|
|
data-testid="form-field"
|
|
|
|
setValue={(value) => {
|
|
|
|
setResponse((response) => {
|
|
|
|
response = response || {};
|
|
|
|
return {
|
|
|
|
...response,
|
|
|
|
[field.id]: {
|
|
|
|
label: field.label,
|
|
|
|
value,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
/>
|
2022-11-10 12:58:07 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|