UNPKG

3.27 kBJavaScriptView Raw
1import React from 'react';
2import PropTypes from 'prop-types';
3import styled from 'styled-components';
4
5const InputStyled = styled.div`
6 input {
7 appearance: none;
8 background-color: ${props => props.theme.bahFormInputBackgroundColor};
9 border: 2px solid ${props => props.theme.bahFormInputBorderColor};
10 border-radius: ${props => props.theme.bahFormFieldsDefaultBorder};
11 color: inherit;
12 font-size: 1rem;
13 height: ${props => props.theme.bahFormInputDefaultHeight};
14 padding: ${props => props.theme.bahFormInputDefaultPadding};
15 vertical-align: middle;
16 box-shadow: inset 0 2px 3px ${props => props.theme.bahFormInputBoxShadowColor};
17 width: 100%;
18 margin-bottom: ${props => props.theme.bahFormFieldsDefaultMarginBottom};
19
20 &:disabled {
21 background-color: ${props => props.theme.bahFormInputDisabledColor};
22 }
23
24 &:focus {
25 box-shadow: inset 0 2px 3px ${props => props.theme.bahFormInputFocusShadowColor};
26 border-color: ${props => props.theme.bahFormInputFocusBorderColor};
27 outline: 0;
28 }
29
30 ${props =>
31 props.isValid &&
32 `
33 box-shadow:inset 0 2px 3px ${props.theme.bahColors.green100};
34 border-color: ${props.theme.bahColors.green300};
35 outline: 0;
36 &:focus {
37 box-shadow:inset 0 2px 3px ${props.theme.bahColors.green200};
38 border-color: ${props.theme.bahColors.green400};
39 outline: 0;
40 }
41 `} ${props =>
42 props.isInvalid &&
43 `
44 box-shadow:inset 0 2px 3px ${props.theme.bahColors.red100};
45 border-color: ${props.theme.bahColors.red300};
46 outline: 0;
47 &:focus {
48 box-shadow:inset 0 2px 3px ${props.theme.bahColors.red200};
49 border-color: ${props.theme.bahColors.red400};
50 outline: 0;
51 }
52 `};
53 }
54`;
55
56const Input = ({ children, name, type, disabled, isValid, isInvalid, ...props }) =>
57 (<InputStyled isValid={isValid} isInvalid={isInvalid}>
58 <label htmlFor={name}>
59 {children}
60 </label>
61 <input {...props} disabled={disabled} type={type} name={name} />
62 </InputStyled>);
63
64export const InputRedux = ({ disabled, isValid, isInvalid, ...props }) =>
65 (<InputStyled isValid={isValid} isInvalid={isInvalid}>
66 <label htmlFor={props.input.name}>
67 {props.label}
68 </label>
69 <input
70 {...props.input}
71 placeholder={props.placeholder}
72 readOnly={props.readOnly}
73 type={props.type}
74 disabled={disabled}
75 />
76 </InputStyled>);
77
78Input.defaultProps = {
79 children: '',
80 name: '',
81 type: 'text',
82 disabled: false,
83 isValid: false,
84 isInvalid: false,
85};
86
87Input.propTypes = {
88 children: PropTypes.node.isRequired,
89 name: PropTypes.string,
90 type: PropTypes.string,
91 disabled: PropTypes.bool,
92 isValid: PropTypes.bool,
93 isInvalid: PropTypes.bool,
94};
95
96InputRedux.defaultProps = {
97 name: '',
98 label: '',
99 input: {},
100 placeholder: '',
101 type: 'text',
102 readOnly: false,
103 disabled: false,
104 isValid: false,
105 isInvalid: false,
106};
107
108InputRedux.propTypes = {
109 name: PropTypes.string,
110 label: PropTypes.string,
111 input: PropTypes.object,
112 placeholder: PropTypes.string,
113 type: PropTypes.string,
114 readOnly: PropTypes.bool,
115 disabled: PropTypes.bool,
116 isValid: PropTypes.bool,
117 isInvalid: PropTypes.bool,
118};
119
120export default Input;