import React, { Component } from "react"; import PropTypes from "prop-types"; import equal from "fast-deep-equal/react"; import IconButton from "../icon-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 ...rest } = this.props; let iconSize = 0; switch (size) { case "base": iconSize = 15; break; case "middle": iconSize = 15; break; case "big": iconSize = 16; break; case "huge": iconSize = 16; break; case "large": iconSize = 16; break; } return (
); } } FileInput.propTypes = { /** Accepts css style */ style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]), /** Placeholder text for the input */ placeholder: PropTypes.string, /** Supported size of the input fields */ size: PropTypes.oneOf(["base", "middle", "big", "huge", "large"]), /** Indicates the input field has scale */ scale: PropTypes.bool, /** Accepts class */ className: PropTypes.string, /** Indicates the input field has an error */ hasError: PropTypes.bool, /** Indicates the input field has a warning */ hasWarning: PropTypes.bool, /** Used as HTML `id` property */ id: PropTypes.string, /** Indicates that the field cannot be used (e.g not authorised, or changes not saved) */ isDisabled: PropTypes.bool, /** Used as HTML `name` property */ name: PropTypes.string, /** Called when a file is selected */ onInput: PropTypes.func, /**Specifies files visible for upload */ accept: PropTypes.string, }; FileInput.defaultProps = { size: "base", scale: false, hasWarning: false, hasError: false, isDisabled: false, accept: "", }; export default FileInput;