-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create network picker component (#4677)
* Create network picker component * Update test
- Loading branch information
Showing
10 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
app/component-library/components/NetworkPicker/NetworkPicker.data.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/* eslint-disable import/prefer-default-export */ | ||
export const testImageUrl = | ||
'https://assets.coingecko.com/coins/images/279/small/ethereum.png?1595348880'; |
33 changes: 33 additions & 0 deletions
33
app/component-library/components/NetworkPicker/NetworkPicker.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* eslint-disable no-console */ | ||
import React from 'react'; | ||
import { storiesOf } from '@storybook/react-native'; | ||
import { text } from '@storybook/addon-knobs'; | ||
import NetworkPicker from './NetworkPicker'; | ||
import { testImageUrl } from './NetworkPicker.data'; | ||
import { StyleSheet } from 'react-native'; | ||
|
||
const styles = StyleSheet.create({ | ||
networkPicker: { | ||
alignSelf: 'flex-start', | ||
}, | ||
}); | ||
|
||
storiesOf('Component Library / Network Picker', module).add('Default', () => { | ||
const groupId = 'Props'; | ||
const networkLabelSelector = text( | ||
'networkLabel', | ||
'Ethereum Mainnet', | ||
groupId, | ||
); | ||
|
||
return ( | ||
<NetworkPicker | ||
onPress={() => { | ||
console.log('Picking network!'); | ||
}} | ||
networkLabel={networkLabelSelector} | ||
networkImageUrl={testImageUrl} | ||
style={styles.networkPicker} | ||
/> | ||
); | ||
}); |
39 changes: 39 additions & 0 deletions
39
app/component-library/components/NetworkPicker/NetworkPicker.styles.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { StyleSheet, ViewStyle } from 'react-native'; | ||
import { Theme } from 'app/util/theme/models'; | ||
import { NetworkPickerStyleSheetVars } from './NetworkPicker.types'; | ||
|
||
/** | ||
* Style sheet function for NetworkPicker component. | ||
* | ||
* @param params Style sheet params. | ||
* @param params.theme App theme from ThemeContext. | ||
* @param params.vars NetworkPicker stylesheet vars. | ||
* @returns StyleSheet object. | ||
*/ | ||
const styleSheet = (params: { | ||
theme: Theme; | ||
vars: NetworkPickerStyleSheetVars; | ||
}) => { | ||
const { vars, theme } = params; | ||
const { colors } = theme; | ||
const { style } = vars; | ||
|
||
return StyleSheet.create({ | ||
base: Object.assign( | ||
{ | ||
height: 32, | ||
borderRadius: 16, | ||
paddingHorizontal: 8, | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
backgroundColor: colors.background.alternative, | ||
} as ViewStyle, | ||
style, | ||
) as ViewStyle, | ||
label: { | ||
marginHorizontal: 8, | ||
}, | ||
}); | ||
}; | ||
|
||
export default styleSheet; |
17 changes: 17 additions & 0 deletions
17
app/component-library/components/NetworkPicker/NetworkPicker.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import { shallow } from 'enzyme'; | ||
import NetworkPicker from './NetworkPicker'; | ||
import { testImageUrl } from './NetworkPicker.data'; | ||
|
||
describe('NetworkPicker', () => { | ||
it('should render correctly', () => { | ||
const wrapper = shallow( | ||
<NetworkPicker | ||
onPress={jest.fn} | ||
networkLabel={'Ethereum Mainnet'} | ||
networkImageUrl={testImageUrl} | ||
/>, | ||
); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
app/component-library/components/NetworkPicker/NetworkPicker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* eslint-disable react/prop-types */ | ||
import React from 'react'; | ||
import { TouchableOpacity } from 'react-native'; | ||
import { BaseAvatarSize } from '../BaseAvatar'; | ||
import { NetworkPickerProps } from './NetworkPicker.types'; | ||
import BaseText, { BaseTextVariant } from '../BaseText'; | ||
import stylesheet from './NetworkPicker.styles'; | ||
import { useStyles } from '../../../component-library/hooks'; | ||
import Icon, { IconName, IconSize } from '../Icon'; | ||
import NetworkAvatar from '../NetworkAvatar'; | ||
|
||
const NetworkPicker = ({ | ||
onPress, | ||
style, | ||
networkLabel, | ||
networkImageUrl, | ||
}: NetworkPickerProps) => { | ||
const { styles } = useStyles(stylesheet, { style }); | ||
|
||
return ( | ||
<TouchableOpacity style={styles.base} onPress={onPress}> | ||
<NetworkAvatar | ||
size={BaseAvatarSize.Xs} | ||
networkImageUrl={networkImageUrl} | ||
/> | ||
<BaseText style={styles.label} variant={BaseTextVariant.sBodyMD}> | ||
{networkLabel} | ||
</BaseText> | ||
<Icon size={IconSize.Xs} name={IconName.ArrowDownOutline} /> | ||
</TouchableOpacity> | ||
); | ||
}; | ||
|
||
export default NetworkPicker; |
21 changes: 21 additions & 0 deletions
21
app/component-library/components/NetworkPicker/NetworkPicker.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { TouchableOpacityProps } from 'react-native'; | ||
|
||
/** | ||
* NetworkPicker component props. | ||
*/ | ||
export interface NetworkPickerProps extends TouchableOpacityProps { | ||
/** | ||
* Network image url. | ||
*/ | ||
networkImageUrl: string; | ||
/** | ||
* Network label to display. | ||
*/ | ||
networkLabel: string; | ||
/** | ||
* Callback to trigger when picker is pressed. | ||
*/ | ||
onPress: () => void; | ||
} | ||
|
||
export type NetworkPickerStyleSheetVars = Pick<NetworkPickerProps, 'style'>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# NetworkPicker | ||
|
||
NetworkPicker is a component that renders an avatar based on the user selected network. | ||
|
||
## Props | ||
|
||
This component extends `TouchableOpacityProps` from React Native's [TouchableOpacity Component](https://reactnative.dev/docs/touchableOpacity). | ||
|
||
### `networkImageUrl` | ||
|
||
Network image url. | ||
|
||
| <span style="color:gray;font-size:14px">TYPE</span> | <span style="color:gray;font-size:14px">REQUIRED</span> | | ||
| :-------------------------------------------------- | :------------------------------------------------------ | | ||
| string | Yes | | ||
|
||
### `networkLabel` | ||
|
||
Network label to display. | ||
|
||
| <span style="color:gray;font-size:14px">TYPE</span> | <span style="color:gray;font-size:14px">REQUIRED</span> | | ||
| :-------------------------------------------------- | :------------------------------------------------------ | | ||
| string | Yes | | ||
|
||
### `onPress` | ||
|
||
Callback to trigger when picker is pressed. | ||
|
||
| <span style="color:gray;font-size:14px">TYPE</span> | <span style="color:gray;font-size:14px">REQUIRED</span> | | ||
| :-------------------------------------------------- | :------------------------------------------------------ | | ||
| function | Yes | |
36 changes: 36 additions & 0 deletions
36
app/component-library/components/NetworkPicker/__snapshots__/NetworkPicker.test.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`NetworkPicker should render correctly 1`] = ` | ||
<TouchableOpacity | ||
onPress={[Function]} | ||
style={ | ||
Object { | ||
"alignItems": "center", | ||
"backgroundColor": "#F2F4F6", | ||
"borderRadius": 16, | ||
"flexDirection": "row", | ||
"height": 32, | ||
"paddingHorizontal": 8, | ||
} | ||
} | ||
> | ||
<NetworkAvatar | ||
networkImageUrl="https://assets.coingecko.com/coins/images/279/small/ethereum.png?1595348880" | ||
size="16" | ||
/> | ||
<BaseText | ||
style={ | ||
Object { | ||
"marginHorizontal": 8, | ||
} | ||
} | ||
variant="sBodyMD" | ||
> | ||
Ethereum Mainnet | ||
</BaseText> | ||
<Icon | ||
name="ArrowDownOutline" | ||
size="12" | ||
/> | ||
</TouchableOpacity> | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './NetworkPicker'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters