DocSpace-client/packages/asc-web-components/file-input/index.js

164 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-07-16 08:42:10 +00:00
import React, { Component } from "react";
import PropTypes from "prop-types";
import equal from "fast-deep-equal/react";
2020-07-16 13:38:34 +00:00
2020-09-06 10:04:33 +00:00
import IconButton from "../icon-button";
import TextInput from "../text-input";
import StyledFileInput from "./styled-file-input"
2020-07-16 08:42:10 +00:00
2020-09-06 10:04:33 +00:00
class FileInput extends Component {
2020-07-16 08:42:10 +00:00
constructor(props) {
super(props);
2020-07-16 13:38:34 +00:00
this.inputRef = React.createRef();
2020-07-16 08:42:10 +00:00
this.state = {
2020-09-06 10:04:33 +00:00
fileName: "",
file: null,
2020-09-06 10:04:33 +00:00
};
2020-07-16 08:42:10 +00:00
}
2020-07-20 07:21:20 +00:00
shouldComponentUpdate(nextProps, nextState) {
return !equal(this.props, nextProps) || !equal(this.state, nextState);
2020-07-17 12:28:58 +00:00
}
onIconFileClick = (e) => {
2020-07-17 12:58:28 +00:00
const { isDisabled } = this.props;
2020-09-06 10:04:33 +00:00
if (isDisabled) {
2020-07-17 12:58:28 +00:00
return false;
}
2020-07-16 08:42:10 +00:00
e.target.blur();
this.inputRef.current.click();
2020-09-06 10:04:33 +00:00
};
2020-07-16 08:42:10 +00:00
onChangeHandler = (e) => {
2020-07-17 08:31:23 +00:00
this.setState({
fileName: e.target.value,
2020-09-06 10:04:33 +00:00
});
};
2020-07-16 08:42:10 +00:00
2020-07-17 08:31:23 +00:00
onInputFile = () => {
const { onInput } = this.props;
2020-07-17 12:58:28 +00:00
2020-09-06 10:04:33 +00:00
if (this.inputRef.current.files.length > 0) {
this.setState(
{
fileName: this.inputRef.current.files[0].name,
file: this.inputRef.current.files[0],
2020-09-06 10:04:33 +00:00
},
() => {
if (onInput) {
this.inputRef.current.value = "";
onInput(this.state.file);
}
}
2020-09-06 10:04:33 +00:00
);
}
};
2020-07-16 08:42:10 +00:00
render() {
2020-07-17 12:28:58 +00:00
//console.log('render FileInput');
2020-07-16 08:42:10 +00:00
const { fileName } = this.state;
2020-09-06 10:04:33 +00:00
const {
size,
2020-07-17 07:15:58 +00:00
placeholder,
2020-09-06 10:04:33 +00:00
isDisabled,
scale,
2020-07-16 14:40:45 +00:00
hasError,
hasWarning,
2020-07-17 08:34:52 +00:00
accept,
2020-09-06 10:04:33 +00:00
id,
onInput, // eslint-disable-line no-unused-vars
2020-09-06 10:04:33 +00:00
...rest
2020-07-16 14:40:45 +00:00
} = this.props;
2020-07-16 13:38:34 +00:00
let iconSize = 0;
switch (size) {
2020-09-06 10:04:33 +00:00
case "base":
2020-07-16 13:38:34 +00:00
iconSize = 15;
break;
2020-09-06 10:04:33 +00:00
case "middle":
2020-07-16 13:38:34 +00:00
iconSize = 15;
break;
2020-09-06 10:04:33 +00:00
case "big":
2020-07-16 13:38:34 +00:00
iconSize = 16;
break;
2020-09-06 10:04:33 +00:00
case "huge":
2020-07-16 13:38:34 +00:00
iconSize = 16;
break;
2020-09-06 10:04:33 +00:00
case "large":
2020-07-16 13:38:34 +00:00
iconSize = 16;
break;
}
2020-09-06 10:04:33 +00:00
return (
<StyledFileInput
size={size}
2020-07-16 20:32:18 +00:00
scale={scale ? 1 : 0}
2020-07-16 14:40:45 +00:00
hasError={hasError}
hasWarning={hasWarning}
isDisabled={isDisabled}
2020-07-17 11:33:59 +00:00
{...rest}
2020-07-16 14:40:45 +00:00
>
2020-07-16 13:38:34 +00:00
<TextInput
2020-07-16 20:32:18 +00:00
className="text-input"
2020-07-17 07:15:58 +00:00
placeholder={placeholder}
2020-07-16 13:38:34 +00:00
value={fileName}
size={size}
2020-07-16 14:40:45 +00:00
isDisabled={isDisabled}
hasError={hasError}
hasWarning={hasWarning}
2020-07-16 20:32:18 +00:00
scale={scale}
2020-07-17 07:15:58 +00:00
onFocus={this.onIconFileClick}
2020-07-17 08:31:23 +00:00
onChange={this.onChangeHandler}
2020-07-16 08:42:10 +00:00
/>
2020-07-16 20:32:18 +00:00
<input
type="file"
2020-09-06 10:04:33 +00:00
id={id}
2020-07-16 20:32:18 +00:00
ref={this.inputRef}
2020-09-06 10:04:33 +00:00
style={{ display: "none" }}
2020-07-17 08:34:52 +00:00
accept={accept}
2020-07-17 07:15:58 +00:00
onInput={this.onInputFile}
2020-07-16 20:32:18 +00:00
/>
2020-09-06 10:04:33 +00:00
<div className="icon" onClick={this.onIconFileClick}>
<IconButton
className="icon-button"
2020-07-16 13:38:34 +00:00
iconName={"CatalogFolderIcon"}
2020-07-20 10:55:14 +00:00
color={"#A3A9AE"}
2020-07-16 13:38:34 +00:00
size={iconSize}
2020-07-16 14:40:45 +00:00
isDisabled={isDisabled}
2020-07-16 13:38:34 +00:00
/>
</div>
</StyledFileInput>
2020-09-06 10:04:33 +00:00
);
2020-07-16 08:42:10 +00:00
}
}
FileInput.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
placeholder: PropTypes.string,
2020-09-06 10:04:33 +00:00
size: PropTypes.oneOf(["base", "middle", "big", "huge", "large"]),
2020-07-17 12:16:14 +00:00
scale: PropTypes.bool,
className: PropTypes.string,
hasError: PropTypes.bool,
hasWarning: PropTypes.bool,
id: PropTypes.string,
isDisabled: PropTypes.bool,
name: PropTypes.string,
onInput: PropTypes.func,
accept: PropTypes.string,
2020-07-17 12:16:14 +00:00
};
FileInput.defaultProps = {
2020-09-06 10:04:33 +00:00
size: "base",
2020-07-17 12:16:14 +00:00
scale: false,
hasWarning: false,
hasError: false,
isDisabled: false,
accept: "",
2020-09-06 10:04:33 +00:00
};
2020-07-16 08:42:10 +00:00
2020-09-06 10:04:33 +00:00
export default FileInput;