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

238 lines
5.4 KiB
JavaScript
Raw Normal View History

import React from "react";
import PropTypes from "prop-types";
import styled from "styled-components";
import TextInput from "../text-input";
import { Icons } from "../icons";
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;
cursor: ${props =>
props.isDisabled || !props.isClickable ? "default" : "pointer"};
height: 100%;
2019-12-26 08:47:02 +00:00
padding-right: 8px;
padding-left: 1px;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
`;
const StyledChildrenBlock = styled.div`
display: flex;
align-items: center;
2019-07-15 14:24:17 +00:00
padding: 2px 0px 2px 2px;
`;
// eslint-disable-next-line react/prop-types, no-unused-vars
const CustomInputGroup = ({
isIconFill,
hasError,
hasWarning,
isDisabled,
scale,
...props
}) => <div {...props}></div>;
const StyledInputGroup = styled(CustomInputGroup)`
display: flex;
.prepend {
display: flex;
align-items: center;
}
.append {
align-items: center;
margin: 0;
}
${commonInputStyle} :focus-within {
border-color: #2da7db;
}
`;
2019-09-12 11:10:15 +00:00
class InputBlock extends React.Component {
constructor(props) {
super(props);
}
onIconClick = e => {
if (typeof this.props.onIconClick === "function" && !this.props.isDisabled)
this.props.onIconClick(e);
};
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;
const {
hasError,
hasWarning,
isDisabled,
scale,
size,
className,
style,
children,
id,
name,
type,
value,
placeholder,
tabIndex,
maxLength,
onBlur,
onFocus,
isReadOnly,
isAutoFocussed,
autoComplete,
mask,
keepCharPositions,
iconName,
iconColor,
2019-12-26 08:47:02 +00:00
hoverColor,
isIconFill,
onIconClick,
iconSize
} = this.props;
if (typeof iconSize == "number" && iconSize > 0) {
iconButtonSize = iconSize;
2019-09-12 11:10:15 +00:00
} else {
switch (size) {
case "base":
2019-09-12 11:10:15 +00:00
iconButtonSize = 15;
break;
case "middle":
2019-09-12 11:10:15 +00:00
iconButtonSize = 18;
break;
case "big":
2019-09-12 11:10:15 +00:00
iconButtonSize = 21;
break;
case "huge":
2019-09-12 11:10:15 +00:00
iconButtonSize = 24;
break;
}
2019-09-12 11:10:15 +00:00
}
return (
<StyledInputGroup
hasError={hasError}
hasWarning={hasWarning}
isDisabled={isDisabled}
scale={scale}
size={size}
className={className}
style={style}
>
<div className="prepend">
<StyledChildrenBlock>{children}</StyledChildrenBlock>
</div>
2019-09-12 11:10:15 +00:00
<TextInput
id={id}
name={name}
type={type}
value={value}
isDisabled={isDisabled}
hasError={hasError}
hasWarning={hasWarning}
placeholder={placeholder}
tabIndex={tabIndex}
maxLength={maxLength}
onBlur={onBlur}
onFocus={onFocus}
isReadOnly={isReadOnly}
isAutoFocussed={isAutoFocussed}
autoComplete={autoComplete}
size={size}
scale={scale}
2019-09-12 11:10:15 +00:00
onChange={this.onChange}
withBorder={false}
mask={mask}
keepCharPositions={keepCharPositions}
2019-09-12 11:10:15 +00:00
/>
{iconNames.includes(iconName) && (
<div className="append">
<StyledIconBlock
isDisabled={isDisabled}
onClick={this.onIconClick}
isClickable={typeof onIconClick === "function"}
>
<IconButton
size={iconButtonSize}
color={iconColor}
hoverColor={hoverColor}
iconName={iconName}
isFill={isIconFill}
isDisabled={isDisabled}
isClickable={typeof onIconClick === "function"}
/>
</StyledIconBlock>
</div>
)}
2019-09-12 11:10:15 +00:00
</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", "large"]),
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,
2019-12-26 08:47:02 +00:00
hoverColor: PropTypes.string,
iconSize: PropTypes.number,
isIconFill: PropTypes.bool,
onIconClick: PropTypes.func,
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node),
PropTypes.node
]),
className: PropTypes.string,
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array])
};
InputBlock.defaultProps = {
type: "text",
maxLength: 255,
size: "base",
scale: false,
tabIndex: -1,
hasError: false,
hasWarning: false,
autoComplete: "off",
value: "",
iconName: "",
iconColor: "#ffffff",
2019-12-26 08:47:02 +00:00
hoverColor: "#ffffff",
isIconFill: false,
isDisabled: false,
keepCharPositions: false
};
export default InputBlock;