// @flow import type { Skin, Color } from 'components/ui'; import type { MessageDescriptor } from 'react-intl'; import React from 'react'; import classNames from 'classnames'; import { uniqueId, omit } from 'functions'; import copy from 'services/copy'; import icons from 'components/ui/icons.scss'; import { SKIN_DARK, COLOR_GREEN } from 'components/ui'; import styles from './form.scss'; import FormInputComponent from './FormInputComponent'; 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, }> { static defaultProps = { color: COLOR_GREEN, skin: SKIN_DARK, center: false, disabled: false, }; state = { wasCopied: false, }; 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 = ( ); } props.placeholder = this.formatMessage(props.placeholder); let baseClass = styles.formRow; if (icon) { baseClass = styles.formIconRow; icon = ( ); } if (copy) { copy = (
); } return (
{label}
{icon} {copy}
{this.renderError()}
); } getValue() { return this.el && this.el.value; } focus() { const el = this.el; if (!el) { return; } el.focus(); setTimeout(el.focus.bind(el), 10); } onCopy = async () => { const value = this.getValue(); if (!value) { return; } try { clearTimeout(copiedStateTimeout); await copy(value); this.setState({wasCopied: true}); copiedStateTimeout = setTimeout(() => this.setState({wasCopied: false}), 2000); } catch (err) { // it's okay } }; }