Sha256: 6942346c2df0c14002f381087c0ead11ebfbbaed11bd5c671af0ec2066da15ed

Contents?: true

Size: 1.72 KB

Versions: 28

Compression:

Stored size: 1.72 KB

Contents

import React, { useCallback } from 'react'
import { useDropzone } from 'react-dropzone'
import classnames from 'classnames'

import { buildCss, buildDataProps, noop } from '../utilities/props'
import { globalProps } from '../utilities/globalProps'
import type { Callback } from '../types'

import Body from '../pb_body/_body'
import Card from '../pb_card/_card'

type FileUploadProps = {
  accept?: string[],
  className?: string,
  data?: object,
  acceptedFilesDescription?: string,
  onFilesAccepted: Callback<File, File>,
}

const FileUpload = (props: FileUploadProps) => {
  const {
    accept = null,
    acceptedFilesDescription = '',
    className,
    data = {},
    onFilesAccepted = noop,
  } = props

  const onDrop = useCallback((files) => onFilesAccepted(files), []);
  const { getRootProps, getInputProps, isDragActive } = useDropzone({
    accept,
    onDrop,
  })

  const acceptedFileTypes = () => {
    return accept.map((fileType) => {
      if (fileType.startsWith('image/')) {
        return fileType.replace('image/', ' ')
      } else {
        return fileType
      }
    })
  }

  const dataProps = buildDataProps(data)

  return (
    <div
        className={classnames(buildCss('pb_file_upload_kit'), globalProps(props), className)}
        {...dataProps}
        {...getRootProps()}
    >
      <Card>
        <input {...getInputProps()} />
        <Body color="light">
          {isDragActive ?
            <p>{'Drop the files here ...'}</p>
            :
            <p>{accept === null ? 'Choose a file or drag it here' : `Choose a file or drag it here. The accepted file types are: ${acceptedFilesDescription || acceptedFileTypes()}`}</p>
          }
        </Body>
      </Card>
    </div>
  )
}

export default FileUpload

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
playbook_ui-11.12.1.pre.alpha.charts1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.12.1.pre.alpha.passphrase1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.12.1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.12.0 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.11.0.pre.alpha.paginate2 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.11.0.pre.alpha.dialog2 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.11.0.pre.alpha.paginate1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.11.0.pre.alpha.renderer1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.11.0.pre.alpha.dialog1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.11.0 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.10.0.pre.alpha.pre.bold1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.10.0.pre.alpha.pagination1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.10.0 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.9.0 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.8.1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.8.0 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.7.0.pre.alpha.table1 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.7.0 app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.6.0.pre.alpha.fontawesome app/pb_kits/playbook/pb_file_upload/_file_upload.tsx
playbook_ui-11.5.0.pre.alpha.fontawesome app/pb_kits/playbook/pb_file_upload/_file_upload.tsx