From 3a74dfc927456dc78b781d373983af34b06f5e47 Mon Sep 17 00:00:00 2001 From: Vasek Mlejnsky Date: Fri, 31 Aug 2018 12:37:07 +0200 Subject: [PATCH] Refactorize Input component --- src/components/Input/index.js | 220 +++++++++++++++++----------------- 1 file changed, 111 insertions(+), 109 deletions(-) diff --git a/src/components/Input/index.js b/src/components/Input/index.js index 5d027da3..e0fad58c 100644 --- a/src/components/Input/index.js +++ b/src/components/Input/index.js @@ -1,4 +1,4 @@ -import React from 'react'; +import React, { Component } from 'react'; import PropTypes from 'prop-types'; import styled, { css } from 'styled-components'; import colors from 'config/colors'; @@ -6,6 +6,28 @@ import ICONS from 'config/icons'; import Icon from 'components/Icon'; import { FONT_SIZE, FONT_WEIGHT, TRANSITION } from 'config/variables'; +const Wrapper = styled.div` + width: 100%; + display: flex; + flex-direction: column; + justify-content: flex-start; +`; + +const InputWrapper = styled.div` + display: flex; +`; + +const InputIconWrapper = styled.div` + flex: 1; + position: relative; + display: inline-block; +`; + +const InputLabel = styled.span` + padding-bottom: 4px; + color: ${colors.TEXT_SECONDARY}; +`; + const StyledInput = styled.input` width: 100%; padding: 6px 12px; @@ -16,34 +38,14 @@ const StyledInput = styled.input` color: ${colors.TEXT_PRIMARY}; border-radius: 2px; + ${props => props.hasAddon && css` + border-top-right-radius: 0; + border-bottom-right-radius: 0; + `} border: 1px solid ${colors.DIVIDER}; + border-color: ${props => props.borderColor}; background-color: ${colors.WHITE}; - &:focus { - box-shadow: 0 1px 2px 0 rgba(169, 169, 169, 0.25); - } - - ${props => props.isSuccess && css` - border-color: ${colors.SUCCESS_PRIMARY}; - &:focus { - box-shadow: 0 1px 4px 0 rgba(1, 183, 87, 0.25); - } - `} - - ${props => props.isWarning && css` - border-color: ${colors.WARNING_PRIMARY}; - &:focus { - box-shadow: 0 1px 4px 0 rgba(235, 138, 0, 0.25); - } - `} - - ${props => props.isError && css` - border-color: ${colors.ERROR_PRIMARY}; - &:focus { - box-shadow: 0 1px 4px 0 rgba(255, 111, 109, 0.25); - } - `} - transition: ${TRANSITION.HOVER}; &:disabled { background: ${colors.GRAY_LIGHT}; @@ -51,91 +53,88 @@ const StyledInput = styled.input` } `; -const Wrapper = styled.div` - display: flex; - flex-direction: column; - justify-content: flex-start; -`; - -const InputWrapper = styled.div``; - -const InputLabel = styled.span` - padding-bottom: 4px; - color: ${colors.TEXT_SECONDARY}; -`; - -const ErrorLabel = styled.span` - margin-top: 10px; - font-size: ${FONT_SIZE.SMALLER}; - color: ${colors.ERROR_PRIMARY}; -`; - const StyledIcon = styled(Icon)` position: absolute; - right: 55px; + left: auto; + right: 10px; `; -const Input = ({ - type, - autoComplete, - placeholder - autoCorrect, - autoCapitalize, - spellCheck, - value, - onChange, - isSuccess, - isWarning, - isError, - errorText, - inputLabel, -}) => ( - - {inputLabel && ( - {inputLabel} - )} - - - {isError && ( - - )} - {isWarning && ( - - )} - {isSuccess && ( - - )} - - {isError && ( - - {errorText} - - )} - -); +const BottomText = styled.span` + margin-top: 10px; + font-size: ${FONT_SIZE.SMALLER}; + color: ${props => (props.color ? props.color : colors.TEXT_SECONDARY)}; +`; + +class Input extends Component { + getIcon(inputState) { + let icon = []; + if (inputState === 'success') { + icon = ICONS.CHECKED; + } else if (inputState === 'warning') { + icon = ICONS.WARNING; + } else if (inputState === 'error') { + icon = ICONS.ERROR; + } + return icon; + } + + getColor(inputState) { + let color = ''; + if (inputState === 'success') { + color = colors.SUCCESS_PRIMARY; + } else if (inputState === 'warning') { + color = colors.WARNING_PRIMARY; + } else if (inputState === 'error') { + color = colors.ERROR_PRIMARY; + } + return color; + } + + render() { + return ( + + {this.props.inputLabel && ( + {this.props.inputLabel} + )} + + + {this.props.state && ( + + )} + + + {this.props.sideAddon} + + {this.props.bottomText && ( + + {this.props.bottomText} + + )} + + ); + } +} Input.propTypes = { + className: PropTypes.string, placeholder: PropTypes.string, type: PropTypes.string, autoComplete: PropTypes.string, @@ -144,11 +143,14 @@ Input.propTypes = { spellCheck: PropTypes.string, value: PropTypes.string.isRequired, onChange: PropTypes.func, - isSuccess: PropTypes.bool, - isWarning: PropTypes.bool, - isError: PropTypes.bool, - errorText: PropTypes.string, + state: PropTypes.string, + bottomText: PropTypes.string, inputLabel: PropTypes.string, + sideAddon: PropTypes.node, +}; + +Input.defaultProps = { + type: 'text', }; export default Input;