|
1 |
| -import React from 'react'; |
| 1 | +import React, { useState, CSSProperties } from 'react'; |
2 | 2 |
|
3 |
| -import { usePapaParse } from 'react-papaparse'; |
| 3 | +import { |
| 4 | + useCSVReader, |
| 5 | + lightenDarkenColor, |
| 6 | + formatFileSize, |
| 7 | +} from 'react-papaparse'; |
4 | 8 |
|
5 |
| -export default function ReadRemoteFile() { |
6 |
| - const { readRemoteFile } = usePapaParse(); |
| 9 | +const GREY = '#CCC'; |
| 10 | +const GREY_LIGHT = 'rgba(255, 255, 255, 0.4)'; |
| 11 | +const DEFAULT_REMOVE_HOVER_COLOR = '#A01919'; |
| 12 | +const REMOVE_HOVER_COLOR_LIGHT = lightenDarkenColor( |
| 13 | + DEFAULT_REMOVE_HOVER_COLOR, |
| 14 | + 40 |
| 15 | +); |
| 16 | +const GREY_DIM = '#686868'; |
7 | 17 |
|
8 |
| - const handleReadRemoteFile = () => { |
9 |
| - readRemoteFile('https://react-papaparse.js.org/static/csv/normal.csv', { |
10 |
| - complete: (results) => { |
| 18 | +const styles = { |
| 19 | + zone: { |
| 20 | + alignItems: 'center', |
| 21 | + border: `2px dashed ${GREY}`, |
| 22 | + borderRadius: 20, |
| 23 | + display: 'flex', |
| 24 | + flexDirection: 'column', |
| 25 | + height: '100%', |
| 26 | + justifyContent: 'center', |
| 27 | + padding: 20, |
| 28 | + } as CSSProperties, |
| 29 | + file: { |
| 30 | + background: 'linear-gradient(to bottom, #EEE, #DDD)', |
| 31 | + borderRadius: 20, |
| 32 | + display: 'flex', |
| 33 | + height: 120, |
| 34 | + width: 120, |
| 35 | + position: 'relative', |
| 36 | + zIndex: 10, |
| 37 | + flexDirection: 'column', |
| 38 | + justifyContent: 'center', |
| 39 | + } as CSSProperties, |
| 40 | + info: { |
| 41 | + alignItems: 'center', |
| 42 | + display: 'flex', |
| 43 | + flexDirection: 'column', |
| 44 | + paddingLeft: 10, |
| 45 | + paddingRight: 10, |
| 46 | + } as CSSProperties, |
| 47 | + size: { |
| 48 | + backgroundColor: GREY_LIGHT, |
| 49 | + borderRadius: 3, |
| 50 | + marginBottom: '0.5em', |
| 51 | + justifyContent: 'center', |
| 52 | + display: 'flex', |
| 53 | + } as CSSProperties, |
| 54 | + name: { |
| 55 | + backgroundColor: GREY_LIGHT, |
| 56 | + borderRadius: 3, |
| 57 | + fontSize: 12, |
| 58 | + marginBottom: '0.5em', |
| 59 | + } as CSSProperties, |
| 60 | + progressBar: { |
| 61 | + bottom: 14, |
| 62 | + position: 'absolute', |
| 63 | + width: '100%', |
| 64 | + paddingLeft: 10, |
| 65 | + paddingRight: 10, |
| 66 | + } as CSSProperties, |
| 67 | + zoneHover: { |
| 68 | + borderColor: GREY_DIM, |
| 69 | + } as CSSProperties, |
| 70 | + default: { |
| 71 | + borderColor: GREY, |
| 72 | + } as CSSProperties, |
| 73 | + remove: { |
| 74 | + height: 23, |
| 75 | + position: 'absolute', |
| 76 | + right: 6, |
| 77 | + top: 6, |
| 78 | + width: 23, |
| 79 | + } as CSSProperties, |
| 80 | +}; |
| 81 | + |
| 82 | +export default function CSVReader() { |
| 83 | + const { CSVReader } = useCSVReader(); |
| 84 | + const [zoneHover, setZoneHover] = useState(false); |
| 85 | + const [removeHoverColor, setRemoveHoverColor] = useState( |
| 86 | + DEFAULT_REMOVE_HOVER_COLOR |
| 87 | + ); |
| 88 | + |
| 89 | + return ( |
| 90 | + <CSVReader |
| 91 | + onUploadAccepted={(results: any) => { |
11 | 92 | console.log('---------------------------');
|
12 |
| - console.log('Results:', results); |
| 93 | + console.log(results); |
13 | 94 | console.log('---------------------------');
|
14 |
| - }, |
15 |
| - }); |
16 |
| - }; |
17 |
| - |
18 |
| - return <button onClick={() => handleReadRemoteFile()}>readRemoteFile</button>; |
| 95 | + setZoneHover(false); |
| 96 | + }} |
| 97 | + onDragOver={(event: DragEvent) => { |
| 98 | + event.preventDefault(); |
| 99 | + setZoneHover(true); |
| 100 | + }} |
| 101 | + onDragLeave={(event: DragEvent) => { |
| 102 | + event.preventDefault(); |
| 103 | + setZoneHover(false); |
| 104 | + }} |
| 105 | + > |
| 106 | + {({ |
| 107 | + getRootProps, |
| 108 | + acceptedFile, |
| 109 | + ProgressBar, |
| 110 | + getRemoveFileProps, |
| 111 | + Remove, |
| 112 | + }: any) => ( |
| 113 | + <> |
| 114 | + <div |
| 115 | + {...getRootProps()} |
| 116 | + style={Object.assign( |
| 117 | + {}, |
| 118 | + styles.zone, |
| 119 | + zoneHover && styles.zoneHover |
| 120 | + )} |
| 121 | + > |
| 122 | + {acceptedFile ? ( |
| 123 | + <> |
| 124 | + <div style={styles.file}> |
| 125 | + <div style={styles.info}> |
| 126 | + <span style={styles.size}> |
| 127 | + {formatFileSize(acceptedFile.size)} |
| 128 | + </span> |
| 129 | + <span style={styles.name}>{acceptedFile.name}</span> |
| 130 | + </div> |
| 131 | + <div style={styles.progressBar}> |
| 132 | + <ProgressBar /> |
| 133 | + </div> |
| 134 | + <div |
| 135 | + {...getRemoveFileProps()} |
| 136 | + style={styles.remove} |
| 137 | + onMouseOver={(event: Event) => { |
| 138 | + event.preventDefault(); |
| 139 | + setRemoveHoverColor(REMOVE_HOVER_COLOR_LIGHT); |
| 140 | + }} |
| 141 | + onMouseOut={(event: Event) => { |
| 142 | + event.preventDefault(); |
| 143 | + setRemoveHoverColor(DEFAULT_REMOVE_HOVER_COLOR); |
| 144 | + }} |
| 145 | + > |
| 146 | + <Remove color={removeHoverColor} /> |
| 147 | + </div> |
| 148 | + </div> |
| 149 | + </> |
| 150 | + ) : ( |
| 151 | + 'Drop CSV file here or click to upload' |
| 152 | + )} |
| 153 | + </div> |
| 154 | + </> |
| 155 | + )} |
| 156 | + </CSVReader> |
| 157 | + ); |
19 | 158 | }
|
0 commit comments