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

167 lines
3.9 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-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'};
:hover{
2020-07-16 20:32:18 +00:00
border-color: ${props => (props.hasError && '#c30') || (props.hasWarning && '#f1ca92') || (props.isDisabled && '#ECEEF1')|| '#A3A9AE'};
2020-07-16 13:38:34 +00:00
}
2020-07-16 14:40:45 +00:00
2020-07-16 20:32:18 +00:00
:active{
border-color: ${props => (props.hasError && '#c30') || (props.hasWarning && '#f1ca92') || (props.isDisabled && '#ECEEF1')|| '#2DA7DB'};
2020-07-16 14:40:45 +00:00
}
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();
}
onChangeFile = e => this.setState({
path: e.target.value
});
onInputFile = () => this.setState({
fileName: this.inputRef.current.files[0].name
});
render() {
const { fileName } = this.state;
2020-07-16 14:40:45 +00:00
const {
size,
isDisabled,
scale,
hasError,
hasWarning,
...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-16 13:38:34 +00:00
value={fileName}
onChange={this.onChangeFile}
onFocus={this.onIconFileClick}
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-16 08:42:10 +00:00
/>
2020-07-16 20:32:18 +00:00
<input
type="file"
onInput={this.onInputFile}
ref={this.inputRef}
style={{ display: 'none' }}
/>
<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;