🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@idui/react-file-input

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@idui/react-file-input

React FileInput Component

latest
Source
npmnpm
Version
1.2.11
Version published
Maintainers
1
Created
Source

FileInput React Component

NPM Size JavaScript Style Guide Coverage Status LICENSE

  • Docs
  • Playground

Install

npm install --save @idui/react-file-input
yarn add @idui/react-file-input

See props in Docs

Basic Example without uploading.

  • Live example
  • if onUpload not specified onChange called with FileReader result
import React from 'react'
import FileInput, { UploadArea } from '@idui/react-file-input'

function Example() {
  const [src, setSrc] = useState();
  
  return  <UploadArea>
      <FileInput onChange={setValue} accept="image/*" />
      {src ? <img alt="" src={src} /> : <span>Drop file here or click to upload</span>}
  </UploadArea>
}

Basic Example with uploading.

  • if onUpload specified then onChange called with onUpload result
import React from 'react'
import FileInput, {UploadArea} from '@idui/react-file-input'

// if multiple then here would be files array
const upload = (file) => {
    // should return Promise
    return fetch('http://example.com', {
        method: 'POST',
        body: file
    }).then(response => response.json().data.src)
};

function Example() {
    const [src, setSrc] = useState();
    const [isUploading, setUploading] = useState();
    
    const handleChange = (newSrc) => {
        setUploading(false);
        setSrc(newSrc)
    };

    return <UploadArea>
        <FileInput
            accept="image/*"
            onStartUploading={() => setUploading(true)}
            onUpload={upload}
            onChange={handleChange}
            onError={() => setUploading(false)}
            maxFileSize={10}
        />
        {src ? <img alt="" src={src}/> : <span>Drop file here or click to upload</span>}
    </UploadArea>
}

Custom Upload Area

import React from 'react'
import styled from 'styled-components';
import FileInput from '@idui/react-file-input'

const CustomUploadArea = styled.div`
  position: relative;
  width: 15rem;
  height: 15rem;
  border-radius: 50%;
  backgroun-color: orangered;
  img {
    height: 100%;
    width: auto;
    max-width: 100%;
  }
`

function Example() {
  const [src, setSrc] = useState();
  
  return  <CustomUploadArea>
      <FileInput onChange={setValue} accept="image/*" />
      {src && <img alt="" src={src} />}
  </CustomUploadArea>
}

Show dragging

import React from 'react'
import FileInput, { UploadArea } from '@idui/react-file-input'

const DragUploadArea = styled(UploadArea)`
  height: 40rem;
  width: 60rem;
  background-color: ${props => props.isDragging ? '#C7F9F1' : '#FFFFFF'};
`

export function DragAndDropExample({ onUpload, ...props }) {
    const [value, setValue] = useState();

    return (
        <FileInput {...props} onChange={setValue} accept="image/*" >
            {({ dragContainerProps, fileInput }) =>   <DragUploadArea {...dragContainerProps}>
                {fileInput}

                {value ? <img alt="" src={value} /> : <span>{dragContainerProps.isDragging ? 'Drop here' : 'Drop file here or click to upload'}</span>}
            </DragUploadArea>}
        </FileInput>
    );
}

See more details in storybook

License

MIT © kaprisa57@gmail.com

FAQs

Package last updated on 30 Nov 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts