2017-06-26 22:27:55 +00:00
|
|
|
// @flow
|
|
|
|
import React from 'react';
|
|
|
|
import { Identicon } from 'components/ui';
|
|
|
|
import { getEnsAddress } from 'selectors/ens';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import type { State } from 'reducers';
|
2017-06-26 23:27:26 +00:00
|
|
|
import { isValidENSorEtherAddress, isValidENSAddress } from 'libs/validators';
|
2017-06-26 22:27:55 +00:00
|
|
|
import { resolveEnsName } from 'actions/ens';
|
2017-07-06 00:36:11 +00:00
|
|
|
import translate from 'translations';
|
2017-06-26 22:27:55 +00:00
|
|
|
|
|
|
|
type PublicProps = {
|
2017-07-02 05:49:06 +00:00
|
|
|
placeholder: string,
|
|
|
|
value: string,
|
|
|
|
onChange?: (value: string) => void
|
2017-06-26 22:27:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export class AddressField extends React.Component {
|
2017-07-02 05:49:06 +00:00
|
|
|
props: PublicProps & {
|
|
|
|
ensAddress: ?string,
|
|
|
|
resolveEnsName: typeof resolveEnsName
|
|
|
|
};
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
render() {
|
|
|
|
const { placeholder, value, ensAddress } = this.props;
|
|
|
|
const isReadonly = !this.props.onChange;
|
|
|
|
return (
|
|
|
|
<div className="row form-group">
|
|
|
|
<div className="col-xs-11">
|
2017-07-06 00:36:11 +00:00
|
|
|
<label>{translate('SEND_addr')}:</label>
|
2017-07-02 05:49:06 +00:00
|
|
|
<input
|
|
|
|
className={`form-control ${isValidENSorEtherAddress(value)
|
|
|
|
? 'is-valid'
|
|
|
|
: 'is-invalid'}`}
|
|
|
|
type="text"
|
|
|
|
placeholder={placeholder}
|
|
|
|
onChange={this.onChange}
|
|
|
|
disabled={isReadonly}
|
|
|
|
/>
|
|
|
|
{!!ensAddress &&
|
|
|
|
<p className="ens-response">
|
|
|
|
↳
|
|
|
|
<span className="mono">
|
|
|
|
{ensAddress}
|
|
|
|
</span>
|
|
|
|
</p>}
|
|
|
|
</div>
|
|
|
|
<div className="col-xs-1 address-identicon-container">
|
|
|
|
<Identicon address={ensAddress || value} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2017-06-26 22:27:55 +00:00
|
|
|
|
2017-07-02 05:49:06 +00:00
|
|
|
onChange = (e: SyntheticInputEvent) => {
|
|
|
|
const newValue = e.target.value;
|
|
|
|
const { onChange } = this.props;
|
|
|
|
if (!onChange) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// FIXME debounce?
|
|
|
|
if (isValidENSAddress(newValue)) {
|
|
|
|
this.props.resolveEnsName(newValue);
|
|
|
|
}
|
|
|
|
onChange(newValue);
|
|
|
|
};
|
2017-06-26 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapStateToProps(state: State, props: PublicProps) {
|
2017-07-02 05:49:06 +00:00
|
|
|
return {
|
|
|
|
ensAddress: getEnsAddress(state, props.value)
|
|
|
|
};
|
2017-06-26 22:27:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, { resolveEnsName })(AddressField);
|