Browse Source

Fix SelectAccounts components, clean stuffs

master
Loëck Vézien 7 years ago
parent
commit
2ffd2a8e0f
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  1. 6
      src/components/IsUnlocked/index.js
  2. 12
      src/components/SelectAccount.js
  3. 10
      src/components/base/Select/index.js
  4. 2
      src/constants.js

6
src/components/IsUnlocked/index.js

@ -57,6 +57,12 @@ class IsUnlocked extends PureComponent<Props, State> {
...defaultState, ...defaultState,
} }
componentWillMount() {
if (this.props.isLocked) {
stopSyncAccounts()
}
}
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
if (this.props.isLocked && !nextProps.isLocked) { if (this.props.isLocked && !nextProps.isLocked) {
startSyncAccounts(nextProps.accounts) startSyncAccounts(nextProps.accounts)

12
src/components/SelectAccount.js

@ -12,6 +12,10 @@ import Text from 'components/base/Text'
import type { Account } from 'types/common' import type { Account } from 'types/common'
function renderItem(accounts) {
return item => <span>{(accounts.find(a => a.id === item) || {}).name}</span>
}
const mapStateToProps: MapStateToProps<*, *, *> = state => ({ const mapStateToProps: MapStateToProps<*, *, *> = state => ({
accounts: values(getAccounts(state)), accounts: values(getAccounts(state)),
}) })
@ -24,12 +28,13 @@ type Props = {
const SelectAccount = ({ accounts, value, onChange }: Props) => ( const SelectAccount = ({ accounts, value, onChange }: Props) => (
<Select <Select
itemToString={item => item}
value={value} value={value}
placeholder="Choose an account" placeholder="Choose an account"
items={accounts} items={accounts.map(a => a.id)}
keyProp="id"
onChange={onChange} onChange={onChange}
itemToString={item => (item ? item.name : '')} renderItem={renderItem(accounts)}
renderSelected={renderItem(accounts)}
renderHighlight={(text, key) => ( renderHighlight={(text, key) => (
<Text key={key} fontWeight="bold"> <Text key={key} fontWeight="bold">
{text} {text}
@ -37,5 +42,4 @@ const SelectAccount = ({ accounts, value, onChange }: Props) => (
)} )}
/> />
) )
export default connect(mapStateToProps)(SelectAccount) export default connect(mapStateToProps)(SelectAccount)

10
src/components/base/Select/index.js

@ -15,7 +15,7 @@ import Search from 'components/base/Search'
import Triangles from './Triangles' import Triangles from './Triangles'
type Props = { type Props = {
items: Array<Object>, items: Array<any>,
value?: Object | null, value?: Object | null,
itemToString?: Function, itemToString?: Function,
onChange?: Function, onChange?: Function,
@ -24,7 +24,7 @@ type Props = {
searchable?: boolean, searchable?: boolean,
placeholder?: string, placeholder?: string,
renderHighlight?: string => Element<*>, renderHighlight?: string => Element<*>,
renderSelected?: Object => Element<*>, renderSelected?: any => Element<*>,
renderItem?: (*) => Element<*>, renderItem?: (*) => Element<*>,
keyProp?: string, keyProp?: string,
} }
@ -98,11 +98,15 @@ class Select extends PureComponent<Props> {
renderItems = (items: Array<Object>, downshiftProps: Object) => { renderItems = (items: Array<Object>, downshiftProps: Object) => {
const { renderItem, keyProp } = this.props const { renderItem, keyProp } = this.props
const { getItemProps, highlightedIndex } = downshiftProps const { getItemProps, highlightedIndex } = downshiftProps
return ( return (
<Dropdown> <Dropdown>
{items.length ? ( {items.length ? (
items.map((item, i) => ( items.map((item, i) => (
<ItemWrapper key={keyProp ? item[keyProp] : item.key} {...getItemProps({ item })}> <ItemWrapper
key={keyProp ? item[keyProp] : item.key || item}
{...getItemProps({ item })}
>
<Item highlighted={i === highlightedIndex}> <Item highlighted={i === highlightedIndex}>
{renderItem ? renderItem(item) : <span>{item.name_highlight || item.name}</span>} {renderItem ? renderItem(item) : <span>{item.name_highlight || item.name}</span>}
</Item> </Item>

2
src/constants.js

@ -1,2 +1,2 @@
export const CHECK_UPDATE_TIMEOUT = 5e3 export const CHECK_UPDATE_TIMEOUT = 5e3
export const SYNC_ACCOUNT_TIMEOUT = 2e3 export const SYNC_ACCOUNT_TIMEOUT = 3e3

Loading…
Cancel
Save