UNPKG

@amsterdam/bmi-component-library

Version:

A React component library based on ASC and Material-UI aimed at repurposing and sharing components across BMI projects

2 lines 4.91 kB
function _define_property(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _object_spread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};var ownKeys=Object.keys(source);if(typeof Object.getOwnPropertySymbols==="function"){ownKeys=ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym){return Object.getOwnPropertyDescriptor(source,sym).enumerable}))}ownKeys.forEach(function(key){_define_property(target,key,source[key])})}return target}function ownKeys(object,enumerableOnly){var keys=Object.keys(object);if(Object.getOwnPropertySymbols){var symbols=Object.getOwnPropertySymbols(object);if(enumerableOnly){symbols=symbols.filter(function(sym){return Object.getOwnPropertyDescriptor(object,sym).enumerable})}keys.push.apply(keys,symbols)}return keys}function _object_spread_props(target,source){source=source!=null?source:{};if(Object.getOwnPropertyDescriptors){Object.defineProperties(target,Object.getOwnPropertyDescriptors(source))}else{ownKeys(Object(source)).forEach(function(key){Object.defineProperty(target,key,Object.getOwnPropertyDescriptor(source,key))})}return target}function _object_without_properties(source,excluded){if(source==null)return{};var target=_object_without_properties_loose(source,excluded);var key,i;if(Object.getOwnPropertySymbols){var sourceSymbolKeys=Object.getOwnPropertySymbols(source);for(i=0;i<sourceSymbolKeys.length;i++){key=sourceSymbolKeys[i];if(excluded.indexOf(key)>=0)continue;if(!Object.prototype.propertyIsEnumerable.call(source,key))continue;target[key]=source[key]}}return target}function _object_without_properties_loose(source,excluded){if(source==null)return{};var target={};var sourceKeys=Object.keys(source);var key,i;for(i=0;i<sourceKeys.length;i++){key=sourceKeys[i];if(excluded.indexOf(key)>=0)continue;target[key]=source[key]}return target}import{jsx as _jsx,jsxs as _jsxs,Fragment as _Fragment}from"react/jsx-runtime";import{useState}from"react";import{useDropzone}from"react-dropzone";import{FileUploadStyle,FileUploadContentStyle,FileUploadPlaceholderStyle,FileUploadSelectFilesButtonStyle}from"./FileUploadStyles";import{useFileUpload}from"./hooks";import FileList from"./FileList/FileList";const detectIsTouchScreenOnly=()=>{var _window;return((_window=window)===null||_window===void 0?void 0:_window.matchMedia("(pointer: coarse)").matches)?true:false};const FileUpload=_param=>{var{getPostUrl,getHeaders,droppingLabel,placeholder,removeLabel,cancelLabel,selectFilesLabel,fileUploadErrorLabel,fileUploadInProgressLabel,fileListTitle,onFileRemove,onFileSuccess,onFileNameClick,removeCompletedFromList,options,storedFiles=[],httpMethod="POST"}=_param,otherProps=_object_without_properties(_param,["getPostUrl","getHeaders","droppingLabel","placeholder","removeLabel","cancelLabel","selectFilesLabel","fileUploadErrorLabel","fileUploadInProgressLabel","fileListTitle","onFileRemove","onFileSuccess","onFileNameClick","removeCompletedFromList","options","storedFiles","httpMethod"]);const[numberOfDraggedFiles,setNumberOfDraggedFiles]=useState(0);const isTouchScreen=detectIsTouchScreenOnly();const{files,handleOnDrop,handleOnCancel,handleOnFileRemove}=useFileUpload(getPostUrl,getHeaders,httpMethod,storedFiles,Math.max(...[...storedFiles.length?storedFiles.map(file=>file.tmpId):[0]]),onFileSuccess);const{open,getRootProps,getInputProps,isDragActive}=useDropzone(_object_spread_props(_object_spread({},options),{onDrop:handleOnDrop,onDragEnter:event=>{setNumberOfDraggedFiles(event.dataTransfer.items.length)},noDrag:isTouchScreen}));return _jsx("div",_object_spread_props(_object_spread({},otherProps),{children:_jsxs(FileUploadStyle,_object_spread_props(_object_spread({"data-testid":"file-upload"},getRootProps({isDragActive})),{children:[_jsx("input",_object_spread({"data-testid":"file-upload__input"},getInputProps())),_jsx(FileUploadContentStyle,{children:_jsxs(_Fragment,{children:[_jsx("div",{children:isDragActive?_jsxs(FileUploadPlaceholderStyle,{children:[numberOfDraggedFiles,"\xa0",droppingLabel]}):_jsxs(_Fragment,{children:[_jsx(FileUploadPlaceholderStyle,{children:placeholder}),_jsx(FileUploadSelectFilesButtonStyle,{variant:"textButton",onClick:open,type:"button",children:selectFilesLabel})]})}),_jsx(FileList,{files:removeCompletedFromList?files.filter(file=>{var _file;return file.progress!==100||((_file=file)===null||_file===void 0?void 0:_file.uploadXhrError)===true}):files,onFileNameClick:onFileNameClick,removeLabel:removeLabel,cancelLabel:cancelLabel,onCancel:handleOnCancel,onFileRemove:file=>{handleOnFileRemove(file);onFileRemove&&onFileRemove(file)},fileUploadErrorLabel:fileUploadErrorLabel,fileUploadInProgressLabel:fileUploadInProgressLabel,title:fileListTitle})]})})]}))}))};export default FileUpload; //# sourceMappingURL=FileUpload.js.map