1 | import * as React from 'react';
|
2 |
|
3 | import InputStyle from './Input.styled';
|
4 | import { Icons } from '../index';
|
5 |
|
6 | export interface IProps {
|
7 | feedback?: string|Object;
|
8 | placeholder: string;
|
9 | type: string;
|
10 | isInvalid?: boolean|string;
|
11 | isWarning?: boolean|string;
|
12 | comment?: string;
|
13 | label?: string;
|
14 | css?: string|Object;
|
15 | passwordType?: boolean;
|
16 | onChange?: ({ target }: any) => void|any;
|
17 | onBlur?: ({ target }: any) => void|any;
|
18 | onFocus?: ({ target }: any) => void|any;
|
19 | value?: string|number;
|
20 | minLength?: number;
|
21 | maxLength?: number;
|
22 | name?: string;
|
23 | min?: string;
|
24 | max?: string;
|
25 | disabled?: boolean;
|
26 | }
|
27 |
|
28 | interface IState {
|
29 | valid?: boolean;
|
30 | passwordType?: boolean;
|
31 | }
|
32 |
|
33 | export default class Input extends React.PureComponent<IProps, IState> {
|
34 | state = {
|
35 | passwordType: false,
|
36 | };
|
37 |
|
38 | togglePassword = (passwordType: any) => this.setState(({ passwordType }));
|
39 |
|
40 | renderIcon = (type: string) => {
|
41 | switch (type) {
|
42 | case 'password': {
|
43 | return (
|
44 | <Icons.Eye onClick={passwordType => this.togglePassword(passwordType)} />
|
45 | );
|
46 | }
|
47 | case 'search': {
|
48 | return (
|
49 | <svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 18.414 18.414' className='search-icon'>
|
50 | <g fill='none' stroke='#08a5d3' strokeLinecap='square' strokeWidth='2'>
|
51 | <path d='M11.685 1a5.73 5.73 0 1 1-5.73 5.73A5.73 5.73 0 0 1 11.685 1z'/>
|
52 | <path strokeMiterlimit='10' d='M7.63 10.784L1.414 17'/>
|
53 | </g>
|
54 | </svg>
|
55 | );
|
56 | }
|
57 | default:
|
58 | return null;
|
59 | }
|
60 | }
|
61 |
|
62 | render () {
|
63 | const { feedback, comment, label, type, placeholder, isInvalid, isWarning } = this.props;
|
64 | const { passwordType } = this.state;
|
65 | const password = passwordType && { type: 'text' };
|
66 | const moreInformationInInput = feedback || comment || isInvalid || isWarning;
|
67 |
|
68 | return (
|
69 | <InputStyle.GroupInput>
|
70 | <InputStyle.Input {...this.props} passwordType={passwordType} {...password} />
|
71 | <label>{label || placeholder}</label>
|
72 | <span className='bar' />
|
73 | {(moreInformationInInput) && <span className='feedback'>{moreInformationInInput}</span>}
|
74 | {this.renderIcon(type)}
|
75 | </InputStyle.GroupInput>
|
76 | );
|
77 | }
|
78 | }
|