import React, { useState } from 'react' import { FileUpload, List, ListItem, } from 'playbook-ui' const AcceptedFilesList = ({ files }) => ( <List> {files.map((file) => ( <ListItem key={file.name}>{file.name}</ListItem> ))} </List> ) const FileUploadDefault = (props) => { const [filesToUpload, setFilesToUpload] = useState([]) const handleOnFilesAccepted = (files) => { setFilesToUpload([...filesToUpload, ...files]) } return ( <div> <AcceptedFilesList files={filesToUpload} /> <FileUpload onFilesAccepted={handleOnFilesAccepted} /> </div> ) } export default FileUploadDefault
import React, { useState } from 'react' import { FileUpload, List, ListItem, } from 'playbook-ui' const AcceptedFilesList = ({ files }) => ( <List> {files.map((file) => ( <ListItem key={file.name}>{file.name}</ListItem> ))} </List> ) const FileUploadAccept = (props) => { const [filesToUpload, setFilesToUpload] = useState([]) const handleOnFilesAccepted = (files) => { setFilesToUpload([...filesToUpload, ...files]) } return ( <div> <AcceptedFilesList files={filesToUpload} /> <FileUpload accept={['image/svg+xml']} onFilesAccepted={handleOnFilesAccepted} /> </div> ) } export default FileUploadAccept
import React, { useState } from 'react' import { FileUpload, List, ListItem, } from 'playbook-ui' const AcceptedFilesList = ({ files }) => ( <List> {files.map((file) => ( <ListItem key={file.name}>{file.name}</ListItem> ))} </List> ) const FileUploadCustomMessage = (props) => { const [filesToUpload, setFilesToUpload] = useState([]) const handleOnFilesAccepted = (files) => { setFilesToUpload([...filesToUpload, ...files]) } return ( <div> <AcceptedFilesList files={filesToUpload} /> <FileUpload customMessage="Playbook is awesome!" onFilesAccepted={handleOnFilesAccepted} /> </div> ) } export default FileUploadCustomMessage
Sometimes you may want to create a custom description that is easier for users to read. In this case, you can use the acceptedFilesDescription
prop.
/* eslint-disable react/no-multi-comp */ import React, { useState } from "react"; import { FileUpload, List, ListItem } from "playbook-ui"; const AcceptedFilesList = ({ files }) => ( <List> {files.map((file) => ( <ListItem key={file.name}>{file.name}</ListItem> ))} </List> ); const FileUploadCustomDescription = (props) => { const [filesToUpload, setFilesToUpload] = useState([]); const handleOnFilesAccepted = (files) => { setFilesToUpload([...filesToUpload, ...files]); }; return ( <div> <AcceptedFilesList files={filesToUpload} /> <FileUpload accept={['application/pdf','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']} acceptedFilesDescription="Adobe (.pdf) and Microsoft (.xslx)" onFilesAccepted={handleOnFilesAccepted} /> </div> ); }; export default FileUploadCustomDescription;
/* eslint-disable react/no-multi-comp */ import React, { useState } from 'react' import { Body, FileUpload, List, ListItem, } from 'playbook-ui' const AcceptedFilesList = ({ files }) => ( <List> {files.map((file) => ( <ListItem key={file.name}>{file.name}</ListItem> ))} </List> ) const RejectedFilesList = ({ files }) => ( <List> {files.map((file) => ( <ListItem key={file.name}><Body color="error">{`${file.name} (file too large)`}</Body></ListItem> ))} </List> ) const FileUploadMaxSize = (props) => { const [filesToUpload, setFilesToUpload] = useState([]) const [filesRejected, setFilesRejected] = useState([]) const [error, setError] = useState() const handleOnFilesAccepted = (files) => { if (files.length) setError() setFilesToUpload([...filesToUpload, ...files]) } const handleOnFilesRejected = (error, files) => { setError(error) setFilesRejected([...filesRejected, ...files]) } return ( <div> <AcceptedFilesList files={filesToUpload} /> <RejectedFilesList files={filesRejected} /> <FileUpload acceptedFilesDescription="Choose a file or drag it here. 1 MB size limit." maxSize={1000000} onFilesAccepted={handleOnFilesAccepted} onFilesRejected={handleOnFilesRejected} /> { error && ( <Body color="error" marginY="md" > {error} </Body> )} </div> ) } export default FileUploadMaxSize