1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Convert SimpleDropdown component to use JSX (#7540)

This commit is contained in:
Whymarrh Whitby 2019-11-24 17:48:58 -03:30 committed by GitHub
parent 7d2b5b26a5
commit 1cac6f2768
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,18 @@
const { Component } = require('react')
const PropTypes = require('prop-types')
const h = require('react-hyperscript')
const classnames = require('classnames')
const R = require('ramda')
import classnames from 'classnames'
import PropTypes from 'prop-types'
import React, { Component } from 'react'
import R from 'ramda'
class SimpleDropdown extends Component {
constructor (props) {
super(props)
this.state = {
isOpen: false,
}
static propTypes = {
options: PropTypes.array.isRequired,
placeholder: PropTypes.string,
onSelect: PropTypes.func,
selectedOption: PropTypes.string,
}
state = {
isOpen: false,
}
getDisplayValue () {
@ -26,67 +29,58 @@ class SimpleDropdown extends Component {
}
toggleOpen () {
const { isOpen } = this.state
this.setState({ isOpen: !isOpen })
this.setState((prevState) => ({
isOpen: !prevState.isOpen,
}))
}
renderOptions () {
const { options, onSelect, selectedOption } = this.props
return h('div', [
h('div.simple-dropdown__close-area', {
onClick: event => {
event.stopPropagation()
this.handleClose()
},
}),
h('div.simple-dropdown__options', [
...options.map(option => {
return h(
'div.simple-dropdown__option',
{
className: classnames({
return (
<div>
<div
className="simple-dropdown__close-area"
onClick={event => {
event.stopPropagation()
this.handleClose()
}}
/>
<div className="simple-dropdown__options">
{options.map((option) => (
<div
className={classnames('simple-dropdown__option', {
'simple-dropdown__option--selected': option.value === selectedOption,
}),
key: option.value,
onClick: () => {
})}
key={option.value}
onClick={() => {
if (option.value !== selectedOption) {
onSelect(option.value)
}
this.handleClose()
},
},
option.displayValue || option.value,
)
}),
]),
])
}}
>
{option.displayValue || option.value}
</div>
))}
</div>
</div>
)
}
render () {
const { placeholder } = this.props
const { isOpen } = this.state
return h(
'div.simple-dropdown',
{
onClick: () => this.toggleOpen(),
},
[
h('div.simple-dropdown__selected', this.getDisplayValue() || placeholder || 'Select'),
h('i.fa.fa-caret-down.fa-lg.simple-dropdown__caret'),
isOpen && this.renderOptions(),
]
return (
<div className="simple-dropdown" onClick={() => this.toggleOpen()}>
<div className="simple-dropdown__selected">{this.getDisplayValue() || placeholder || 'Select'}</div>
<i className="fa fa-caret-down fa-lg simple-dropdown__caret" />
{isOpen && this.renderOptions()}
</div>
)
}
}
SimpleDropdown.propTypes = {
options: PropTypes.array.isRequired,
placeholder: PropTypes.string,
onSelect: PropTypes.func,
selectedOption: PropTypes.string,
}
module.exports = SimpleDropdown