2018-03-26 00:46:45 +05:30
|
|
|
// @flow
|
2017-08-23 00:19:50 +05:30
|
|
|
import React from 'react';
|
2016-05-02 12:45:42 +05:30
|
|
|
|
|
|
|
import classNames from 'classnames';
|
|
|
|
|
2016-07-29 22:44:52 +05:30
|
|
|
import { uniqueId, omit } from 'functions';
|
2018-03-26 00:46:45 +05:30
|
|
|
import copy from 'services/copy';
|
2016-05-02 12:45:42 +05:30
|
|
|
import icons from 'components/ui/icons.scss';
|
2018-03-26 00:46:45 +05:30
|
|
|
import { SKIN_DARK, COLOR_GREEN } from 'components/ui';
|
2016-05-02 12:45:42 +05:30
|
|
|
|
|
|
|
import styles from './form.scss';
|
2016-05-02 14:50:50 +05:30
|
|
|
import FormInputComponent from './FormInputComponent';
|
2016-05-02 12:45:42 +05:30
|
|
|
|
2018-03-26 00:46:45 +05:30
|
|
|
import type { Skin, Color } from 'components/ui';
|
|
|
|
import type { MessageDescriptor } from 'react-intl';
|
|
|
|
|
|
|
|
let copiedStateTimeout;
|
|
|
|
|
|
|
|
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-02 12:45:42 +05:30
|
|
|
static displayName = 'Input';
|
|
|
|
|
2016-05-30 10:10:59 +05:30
|
|
|
static defaultProps = {
|
|
|
|
color: COLOR_GREEN,
|
2018-03-26 00:46:45 +05:30
|
|
|
skin: SKIN_DARK,
|
|
|
|
center: false,
|
|
|
|
disabled: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
wasCopied: false,
|
2016-05-02 12:45:42 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2016-06-16 18:41:55 +05:30
|
|
|
const { color, skin, center } = this.props;
|
2018-03-26 00:46:45 +05:30
|
|
|
let { icon, label, copy } = this.props;
|
|
|
|
const { wasCopied } = this.state;
|
2016-05-02 12:45:42 +05:30
|
|
|
|
2016-07-29 22:44:52 +05:30
|
|
|
const props = omit({
|
2016-05-02 12:45:42 +05:30
|
|
|
type: 'text',
|
2018-03-26 00:46:45 +05:30
|
|
|
...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');
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:50:50 +05:30
|
|
|
label = this.formatMessage(label);
|
2016-05-02 12:45:42 +05:30
|
|
|
|
|
|
|
label = (
|
|
|
|
<label className={styles.textFieldLabel} htmlFor={props.id}>
|
|
|
|
{label}
|
|
|
|
</label>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-02 14:50:50 +05:30
|
|
|
props.placeholder = this.formatMessage(props.placeholder);
|
2016-05-02 12:45:42 +05:30
|
|
|
|
|
|
|
let baseClass = styles.formRow;
|
|
|
|
if (icon) {
|
|
|
|
baseClass = styles.formIconRow;
|
|
|
|
icon = (
|
2016-06-16 18:41:55 +05:30
|
|
|
<span className={classNames(styles.textFieldIcon, icons[icon])} />
|
2016-05-02 12:45:42 +05:30
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-26 00:46:45 +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}>
|
2016-05-02 14:50:50 +05:30
|
|
|
<input ref={this.setEl}
|
|
|
|
className={classNames(
|
|
|
|
styles[`${skin}TextField`],
|
2016-06-16 18:41:55 +05:30
|
|
|
styles[`${color}TextField`],
|
|
|
|
{
|
|
|
|
[styles.textFieldCenter]: center
|
|
|
|
}
|
2016-05-02 14:50:50 +05:30
|
|
|
)}
|
|
|
|
{...props}
|
|
|
|
/>
|
2016-05-02 12:45:42 +05:30
|
|
|
{icon}
|
2018-03-26 00:46:45 +05:30
|
|
|
{copy}
|
2016-05-02 12:45:42 +05:30
|
|
|
</div>
|
2016-05-02 14:50:50 +05:30
|
|
|
{this.renderError()}
|
2016-05-02 12:45:42 +05:30
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
getValue() {
|
|
|
|
return this.el.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
focus() {
|
|
|
|
this.el.focus();
|
|
|
|
setTimeout(this.el.focus.bind(this.el), 10);
|
|
|
|
}
|
2018-03-26 00:46:45 +05:30
|
|
|
|
|
|
|
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
|
|
|
}
|