All files / Files/FileUploader FileUploader.js

37.5% Statements 3/8
0% Branches 0/9
0% Functions 0/1
37.5% Lines 3/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135                      27x     27x                                                                                                                                                                                         27x                                                      
import PropTypes from 'prop-types';
 
import { useDropzone } from 'react-dropzone';
import isFunction from 'lodash/isFunction';
import classNames from 'classnames/bind';
 
import { Button } from '@folio/stripes/components';
 
import css from '../../../../styles/FileUploader.css';
import { useKintIntl } from '../../hooks';
 
const cx = classNames.bind(css);
 
// This is copied from stripes-data-transfer-components and tweaked
const FileUploader = ({
  accept,
  children,
  disabled,
  dropzoneProps = {}, // Any additional dropzone props
  intlKey: passedIntlKey,
  intlNS: passedIntlNS,
  labelOverrides = {},
  maxFiles,
  multiple = true,
  onDrop,
  onDragEnter,
  onDragLeave,
}) => {
  const {
    getRootProps,
    getInputProps,
    open,
    isDragActive,
  } = useDropzone({
    accept,
    disabled,
    multiple,
    maxFiles,
    noClick: true,
    onDrop,
    onDragEnter,
    onDragLeave,
    ...dropzoneProps
  });
  const kintIntl = useKintIntl(passedIntlKey, passedIntlNS);
 
  const updateClassName = cx({
    upload: true,
    activeUpload: isDragActive,
  });
 
  const titleClassName = cx({
    uploadTitle: true,
    activeUploadTitle: isDragActive,
  });
 
  return (
    <div
      aria-disabled={disabled}
      data-test-file-uploader
      {...getRootProps({ role: 'presentation' })}
      className={updateClassName}
      data-testid="fileUploader-input"
    >
      <input
        {...getInputProps()}
      />
      <span
        className={titleClassName}
        data-test-title
      >
        {
          isDragActive ?
            kintIntl.formatKintMessage({
              id: 'files.dropToUpload',
              overrideValue: labelOverrides.dropToUpload,
            }) :
            kintIntl.formatKintMessage({
              id: 'files.dragAndDropToUpload',
              overrideValue: labelOverrides.dragAndDropToUpload,
            })
        }
      </span>
      {!isDragActive && (
        <>
          <div className={css.secondaryArea}>
            <Button
              buttonStyle="primary"
              disabled={disabled}
              marginBottom0
              onClick={open}
            >
              {kintIntl.formatKintMessage({
                id: 'files.uploadButton',
                overrideValue: labelOverrides.uploadButton
              })}
            </Button>
          </div>
          <div className={css.children}>
            {isFunction(children) ? children(open) : children}
          </div>
        </>
      )}
    </div>
  );
};
 
FileUploader.propTypes = {
  accept: PropTypes.oneOfType([
    PropTypes.string,
    PropTypes.arrayOf(PropTypes.string),
  ]),
  children: PropTypes.oneOfType([
    PropTypes.node,
    PropTypes.func,
  ]),
  disabled: PropTypes.bool,
  // eslint-disable-next-line react/forbid-prop-types
  dropzoneProps: PropTypes.object,
  intlKey: PropTypes.string,
  intlNS: PropTypes.string,
  labelOverrides: PropTypes.shape({
    dropToUpload: PropTypes.string,
    dragAndDropToUpload: PropTypes.string,
    uploadButton: PropTypes.string,
  }),
  maxFiles: PropTypes.number,
  multiple: PropTypes.bool,
  onDrop: PropTypes.func.isRequired,
  onDragEnter: PropTypes.func,
  onDragLeave: PropTypes.func,
};
 
export default FileUploader;