# Summary Refactor the React OpenAPI admin framework to support fully customizable field rendering and UI composition. ## Changes ### Admin UI Customization * Added support for custom: * Dashboard component * Layout component * Login page component * Introduced `AdminAppProps` and extended `Admin` configuration API. * Renamed internal dashboard implementation to `DefaultDashboard`. ### Field Component Architecture * Extracted field rendering into dedicated field components: * TextField * NumberField * BooleanField * DateField * EnumField * RelationField * ObjectField * FallbackField * DateRangeField * NumberRangeField * Added `defaultFieldComponents` registry. * Refactored `FormField` to resolve components dynamically from a component map instead of hardcoded field type handling. ### Resource Customization * Added `FieldComponents` support across: * Admin * ResourceView * GenericForm * useResource * Introduced wrapped `FormField` and `GenericForm` components generated from configured field overrides. ### Table Customization * Added `EnhancedTableComponents`. * Added support for custom cell renderers per field type. * Enabled custom rendering for both desktop and mobile table layouts. ### Filter Improvements * Exported `FilterAutocomplete`. * Added support for custom date-range and number-range filter components. * Added filter component extension points. * Updated filter option label resolution to support `displayFormat`. ### Display Formatting * Replaced `displayField` usage with `displayFormat`. * Added template-based display rendering support through `resolveTemplate`. * Improved relation display configuration handling. ### TypeScript Improvements * Added TypeScript as a project dependency. * Removed multiple `@ts-ignore` usages. * Added strongly typed Axios wrapper methods with generic response support. * Improved typing across hooks and component interfaces. ### OpenAPI Configuration Validation * Added validation for enum fields without enum values. * Added validation for relation resources missing `referenceOptions.enumOption`. * Improved relation metadata propagation during schema parsing. ### Library Exports * Exported: * Field component types * Override types * EnhancedTable * GenericForm * ResourceView * Field components and defaults * Expanded public API surface for consumers extending the framework. ## Benefits * Enables complete UI customization without modifying framework internals. * Simplifies creation of custom field types and renderers. * Improves type safety and developer experience. * Provides consistent extension points for forms, tables, filters, and admin layouts. * Makes the framework more suitable for reusable library distribution. Reviewed-on: #11 Co-authored-by: Vishesh 'ironeagle' Bangotra <aetoskia@gmail.com> Co-committed-by: Vishesh 'ironeagle' Bangotra <aetoskia@gmail.com>
144 lines
4.4 KiB
TypeScript
144 lines
4.4 KiB
TypeScript
import * as React from 'react';
|
|
import {
|
|
Box,
|
|
Button,
|
|
Typography,
|
|
Divider,
|
|
CircularProgress,
|
|
} from '@mui/material';
|
|
import { ResourceConfig } from '../types/config';
|
|
import { FieldComponents } from '../types/overrides';
|
|
import { useUpload } from '../providers/UploadProvider';
|
|
import { useQueries } from '@tanstack/react-query';
|
|
import { useResource } from '../hooks/useResource';
|
|
import FormField from './fields/FormField';
|
|
import { ConfigContext } from '../providers/ConfigContext';
|
|
|
|
interface GenericFormProps {
|
|
config: ResourceConfig;
|
|
initialData?: any;
|
|
onSave: (data: any) => Promise<void>;
|
|
onCancel: () => void;
|
|
loading?: boolean;
|
|
readOnly?: boolean;
|
|
onEditClick?: () => void;
|
|
fieldComponents: FieldComponents;
|
|
}
|
|
|
|
export default function GenericForm({
|
|
config,
|
|
initialData = {},
|
|
onSave,
|
|
onCancel,
|
|
loading: saving,
|
|
readOnly = false,
|
|
onEditClick,
|
|
fieldComponents,
|
|
}: GenericFormProps) {
|
|
initialData = initialData || {};
|
|
const [formData, setFormData] = React.useState(initialData);
|
|
const { uploadFile, uploading } = useUpload();
|
|
const appConfig = React.useContext(ConfigContext);
|
|
|
|
// 1. Identify all unique relations in the schema (including nested ones)
|
|
const getRelationFields = (fields: Record<string, any>): string[] => {
|
|
let relations: string[] = [];
|
|
Object.values(fields).forEach(field => {
|
|
if (field.relation) relations.push(field.relation);
|
|
if (field.schema) relations = [...relations, ...getRelationFields(field.schema)];
|
|
});
|
|
return Array.from(new Set(relations));
|
|
};
|
|
|
|
const allRelations = React.useMemo(() => getRelationFields(config.fields), [config.fields]);
|
|
|
|
// 2. Parallel fetch for all related resource lists
|
|
const queries = useQueries({
|
|
queries: allRelations.map(relName => {
|
|
const relatedRes = appConfig?.resources.find(r => r.name === relName);
|
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
const { getListQueryOptions } = useResource(relatedRes!, { fieldComponents });
|
|
return {
|
|
...getListQueryOptions(),
|
|
enabled: !!relatedRes,
|
|
};
|
|
}),
|
|
});
|
|
|
|
const isLoadingRelations = queries.some(q => q.isLoading);
|
|
|
|
const relationDataMap = React.useMemo(() => {
|
|
const map: Record<string, any[]> = {};
|
|
allRelations.forEach((relName, index) => {
|
|
// @ts-ignore
|
|
map[relName] = queries[index].data || [];
|
|
});
|
|
return map;
|
|
}, [allRelations, queries]);
|
|
|
|
const handleChange = (key: string, value: any) => {
|
|
if (readOnly) return;
|
|
setFormData((prev: any) => ({ ...prev, [key]: value }));
|
|
};
|
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
if (readOnly) return;
|
|
onSave(formData);
|
|
};
|
|
|
|
const getTitle = () => {
|
|
if (readOnly) return `View ${config.label}`;
|
|
return initialData[config.primaryKey] ? `Edit ${config.label}` : `New ${config.label}`;
|
|
};
|
|
|
|
if (isLoadingRelations) {
|
|
return (
|
|
<Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', p: 8, gap: 2 }}>
|
|
<CircularProgress />
|
|
<Typography variant="body2" color="text.secondary">Loading relationships...</Typography>
|
|
</Box>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Box component="form" onSubmit={handleSubmit} sx={{ display: 'flex', flexDirection: 'column', gap: 3 }}>
|
|
<Typography variant="h5">
|
|
{getTitle()}
|
|
</Typography>
|
|
<Divider />
|
|
|
|
{Object.entries(config.fields).map(([key, field]) => (
|
|
<FormField
|
|
key={key}
|
|
name={key}
|
|
field={field}
|
|
value={formData[key]}
|
|
onChange={(val: any) => handleChange(key, val)}
|
|
disabled={readOnly || field.readOnly}
|
|
uploadFile={uploadFile}
|
|
uploading={uploading}
|
|
baseUrl={appConfig?.baseUrl || ""}
|
|
relationDataMap={relationDataMap}
|
|
components={fieldComponents}
|
|
/>
|
|
))}
|
|
|
|
<Box sx={{ display: 'flex', justifyContent: 'flex-end', gap: 2, mt: 4 }}>
|
|
<Button variant="outlined" onClick={onCancel} disabled={saving}>
|
|
{readOnly ? 'Back to List' : 'Cancel'}
|
|
</Button>
|
|
{readOnly ? (
|
|
<Button variant="contained" color="primary" onClick={onEditClick}>
|
|
Edit {config.label}
|
|
</Button>
|
|
) : (
|
|
<Button variant="contained" type="submit" loading={saving} disabled={saving || uploading}>
|
|
Save {config.label}
|
|
</Button>
|
|
)}
|
|
</Box>
|
|
</Box>
|
|
);
|
|
}
|