import React from 'react'; import translate, { translateRaw } from 'translations'; import noop from 'lodash/noop'; import { Input, Identicon } from 'components/ui'; interface Props { index: number; address: string; label: string; temporaryLabel: string; labelError?: string; isEditing: boolean; onChange(label: string): void; onSave(): void; onLabelInputBlur(): void; onEditClick(): void; onRemoveClick(): void; } interface State { labelInputTouched: boolean; } class AddressBookTableRow extends React.Component { public state: State = { labelInputTouched: false }; private labelInput: HTMLInputElement | null = null; public componentWillReceiveProps(nextProps: Props) { this.setState({ label: nextProps.label, mostRecentValidLabel: nextProps.label }); } public render() { const { address, temporaryLabel, labelError, isEditing, onEditClick, onRemoveClick } = this.props; const { labelInputTouched } = this.state; const trOnClick = isEditing ? noop : onEditClick; const hashName = `${address}-hash`; const labelName = `${address}-label`; return (
{labelError && (
)}
{labelError && (
)}
); } private handleKeyDown = (e: React.KeyboardEvent) => { const { labelInputTouched } = this.state; e.stopPropagation(); if (e.key === 'Enter' && this.labelInput) { this.labelInput.blur(); } else if (!labelInputTouched) { this.setLabelTouched(); } }; private handleBlur = () => { this.clearLabelTouched(); this.props.onSave(); this.props.onLabelInputBlur(); }; private setLabelInputRef = (node: HTMLInputElement) => (this.labelInput = node); private setLabelTouched = () => !this.state.labelInputTouched && this.setState({ labelInputTouched: true }); private clearLabelTouched = () => this.setState({ labelInputTouched: false }); private handleLabelChange = (e: React.ChangeEvent) => { const label = e.target.value; this.props.onChange(label); this.setState( { labelInputTouched: true }, () => label.length === 0 && this.clearLabelTouched() ); }; } export default AddressBookTableRow;