ImageInput

<ImageInput> allows editing and uploading image attachments. It is a thin wrapper around <FileInput> that defaults to accepting only images and renders an <ImageField> thumbnail preview by default.

Installation

pnpm dlx shadcn@latest add @shadmin/image-input

Usage

import { ImageInput, ImageField } from "@/components/admin";
 
<ImageInput source="pictures" multiple>
  <ImageField source="src" title="title" />
</ImageInput>;

<ImageInput> uses its child component to give a preview of the images. <ImageInput> renders its child once per file, inside a <RecordContext>, so the child can be a Field component. When no child is provided, a default <ImageField> is used with a thumbnail style.

The input value must be an object or an array of objects with a title and a src property, e.g.:

{
    id: 123,
    pictures: [
        {
            title: 'photo-1.jpg',
            src: 'https://example.com/uploads/photo-1.jpg',
        },
        {
            title: 'photo-2.jpg',
            src: 'https://example.com/uploads/photo-2.jpg',
        },
    ],
}

After modification by the user, the value is stored as an array of objects with 3 properties:

It is the responsibility of your dataProvider to send the file to the server (encoded in Base64, or using multipart upload) and to transform the src property. See the Data Provider documentation for an example.

Files are accepted or rejected based on the accept, multiple, minSize and maxSize props. By default, <ImageInput> accepts any image MIME type ({ "image/*": [] }).

Props

<ImageInput> accepts the same props as <FileInput>:

PropRequiredTypeDefaultDescription
sourceRequiredstring-Field name
acceptOptionalDropzoneOptions['accept']{ "image/*": [] }MIME / extension accept map
childrenOptionalReactNode<ImageField>Preview element (single)
classNameOptionalstring-Wrapper classes
helperTextOptionalReactNode-Help text
labelMultipleOptionalstringra.input.image.upload_severali18n key for multiple placeholder
labelSingleOptionalstringra.input.image.upload_singlei18n key for single placeholder
maxSizeOptionalnumber-Max bytes
minSizeOptionalnumber-Min bytes
multipleOptionalbooleanfalseAllow multiple files
onRemoveOptional(file:any)=>void-Callback after removing a file
optionsOptionalDropzoneOptions-Extra dropzone options
placeholderOptionalReactNode-Custom placeholder content
validateFileRemovalOptional(file:any)=>boolean|Promise<boolean>-Throw/cancel to prevent removal

accept

By default, <ImageInput> accepts any image MIME type. Override this prop to restrict the accepted formats:

<ImageInput source="picture" accept={{ "image/png": [], "image/jpeg": [] }}>
  <ImageField source="src" title="title" />
</ImageInput>

children

Provide a custom preview element to control how thumbnails are rendered. When omitted, a default <ImageField> is used:

<ImageInput source="pictures" multiple>
  <ImageField
    source="src"
    title="title"
    className="[&_img]:h-32 [&_img]:w-32 [&_img]:rounded-md [&_img]:object-cover"
  />
</ImageInput>

multiple

Set to true to accept a list of images, false to accept only one. Defaults to false.

<ImageInput source="pictures" multiple>
  <ImageField source="src" title="title" />
</ImageInput>