0.0.7 • Published 2 years ago

@tallis/react-dndp v0.0.7

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

react-dndp

A react component for drag and drop photo files

Note

This is a React Component firstly designed for private use and has been chosen to be publicly available, so the implementation details can be confusing. And so there won't be regular updates.

Installing

yarn add @tallis/react-dndp

Props & Functions

PropFuncTypeRequiredDefaultNotePurpose
ReactReacttruenullNeeded for the component to operate
NextImagenext/imagefalsenullWhen it is a Next ProjectTo replace basic tag for showing uploaded photo
formikbooleanfalsenullTo set the return data for setData prop
clickWholebooleanfalsenullIf it true, the whole drag and drop area will be clickable
showIconUploadbooleanfalsenullUsed to toggle displaying placeholder for drag&drop area
showDeletebooleanfalsenullIf true, there will be a delete icon when the photo has been uploaded
helperTextstringfalse"Select a photo from the gallery or drag and drop it here."Helpertext for the area
database64 stringfalsenullDisplayed photo
setDatafunction(param1, param2)truenullDepends on prop "formik", there can be one or two params. If "formik", then setData(file, base64), else setData(base64)
getFileSizefunction(fileSize)falsenullUsed to get the size of photo in Bytes
setFileNamefunction(fileName)falsenullUsed to get the name of photo
setSizefunction(sizes)falsenull{width, height}Get the dimensions of photo
onErrorfunction()falsenullCallback when there is an error
CustomIconReact ElementfalsenullMust be react componentBe displayed when there is no photo
containerPropsReact PropsfalsenullProps for - MUI
stylesImage, stylesImage2React PropsfalsenullProps for NextImage's wrappers (see example below)

Example

import Image from 'next/image';

import Uploader from '@tallis/react-dndp';
import Paper from '@mui/material';

import UploadIconIllustration from './UploadIconIllustration';

// other code

<Paper variant='outlined' sx={{
    position: 'relative',
    height: photoSizes?.width * 0.7,
    width: photoSizes?.width * 0.7,
    overflow: 'hidden',
    borderRadius: '10px',
    mt: 1
}}>
    <Uploader
         containerProps={{
             sx: {
                 position: 'absolute',
                 top: 0,
                 zIndex: 2,
                 width: '100%',
                 height: '100%',
                 cursor: 'pointer',
                 display: 'flex',
                 flexDirection: 'row',
                 justifyContent: 'center',
                 alignItems: 'center'
                 },
             container: true
          }}
          stylesImage={{
             style: {
                 position: 'absolute',
                 top: 0,
                 left: 0,
                 width: '100%',
                 height: '100%',
             }
          }}
          stylesImage2={{
             style: {
                 width: '100%',
                 height: '100%',
                 position: 'relative'
             }
          }}
          setData={(data, base64) => {
             photo?.set('illustration', data);
             photo?.set('photo', base64);
             setPhoto(photo);
         }}
         data={Boolean(photo?.get('photo')) ? photo?.get('photo') : null}
         CustomIcon={UploadIconIllustration}
         showIconUpload={Boolean(photo?.get('photo')) ? false : true}
         getFileSize={(data) => setIsOver10MB(data)}
         isFormik
         clickWhole
         NextImage={Image}
         React={React}
         onError={() => dispatch({ type: t.SHOW_SNACKBAR, payload: { message: 'Only image file is allowed', type: 'error' } })}
     />
 </Paper>

// other code