10 changed files with 167 additions and 51 deletions
@ -1,39 +0,0 @@ |
|||||
// @flow
|
|
||||
|
|
||||
import React from 'react' |
|
||||
import { connect } from 'react-redux' |
|
||||
|
|
||||
import type { MapStateToProps } from 'react-redux' |
|
||||
|
|
||||
import { getAccounts } from 'reducers/accounts' |
|
||||
import Select from 'components/base/Select' |
|
||||
|
|
||||
import type { Account } from 'types/common' |
|
||||
|
|
||||
const mapStateToProps: MapStateToProps<*, *, *> = state => ({ |
|
||||
accounts: Object.entries(getAccounts(state)).map(([, account]: [string, any]) => account), |
|
||||
}) |
|
||||
|
|
||||
type Props = { |
|
||||
accounts: Array<Account>, |
|
||||
onChange: () => Account | void, |
|
||||
value: Account | null, |
|
||||
} |
|
||||
|
|
||||
const SelectAccount = ({ accounts, value, onChange }: Props) => ( |
|
||||
<Select |
|
||||
value={value} |
|
||||
renderSelected={item => item.name} |
|
||||
renderItem={item => ( |
|
||||
<div key={item.id}> |
|
||||
{item.name} - {item.data.balance} |
|
||||
</div> |
|
||||
)} |
|
||||
keyProp="id" |
|
||||
items={accounts} |
|
||||
placeholder="Choose an account" |
|
||||
onChange={onChange} |
|
||||
/> |
|
||||
) |
|
||||
|
|
||||
export default connect(mapStateToProps)(SelectAccount) |
|
@ -0,0 +1,63 @@ |
|||||
|
// @flow
|
||||
|
|
||||
|
import React from 'react' |
||||
|
import { compose } from 'redux' |
||||
|
import { connect } from 'react-redux' |
||||
|
import { translate } from 'react-i18next' |
||||
|
import noop from 'lodash/noop' |
||||
|
|
||||
|
import type { MapStateToProps } from 'react-redux' |
||||
|
import type { T, Account } from 'types/common' |
||||
|
|
||||
|
import { formatBTC } from 'helpers/format' |
||||
|
|
||||
|
import { getAccounts } from 'reducers/accounts' |
||||
|
|
||||
|
import Select from 'components/base/Select' |
||||
|
import Box from 'components/base/Box' |
||||
|
import Text from 'components/base/Text' |
||||
|
|
||||
|
const mapStateToProps: MapStateToProps<*, *, *> = state => ({ |
||||
|
accounts: Object.entries(getAccounts(state)).map(([, account]: [string, any]) => account), |
||||
|
}) |
||||
|
|
||||
|
type Props = { |
||||
|
accounts: Array<Account>, |
||||
|
onChange?: () => Account | void, |
||||
|
value?: Account | null, |
||||
|
t: T, |
||||
|
} |
||||
|
|
||||
|
const renderItem = item => ( |
||||
|
<Box horizontal align="center"> |
||||
|
<Box grow> |
||||
|
<Text color="night" fontSize={0} fontWeight="bold"> |
||||
|
{item.name} |
||||
|
</Text> |
||||
|
</Box> |
||||
|
<Box> |
||||
|
<Text color="mouse" fontSize={0}> |
||||
|
{formatBTC(item.data.balance)} |
||||
|
</Text> |
||||
|
</Box> |
||||
|
</Box> |
||||
|
) |
||||
|
|
||||
|
export const SelectAccount = ({ accounts, value, onChange, t }: Props) => ( |
||||
|
<Select |
||||
|
value={value && accounts.find(a => value && a.id === value.id)} |
||||
|
renderSelected={renderItem} |
||||
|
renderItem={renderItem} |
||||
|
keyProp="id" |
||||
|
items={accounts} |
||||
|
placeholder={t('SelectAccount.placeholder')} |
||||
|
onChange={onChange} |
||||
|
/> |
||||
|
) |
||||
|
|
||||
|
SelectAccount.defaultProps = { |
||||
|
onChange: noop, |
||||
|
value: undefined, |
||||
|
} |
||||
|
|
||||
|
export default compose(connect(mapStateToProps), translate())(SelectAccount) |
@ -0,0 +1,49 @@ |
|||||
|
// @flow
|
||||
|
|
||||
|
import React, { PureComponent } from 'react' |
||||
|
import { storiesOf } from '@storybook/react' |
||||
|
import Chance from 'chance' |
||||
|
|
||||
|
import { SelectAccount } from 'components/SelectAccount' |
||||
|
|
||||
|
const chance = new Chance() |
||||
|
const stories = storiesOf('SelectAccount', module) |
||||
|
|
||||
|
const accounts = [...Array(20)].map(() => ({ |
||||
|
id: chance.string(), |
||||
|
name: chance.name(), |
||||
|
type: 'BTC', |
||||
|
data: { |
||||
|
address: chance.string(), |
||||
|
balance: chance.floating({ min: 0, max: 20 }), |
||||
|
currentIndex: chance.integer({ min: 0, max: 20 }), |
||||
|
transactions: [], |
||||
|
}, |
||||
|
})) |
||||
|
|
||||
|
type State = { |
||||
|
value: any, |
||||
|
} |
||||
|
|
||||
|
class Wrapper extends PureComponent<any, State> { |
||||
|
state = { |
||||
|
value: '', |
||||
|
} |
||||
|
|
||||
|
handleChange = item => this.setState({ value: item }) |
||||
|
|
||||
|
render() { |
||||
|
const { render } = this.props |
||||
|
const { value } = this.state |
||||
|
|
||||
|
return render({ onChange: this.handleChange, value }) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
stories.add('basic', () => ( |
||||
|
<Wrapper |
||||
|
render={({ onChange, value }) => ( |
||||
|
<SelectAccount onChange={onChange} value={value} accounts={accounts} t={k => k} /> |
||||
|
)} |
||||
|
/> |
||||
|
)) |
Loading…
Reference in new issue