Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Enhance address input #3065

Merged
merged 2 commits into from
Nov 1, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 12 additions & 33 deletions js/src/ui/Form/InputAddress/inputAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { bindActionCreators } from 'redux';

import Input from '../Input';
import IdentityIcon from '../../IdentityIcon';
import util from '../../../api/util';

import styles from './inputAddress.css';

Expand All @@ -38,43 +39,16 @@ class InputAddress extends Component {
onSubmit: PropTypes.func
};

state = {
isEmpty: false
}

componentWillMount () {
const { value, text, accountsInfo, tokens } = this.props;
render () {
const { className, disabled, error, label, hint, value, text, onSubmit, accountsInfo, tokens } = this.props;

const account = accountsInfo[value] || tokens[value];
const hasAccount = account && (!account.meta || !account.meta.deleted);
const inputValue = text && hasAccount ? account.name : value;
const isEmpty = (!inputValue || inputValue.length === 0);

this.setState({ isEmpty });
}

componentWillReceiveProps (newProps) {
const { value, text } = newProps;

if (value === this.props.value && text === this.props.text) {
return;
}

const inputValue = text || value;
const isEmpty = (!inputValue || inputValue.length === 0);

this.setState({ isEmpty });
}

render () {
const { className, disabled, error, label, hint, value, text, onSubmit, accountsInfo, tokens } = this.props;
const { isEmpty } = this.state;
const icon = this.renderIcon();

const classes = [ className ];
classes.push(isEmpty ? styles.inputEmpty : styles.input);

const account = accountsInfo[value] || tokens[value];
const hasAccount = account && (!account.meta || !account.meta.deleted);
classes.push(!icon ? styles.inputEmpty : styles.input);

return (
<div className={ styles.container }>
Expand All @@ -87,15 +61,15 @@ class InputAddress extends Component {
value={ text && hasAccount ? account.name : value }
onChange={ this.handleInputChange }
onSubmit={ onSubmit } />
{ this.renderIcon() }
{ icon }
</div>
);
}

renderIcon () {
const { value } = this.props;

if (!value || !value.length) {
if (!value || !value.length || !util.isAddressValid(value)) {
return null;
}

Expand All @@ -112,6 +86,11 @@ class InputAddress extends Component {
const isEmpty = (value.length === 0);

this.setState({ isEmpty });

if (!/^0x/.test(value) && util.isAddressValid(`0x${value}`)) {
return this.props.onChange(event, `0x${value}`);
}

this.props.onChange(event, value);
}
}
Expand Down