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

134 lines
3.5 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
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
};
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.setEl}
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() {
2018-05-03 10:45:09 +05:30
return this.el || this.el.value;
2016-05-02 12:45:42 +05:30
}
focus() {
2018-05-03 10:45:09 +05:30
if (!this.el) {
return;
}
2016-05-02 12:45:42 +05:30
this.el.focus();
setTimeout(this.el.focus.bind(this.el), 10);
}
onCopy = async () => {
try {
clearTimeout(copiedStateTimeout);
await copy(this.getValue());
this.setState({wasCopied: true});
copiedStateTimeout = setTimeout(() => this.setState({wasCopied: false}), 2000);
} catch (err) {
// it's okay
}
};
2016-05-02 12:45:42 +05:30
}