1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
import { mdiEye, mdiEyeOff } from '@mdi/js';
import Icon from '@mdi/react';
import classnames from 'classnames';
import pick from 'lodash/pick';
import { observer } from 'mobx-react';
import React, { Component, createRef } from 'react';
import htmlElementAttributes from 'react-html-attributes';
import injectSheet from 'react-jss';
import { IFormField, IWithStyle } from '../typings/generic';
import Error from '../error';
import Label from '../label';
import Wrapper from '../wrapper';
import scorePasswordFunc from './scorePassword';
import styles from './styles';
interface IProps extends React.InputHTMLAttributes<HTMLInputElement>, IFormField, IWithStyle {
focus?: boolean;
prefix?: string;
suffix?: string;
scorePassword?: boolean;
showPasswordToggle?: boolean;
}
interface IState {
showPassword: boolean;
passwordScore: number;
}
@observer
class InputComponent extends Component<IProps, IState> {
public static defaultProps = {
focus: false,
onChange: () => {},
scorePassword: false,
showLabel: true,
showPasswordToggle: false,
type: 'text',
disabled: false,
};
state = {
passwordScore: 0,
showPassword: false,
};
private inputRef = createRef<HTMLInputElement>();
componentDidMount() {
const { focus } = this.props;
if (focus && this.inputRef && this.inputRef.current) {
this.inputRef.current.focus();
}
}
onChange(e: React.ChangeEvent<HTMLInputElement>) {
const {
scorePassword,
onChange,
} = this.props;
if (onChange) {
onChange(e);
}
if (this.inputRef && this.inputRef.current && scorePassword) {
this.setState({ passwordScore: scorePasswordFunc(this.inputRef.current.value) });
}
}
render() {
const {
classes,
disabled,
error,
id,
label,
prefix,
scorePassword,
suffix,
showLabel,
showPasswordToggle,
type,
} = this.props;
const {
showPassword,
passwordScore,
} = this.state;
const inputProps = pick(this.props, htmlElementAttributes['input']);
if (type === 'password' && showPassword) {
inputProps.type = 'text';
}
inputProps.onChange = this.onChange.bind(this);
const cssClasses = classnames({
[`${inputProps.className}`]: inputProps.className,
[`${classes.input}`]: true,
});
return (
<Wrapper>
<Label
title={label}
showLabel={showLabel}
htmlFor={id}
>
<div
className={classnames({
[`${classes.hasPasswordScore}`]: showPasswordToggle,
[`${classes.wrapper}`]: true,
[`${classes.disabled}`]: disabled,
[`${classes.hasError}`]: error,
})}>
{prefix && (
<span className={classes.prefix}>
{prefix}
</span>
)}
<input
{...inputProps}
className={cssClasses}
ref={this.inputRef}
/>
{suffix && (
<span className={classes.suffix}>
{suffix}
</span>
)}
{showPasswordToggle && (
<button
type="button"
className={classes.formModifier}
onClick={() => this.setState(prevState => ({ showPassword: !prevState.showPassword }))}
tabIndex={-1}
>
<Icon
path={!showPassword ? mdiEye : mdiEyeOff}
size={1}
/>
</button>
)}
</div>
{scorePassword && (
<div className={classnames({
[`${classes.passwordScore}`]: true,
[`${classes.hasError}`]: error,
})}>
<meter
value={passwordScore < 5 ? 5 : passwordScore}
low={30}
high={75}
optimum={100}
max={100}
/>
</div>
)}
</Label>
{error && (
<Error message={error} />
)}
</Wrapper>
);
}
}
export const Input = injectSheet(styles)(InputComponent);
|