|
1 | | -export function FormInput({label, help_text, error, ...props}) { |
| 1 | +import Button from './buttons'; |
| 2 | + |
| 3 | + |
| 4 | +export function FormInput({label, help_text, error, inputRef, ...props}) { |
2 | 5 |
|
3 | 6 | if (props.type === 'string') |
4 | 7 | props.type = 'text' |
5 | 8 |
|
| 9 | + if (inputRef) |
| 10 | + props.ref = inputRef; |
| 11 | + |
6 | 12 | return ( |
7 | 13 | <div> |
8 | 14 | {label && <label>{label}</label>} |
@@ -86,7 +92,140 @@ export function FormSelectInput({label, help_text, error, value, options, ...pro |
86 | 92 | ); |
87 | 93 | } |
88 | 94 |
|
| 95 | +export function dataURItoBlob(dataURI) { |
| 96 | + // Split metadata from data |
| 97 | + const splitted = dataURI.split(","); |
| 98 | + // Split params |
| 99 | + const params = splitted[0].split(";"); |
| 100 | + // Get mime-type from params |
| 101 | + const type = params[0].replace("data:", ""); |
| 102 | + // Filter the name property from params |
| 103 | + const properties = params.filter(param => { |
| 104 | + return param.split("=")[0] === "name"; |
| 105 | + }); |
| 106 | + // Look for the name and use unknown if no name property. |
| 107 | + let name; |
| 108 | + if (properties.length !== 1) { |
| 109 | + name = "unknown"; |
| 110 | + } else { |
| 111 | + // Because we filtered out the other property, |
| 112 | + // we only have the name case here. |
| 113 | + name = properties[0].split("=")[1]; |
| 114 | + } |
| 115 | + |
| 116 | + // Built the Uint8Array Blob parameter from the base64 string. |
| 117 | + const binary = atob(splitted[1]); |
| 118 | + const array = []; |
| 119 | + for (let i = 0; i < binary.length; i++) { |
| 120 | + array.push(binary.charCodeAt(i)); |
| 121 | + } |
| 122 | + // Create the blob object |
| 123 | + const blob = new window.Blob([new Uint8Array(array)], { type }); |
| 124 | + |
| 125 | + return {blob, name}; |
| 126 | +} |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +export class FormFileInput extends React.Component { |
| 131 | + constructor(props) { |
| 132 | + super(props); |
| 133 | + |
| 134 | + this.state = { |
| 135 | + value: props.value, |
| 136 | + fileName: this.getFileName() |
| 137 | + }; |
| 138 | + |
| 139 | + this.inputRef = React.createRef(); |
| 140 | + } |
| 141 | + |
| 142 | + componentDidUpdate(prevProps, prevState) { |
| 143 | + if (this.props.value !== prevProps.value) { |
| 144 | + this.setState({ |
| 145 | + value: this.props.value, |
| 146 | + fileName: this.getFileName() |
| 147 | + }); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + getFileName = () => { |
| 152 | + if (!this.props.value) |
| 153 | + return ''; |
| 154 | + |
| 155 | + if (this.props.type === 'data-url') { |
| 156 | + return this.extractFileInfo(this.props.value).name; |
| 157 | + } else if (this.props.type === 'file-url') { |
| 158 | + return this.props.value; |
| 159 | + } else { |
| 160 | + return 'Unknown file'; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + extractFileInfo = (dataURL) => { |
| 165 | + const {blob, name} = dataURItoBlob(dataURL); |
| 166 | + return { |
| 167 | + name: name, |
| 168 | + size: blob.size, |
| 169 | + type: blob.type |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + addNameToDataURL = (dataURL, name) => { |
| 174 | + return dataURL.replace(';base64', ';name=' + encodeURIComponent(name) + ';base64'); |
| 175 | + } |
89 | 176 |
|
90 | | -export function FileInput(props) { |
91 | | - return <FormInput {...props} /> |
| 177 | + handleChange = (e) => { |
| 178 | + if (this.props.type === 'data-url') { |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + let file = e.target.files[0]; |
| 183 | + let fileName = file.name |
| 184 | + |
| 185 | + let reader = new FileReader(); |
| 186 | + |
| 187 | + reader.onload = () => { |
| 188 | + |
| 189 | + // this.setState({src: reader.result}); |
| 190 | + |
| 191 | + // we create a fake event object |
| 192 | + let event = { |
| 193 | + target: { |
| 194 | + type: 'text', |
| 195 | + value: this.addNameToDataURL(reader.result, fileName), |
| 196 | + name: this.props.name |
| 197 | + } |
| 198 | + }; |
| 199 | + |
| 200 | + this.props.onChange(event); |
| 201 | + |
| 202 | + } |
| 203 | + reader.readAsDataURL(file); |
| 204 | + |
| 205 | + } |
| 206 | + |
| 207 | + showFileBrowser = () => { |
| 208 | + this.inputRef.current.click(); |
| 209 | + } |
| 210 | + |
| 211 | + render() { |
| 212 | + let {label, value, ...props} = {value, ...this.props}; |
| 213 | + props.type = 'file'; |
| 214 | + props.onChange = this.handleChange; |
| 215 | + |
| 216 | + return ( |
| 217 | + <div> |
| 218 | + {label && <label>{label}</label>} |
| 219 | + <div className="rjf-file-field"> |
| 220 | + {this.state.value && |
| 221 | + <div className="rjf-current-file-name">Current file: <span>{this.state.fileName}</span></div> |
| 222 | + } |
| 223 | + {this.state.value && 'Change:'} |
| 224 | + <div className="rjf-file-field-input"> |
| 225 | + <FormInput {...props} inputRef={this.inputRef} /> |
| 226 | + </div> |
| 227 | + </div> |
| 228 | + </div> |
| 229 | + ); |
| 230 | + } |
92 | 231 | } |
0 commit comments