Allow ResourceView fields to link to their value

This commit is contained in:
Mustafa Gezen 2023-08-28 16:56:17 +02:00
parent 5ac7179277
commit 74e92b6ec8
Signed by: mustafa
GPG Key ID: DCDF010D946438C1

View File

@ -25,6 +25,7 @@ export interface ResourceViewField {
key: string;
label: string;
type?: 'text' | 'number' | 'date';
linkToSelf?: boolean;
}
export interface ResourceViewProps<T> {
@ -46,7 +47,18 @@ export function ResourceView<T>(props: ResourceViewProps<T>) {
<Box sx={{ my: 2.5 }}>
<Typography variant="subtitle2">{field.label}</Typography>
<Typography variant="body1">
{(props.resource as any)[field.key]?.toString() || '--'}
{field.linkToSelf ? (() => {
const value = (props.resource as any)[field.key]?.toString();
if (!value) {
return '--';
}
return (
<a href={value} target="_blank" rel="noreferrer">
{value}
</a>
);
})() : (props.resource as any)[field.key]?.toString() || '--'}
</Typography>
</Box>
))}