2018-06-03 04:18:03 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
2018-11-04 06:22:46 +00:00
|
|
|
import { FastField } from 'formik';
|
2018-06-03 04:18:03 +00:00
|
|
|
import classnames from 'classnames';
|
2018-11-04 06:22:46 +00:00
|
|
|
import capitalize from 'lodash/capitalize';
|
|
|
|
import Error from 'components/ui/formik/Error';
|
2018-05-16 03:02:48 +00:00
|
|
|
|
2018-06-03 04:18:03 +00:00
|
|
|
export default class TextInput extends PureComponent {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.input = React.createRef();
|
|
|
|
window.addEventListener('resize', this.handleResize);
|
|
|
|
}
|
2018-05-16 03:02:48 +00:00
|
|
|
|
2018-06-03 04:18:03 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
window.removeEventListener('resize', this.handleResize);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleResize = () => {
|
|
|
|
if (this.scroll) {
|
|
|
|
this.scroll = false;
|
|
|
|
this.scrollIntoView();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
handleFocus = () => {
|
|
|
|
this.scroll = true;
|
|
|
|
setTimeout(() => {
|
|
|
|
this.scroll = false;
|
|
|
|
}, 2000);
|
|
|
|
};
|
|
|
|
|
|
|
|
scrollIntoView = () => {
|
|
|
|
if (this.input.current.scrollIntoViewIfNeeded) {
|
|
|
|
this.input.current.scrollIntoViewIfNeeded();
|
|
|
|
} else {
|
|
|
|
this.input.current.scrollIntoView();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2018-12-19 01:55:50 +00:00
|
|
|
const {
|
|
|
|
name,
|
|
|
|
label = capitalize(name),
|
|
|
|
noError,
|
|
|
|
noTrim,
|
|
|
|
transform,
|
|
|
|
blurTransform,
|
|
|
|
...props
|
|
|
|
} = this.props;
|
2018-06-03 04:18:03 +00:00
|
|
|
|
|
|
|
return (
|
2018-11-04 06:22:46 +00:00
|
|
|
<FastField
|
2018-06-03 04:18:03 +00:00
|
|
|
name={name}
|
2018-12-19 01:55:50 +00:00
|
|
|
render={({ field, form }) => (
|
|
|
|
<>
|
|
|
|
<div className="textinput">
|
|
|
|
<input
|
|
|
|
className={field.value && 'value'}
|
|
|
|
type="text"
|
|
|
|
name={name}
|
2020-05-12 05:48:12 +00:00
|
|
|
id={name}
|
2018-12-19 01:55:50 +00:00
|
|
|
autoCapitalize="off"
|
|
|
|
autoCorrect="off"
|
|
|
|
autoComplete="off"
|
|
|
|
spellCheck="false"
|
|
|
|
ref={this.input}
|
|
|
|
onFocus={this.handleFocus}
|
|
|
|
{...field}
|
|
|
|
{...props}
|
|
|
|
onChange={e => {
|
2020-05-10 00:53:39 +00:00
|
|
|
let v = e.target.value;
|
|
|
|
|
|
|
|
if (!noTrim) {
|
|
|
|
v = v.trim();
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:55:50 +00:00
|
|
|
if (transform) {
|
|
|
|
v = transform(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v !== field.value) {
|
|
|
|
form.setFieldValue(name, v);
|
|
|
|
|
|
|
|
if (props.onChange) {
|
|
|
|
props.onChange(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onBlur={e => {
|
2020-05-10 00:53:39 +00:00
|
|
|
field.onBlur(e);
|
|
|
|
if (props.onBlur) {
|
|
|
|
props.onBlur(e);
|
|
|
|
}
|
|
|
|
|
2018-12-19 01:55:50 +00:00
|
|
|
if (blurTransform) {
|
2020-05-10 00:53:39 +00:00
|
|
|
const v = blurTransform(e.target.value);
|
2018-12-19 01:55:50 +00:00
|
|
|
|
|
|
|
if (v && v !== field.value) {
|
2020-05-10 00:53:39 +00:00
|
|
|
form.setFieldValue(name, v);
|
2018-12-19 01:55:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2020-05-12 05:48:12 +00:00
|
|
|
<label
|
|
|
|
htmlFor={name}
|
|
|
|
className={classnames('textinput-label', 'textinput-1', {
|
2018-12-19 01:55:50 +00:00
|
|
|
value: field.value,
|
|
|
|
error: form.touched[name] && form.errors[name]
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{label}
|
2020-05-12 05:48:12 +00:00
|
|
|
</label>
|
2018-12-19 01:55:50 +00:00
|
|
|
<span
|
2020-05-12 05:48:12 +00:00
|
|
|
className={classnames('textinput-label', 'textinput-2', {
|
2018-12-19 01:55:50 +00:00
|
|
|
value: field.value,
|
|
|
|
error: form.touched[name] && form.errors[name]
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
{!noError && <Error name={name} />}
|
|
|
|
</>
|
|
|
|
)}
|
2018-06-03 04:18:03 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|