new react-openapi
This commit is contained in:
@@ -1,24 +1,71 @@
|
||||
import React from "react";
|
||||
import { Box, Typography, Chip, Avatar } from "@mui/material";
|
||||
import React, { useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { Box, Typography, Chip, Avatar, Dialog, DialogTitle, DialogContent, DialogActions, Button, Grid } from "@mui/material";
|
||||
import type { FieldConfig } from "../../types";
|
||||
import { applyDisplayFormat } from "./utils";
|
||||
import { InlineRefField } from "./renderers/InlineRefField";
|
||||
import { extractFields } from "../../transformers/field-config";
|
||||
import { useAppContext } from "../../context/AppContext";
|
||||
|
||||
interface ListCellProps {
|
||||
field: FieldConfig;
|
||||
value: any;
|
||||
displayFormat?: string;
|
||||
basePath?: string;
|
||||
}
|
||||
|
||||
export function ListCellRenderer({ field, value, displayFormat }: ListCellProps) {
|
||||
export function ListCellRenderer({ field, value, displayFormat, basePath }: ListCellProps) {
|
||||
const navigate = useNavigate();
|
||||
const { schemas } = useAppContext();
|
||||
const [inlineItem, setInlineItem] = useState<any>(null);
|
||||
|
||||
if (value === null || value === undefined) {
|
||||
return <Typography variant="body2" color="text.disabled">—</Typography>;
|
||||
}
|
||||
|
||||
const handleFkClick = (e: React.MouseEvent, fkValue: any) => {
|
||||
e.stopPropagation();
|
||||
if (!basePath || !field.fk || typeof fkValue !== "object") return;
|
||||
const id = fkValue?.id;
|
||||
if (id != null) {
|
||||
navigate(`${basePath}/${field.fk.resource}/${id}`);
|
||||
}
|
||||
};
|
||||
|
||||
if (field.refSchema && !field.fk && !field.isArray && typeof value === "object") {
|
||||
return <InlineRefField field={field} value={value} displayFormat={displayFormat} />;
|
||||
return <InlineRefField field={field} value={value} />;
|
||||
}
|
||||
|
||||
const renderInlineItemFields = (itemValue: any) => {
|
||||
const schema = field.refSchema ? schemas[field.refSchema] : undefined;
|
||||
let fields: FieldConfig[] = [];
|
||||
if (field.oneOfOptions && field.discriminatorProperty) {
|
||||
const opt = field.oneOfOptions.find((o) => o.value === itemValue?.[field.discriminatorProperty!]);
|
||||
if (opt) fields = opt.fields;
|
||||
} else if (schema && field.refSchema) {
|
||||
fields = extractFields(field.refSchema, schema, schemas);
|
||||
}
|
||||
return (
|
||||
<Grid container spacing={2} sx={{ mt: 0.5 }}>
|
||||
{fields.map((sf) => {
|
||||
const fv = itemValue?.[sf.name];
|
||||
return (
|
||||
<Grid key={sf.name} item xs={12} sm={6}>
|
||||
<Box sx={{ mb: 2 }}>
|
||||
<Typography variant="caption" color="text.secondary" fontWeight={600} sx={{ mb: 0.25, display: "block" }}>
|
||||
{sf.label}
|
||||
</Typography>
|
||||
<Typography variant="body2">
|
||||
{fv == null ? "—" : typeof fv === "object" ? (sf.inlineDisplayFormat ? applyDisplayFormat(fv, sf.inlineDisplayFormat) : JSON.stringify(fv)) : String(fv)}
|
||||
</Typography>
|
||||
</Box>
|
||||
</Grid>
|
||||
);
|
||||
})}
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
|
||||
if (field.isArray && Array.isArray(value) && field.refSchema && !field.fk) {
|
||||
if (value.length === 0) {
|
||||
return <Typography variant="body2" color="text.disabled">—</Typography>;
|
||||
@@ -29,14 +76,40 @@ export function ListCellRenderer({ field, value, displayFormat }: ListCellProps)
|
||||
const label = typeof item === "object"
|
||||
? applyDisplayFormat(item, displayFormat ?? "")
|
||||
: String(item);
|
||||
return <Chip key={i} label={label} size="small" variant="outlined" />;
|
||||
return (
|
||||
<Chip
|
||||
key={i}
|
||||
label={label}
|
||||
size="small"
|
||||
variant="outlined"
|
||||
onClick={(e) => { e.stopPropagation(); setInlineItem(item); }}
|
||||
sx={{ cursor: "pointer" }}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
<Dialog open={!!inlineItem} onClose={() => setInlineItem(null)} maxWidth="sm" fullWidth>
|
||||
<DialogTitle>{field.label}</DialogTitle>
|
||||
<DialogContent dividers>
|
||||
{inlineItem && renderInlineItemFields(inlineItem)}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={() => setInlineItem(null)}>Close</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
if (field.fk && typeof value === "object" && !field.isArray) {
|
||||
return <Typography variant="body2">{applyDisplayFormat(value, displayFormat ?? "")}</Typography>;
|
||||
return (
|
||||
<Chip
|
||||
label={applyDisplayFormat(value, displayFormat ?? "")}
|
||||
size="small"
|
||||
variant="outlined"
|
||||
onClick={(e) => handleFkClick(e, value)}
|
||||
sx={basePath ? { cursor: "pointer" } : undefined}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
if (field.isArray && Array.isArray(value) && field.fk) {
|
||||
@@ -44,7 +117,16 @@ export function ListCellRenderer({ field, value, displayFormat }: ListCellProps)
|
||||
<Box sx={{ display: "flex", gap: 0.5, flexWrap: "wrap" }}>
|
||||
{value.map((item: any, i: number) => {
|
||||
const label = typeof item === "object" ? applyDisplayFormat(item, displayFormat ?? "") : String(item);
|
||||
return <Chip key={i} label={label} size="small" variant="outlined" />;
|
||||
return (
|
||||
<Chip
|
||||
key={i}
|
||||
label={label}
|
||||
size="small"
|
||||
variant="outlined"
|
||||
onClick={(e) => handleFkClick(e, item)}
|
||||
sx={basePath ? { cursor: "pointer" } : undefined}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</Box>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user