mirror of
https://github.com/trezor/trezor-wallet
synced 2025-07-04 05:42:34 +00:00
Refactorize Input component
This commit is contained in:
parent
7014331840
commit
3a74dfc927
@ -1,4 +1,4 @@
|
|||||||
import React from 'react';
|
import React, { Component } from 'react';
|
||||||
import PropTypes from 'prop-types';
|
import PropTypes from 'prop-types';
|
||||||
import styled, { css } from 'styled-components';
|
import styled, { css } from 'styled-components';
|
||||||
import colors from 'config/colors';
|
import colors from 'config/colors';
|
||||||
@ -6,6 +6,28 @@ import ICONS from 'config/icons';
|
|||||||
import Icon from 'components/Icon';
|
import Icon from 'components/Icon';
|
||||||
import { FONT_SIZE, FONT_WEIGHT, TRANSITION } from 'config/variables';
|
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`
|
const StyledInput = styled.input`
|
||||||
width: 100%;
|
width: 100%;
|
||||||
padding: 6px 12px;
|
padding: 6px 12px;
|
||||||
@ -16,34 +38,14 @@ const StyledInput = styled.input`
|
|||||||
color: ${colors.TEXT_PRIMARY};
|
color: ${colors.TEXT_PRIMARY};
|
||||||
|
|
||||||
border-radius: 2px;
|
border-radius: 2px;
|
||||||
|
${props => props.hasAddon && css`
|
||||||
|
border-top-right-radius: 0;
|
||||||
|
border-bottom-right-radius: 0;
|
||||||
|
`}
|
||||||
border: 1px solid ${colors.DIVIDER};
|
border: 1px solid ${colors.DIVIDER};
|
||||||
|
border-color: ${props => props.borderColor};
|
||||||
|
|
||||||
background-color: ${colors.WHITE};
|
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};
|
transition: ${TRANSITION.HOVER};
|
||||||
&:disabled {
|
&:disabled {
|
||||||
background: ${colors.GRAY_LIGHT};
|
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)`
|
const StyledIcon = styled(Icon)`
|
||||||
position: absolute;
|
position: absolute;
|
||||||
right: 55px;
|
left: auto;
|
||||||
|
right: 10px;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const Input = ({
|
const BottomText = styled.span`
|
||||||
type,
|
margin-top: 10px;
|
||||||
autoComplete,
|
font-size: ${FONT_SIZE.SMALLER};
|
||||||
placeholder
|
color: ${props => (props.color ? props.color : colors.TEXT_SECONDARY)};
|
||||||
autoCorrect,
|
`;
|
||||||
autoCapitalize,
|
|
||||||
spellCheck,
|
class Input extends Component {
|
||||||
value,
|
getIcon(inputState) {
|
||||||
onChange,
|
let icon = [];
|
||||||
isSuccess,
|
if (inputState === 'success') {
|
||||||
isWarning,
|
icon = ICONS.CHECKED;
|
||||||
isError,
|
} else if (inputState === 'warning') {
|
||||||
errorText,
|
icon = ICONS.WARNING;
|
||||||
inputLabel,
|
} else if (inputState === 'error') {
|
||||||
}) => (
|
icon = ICONS.ERROR;
|
||||||
<Wrapper>
|
}
|
||||||
{inputLabel && (
|
return icon;
|
||||||
<InputLabel>{inputLabel}</InputLabel>
|
}
|
||||||
|
|
||||||
|
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 (
|
||||||
|
<Wrapper
|
||||||
|
className={this.props.className}
|
||||||
|
>
|
||||||
|
{this.props.inputLabel && (
|
||||||
|
<InputLabel>{this.props.inputLabel}</InputLabel>
|
||||||
)}
|
)}
|
||||||
<InputWrapper>
|
<InputWrapper>
|
||||||
|
<InputIconWrapper>
|
||||||
|
{this.props.state && (
|
||||||
|
<StyledIcon
|
||||||
|
icon={this.getIcon(this.props.state)}
|
||||||
|
color={this.getColor(this.props.state)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
<StyledInput
|
<StyledInput
|
||||||
type={type}
|
hasAddon={!!this.props.sideAddon}
|
||||||
placeholder={placeholder}
|
type={this.props.type}
|
||||||
autoComplete={autoComplete}
|
placeholder={this.props.placeholder}
|
||||||
autoCorrect={autoCorrect}
|
autoComplete={this.props.autoComplete}
|
||||||
autoCapitalize={autoCapitalize}
|
autoCorrect={this.props.autoCorrect}
|
||||||
spellCheck={spellCheck}
|
autoCapitalize={this.props.autoCapitalize}
|
||||||
value={value}
|
spellCheck={this.props.spellCheck}
|
||||||
onChange={onChange}
|
value={this.props.value}
|
||||||
isSuccess={isSuccess}
|
onChange={this.props.onChange}
|
||||||
isWarning={isWarning}
|
borderColor={this.getColor(this.props.state)}
|
||||||
isError={isError}
|
|
||||||
/>
|
/>
|
||||||
{isError && (
|
</InputIconWrapper>
|
||||||
<StyledIcon
|
{this.props.sideAddon}
|
||||||
icon={ICONS.ERROR}
|
|
||||||
color={colors.ERROR_PRIMARY}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{isWarning && (
|
|
||||||
<StyledIcon
|
|
||||||
icon={ICONS.WARNING}
|
|
||||||
color={colors.WARNING_PRIMARY}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
{isSuccess && (
|
|
||||||
<StyledIcon
|
|
||||||
icon={ICONS.CHECKED}
|
|
||||||
color={colors.SUCCESS_PRIMARY}
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</InputWrapper>
|
</InputWrapper>
|
||||||
{isError && (
|
{this.props.bottomText && (
|
||||||
<ErrorLabel>
|
<BottomText
|
||||||
{errorText}
|
color={this.getColor(this.props.state)}
|
||||||
</ErrorLabel>
|
>
|
||||||
|
{this.props.bottomText}
|
||||||
|
</BottomText>
|
||||||
)}
|
)}
|
||||||
</Wrapper>
|
</Wrapper>
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Input.propTypes = {
|
Input.propTypes = {
|
||||||
|
className: PropTypes.string,
|
||||||
placeholder: PropTypes.string,
|
placeholder: PropTypes.string,
|
||||||
type: PropTypes.string,
|
type: PropTypes.string,
|
||||||
autoComplete: PropTypes.string,
|
autoComplete: PropTypes.string,
|
||||||
@ -144,11 +143,14 @@ Input.propTypes = {
|
|||||||
spellCheck: PropTypes.string,
|
spellCheck: PropTypes.string,
|
||||||
value: PropTypes.string.isRequired,
|
value: PropTypes.string.isRequired,
|
||||||
onChange: PropTypes.func,
|
onChange: PropTypes.func,
|
||||||
isSuccess: PropTypes.bool,
|
state: PropTypes.string,
|
||||||
isWarning: PropTypes.bool,
|
bottomText: PropTypes.string,
|
||||||
isError: PropTypes.bool,
|
|
||||||
errorText: PropTypes.string,
|
|
||||||
inputLabel: PropTypes.string,
|
inputLabel: PropTypes.string,
|
||||||
|
sideAddon: PropTypes.node,
|
||||||
|
};
|
||||||
|
|
||||||
|
Input.defaultProps = {
|
||||||
|
type: 'text',
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Input;
|
export default Input;
|
||||||
|
Loading…
Reference in New Issue
Block a user