import React from 'react'; import PropTypes from 'prop-types'; import {Row, LineInput, Label, Input, TextArea} from './'; import {IconButton} from '../'; import {onEventCapture} from '../utils'; import {gettext} from 'core/utils'; import {get} from 'lodash'; import './style.scss'; /** * @ngdoc react * @name FileInput * @description Component to sattach files as input */ export class FileInput extends React.Component { static propTypes: any; static defaultProps: any; dom: any; constructor(props) { super(props); this.dom = {fileInput: null}; this.onAdd = this.onAdd.bind(this); this.onDrop = this.onDrop.bind(this); this.getComponent = this.getComponent.bind(this); this.onBrowseClick = this.onBrowseClick.bind(this); this.onRemove = this.onRemove.bind(this); this.onDragEnter = this.onDragEnter.bind(this); } onBrowseClick() { if (this.dom.fileInput) { this.handleOnFocus(); this.dom.fileInput.click(); } } onDragEnter(e) { e.dataTransfer.dropEffect = 'copy'; } handleOnFocus() { if (this.props.onFocus) { this.props.onFocus(); } } onDrop(event) { onEventCapture(event); if (get(event, 'dataTransfer.files')) { this.handleOnFocus(); this.onAdd(null, event.dataTransfer.files); } } onAdd(field, fileList) { const files = Array.from(fileList).map((f) => [f]); this.props.onChange(this.props.field, [...this.props.value, ...files]); } onRemove(index) { this.handleOnFocus(); this.props.value.splice(index, 1); this.props.onChange(this.props.field, [...this.props.value]); } getComponent(val, index = 0) { const {readOnly, onFocus, field, createLink} = this.props; return readOnly ? ( {get(val, 'media') && ( )} ) : ( {get(val, 'media') && ( {val.media.name}  ({Math.round(val.media.length / 1024)}kB) ) || (