1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-24 19:10:22 +01:00
metamask-extension/ui/app/components/send/to-autocomplete.js

115 lines
2.8 KiB
JavaScript
Raw Normal View History

2017-10-06 12:30:45 +02:00
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
2017-10-23 18:24:47 +02:00
const AccountListItem = require('./account-list-item')
const connect = require('../../metamask-connect')
2017-10-06 12:30:45 +02:00
module.exports = connect()(ToAutoComplete)
2017-10-06 12:30:45 +02:00
inherits(ToAutoComplete, Component)
function ToAutoComplete () {
Component.call(this)
2017-10-23 18:24:47 +02:00
this.state = { accountsToRender: [] }
}
ToAutoComplete.prototype.getListItemIcon = function (listItemAddress, toAddress) {
const listItemIcon = h(`i.fa.fa-check.fa-lg`, { style: { color: '#02c9b1' } })
return toAddress && listItemAddress === toAddress
? listItemIcon
: null
}
ToAutoComplete.prototype.renderDropdown = function () {
const {
closeDropdown,
onChange,
to,
} = this.props
const { accountsToRender } = this.state
return accountsToRender.length && h('div', {}, [
h('div.send-v2__from-dropdown__close-area', {
onClick: closeDropdown,
}),
h('div.send-v2__from-dropdown__list', {}, [
...accountsToRender.map(account => h(AccountListItem, {
2017-11-02 13:15:59 +01:00
account,
className: 'account-list-item__dropdown',
2017-10-23 18:24:47 +02:00
handleClick: () => {
onChange(account.address)
closeDropdown()
2017-11-02 13:15:59 +01:00
},
2017-10-23 18:24:47 +02:00
icon: this.getListItemIcon(account.address, to),
displayBalance: false,
displayAddress: true,
2017-11-02 13:15:59 +01:00
})),
2017-10-23 18:24:47 +02:00
]),
])
}
ToAutoComplete.prototype.handleInputEvent = function (event = {}, cb) {
const {
to,
accounts,
closeDropdown,
openDropdown,
} = this.props
const matchingAccounts = accounts.filter(({ address }) => address.match(to || ''))
const matches = matchingAccounts.length
2017-10-23 18:24:47 +02:00
if (!matches || matchingAccounts[0].address === to) {
2017-10-23 18:24:47 +02:00
this.setState({ accountsToRender: [] })
event.target && event.target.select()
closeDropdown()
2017-11-02 13:15:59 +01:00
} else {
2017-10-23 18:24:47 +02:00
this.setState({ accountsToRender: matchingAccounts })
openDropdown()
}
cb && cb(event.target.value)
}
ToAutoComplete.prototype.componentDidUpdate = function (nextProps, nextState) {
if (this.props.to !== nextProps.to) {
this.handleInputEvent()
}
2017-10-06 12:30:45 +02:00
}
ToAutoComplete.prototype.render = function () {
2017-10-23 18:24:47 +02:00
const {
to,
dropdownOpen,
onChange,
inError,
} = this.props
2017-10-06 12:30:45 +02:00
return h('div.send-v2__to-autocomplete', {}, [
2017-10-06 12:30:45 +02:00
h('input.send-v2__to-autocomplete__input', {
placeholder: this.props.t('recipientAddress'),
2017-11-02 13:15:59 +01:00
className: inError ? `send-v2__error-border` : '',
2017-10-06 12:30:45 +02:00
value: to,
2017-10-23 18:24:47 +02:00
onChange: event => onChange(event.target.value),
onFocus: event => this.handleInputEvent(event),
2017-10-17 22:13:20 +02:00
style: {
borderColor: inError ? 'red' : null,
2017-11-02 13:15:59 +01:00
},
2017-10-06 12:30:45 +02:00
}),
2017-10-23 18:24:47 +02:00
!to && h(`i.fa.fa-caret-down.fa-lg.send-v2__to-autocomplete__down-caret`, {
style: { color: '#dedede' },
onClick: () => this.handleInputEvent(),
}),
dropdownOpen && this.renderDropdown(),
2017-10-06 12:30:45 +02:00
])
}