2019-07-10 12:14:53 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import styled from 'styled-components';
|
|
|
|
import TextInput from '../text-input';
|
|
|
|
import { Icons } from '../icons';
|
2019-07-11 09:39:36 +00:00
|
|
|
import IconButton from '../icon-button';
|
2019-07-10 12:14:53 +00:00
|
|
|
import commonInputStyle from '../text-input/common-input-styles';
|
|
|
|
|
|
|
|
const iconNames = Object.keys(Icons);
|
|
|
|
|
|
|
|
const StyledIconBlock = styled.div`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2019-12-25 08:22:19 +00:00
|
|
|
|
2019-12-13 09:25:21 +00:00
|
|
|
cursor: ${props =>
|
|
|
|
props.isDisabled || !props.isClickable ? "default" : "pointer"};
|
2019-07-10 12:14:53 +00:00
|
|
|
height: 100%;
|
|
|
|
padding-right: 7px;
|
2019-12-24 13:13:13 +00:00
|
|
|
-webkit-tap-highlight-color: rgba(0,0,0,0);
|
2019-07-10 12:14:53 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
const StyledChildrenBlock = styled.div`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2019-07-15 14:24:17 +00:00
|
|
|
padding: 2px 0px 2px 2px;
|
2019-07-10 12:14:53 +00:00
|
|
|
`;
|
|
|
|
|
2019-12-04 07:29:36 +00:00
|
|
|
// eslint-disable-next-line react/prop-types, no-unused-vars
|
2019-07-11 09:39:36 +00:00
|
|
|
const CustomInputGroup = ({ isIconFill, hasError, hasWarning, isDisabled, scale, ...props }) => (
|
2019-12-03 12:00:10 +00:00
|
|
|
<div {...props}></div>
|
2019-09-12 11:10:15 +00:00
|
|
|
);
|
2019-07-10 12:14:53 +00:00
|
|
|
const StyledInputGroup = styled(CustomInputGroup)`
|
2019-12-03 12:00:10 +00:00
|
|
|
display: flex;
|
|
|
|
|
|
|
|
.prepend {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.append {
|
|
|
|
align-items: center;
|
|
|
|
margin: 0;
|
2019-07-10 12:14:53 +00:00
|
|
|
}
|
2019-12-03 12:00:10 +00:00
|
|
|
${commonInputStyle} :focus-within {
|
|
|
|
border-color: #2da7db;
|
2019-07-10 12:14:53 +00:00
|
|
|
}
|
|
|
|
`;
|
2019-09-12 11:10:15 +00:00
|
|
|
class InputBlock extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
}
|
2019-12-25 09:34:07 +00:00
|
|
|
onIconClick = (e) => {
|
2019-12-13 13:23:42 +00:00
|
|
|
if (typeof this.props.onIconClick === "function" && !this.props.isDisabled) this.props.onIconClick(e);
|
2019-09-12 11:10:15 +00:00
|
|
|
}
|
2019-12-25 09:34:07 +00:00
|
|
|
onChange = (e) => {
|
2019-09-27 12:56:15 +00:00
|
|
|
if (typeof this.props.onChange === "function") this.props.onChange(e);
|
2019-07-31 19:36:26 +00:00
|
|
|
}
|
2019-07-11 09:39:36 +00:00
|
|
|
|
2019-09-12 11:10:15 +00:00
|
|
|
render() {
|
|
|
|
let iconButtonSize = 0;
|
2019-11-28 11:35:32 +00:00
|
|
|
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,
|
|
|
|
isIconFill,
|
|
|
|
onIconClick,
|
|
|
|
iconSize
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (typeof iconSize == "number" && iconSize > 0) {
|
|
|
|
iconButtonSize = iconSize;
|
2019-09-12 11:10:15 +00:00
|
|
|
} else {
|
2019-11-28 11:35:32 +00:00
|
|
|
switch (size) {
|
2019-09-12 11:10:15 +00:00
|
|
|
case 'base':
|
|
|
|
iconButtonSize = 15;
|
|
|
|
break;
|
|
|
|
case 'middle':
|
|
|
|
iconButtonSize = 18;
|
|
|
|
break;
|
|
|
|
case 'big':
|
|
|
|
iconButtonSize = 21;
|
|
|
|
break;
|
|
|
|
case 'huge':
|
|
|
|
iconButtonSize = 24;
|
|
|
|
break;
|
2019-07-31 19:36:26 +00:00
|
|
|
}
|
2019-09-12 11:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2019-11-28 11:35:32 +00:00
|
|
|
<StyledInputGroup
|
|
|
|
hasError={hasError}
|
|
|
|
hasWarning={hasWarning}
|
|
|
|
isDisabled={isDisabled}
|
|
|
|
scale={scale}
|
|
|
|
size={size}
|
|
|
|
className={className}
|
|
|
|
style={style}
|
|
|
|
>
|
2019-12-03 12:00:10 +00:00
|
|
|
<div className="prepend">
|
2019-09-12 11:10:15 +00:00
|
|
|
<StyledChildrenBlock>
|
2019-11-28 11:35:32 +00:00
|
|
|
{children}
|
2019-09-12 11:10:15 +00:00
|
|
|
</StyledChildrenBlock>
|
2019-12-03 12:00:10 +00:00
|
|
|
</div>
|
2019-09-12 11:10:15 +00:00
|
|
|
<TextInput
|
2019-11-28 11:35:32 +00:00
|
|
|
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}
|
2019-11-28 11:35:32 +00:00
|
|
|
mask={mask}
|
|
|
|
keepCharPositions={keepCharPositions}
|
2019-09-12 11:10:15 +00:00
|
|
|
/>
|
|
|
|
{
|
2019-12-04 07:29:36 +00:00
|
|
|
iconNames.includes(iconName) && (
|
|
|
|
<div className="append">
|
2019-12-13 09:25:21 +00:00
|
|
|
<StyledIconBlock
|
|
|
|
isDisabled={isDisabled}
|
|
|
|
onClick={this.onIconClick}
|
|
|
|
isClickable={typeof onIconClick === 'function'}>
|
2019-12-04 07:29:36 +00:00
|
|
|
<IconButton
|
|
|
|
size={iconButtonSize}
|
|
|
|
color={iconColor}
|
|
|
|
iconName={iconName}
|
|
|
|
isFill={isIconFill}
|
|
|
|
isDisabled={isDisabled}
|
|
|
|
isClickable={typeof onIconClick === 'function'}
|
|
|
|
/>
|
|
|
|
</StyledIconBlock>
|
|
|
|
</div>
|
|
|
|
)}
|
2019-09-12 11:10:15 +00:00
|
|
|
</StyledInputGroup>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-07-10 12:14:53 +00:00
|
|
|
|
|
|
|
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]),
|
2019-09-06 08:54:19 +00:00
|
|
|
keepCharPositions: PropTypes.bool,
|
2019-07-10 12:14:53 +00:00
|
|
|
|
|
|
|
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,
|
2019-07-10 12:14:53 +00:00
|
|
|
iconName: PropTypes.string,
|
|
|
|
iconColor: PropTypes.string,
|
2019-07-17 13:50:29 +00:00
|
|
|
iconSize: PropTypes.number,
|
2019-07-10 12:14:53 +00:00
|
|
|
isIconFill: PropTypes.bool,
|
|
|
|
onIconClick: PropTypes.func,
|
|
|
|
|
|
|
|
children: PropTypes.oneOfType([
|
|
|
|
PropTypes.arrayOf(PropTypes.node),
|
|
|
|
PropTypes.node
|
2019-11-28 11:35:32 +00:00
|
|
|
]),
|
|
|
|
|
|
|
|
className: PropTypes.string,
|
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array])
|
2019-07-10 12:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InputBlock.defaultProps = {
|
|
|
|
|
|
|
|
type: 'text',
|
|
|
|
maxLength: 255,
|
|
|
|
size: 'base',
|
|
|
|
scale: false,
|
|
|
|
tabIndex: -1,
|
|
|
|
hasError: false,
|
|
|
|
hasWarning: false,
|
|
|
|
autoComplete: 'off',
|
|
|
|
|
|
|
|
value: '',
|
|
|
|
iconName: "",
|
|
|
|
iconColor: "#ffffff",
|
|
|
|
isIconFill: false,
|
2019-09-06 08:54:19 +00:00
|
|
|
isDisabled: false,
|
|
|
|
keepCharPositions: false
|
2019-07-10 12:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default InputBlock
|