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

146 lines
3.7 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: bool,
disabled: bool,
label?: string | MessageDescriptor,
placeholder?: string | MessageDescriptor,
error?: string | {type: string, payload: string},
icon?: string,
copy?: bool,
}, {
wasCopied: bool,
}> {
2016-05-30 10:10:59 +05:30
static defaultProps = {
color: COLOR_GREEN,
skin: SKIN_DARK,
center: false,
disabled: false,
};
state = {
wasCopied: false,
2016-05-02 12:45:42 +05:30
};
elRef = React.createRef<HTMLInputElement>();
2016-05-02 12:45:42 +05:30
render() {
const { color, skin, center } = this.props;
let { icon, label, copy } = this.props;
const { wasCopied } = this.state;
2016-05-02 12:45:42 +05:30
const props = omit({
2016-05-02 12:45:42 +05:30
type: 'text',
...this.props,
}, ['label', 'error', 'skin', 'color', 'center', 'icon', 'copy']);
2016-05-02 12:45:42 +05:30
if (label) {
if (!props.id) {
props.id = uniqueId('input');
}
label = this.formatMessage(label);
2016-05-02 12:45:42 +05:30
label = (
<label className={styles.textFieldLabel} htmlFor={props.id}>
{label}
</label>
);
}
props.placeholder = this.formatMessage(props.placeholder);
2016-05-02 12:45:42 +05:30
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])} />
2016-05-02 12:45:42 +05:30
);
}
if (copy) {
copy = (
<div
className={classNames(styles.copyIcon, {
[icons.clipboard]: !wasCopied,
[icons.checkmark]: wasCopied,
[styles.copyCheckmark]: wasCopied,
})}
onClick={this.onCopy}
/>
);
}
2016-05-02 12:45:42 +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}
/>
2016-05-02 12:45:42 +05:30
{icon}
{copy}
2016-05-02 12:45:42 +05:30
</div>
{this.renderError()}
2016-05-02 12:45:42 +05:30
</div>
);
}
getValue() {
const { current: el } = this.elRef;
return el && el.value;
2016-05-02 12:45:42 +05:30
}
focus() {
const { current: el } = this.elRef;
2018-05-05 14:31:25 +05:30
if (!el) {
2018-05-03 10:45:09 +05:30
return;
}
2018-05-05 14:31:25 +05:30
el.focus();
setTimeout(el.focus.bind(el), 10);
2016-05-02 12:45:42 +05:30
}
onCopy = async () => {
2018-05-05 14:31:25 +05:30
const value = this.getValue();
if (!value) {
2018-05-05 12:35:57 +05:30
return;
}
try {
clearTimeout(copiedStateTimeout);
2018-05-05 14:31:25 +05:30
await copy(value);
this.setState({wasCopied: true});
copiedStateTimeout = setTimeout(() => this.setState({wasCopied: false}), 2000);
} catch (err) {
// it's okay
}
};
2016-05-02 12:45:42 +05:30
}