diff --git a/src/app/admin/offcuts/Create.tsx b/src/app/admin/offcuts/Create.tsx index a2b44b2..7d98e58 100644 --- a/src/app/admin/offcuts/Create.tsx +++ b/src/app/admin/offcuts/Create.tsx @@ -4,7 +4,6 @@ import { AutocompleteInput, CheckboxGroupInput, Create, - ImageField, ImageInput, RadioButtonGroupInput, ReferenceArrayInput, @@ -17,6 +16,7 @@ import { import { useWatch } from "react-hook-form"; import RemoveChoiceButton from "./RemoveChoiceButton"; import OffcutReferenceInput from "./OffcutReferenceInput"; +import { ImageField } from "./ImageField"; export const MaterialInput = ({ source, @@ -111,21 +111,9 @@ export const Fields = () => { ); }; -export const addBasePathToOffcutPictures = (data: any) => { - return { - ...data, - pictures: data.pictures.map((picture: any) => { - return { - ...picture, - src: `/scrrap-lab${picture.src}`, - }; - }), - }; -}; - const OffcutCreate = () => { return ( - + diff --git a/src/app/admin/offcuts/Edit.tsx b/src/app/admin/offcuts/Edit.tsx index 23e370e..1289eb7 100644 --- a/src/app/admin/offcuts/Edit.tsx +++ b/src/app/admin/offcuts/Edit.tsx @@ -1,12 +1,11 @@ import { Edit, SimpleForm } from "react-admin"; -import { addBasePathToOffcutPictures, Fields } from "./Create"; +import { Fields } from "./Create"; const OffcutEdit = () => { return ( diff --git a/src/app/admin/offcuts/ImageField.tsx b/src/app/admin/offcuts/ImageField.tsx new file mode 100644 index 0000000..8115e75 --- /dev/null +++ b/src/app/admin/offcuts/ImageField.tsx @@ -0,0 +1,153 @@ +/* eslint-disable @next/next/no-img-element */ +import * as React from "react"; +import { styled } from "@mui/material/styles"; +import { Box, Typography } from "@mui/material"; +import PropTypes from "prop-types"; +import get from "lodash/get"; +import { useRecordContext, useTranslate } from "ra-core"; +import { TableCellProps } from "@mui/material/TableCell"; +import { SxProps } from "@mui/system"; +import { sanitizeFieldRestProps, FieldProps } from "react-admin"; +import { PropertyPath } from "lodash"; + +type TextAlign = TableCellProps["align"]; +type SortOrder = "ASC" | "DESC"; + +const fieldPropTypes = { + sortBy: PropTypes.string, + sortByOrder: PropTypes.oneOf(["ASC", "DESC"]), + source: PropTypes.string, + label: PropTypes.oneOfType([ + PropTypes.string, + PropTypes.element, + PropTypes.bool, + ]), + sortable: PropTypes.bool, + className: PropTypes.string, + cellClassName: PropTypes.string, + headerClassName: PropTypes.string, + textAlign: PropTypes.oneOf([ + "inherit", + "left", + "center", + "right", + "justify", + ]), + emptyText: PropTypes.string, +}; + +export const ImageField = < + RecordType extends Record = Record +>( + props: ImageFieldProps +) => { + const { className, emptyText, source, src, title, ...rest } = props; + const record = useRecordContext(props); + const sourceValue = get(record, source as PropertyPath); + const translate = useTranslate(); + + if (!sourceValue) { + return emptyText ? ( + + {emptyText && translate(emptyText, { _: emptyText })} + + ) : ( + + ); + } + + if (Array.isArray(sourceValue)) { + return ( + +
    + {sourceValue.map((file, index) => { + const fileTitleValue = get(file, title as PropertyPath) || title; + const srcValue = get(file, src as PropertyPath) || title; + + return ( +
  • + {fileTitleValue} +
  • + ); + })} +
+
+ ); + } + + const titleValue = + get(record, title as PropertyPath, "")?.toString() || title; + + return ( + + {titleValue} + + ); +}; + +// What? TypeScript loses the displayName if we don't set it explicitly +ImageField.displayName = "ImageField"; + +ImageField.propTypes = { + ...fieldPropTypes, + src: PropTypes.string, + title: PropTypes.string, +}; + +const PREFIX = "RaImageField"; + +export const ImageFieldClasses = { + list: `${PREFIX}-list`, + image: `${PREFIX}-image`, +}; + +const Root = styled(Box, { + name: PREFIX, + overridesResolver: (props, styles) => styles.root, +})({ + [`& .${ImageFieldClasses.list}`]: { + display: "flex", + listStyleType: "none", + }, + [`& .${ImageFieldClasses.image}`]: { + margin: "0.25rem", + width: 200, + height: 100, + objectFit: "contain", + }, +}); + +export interface ImageFieldProps< + RecordType extends Record = Record +> extends FieldProps { + src?: string; + title?: string; + sx?: SxProps; +}