accounts-frontend/src/components/ui/form/Input.js

155 lines
3.2 KiB
JavaScript
Raw Normal View History

// @flow
import type { Skin, Color } from 'components/ui';
import type { MessageDescriptor } from 'react-intl';
import React from 'react';
2016-05-02 12:45:42 +05:30
import classNames from 'classnames';
import { uniqueId, omit } from 'functions';
import copy from 'services/copy';
2016-05-02 12:45:42 +05:30
import icons from 'components/ui/icons.scss';
import { SKIN_DARK, COLOR_GREEN } from 'components/ui';
2016-05-02 12:45:42 +05:30
import styles from './form.scss';
import FormInputComponent from './FormInputComponent';
2016-05-02 12:45:42 +05:30
2018-05-05 12:35:57 +05:30
let copiedStateTimeout: TimeoutID;
export default class Input extends FormInputComponent<
{
skin: Skin,
color: Color,
center: boolean,
disabled: boolean,
label?: string | MessageDescriptor,
placeholder?: string | MessageDescriptor,
error?: string | { type: string, payload: string },
icon?: string,
copy?: boolean,
},
{
wasCopied: boolean,
},
> {
static defaultProps = {
color: COLOR_GREEN,
skin: SKIN_DARK,
center: false,
disabled: false,
};
state = {
wasCopied: false,
};
elRef = React.createRef<HTMLInputElement>();
render() {
const { color, skin, center } = this.props;
let { icon, label, copy } = this.props;
const { wasCopied } = this.state;
const props = omit(
{
type: 'text',
...this.props,
},
['label', 'error', 'skin', 'color', 'center', 'icon', 'copy'],
);
if (label) {
if (!props.id) {
props.id = uniqueId('input');
}
label = this.formatMessage(label);
label = (
<label className={styles.textFieldLabel} htmlFor={props.id}>
{label}
</label>
);
2016-05-02 12:45:42 +05:30
}
props.placeholder = this.formatMessage(props.placeholder);
let baseClass = styles.formRow;
2016-05-02 12:45:42 +05:30
if (icon) {
baseClass = styles.formIconRow;
icon = <span className={classNames(styles.textFieldIcon, icons[icon])} />;
}
2018-05-05 14:31:25 +05:30
if (copy) {
copy = (
<div
className={classNames(styles.copyIcon, {
[icons.clipboard]: !wasCopied,
[icons.checkmark]: wasCopied,
[styles.copyCheckmark]: wasCopied,
})}
onClick={this.onCopy}
/>
);
}
2018-05-03 10:45:09 +05:30
return (
<div className={baseClass}>
{label}
<div className={styles.textFieldContainer}>
<input
ref={this.elRef}
className={classNames(
styles[`${skin}TextField`],
styles[`${color}TextField`],
{
[styles.textFieldCenter]: center,
},
)}
{...props}
/>
{icon}
{copy}
</div>
{this.renderError()}
</div>
);
}
getValue() {
const { current: el } = this.elRef;
return el && el.value;
}
focus() {
const { current: el } = this.elRef;
if (!el) {
return;
2016-05-02 12:45:42 +05:30
}
el.focus();
setTimeout(el.focus.bind(el), 10);
}
2018-05-05 14:31:25 +05:30
onCopy = async () => {
const value = this.getValue();
2018-05-05 12:35:57 +05:30
if (!value) {
return;
}
2019-11-09 18:37:03 +05:30
try {
clearTimeout(copiedStateTimeout);
copiedStateTimeout = setTimeout(
() => this.setState({ wasCopied: false }),
2000,
);
await copy(value);
this.setState({ wasCopied: true });
} catch (err) {
// it's okay
}
};
2016-05-02 12:45:42 +05:30
}