@spaced-out/ui-design-system
Version:
Sense UI components library
153 lines (145 loc) • 5.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.FileUpload = void 0;
var React = _interopRequireWildcard(require("react"));
var _useFileUpload = require("../../hooks/useFileUpload");
var _classify = _interopRequireDefault(require("../../utils/classify"));
var _qa = require("../../utils/qa");
var _Button = require("../Button");
var _FileBlock = require("./FileBlock");
var _Truncate = require("../Truncate");
var _FileUploadModule = _interopRequireDefault(require("./FileUpload.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
// This is a file error object that is passed to onRejectedFilesDrop callback in useFileUpload hook
// This is a file rejection object that is passed to handleDropRejected function in useFileUpload hook
// This is a ref object that is passed to FileUpload component for managing state of a single file
// These props are shared between FileUpload component and useFileUpload hook
const FileUploadBase = (props, ref) => {
const {
classNames,
label = 'Upload File',
disabled = false,
instruction = 'Drag and drop or click to upload',
draggingInstruction = 'Drop here to start uploading..',
error = false,
required = false,
secondaryInstruction = '',
maxSize,
accept,
onValidFilesDrop,
onRejectedFilesDrop,
onFileClear,
onFileRefreshClick,
maxFiles = 1,
handleFileDeletionExternally,
testId
} = props;
// Get file upload state from useFileUpload hook
const {
validFiles,
rejectedFiles,
isDragActive,
getRootProps,
shouldAcceptFiles,
getInputProps,
handleFileClear,
moveFileToProgress,
moveFileToSuccess,
moveFileToReject,
setShowReUpload
} = (0, _useFileUpload.useFileUpload)({
maxFiles,
maxSize,
accept,
disabled,
onValidFilesDrop,
onRejectedFilesDrop,
...(handleFileDeletionExternally ? {} : {
onFileClear
})
});
// Expose file upload actions to parent component
React.useImperativeHandle(ref, () => ({
moveFileToProgress,
moveFileToSuccess,
moveFileToReject,
handleFileClear,
setShowReUpload,
validFiles,
rejectedFiles,
files: [...validFiles, ...rejectedFiles]
}));
// Merge valid and rejected files
const files = [...validFiles, ...rejectedFiles];
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: (0, _classify.default)(_FileUploadModule.default.wrapper, classNames?.wrapper),
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'wrapper'
}),
children: [Boolean(label) && /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
className: _FileUploadModule.default.label,
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_Truncate.Truncate, {
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'label'
}),
children: label
}), required && /*#__PURE__*/(0, _jsxRuntime.jsx)("span", {
className: _FileUploadModule.default.required,
children: '*'
})]
}), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_Button.UnstyledButton, {
disabled: disabled || !shouldAcceptFiles,
...getRootProps(),
className: (0, _classify.default)(_FileUploadModule.default.dropzone, {
[_FileUploadModule.default.disabled]: disabled || !shouldAcceptFiles,
[_FileUploadModule.default.dragActive]: isDragActive,
[_FileUploadModule.default.error]: error
}, classNames?.dropZone),
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'dropzone'
}),
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
...getInputProps(),
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'input'
})
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: (0, _classify.default)(_FileUploadModule.default.instruction, classNames?.instruction),
children: isDragActive ? draggingInstruction : instruction
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: (0, _classify.default)(_FileUploadModule.default.secondaryInstruction, classNames?.secondaryInstruction),
children: secondaryInstruction
})]
}), files.length > 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
className: _FileUploadModule.default.files,
"data-testid": (0, _qa.generateTestId)({
base: testId,
slot: 'files'
}),
children: files.map(fileObject => /*#__PURE__*/(0, _jsxRuntime.jsx)(React.Fragment, {
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_FileBlock.FileBlock, {
fileObject: fileObject,
onFileRefreshClick: onFileRefreshClick,
handleFileClear: handleFileDeletionExternally ? onFileClear : handleFileClear,
classNames: {
wrapper: classNames?.files
},
testId: (0, _qa.generateTestId)({
base: testId,
slot: 'file-block',
index: fileObject.id
})
})
}, fileObject.id))
})]
});
};
const FileUpload = exports.FileUpload = /*#__PURE__*/React.forwardRef(FileUploadBase);