| 
 | 1 | +import React from "react";  | 
 | 2 | +import MimetypeDropdown from "./MimetypeDropdown";  | 
 | 3 | +import LayerSelector from "../LayerSelector";  | 
 | 4 | +import FileFormats from "../../common/FileFormats";  | 
 | 5 | +import { LayerInput, FileInput } from "../../common/ComplexInputs";  | 
 | 6 | + | 
 | 7 | +export default class ComplexInput extends React.Component {  | 
 | 8 | + setFormat = ({ mimeType }) => {  | 
 | 9 | + if (mimeType !== this.state.currentMimeType) {  | 
 | 10 | + this.changeValue(null);  | 
 | 11 | + }  | 
 | 12 | + this.setState({ currentMimeType: mimeType });  | 
 | 13 | + };  | 
 | 14 | + | 
 | 15 | + state = {  | 
 | 16 | + currentMimeType: null  | 
 | 17 | + };  | 
 | 18 | + | 
 | 19 | + // in case the input has changed (for example: other process loaded) we will take the new input first format.  | 
 | 20 | + // will set the first format in initializing too.  | 
 | 21 | + static getDerivedStateFromProps(props, state) {  | 
 | 22 | + const { currentMimeType } = state;  | 
 | 23 | + const format = props.formInput.formats.filter(  | 
 | 24 | + ({ mimeType }) => mimeType === currentMimeType  | 
 | 25 | + )[0];  | 
 | 26 | + | 
 | 27 | + if (!format) {  | 
 | 28 | + return { currentMimeType: props.formInput.formats[0].mimeType };  | 
 | 29 | + } else {  | 
 | 30 | + return null;  | 
 | 31 | + }  | 
 | 32 | + }  | 
 | 33 | + | 
 | 34 | + changeValue(value) {  | 
 | 35 | + this.props.onChange(value ? [value] : [], this.props.index);  | 
 | 36 | + }  | 
 | 37 | + | 
 | 38 | + onLayerChange = layerId => {  | 
 | 39 | + this.changeValue(new LayerInput(this.state.currentMimeType, layerId));  | 
 | 40 | + };  | 
 | 41 | + onFileChange = e => {  | 
 | 42 | + this.changeValue(  | 
 | 43 | + new FileInput(this.state.currentMimeType, e.target.files[0])  | 
 | 44 | + );  | 
 | 45 | + };  | 
 | 46 | + | 
 | 47 | + onInputFileRef = element => {  | 
 | 48 | + this.inputFileRef = element;  | 
 | 49 | + };  | 
 | 50 | + | 
 | 51 | + render() {  | 
 | 52 | + const { layers, index, formInput } = this.props;  | 
 | 53 | + const { currentMimeType } = this.state;  | 
 | 54 | + const isLayer =  | 
 | 55 | + FileFormats.hasOwnProperty(currentMimeType) &&  | 
 | 56 | + FileFormats[currentMimeType].isLayer;  | 
 | 57 | + return (  | 
 | 58 | + <div>  | 
 | 59 | + <MimetypeDropdown  | 
 | 60 | + index={index}  | 
 | 61 | + onChange={this.setFormat}  | 
 | 62 | + options={formInput.formats}  | 
 | 63 | + />  | 
 | 64 | + {isLayer ? (  | 
 | 65 | + <LayerSelector  | 
 | 66 | + className="layer-select"  | 
 | 67 | + layers={layers}  | 
 | 68 | + key={index}  | 
 | 69 | + value={formInput.values[0]}  | 
 | 70 | + onSelect={this.onLayerChange}  | 
 | 71 | + />  | 
 | 72 | + ) : (  | 
 | 73 | + <input  | 
 | 74 | + onChange={this.onFileChange}  | 
 | 75 | + ref={this.onInputFileRef}  | 
 | 76 | + type="file"  | 
 | 77 | + name="file"  | 
 | 78 | + />  | 
 | 79 | + )}  | 
 | 80 | + </div>  | 
 | 81 | + );  | 
 | 82 | + }  | 
 | 83 | +}  | 
0 commit comments