import React, { Component } from "react"; import PropTypes from "prop-types"; import equal from "fast-deep-equal/react"; import IconButton from "../icon-button"; import Button from "../button"; import TextInput from "../text-input"; import StyledFileInput from "./styled-file-input"; class FileInput extends Component { constructor(props) { super(props); this.inputRef = React.createRef(); this.state = { fileName: "", file: null, }; } shouldComponentUpdate(nextProps, nextState) { return !equal(this.props, nextProps) || !equal(this.state, nextState); } onIconFileClick = (e) => { const { isDisabled } = this.props; if (isDisabled) { return false; } e.target.blur(); this.inputRef.current.click(); }; onChangeHandler = (e) => { this.setState({ fileName: e.target.value, }); }; onInputFile = () => { const { onInput } = this.props; if (this.inputRef.current.files.length > 0) { this.setState( { fileName: this.inputRef.current.files[0].name, file: this.inputRef.current.files[0], }, () => { if (onInput) { this.inputRef.current.value = ""; onInput(this.state.file); } } ); } }; render() { //console.log('render FileInput'); const { fileName } = this.state; const { size, placeholder, isDisabled, scale, hasError, hasWarning, accept, id, onInput, // eslint-disable-line no-unused-vars buttonLabel, ...rest } = this.props; let iconSize = 0; let buttonSize = ""; switch (size) { case "base": iconSize = 15; buttonSize = "extrasmall"; break; case "middle": iconSize = 15; buttonSize = "small"; break; case "big": iconSize = 16; buttonSize = "normal"; break; case "huge": iconSize = 16; buttonSize = "medium"; break; case "large": iconSize = 16; buttonSize = "medium"; break; } return ( {buttonLabel ? (