aboutsummaryrefslogtreecommitdiffstats
path: root/ui/app/components/send/to-autocomplete/to-autocomplete.js
blob: 14e30e84c07de1c6a84b11b46c26c2d48b5963a8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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

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: () => {
          onChange(account.address)
          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,
    qrScanner,
  } = this.props

  return h('div.send-v2__to-autocomplete', {}, [

    h(`input.send-v2__to-autocomplete__input${qrScanner ? '.with-qr' : ''}`, {
      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,
      },
    }),
    qrScanner && h(`i.fa.fa-qrcode.fa-lg.send-v2__to-autocomplete__qr-code`, {
      style: { color: '#33333' },
      onClick: () => this.props.scanQrCode(),
    }),
    !to && h(`i.fa.fa-caret-down.fa-lg.send-v2__to-autocomplete__down-caret`, {
      style: { color: '#dedede' },
      onClick: () => this.handleInputEvent(),
    }),

    dropdownOpen && this.renderDropdown(),

  ])
}