JGuang

ImageUploader <Experimental></Experimental>

ImageUploader <Experimental></Experimental> · Ant Design Mobile

Preview
npm install antd-mobile@5.43.0

ImageUploader <Experimental></Experimental>

Demos

Install

npm install antd-mobile@5.43.0

antd-mobile is for mobile web, not React Native or Mini Programs. React 19 projects must register createRoot with unstableSetRender at the entry, or imperative APIs such as Toast and Dialog cannot render. Switch dark mode with <html data-prefers-color-scheme="dark">.

Examples come from the official Ant Design Mobile repository (MIT) at the commit published as npm 5.43.0. Presentation helpers such as DemoBlock are JGuang rewrites, not antd-mobile APIs.

Demos

ImageUploader

Props

NameDescriptionTypeDefault
acceptThe file types, image/gif image/jpeg image/jpg image/pngstringimage/*
beforeUploadCallback function before file reading, return null to terminate file reading, support return Promise(file: File, files: File[]) => Promise<File | null> | File | null-
capturePicture selection mode, the optional value is camera (directly adjust the camera)boolean | string-
childrenCustom upload buttonReactNode-
columnsNumber of the displayed columnsnumber-
defaultValueDefault list of uploaded filesImageUploadItem[]-
deletableWhether to show the delete buttonbooleantrue
deleteIconThe customized icon in delete buttonReactNode<CloseOutline />
disableUploadWhether to disable the upload buttonbooleanfalse
imageFitThe fill mode of the image, same as Image'contain' | 'cover' | 'fill' | 'none' | 'scale-down'cover
maxCountTo control how many pictures to be uploaded at most, the upload button would be automatically hidden if the number is exceeded, 0 means no limitnumber0
multipleWhether to support selecting multiple picturesbooleanfalse
onChangeTriggered when the list of uploaded files changing(items: ImageUploadItem[]) => void-
onCountExceedTriggered when the number of pictures selected by the user exceeds the maximum limit(exceed: number) => void-
onDeleteTriggered when the successfully uploaded image is deleted, if it returns false, it means that it is prevented from being deleted, and it supports the return of Promise.(item: ImageUploadItem) => boolean | Promise<boolean> | void-
onPreviewCallback when the preview picture is clicked(index: number, item: ImageUploadItem) => void-
previewWhether to support previewbooleantrue
showFailedWhether to display pictures in the failed uploading statebooleantrue
showUploadWhether to display the upload buttonbooleantrue
uploadUpload method, the input parameter is the file object that needs to be uploaded, after asynchronous processing, the upload result is returned(file: File) => Promise<ImageUploadItem>-
valueList of uploaded filesImageUploadItem[]-
onUploadQueueChangeTriggered when the image upload queue changes(tasks: UploadTask[]) => void
renderItemCustom item of uploadList(originNode: React.ReactElement, file: ImageUploadItem, fileList: ImageUploadItem[] ) => React.ReactNode-

Ref

NameDescriptionTypeVersion
nativeElementnative input elementHTMLInputElement | null5.33.0

ImageUploadItem

NameDescriptionTypeDefault
extraAttach some extra dataany-
keyUnique identifierstring | numberThe array index
thumbnailUrlThe resource address of the thumbnail picturestringurl
urlThe resource address of the picturestring-

UploadTask

NameDescriptionTypeDefault
idImage upload idnumber-
statusImage upload status'pending' | 'fail' | 'success'-

CSS Variables

NameDescriptionDefault
--cell-sizeThe size of image and upload button(Valid only in non-columns mode)80px
--gapThe gap between items.12px
--gap-horizontalThe horizontal gap between items.var(--gap)
--gap-verticalThe vertical gap between items.var(--gap)

FAQ

The capture attribute is configured, but why some Android phones will still have the file option?

The capture provided by ImageUploader comes from the native HTML capability, and in some operating systems/browser environments, this attribute may not be supported, so this problem cannot be avoided.

See the discussion in this issue.

Description of the columns Prop

The columns relies on Grid layout and when present, the custom --cell-size is not supported, as the size of image and upload button is calculated automatically.

How to use the client upload capability in the App?

Input native upload can be disabled via the disableUpload and then customise the upload button.

const App = () => {
  const [fileList, setFileList] = useState([])

  const handleUpload = async () => {
    // call app upload
    const url = await hybrid.upload()
    setFileList(fileList => [...fileList, { url }])
  }

  return (
    <ImageUploader
      value={fileList}
      onChange={setFileList}
      disableUpload
    >
      <span
        className='adm-image-uploader-cell adm-image-uploader-upload-button'
        onClick={handleUpload}
      >
        <span className='adm-image-uploader-upload-button-icon'>
          <AddOutline />
        </span>
      </span>
    </ImageUploader>
  )
}