DocSpace-buildtools/packages/components/file-input/index.js

196 lines
4.6 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 Button from "../button";
2020-09-06 10:04:33 +00:00
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
buttonLabel,
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;
let buttonSize = "";
2020-07-16 13:38:34 +00:00
switch (size) {
2020-09-06 10:04:33 +00:00
case "base":
2020-07-16 13:38:34 +00:00
iconSize = 15;
buttonSize = "extrasmall";
2020-07-16 13:38:34 +00:00
break;
2020-09-06 10:04:33 +00:00
case "middle":
2020-07-16 13:38:34 +00:00
iconSize = 15;
buttonSize = "small";
2020-07-16 13:38:34 +00:00
break;
2020-09-06 10:04:33 +00:00
case "big":
2020-07-16 13:38:34 +00:00
iconSize = 16;
buttonSize = "normal";
2020-07-16 13:38:34 +00:00
break;
2020-09-06 10:04:33 +00:00
case "huge":
2020-07-16 13:38:34 +00:00
iconSize = 16;
buttonSize = "medium";
2020-07-16 13:38:34 +00:00
break;
2020-09-06 10:04:33 +00:00
case "large":
2020-07-16 13:38:34 +00:00
iconSize = 16;
buttonSize = "medium";
2020-07-16 13:38:34 +00:00
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
/>
{buttonLabel ? (
<Button
2020-07-16 14:40:45 +00:00
isDisabled={isDisabled}
label={buttonLabel}
onClick={this.onIconFileClick}
size={buttonSize}
2020-07-16 13:38:34 +00:00
/>
) : (
<div className="icon" onClick={this.onIconFileClick}>
<IconButton
className="icon-button"
iconName={"/static/images/catalog.folder.react.svg"}
color={"#A3A9AE"}
size={iconSize}
isDisabled={isDisabled}
/>
</div>
)}
2020-07-16 13:38:34 +00:00
</StyledFileInput>
2020-09-06 10:04:33 +00:00
);
2020-07-16 08:42:10 +00:00
}
}
FileInput.propTypes = {
/** Accepts css style */
2020-07-16 08:42:10 +00:00
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
/** Placeholder text for the input */
2020-07-16 08:42:10 +00:00
placeholder: PropTypes.string,
/** Supported size of the input fields */
2020-09-06 10:04:33 +00:00
size: PropTypes.oneOf(["base", "middle", "big", "huge", "large"]),
/** Indicates the input field has scale */
2020-07-17 12:16:14 +00:00
scale: PropTypes.bool,
/** Accepts class */
2020-07-17 12:16:14 +00:00
className: PropTypes.string,
/** Indicates the input field has an error */
2020-07-17 12:16:14 +00:00
hasError: PropTypes.bool,
/** Indicates the input field has a warning */
2020-07-17 12:16:14 +00:00
hasWarning: PropTypes.bool,
/** Used as HTML `id` property */
2020-07-17 12:16:14 +00:00
id: PropTypes.string,
/** Indicates that the field cannot be used (e.g not authorised, or changes not saved) */
2020-07-17 12:16:14 +00:00
isDisabled: PropTypes.bool,
/** Used as HTML `name` property */
2020-07-17 12:16:14 +00:00
name: PropTypes.string,
/** Called when a file is selected */
2020-07-17 12:16:14 +00:00
onInput: PropTypes.func,
/**Specifies files visible for upload */
accept: PropTypes.string,
/**Specifies label for upload button */
buttonLabel: 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;