Compare commits

...

4 Commits

Author SHA1 Message Date
e01430f60e Adding the injection of a default value in the form 2025-04-23 00:06:23 +02:00
9d835d49d9 Correcting error in foreign key 2025-04-22 21:59:52 +02:00
081b3d08dd Separating Crud and Base form logic 2025-04-22 21:22:21 +02:00
614dc19095 Exporting foreign key json type 2025-04-22 21:20:38 +02:00
4 changed files with 113 additions and 82 deletions

View File

@@ -0,0 +1,43 @@
import validator from "@rjsf/validator-ajv8";
import Form from "@rjsf/mui";
import { RegistryFieldsType, RegistryWidgetsType, RJSFSchema, UiSchema } from "@rjsf/utils";
import CrudTextWidget from "./widgets/crud-text-widget";
import UnionEnumField from "./fields/union-enum";
import { ResourceContext } from "../contexts/ResourceContext";
type BaseFormProps = {
schema: RJSFSchema,
resourceBasePath: string,
onSubmit?: (data: any) => void,
onChange?: (data: any) => void,
uiSchema?: UiSchema,
formData?: any,
}
export const customWidgets: RegistryWidgetsType = {
TextWidget: CrudTextWidget
};
export const customFields: RegistryFieldsType = {
AnyOfField: UnionEnumField
}
export const BaseForm: React.FC<BaseFormProps> = (props) => {
const { schema, uiSchema, resourceBasePath, formData, onSubmit, onChange } = props;
return (
<ResourceContext.Provider value={{basePath: resourceBasePath}} >
<Form
schema={schema}
uiSchema={uiSchema === undefined ? {} : uiSchema}
formData={formData}
onSubmit={(e, id) => onSubmit != undefined && onSubmit(e.formData)}
validator={validator}
omitExtraData={true}
widgets={customWidgets}
fields={customFields}
onChange={(e, id) => onChange != undefined && onChange(e.formData)}
/>
</ResourceContext.Provider>
)
}

View File

@@ -1,81 +1,62 @@
import validator from "@rjsf/validator-ajv8";
import Form from "@rjsf/mui";
import { RegistryFieldsType, RegistryWidgetsType, UiSchema } from "@rjsf/utils";
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { jsonschemaProvider } from "../providers/jsonschema-provider";
import { useForm } from "@refinedev/core";
import CrudTextWidget from "./widgets/crud-text-widget";
import UnionEnumField from "./fields/union-enum";
import { CircularProgress } from "@mui/material"; import { CircularProgress } from "@mui/material";
import { ResourceContext } from "../contexts/ResourceContext"; import { useForm } from "@refinedev/core";
import { UiSchema } from "@rjsf/utils";
import { jsonschemaProvider } from "../providers/jsonschema-provider";
import { BaseForm } from "./base-form";
type CrudFormProps = { type CrudFormProps = {
schemaName: string, schemaName: string,
uiSchema?: UiSchema, uiSchema?: UiSchema,
resource: string,
resourceBasePath?: string, resourceBasePath?: string,
resource: string,
id?: string, id?: string,
//onSubmit: (data: IChangeEvent, event: FormEvent<any>) => void onSuccess?: (data: any) => void,
onSuccess?: (data: any) => void defaultValue?: any
}
const customWidgets: RegistryWidgetsType = {
TextWidget: CrudTextWidget
};
const customFields: RegistryFieldsType = {
AnyOfField: UnionEnumField
} }
export const CrudForm: React.FC<CrudFormProps> = (props) => { export const CrudForm: React.FC<CrudFormProps> = (props) => {
const { schemaName, uiSchema, resourceBasePath="" ,resource, id, onSuccess } = props; const { schemaName, uiSchema, resourceBasePath="" ,resource, id, onSuccess, defaultValue } = props;
const { onFinish, query, formLoading } = useForm({ const { onFinish, query, formLoading } = useForm({
resource: `${resourceBasePath}/${resource}`, resource: resourceBasePath == "" ? resource : `${resourceBasePath}/${resource}`,
action: id === undefined ? "create" : "edit", action: id === undefined ? "create" : "edit",
redirect: "show", redirect: "show",
id, id,
onMutationSuccess: (data: any) => { if (onSuccess) { onSuccess(data) } }, onMutationSuccess: (data: any) => { if (onSuccess) { onSuccess(data) } },
}); });
const schemaValue = id === undefined ? `${schemaName}Create` : `${schemaName}Update`;
const record = query?.data?.data;
const [formData, setFormData] = useState(record);
const [schema, setSchema] = useState({}); const [schema, setSchema] = useState({});
const [loading, setLoading] = useState(true); const [schemaLoading, setSchemaLoading] = useState(true);
useEffect(() => { useEffect(() => {
const fetchSchema = async () => { const fetchSchema = async () => {
try { try {
const resourceSchema = await jsonschemaProvider.getResourceSchema(schemaValue); const schemaFullName = id === undefined ? `${schemaName}Create` : `${schemaName}Update`;
const resourceSchema = await jsonschemaProvider.getResourceSchema(schemaFullName);
setSchema(resourceSchema); setSchema(resourceSchema);
setLoading(false); setSchemaLoading(false);
} catch (error) { } catch (error) {
console.error('Error fetching data:', error); console.error('Error fetching data:', error);
setLoading(false); setSchemaLoading(false);
} }
}; };
fetchSchema(); fetchSchema();
}, []); }, []);
if(formLoading || schemaLoading) {
if(formLoading || loading) {
return <CircularProgress /> return <CircularProgress />
} }
const record = query?.data?.data || defaultValue;
return ( return (
<ResourceContext.Provider value={{basePath: resourceBasePath}} > <BaseForm
<Form schema={schema}
schema={schema} uiSchema={uiSchema}
uiSchema={uiSchema === undefined ? {} : uiSchema} formData={record}
formData={record} resourceBasePath={resourceBasePath}
onChange={(e) => setFormData(e.formData)} onSubmit={
onSubmit={(e) => onFinish(e.formData)} (data: any) => onFinish(data)
validator={validator} }
omitExtraData={true} />
widgets={customWidgets}
fields={customFields}
/>
</ResourceContext.Provider>
) )
} }

View File

@@ -1,21 +1,23 @@
import { FormContextType, RJSFSchema, UiSchema, WidgetProps } from '@rjsf/utils'; import { FormContextType, RJSFSchema, UiSchema, WidgetProps } from '@rjsf/utils';
import { Autocomplete, Button, CircularProgress, Container, Grid2, InputAdornment, Modal, TextField, Box } from "@mui/material"; import {
Autocomplete, Button, CircularProgress, Container, Grid2, InputAdornment, Modal, TextField, Box, DialogContent
} from "@mui/material";
import ClearIcon from '@mui/icons-material/Clear'; import ClearIcon from '@mui/icons-material/Clear';
import EditIcon from '@mui/icons-material/Edit'; import EditIcon from '@mui/icons-material/Edit';
import NoteAddIcon from '@mui/icons-material/NoteAdd'; import NoteAddIcon from '@mui/icons-material/NoteAdd';
import React, { useState, useEffect, useContext } from "react"; import React, { useState, useEffect, useContext, Fragment } from "react";
import { useList, useOne } from "@refinedev/core"; import { useList, useOne } from "@refinedev/core";
import { ResourceContext } from "../../contexts/ResourceContext"; import { ResourceContext } from "../../contexts/ResourceContext";
import { CrudForm } from "../crud-form"; import { CrudForm } from "../crud-form";
type ForeignKeyReference = { export type ForeignKeyReference = {
resource: string, resource: string,
label: string, label?: string,
displayedFields: [string], displayedFields?: [string],
schema: string schema: string
} }
type ForeignKeySchema = RJSFSchema & { export type ForeignKeySchema = RJSFSchema & {
foreignKey?: { foreignKey?: {
reference: ForeignKeyReference reference: ForeignKeyReference
} }
@@ -95,22 +97,24 @@ const RealAutocomplete = <T = any, S extends ForeignKeySchema = ForeignKeySchema
)} )}
/> />
<Modal <Modal
open={openFormModal} open={openFormModal}
onClose={() => setOpenFormModal(false)} onClose={() => setOpenFormModal(false)}
aria-labelledby="modal-modal-title" aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description" aria-describedby="modal-modal-description"
> >
<FormContainer <DialogContent>
schemaName={schema} <FormContainer
resourceBasePath={basePath} schemaName={schema}
resource={resource} resourceBasePath={basePath}
uiSchema={{}} resource={resource}
onSuccess={(data: any) => { uiSchema={{}}
setOpenFormModal(false) onSuccess={(data: any) => {
onChange(data.data.id); setOpenFormModal(false)
}} onChange(data.data.id);
/> }}
</Modal> />
</DialogContent>
</Modal>
</> </>
); );
} }
@@ -161,14 +165,16 @@ const ChosenValue = <T = any, S extends ForeignKeySchema = ForeignKeySchema, F e
aria-labelledby="modal-modal-title" aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description" aria-describedby="modal-modal-description"
> >
<FormContainer <DialogContent>
schemaName={schema} <FormContainer
resourceBasePath={basePath} schemaName={schema}
resource={resource} resourceBasePath={basePath}
uiSchema={{}} resource={resource}
id={value} uiSchema={{}}
onSuccess={() => setOpenFormModal(false)} id={value}
/> onSuccess={() => setOpenFormModal(false)}
/>
</DialogContent>
</Modal> </Modal>
</> </>
) )
@@ -199,10 +205,9 @@ type FormContainerProps = {
const FormContainer = (props: FormContainerProps) => { const FormContainer = (props: FormContainerProps) => {
const { schemaName, resourceBasePath, resource, uiSchema = {}, id = undefined, onSuccess } = props; const { schemaName, resourceBasePath, resource, uiSchema = {}, id = undefined, onSuccess } = props;
return ( return (
<Box sx={{ ...modalStyle, width: 800 }}> <Box sx={{ ...modalStyle, width: 800 }}>
<CrudForm schemaName={schemaName} resourceBasePath={resourceBasePath} resource={resource} uiSchema={uiSchema} id={id} onSuccess={onSuccess} /> <CrudForm schemaName={schemaName} resourceBasePath={resourceBasePath} resource={resource} uiSchema={uiSchema} id={id} onSuccess={(data) => onSuccess(data)} />
</Box> </Box>
) )
} }
@@ -221,12 +226,12 @@ const Preview = (props: {id: string, resource: string, basePath: string, display
return ( return (
<Grid2 container spacing={2}> <Grid2 container spacing={2}>
{displayedFields.map((field: string) => { {displayedFields.map((field: string, index: number) => {
return ( return (
<> <Fragment key={index}>
<Grid2 size={2}><Container>{field}</Container></Grid2> <Grid2 size={2}><Container>{field}</Container></Grid2>
<Grid2 size={9}><Container dangerouslySetInnerHTML={{ __html: data.data[field] }} ></Container></Grid2> <Grid2 size={9}><Container dangerouslySetInnerHTML={{ __html: data.data[field] }} ></Container></Grid2>
</> </Fragment>
) )
})} })}
</Grid2> </Grid2>

View File

@@ -7,10 +7,11 @@ type NewProps = {
resource: string, resource: string,
schemaName: string, schemaName: string,
uiSchema?: UiSchema, uiSchema?: UiSchema,
defaultValue?: any
} }
const New = <T,>(props: NewProps) => { const New = <T,>(props: NewProps) => {
const { schemaName, resource, uiSchema } = props; const { schemaName, resource, uiSchema, defaultValue } = props;
const { currentFirm } = useContext(FirmContext); const { currentFirm } = useContext(FirmContext);
const resourceBasePath = `firm/${currentFirm.instance}/${currentFirm.firm}` const resourceBasePath = `firm/${currentFirm.instance}/${currentFirm.firm}`
@@ -20,7 +21,8 @@ const New = <T,>(props: NewProps) => {
uiSchema={uiSchema} uiSchema={uiSchema}
resourceBasePath={resourceBasePath} resourceBasePath={resourceBasePath}
resource={resource} resource={resource}
/> defaultValue={defaultValue}
/>
) )
} }