DocSpace-buildtools/web/ASC.Web.Components/src/components/input-block/index.js

191 lines
5.1 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { InputGroup, InputGroupAddon } from 'reactstrap';
import TextInput from '../text-input';
import { Icons } from '../icons';
2019-07-11 09:39:36 +00:00
import IconButton from '../icon-button';
import commonInputStyle from '../text-input/common-input-styles';
const iconNames = Object.keys(Icons);
const StyledIconBlock = styled.div`
display: flex;
align-items: center;
2019-07-11 09:39:36 +00:00
width: ${props =>
(props.size === 'base' && '22px') ||
(props.size === 'middle' && '27px') ||
(props.size === 'big' && '30px') ||
(props.size === 'huge' && '30px')
};
height: 100%;
padding-right: 7px;
`;
const StyledChildrenBlock = styled.div`
display: flex;
align-items: center;
2019-07-15 14:24:17 +00:00
padding: 2px 0px 2px 2px;
`;
2019-07-11 09:39:36 +00:00
const CustomInputGroup = ({ isIconFill, hasError, hasWarning, isDisabled, scale, ...props }) => (
<InputGroup {...props}></InputGroup>
2019-09-12 11:10:15 +00:00
);
const StyledInputGroup = styled(CustomInputGroup)`
${commonInputStyle}
:focus-within{
border-color: #2DA7DB;
}
.input-group-prepend,
.input-group-append{
margin: 0;
}
`;
2019-09-12 11:10:15 +00:00
class InputBlock extends React.Component {
constructor(props) {
super(props);
2019-09-12 11:10:15 +00:00
this.onIconClick = this.onIconClick.bind(this);
this.onChange = this.onChange.bind(this);
2019-09-12 11:10:15 +00:00
}
onIconClick(e) {
2019-09-27 12:56:15 +00:00
if (typeof this.props.onIconClick === "function") this.props.onIconClick(e);
2019-09-12 11:10:15 +00:00
}
onChange(e) {
2019-09-27 12:56:15 +00:00
if (typeof this.props.onChange === "function") this.props.onChange(e);
}
2019-07-11 09:39:36 +00:00
2019-09-12 11:10:15 +00:00
render() {
let iconButtonSize = 0;
if (typeof this.props.iconSize == "number" && this.props.iconSize > 0) {
iconButtonSize = this.props.iconSize;
} else {
switch (this.props.size) {
case 'base':
iconButtonSize = 15;
break;
case 'middle':
iconButtonSize = 18;
break;
case 'big':
iconButtonSize = 21;
break;
case 'huge':
iconButtonSize = 24;
break;
default:
break;
}
2019-09-12 11:10:15 +00:00
}
return (
<StyledInputGroup hasError={this.props.hasError} hasWarning={this.props.hasWarning} isDisabled={this.props.isDisabled} scale={this.props.scale} size={this.props.size}>
<InputGroupAddon addonType="prepend">
<StyledChildrenBlock>
{this.props.children}
</StyledChildrenBlock>
</InputGroupAddon>
<TextInput
id={this.props.id}
name={this.props.name}
type={this.props.type}
value={this.props.value}
isDisabled={this.props.isDisabled}
hasError={this.props.hasError}
hasWarning={this.props.hasWarning}
placeholder={this.props.placeholder}
tabIndex={this.props.tabIndex}
maxLength={this.props.maxLength}
onBlur={this.props.onBlur}
onFocus={this.props.onFocus}
isReadOnly={this.props.isReadOnly}
isAutoFocussed={this.props.isAutoFocussed}
autoComplete={this.props.autoComplete}
size={this.props.size}
scale={this.props.scale}
onChange={this.onChange}
withBorder={false}
mask={this.props.mask}
keepCharPositions={this.props.keepCharPositions}
/>
{
iconNames.includes(this.props.iconName)
&&
2019-09-27 12:56:15 +00:00
<InputGroupAddon addonType="append" onClick={this.onIconClick}>
2019-09-12 11:10:15 +00:00
<StyledIconBlock>
<IconButton
size={iconButtonSize}
color={this.props.iconColor}
iconName={this.props.iconName}
isFill={this.props.isIconFill}
isDisabled={this.props.isDisabled}
2019-09-27 13:10:43 +00:00
isClickable={typeof this.props.onIconClick === 'function'}
2019-09-27 12:56:15 +00:00
/>
2019-09-12 11:10:15 +00:00
</StyledIconBlock>
</InputGroupAddon>
}
</StyledInputGroup>
);
}
}
InputBlock.propTypes = {
id: PropTypes.string,
name: PropTypes.string,
type: PropTypes.oneOf(['text', 'password']),
maxLength: PropTypes.number,
placeholder: PropTypes.string,
tabIndex: PropTypes.number,
2019-09-12 11:10:15 +00:00
mask: PropTypes.oneOfType([PropTypes.array, PropTypes.func]),
keepCharPositions: PropTypes.bool,
size: PropTypes.oneOf(['base', 'middle', 'big', 'huge']),
scale: PropTypes.bool,
onChange: PropTypes.func,
onBlur: PropTypes.func,
onFocus: PropTypes.func,
isAutoFocussed: PropTypes.bool,
isDisabled: PropTypes.bool,
isReadOnly: PropTypes.bool,
hasError: PropTypes.bool,
hasWarning: PropTypes.bool,
autoComplete: PropTypes.string,
2019-07-11 13:39:52 +00:00
value: PropTypes.string,
iconName: PropTypes.string,
iconColor: PropTypes.string,
iconSize: PropTypes.number,
isIconFill: PropTypes.bool,
onIconClick: PropTypes.func,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
])
}
InputBlock.defaultProps = {
type: 'text',
maxLength: 255,
size: 'base',
scale: false,
tabIndex: -1,
hasError: false,
hasWarning: false,
autoComplete: 'off',
value: '',
iconName: "",
iconColor: "#ffffff",
isIconFill: false,
isDisabled: false,
keepCharPositions: false
}
export default InputBlock