DocSpace-client/web/ASC.Web.Components/src/components/file-input/index.js

191 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-07-16 08:42:10 +00:00
import React, { Component } from "react";
import PropTypes from "prop-types";
2020-07-16 13:38:34 +00:00
import styled, { css } from 'styled-components';
import IconButton from '../icon-button';
import TextInput from '../text-input';
const StyledFileInput = styled.div`
display: flex;
2020-07-16 14:40:45 +00:00
position: relative;
width: ${props =>
(props.scale && '100%') ||
(props.size === 'base' && '173px') ||
(props.size === 'middle' && '300px') ||
(props.size === 'big' && '350px') ||
(props.size === 'huge' && '500px') ||
(props.size === 'large' && '550px')
};
2020-07-16 13:38:34 +00:00
2020-07-16 20:32:18 +00:00
.text-input {
border-color: ${props => (props.hasError && '#c30') || (props.hasWarning && '#f1ca92') || (props.isDisabled && '#ECEEF1')|| '#D0D5DA'};
}
2020-07-17 07:15:58 +00:00
:hover{
.icon {
border-color: ${props => (props.hasError && '#c30') || (props.hasWarning && '#f1ca92') || (props.isDisabled && '#ECEEF1')|| '#A3A9AE'};
}
}
:active {
.icon {
border-color: ${props => (props.hasError && '#c30') || (props.hasWarning && '#f1ca92') || (props.isDisabled && '#ECEEF1')|| '#2DA7DB'};
}
}
2020-07-16 13:38:34 +00:00
.icon {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
2020-07-16 14:40:45 +00:00
right: 0;
2020-07-16 13:38:34 +00:00
width: ${props => props.size === 'large' ? '48px'
: props.size === 'huge' ? '38px'
: props.size === 'big' ? '37px'
: props.size === 'middle' ? '36px'
: '30px'
};
height: ${props => props.size === 'large' ? '43px'
: props.size === 'huge' ? '37px'
: props.size === 'big' ? '36px'
: props.size === 'middle' ? '36px'
: '30px'
};
margin: 0;
2020-07-16 14:40:45 +00:00
border: 1px solid;
2020-07-16 13:38:34 +00:00
border-radius: 0 3px 3px 0;
2020-07-16 14:40:45 +00:00
border-color: ${props => (props.hasError && '#c30') || (props.hasWarning && '#f1ca92') || (props.isDisabled && '#ECEEF1')|| '#D0D5DA'};
cursor: ${props => (props.isDisabled ? 'default' : 'pointer')}
2020-07-16 13:38:34 +00:00
}
`;
2020-07-16 08:42:10 +00:00
class FileInput extends Component {
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 = {
fileName: '',
path: ''
}
}
onIconFileClick = e => {
e.target.blur();
this.inputRef.current.click();
}
2020-07-17 08:31:23 +00:00
onChangeHandler = e => {
this.setState({
pathName: e.target.value
})
}
2020-07-16 08:42:10 +00:00
2020-07-17 08:31:23 +00:00
onInputFile = () => {
const { onInput } = this.props;
2020-07-17 08:49:54 +00:00
if ( this.inputRef.current.files.length > 0 ) {
2020-07-17 08:31:23 +00:00
this.setState({
fileName: this.inputRef.current.files[0].name,
data: this.inputRef.current.files[0]
2020-07-17 08:49:54 +00:00
});
onInput(this.inputRef.current.files[0]);
2020-07-17 08:31:23 +00:00
} else {
this.setState({
fileName: 'file not choose'
})
}
}
2020-07-16 08:42:10 +00:00
render() {
2020-07-17 08:31:23 +00:00
console.log('render input file')
2020-07-16 08:42:10 +00:00
const { fileName } = this.state;
2020-07-16 14:40:45 +00:00
const {
size,
2020-07-17 07:15:58 +00:00
placeholder,
2020-07-16 14:40:45 +00:00
isDisabled,
scale,
hasError,
hasWarning,
2020-07-17 08:34:52 +00:00
accept,
2020-07-16 14:40:45 +00:00
...rest
} = this.props;
2020-07-16 13:38:34 +00:00
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(
2020-07-16 14:40:45 +00:00
<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-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"
ref={this.inputRef}
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-07-17 07:15:58 +00:00
<div
className="icon"
onClick={this.onIconFileClick}
>
2020-07-16 13:38:34 +00:00
<IconButton
iconName={"CatalogFolderIcon"}
size={iconSize}
2020-07-16 14:40:45 +00:00
isDisabled={isDisabled}
2020-07-16 13:38:34 +00:00
/>
</div>
</StyledFileInput>
2020-07-16 08:42:10 +00:00
)
}
}
FileInput.propTypes = {
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
placeholder: PropTypes.string,
2020-07-16 20:32:18 +00:00
size: PropTypes.string,
scale: PropTypes.bool
2020-07-16 08:42:10 +00:00
}
export default FileInput;