You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
130 lines
3.4 KiB
130 lines
3.4 KiB
7 years ago
|
const Component = require('react').Component
|
||
|
const PropTypes = require('prop-types')
|
||
|
const h = require('react-hyperscript')
|
||
|
const inherits = require('util').inherits
|
||
|
const AccountListItem = require('../account-list-item/account-list-item.component').default
|
||
|
const connect = require('react-redux').connect
|
||
6 years ago
|
const Tooltip = require('../../../components/ui/tooltip')
|
||
|
const checksumAddress = require('../../../helpers/utils/util').checksumAddress
|
||
7 years ago
|
|
||
|
ToAutoComplete.contextTypes = {
|
||
|
t: PropTypes.func,
|
||
|
}
|
||
|
|
||
|
module.exports = connect()(ToAutoComplete)
|
||
|
|
||
|
|
||
|
inherits(ToAutoComplete, Component)
|
||
|
function ToAutoComplete () {
|
||
|
Component.call(this)
|
||
|
|
||
|
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, {
|
||
|
account,
|
||
|
className: 'account-list-item__dropdown',
|
||
|
handleClick: () => {
|
||
6 years ago
|
onChange(checksumAddress(account.address))
|
||
7 years ago
|
closeDropdown()
|
||
|
},
|
||
|
icon: this.getListItemIcon(account.address, to),
|
||
|
displayBalance: false,
|
||
|
displayAddress: true,
|
||
|
})),
|
||
|
|
||
|
]),
|
||
|
|
||
|
])
|
||
|
}
|
||
|
|
||
|
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
|
||
|
|
||
|
if (!matches || matchingAccounts[0].address === to) {
|
||
|
this.setState({ accountsToRender: [] })
|
||
|
event.target && event.target.select()
|
||
|
closeDropdown()
|
||
|
} else {
|
||
|
this.setState({ accountsToRender: matchingAccounts })
|
||
|
openDropdown()
|
||
|
}
|
||
|
cb && cb(event.target.value)
|
||
|
}
|
||
|
|
||
|
ToAutoComplete.prototype.componentDidUpdate = function (nextProps, nextState) {
|
||
|
if (this.props.to !== nextProps.to) {
|
||
|
this.handleInputEvent()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
ToAutoComplete.prototype.render = function () {
|
||
|
const {
|
||
|
to,
|
||
|
dropdownOpen,
|
||
|
onChange,
|
||
|
inError,
|
||
6 years ago
|
qrScanner,
|
||
7 years ago
|
} = this.props
|
||
|
|
||
|
return h('div.send-v2__to-autocomplete', {}, [
|
||
|
|
||
6 years ago
|
h(`input.send-v2__to-autocomplete__input${qrScanner ? '.with-qr' : ''}`, {
|
||
7 years ago
|
placeholder: this.context.t('recipientAddress'),
|
||
|
className: inError ? `send-v2__error-border` : '',
|
||
|
value: to,
|
||
|
onChange: event => onChange(event.target.value),
|
||
|
onFocus: event => this.handleInputEvent(event),
|
||
|
style: {
|
||
|
borderColor: inError ? 'red' : null,
|
||
|
},
|
||
|
}),
|
||
6 years ago
|
qrScanner && h(Tooltip, {
|
||
|
title: this.context.t('scanQrCode'),
|
||
|
position: 'bottom',
|
||
|
}, h(`i.fa.fa-qrcode.fa-lg.send-v2__to-autocomplete__qr-code`, {
|
||
6 years ago
|
style: { color: '#33333' },
|
||
|
onClick: () => this.props.scanQrCode(),
|
||
6 years ago
|
})),
|
||
7 years ago
|
!to && h(`i.fa.fa-caret-down.fa-lg.send-v2__to-autocomplete__down-caret`, {
|
||
|
style: { color: '#dedede' },
|
||
|
onClick: () => this.handleInputEvent(),
|
||
|
}),
|
||
|
|
||
|
dropdownOpen && this.renderDropdown(),
|
||
|
|
||
|
])
|
||
|
}
|