aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/ui/Input.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/ui/Input.tsx')
-rw-r--r--src/components/ui/Input.tsx170
1 files changed, 0 insertions, 170 deletions
diff --git a/src/components/ui/Input.tsx b/src/components/ui/Input.tsx
deleted file mode 100644
index c22dc5838..000000000
--- a/src/components/ui/Input.tsx
+++ /dev/null
@@ -1,170 +0,0 @@
1import {
2 ChangeEvent,
3 ChangeEventHandler,
4 Component,
5 createRef,
6 ReactElement,
7 RefObject,
8} from 'react';
9import { observer } from 'mobx-react';
10import classnames from 'classnames';
11import { defineMessages, injectIntl, WrappedComponentProps } from 'react-intl';
12import { mdiEye, mdiEyeOff } from '@mdi/js';
13import { noop } from 'lodash';
14import { scorePassword as scorePasswordFunc } from '../../helpers/password-helpers';
15import Icon from './icon';
16import { Field } from '../../@types/mobx-form.types';
17
18const messages = defineMessages({
19 passwordToggle: {
20 id: 'settings.app.form.passwordToggle',
21 defaultMessage: 'Password toggle',
22 },
23});
24
25interface IProps extends WrappedComponentProps {
26 field: Field;
27 className?: string;
28 focus?: boolean;
29 showPasswordToggle?: boolean;
30 showLabel?: boolean;
31 scorePassword?: boolean;
32 prefix?: string;
33 suffix?: string;
34 placeholder?: string;
35 onChange?: ChangeEventHandler<HTMLInputElement>;
36}
37
38interface IState {
39 showPassword: boolean;
40 passwordScore: number;
41}
42
43// Can this file be merged into the './input/index.tsx' file?
44@observer
45class Input extends Component<IProps, IState> {
46 private inputElement: RefObject<HTMLInputElement> =
47 createRef<HTMLInputElement>();
48
49 constructor(props: IProps) {
50 super(props);
51
52 this.state = {
53 showPassword: false,
54 passwordScore: 0,
55 };
56 }
57
58 componentDidMount(): void {
59 const { focus = false } = this.props;
60 if (focus) {
61 this.focus();
62 }
63 }
64
65 onChange(e: ChangeEvent<HTMLInputElement>): void {
66 const { field, scorePassword, onChange = noop } = this.props;
67
68 if (field.onChange) {
69 onChange(e);
70 field.onChange(e);
71 }
72
73 if (scorePassword) {
74 this.setState({
75 passwordScore: scorePasswordFunc(field.value as string),
76 });
77 }
78 }
79
80 focus() {
81 if (this.inputElement && this.inputElement.current) {
82 this.inputElement.current!.focus();
83 }
84 }
85
86 render(): ReactElement {
87 const {
88 field,
89 className = null,
90 showPasswordToggle = false,
91 showLabel = true,
92 scorePassword = false,
93 prefix = '',
94 suffix = '',
95 intl,
96 } = this.props;
97
98 const { passwordScore } = this.state;
99
100 let { type } = field;
101 if (type === 'password' && this.state.showPassword) {
102 type = 'text';
103 }
104
105 return (
106 <div
107 className={classnames({
108 'franz-form__field': true,
109 'has-error': field.error,
110 [`${className}`]: className,
111 })}
112 >
113 <div className="franz-form__input-wrapper">
114 {prefix && <span className="franz-form__input-prefix">{prefix}</span>}
115 <input
116 id={field.id}
117 type={type}
118 className="franz-form__input"
119 name={field.name}
120 value={field.value}
121 placeholder={field.placeholder}
122 onChange={e => this.onChange(e)}
123 onBlur={field.onBlur}
124 onFocus={field.onFocus}
125 ref={this.inputElement}
126 disabled={field.disabled}
127 />
128 {suffix && <span className="franz-form__input-suffix">{suffix}</span>}
129 {showPasswordToggle && (
130 <button
131 type="button"
132 className={classnames({
133 'franz-form__input-modifier': true,
134 })}
135 onClick={() =>
136 this.setState(prevState => ({
137 showPassword: !prevState.showPassword,
138 }))
139 }
140 tabIndex={-1}
141 aria-label={intl.formatMessage(messages.passwordToggle)}
142 >
143 <Icon icon={this.state.showPassword ? mdiEye : mdiEyeOff} />
144 </button>
145 )}
146 {scorePassword && (
147 <div className="franz-form__password-score">
148 {/* <progress value={this.state.passwordScore} max="100" /> */}
149 <meter
150 value={passwordScore < 5 ? 5 : passwordScore}
151 low={30}
152 high={75}
153 optimum={100}
154 max={100}
155 />
156 </div>
157 )}
158 </div>
159 {field.label && showLabel && (
160 <label className="franz-form__label" htmlFor={field.name}>
161 {field.label}
162 </label>
163 )}
164 {field.error && <div className="franz-form__error">{field.error}</div>}
165 </div>
166 );
167 }
168}
169
170export default injectIntl(Input);