Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 63x 17x 63x 63x 63x 63x 63x 63x 63x 58x 58x 58x 58x 10x 10x 10x 10x 7x 7x 7x 7x 7x 4x 7x 2x 1x 6x 6x 6x 6x 1x 5x 5x 1x 1x 1x 1x 1x 1x 1x 94x 94x 94x 94x 6x 94x 1x 94x 1x 94x 8x 94x 183x 8x 8x | import React from "react"; import PropTypes from "prop-types"; import { validateRestrictions } from "../../libs/fileValidation"; class FileInput extends React.Component { constructor(props) { super(props); this.fileClickHandler = this.fileClickHandler.bind(this); this.fileChangedHandler = this.fileChangedHandler.bind(this); this.keyboardEventHandler = this.keyboardEventHandler.bind(this); this.dragOverHandler = this.dragOverHandler.bind(this); this.dropHandler = this.dropHandler.bind(this); this.dragExitHandler = this.dragExitHandler.bind(this); this.dragLeaveHandler = this.dragLeaveHandler.bind(this); this.cancelDragging = this.cancelDragging.bind(this); this.state = { isDragging: false, canDrop: false, overLimit: false, }; } componentDidMount() { const { restrictions, inputName, inputId } = this.props; // Handle input creation outside of the react lifecycle to avoid // re-rendering and side-effects. We need the input to maintain a consistent // DOM state throughout the component lifecycle. this.input = document.createElement("input"); this.input.type = "file"; this.input.name = inputName; this.input.id = inputId; if (restrictions && restrictions.accept) { this.input.accept = restrictions.accept; } this.input.hidden = true; this.input.addEventListener("change", this.fileChangedHandler); this.holder.appendChild(this.input); document.addEventListener("dragover", this.dragOverHandler); document.addEventListener("drop", this.dropHandler); Iif ("ondragexit" in document) { // Firefox fires dragexit and we can use the event.relatedTarget to see what the // new element taking focus is. When leaving the window, this is set to null document.addEventListener("dragexit", this.dragExitHandler); } else { // Chrome does not implement dragexit, but it does fire an event as soon as the // mouse leaves the window, setting the mouse position to 0, 0 document.addEventListener("dragleave", this.dragLeaveHandler); } } componentWillUnmount() { document.removeEventListener("dragover", this.dragOverHandler); document.removeEventListener("drop", this.dropHandler); Iif ("ondragexit" in document) { document.removeEventListener("dragexit", this.dragExitHandler); } else { document.removeEventListener("dragleave", this.dragLeaveHandler); } } fileClickHandler() { const { active } = this.props; Eif (active) { this.input.value = ""; this.input.click(); } } fileChangedHandler() { const { fileChangedCallback, restrictions } = this.props; const files = Array.from(this.input.files).map((file) => validateRestrictions(file, restrictions), ); Promise.all(files).then((values) => { // Remove the new file from input if is not valid if (values[0].errors) { this.input.value = ""; } fileChangedCallback(values); }); } keyboardEventHandler(e) { if (e.key === "Enter") { this.fileClickHandler(); } } dragOverHandler(e) { e.preventDefault(); const dataTransfer = e.dataTransfer || e.target.dataTransfer; Eif (dataTransfer.types && dataTransfer.types.indexOf("Files") !== -1) { if (dataTransfer.items.length > 1) { this.setState({ isDragging: true, overLimit: true, }); } else { this.setState({ isDragging: true, canDrop: false, overLimit: false, }); if (e.target === this.holder) { this.setState({ canDrop: true, }); } } } } cancelDragging() { this.setState({ isDragging: false, canDrop: false, overLimit: false, }); } dragExitHandler(e) { const relatedTarget = e.relatedTarget || e.target.relatedTarget; if (relatedTarget === null) { this.cancelDragging(); } } dragLeaveHandler(e) { if (e.clientX + e.clientY === 0) { this.cancelDragging(); } } dropHandler(e) { e.preventDefault(); e.stopPropagation(); const dataTransfer = e.dataTransfer || e.target.dataTransfer; if (dataTransfer.items.length === 1) { this.setState({ isDragging: false, canDrop: false, }); Iif (e.target === this.holder) { // I can't find a way to test this this.input.files = dataTransfer.files; this.fileChangedHandler(); } } else E{ this.cancelDragging(); } } render() { const { className, children, isSmall, noFocus } = this.props; const { isDragging, canDrop, overLimit } = this.state; const localClassName = [className, "p-file-input"]; if (isDragging) { localClassName.push("is-dragging"); } if (canDrop) { localClassName.push("can-drop"); } if (overLimit) { localClassName.push("over-limit"); } if (isSmall) { localClassName.push("p-file-input--is-small"); } return ( <div ref={(el) => (this.holder = el)} className={localClassName.join(" ")} onClick={this.fileClickHandler} onKeyDown={this.keyboardEventHandler} tabIndex={noFocus ? null : 0} > {children} </div> ); } } FileInput.defaultProps = { className: "", inputName: "file-upload", inputId: "file-upload", fileChangedCallback: () => {}, restrictions: { accept: [], }, active: true, isSmall: false, noFocus: false, }; FileInput.propTypes = { className: PropTypes.string, children: PropTypes.node, inputName: PropTypes.string, inputId: PropTypes.string, fileChangedCallback: PropTypes.func, restrictions: PropTypes.shape({ accept: PropTypes.arrayOf(PropTypes.string), }), active: PropTypes.bool, isSmall: PropTypes.bool, noFocus: PropTypes.bool, }; export { FileInput as default }; |