-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/picker component #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
990af6e
2f1b3f1
15a901e
10aaaee
76d5284
0369a8f
be0c152
e1aa370
74fd8eb
e4fa21c
8d77256
a8c57bb
95516cf
204c5ba
7721266
b6bfa2f
40bab8d
a41a06a
4ce7b10
b060099
a4d6c0d
48c698c
344b598
27d5f29
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,4 +39,7 @@ dist/ | |
| .expo | ||
|
|
||
| # Jest | ||
| .jest | ||
| .jest | ||
|
|
||
| # storybook Loader | ||
| .bluebase/storybook-native/storybook/storyLoader.js | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| export default { | ||
| plugins: [ | ||
| require('../../src') ] | ||
| require('../../src')] | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| import * as React from 'react'; | ||
|
|
||
| import { List, ListItem, Picker, Text, View, } from '@bluebase/components'; | ||
| import { Modal, TouchableOpacity, ViewStyle } from 'react-native'; | ||
|
|
||
| import { Theme } from '@bluebase/core'; | ||
|
|
||
| export interface ItemsProps { | ||
| value: string, label: string | ||
| } | ||
| export interface PickerStyles { | ||
| picker: ViewStyle, | ||
| overlay: ViewStyle | ||
| actionSheetOverlay: ViewStyle, | ||
| } | ||
|
|
||
|
|
||
|
|
||
| export interface PickerProps { | ||
| items: ItemsProps[], | ||
| selectedValue: string, | ||
| styles: PickerStyles, | ||
| label: string, | ||
| mode: 'modal' | 'actionsheet'; | ||
| onValueChange: (data: string, index: number) => void | ||
| } | ||
|
|
||
| export interface PickerState { | ||
| selectedValueIndex?: number | ||
| textStyle?: string | ||
| modalVisible?: boolean | ||
| selectedValue: string | ||
| selected?: string | ||
|
|
||
| } | ||
| export class PickerComponent extends React.Component<PickerProps, PickerState> { | ||
| constructor(props: PickerProps) { | ||
| super(props); | ||
|
|
||
| this.state = { | ||
| modalVisible: false, | ||
| selected: '', | ||
| selectedValue: '', | ||
| }; | ||
| } | ||
|
|
||
| static defaultStyles = (_theme: Theme) => ({ | ||
| container: { | ||
| backgroundColor: '#fff', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are colours hard coded |
||
| borderColor: '#ddd', | ||
| borderTopWidth: 0.5, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this hard coded
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the colors are hard coded because android does not have any actionsheet effect so just to make it applicable i have made in modal and poped up through bottom
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So what happens on dark theme? We cannot hardcode Colors. Or themeing will not work |
||
| justifyContent: 'center', | ||
| minHeight: 40, | ||
| padding: 5, | ||
| }, | ||
| overlay: { | ||
| backgroundColor: 'rgba(0,0,0,0.5)', | ||
| flex: 1, | ||
| justifyContent: 'center', | ||
| width: null, | ||
| }, | ||
| actionSheetOverlay: { | ||
| backgroundColor: 'rgba(0,0,0,0.5)', | ||
| flex: 1, | ||
| justifyContent: 'flex-end', | ||
| width: null, | ||
| }, | ||
| picker: { | ||
| backgroundColor: 'white', | ||
| borderColor: '#aaa', | ||
| borderTopWidth: 0.5, | ||
| padding: 10, | ||
| }, | ||
|
|
||
| }) | ||
|
|
||
| onValueChange = (data: string, index: number) => { | ||
| this.setState({ modalVisible: false, selectedValue: data, selected: data }); | ||
| this.props.onValueChange(data, index); | ||
| } | ||
| renderDropdownPicker = () => { | ||
| const { items } = this.props; | ||
| return ( | ||
| <Picker | ||
| selectedValue={this.state.selected} | ||
| onValueChange={this.onValueChange} | ||
| > | ||
| { | ||
| items.map((item: { label: string, value: string }, i: number) => | ||
| <Picker.Item key={i} label={item.label} value={item.value} /> | ||
| ) | ||
| } | ||
| </Picker> | ||
| ); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| renderActionSheetPicker = () => { | ||
| const { items, label, styles } = this.props; | ||
| return ( | ||
| <> | ||
| <View testID="list-id"> | ||
| <List> | ||
| <List.Item | ||
| title={label} | ||
| description={<Text>{this.state.selected}</Text>} | ||
| onPress={this.dialogHandler} | ||
| /> | ||
| </List> | ||
| </View> | ||
| <Modal transparent visible={this.state.modalVisible} animationType="fade"> | ||
| <TouchableOpacity activeOpacity={1} onPress={this.dialogHandler} style={styles.actionSheetOverlay}> | ||
| <View style={styles.picker}> | ||
| { | ||
| items.map((item: { label: string, value: string }, i: number) => ( | ||
| <List> | ||
| <ListItem key={i} title={item.label} onPress={this.onPressHandler(i)} /> | ||
| </List> | ||
|
|
||
| ) | ||
| ) | ||
| } | ||
| </View> | ||
|
|
||
| </TouchableOpacity> | ||
| </Modal> | ||
| </> | ||
| ); | ||
| } | ||
| onPressHandler = (id: number) => () => { | ||
| this.props.items.map((item: { label: string, value: string }, i: number) => { | ||
| if (id === i) { | ||
| this.setState({ selected: item.label }); | ||
| } | ||
| }); | ||
| this.dialogHandler(); | ||
| } | ||
|
|
||
| renderPicker = () => { | ||
|
|
||
| const picker = { modal: this.renderModalPicker(), actionsheet: this.renderActionSheetPicker() } | ||
|
|
||
| return picker[this.props.mode]; | ||
|
|
||
| } | ||
| renderModalPicker = () => { | ||
| const { items, label, styles } = this.props; | ||
| return ( | ||
| <> | ||
| <View> | ||
| <List> | ||
| <List.Item | ||
| title={label} | ||
| description={<Text>{this.state.selected}</Text>} | ||
| onPress={this.dialogHandler} | ||
| /> | ||
| </List> | ||
| </View> | ||
| <Modal transparent visible={this.state.modalVisible} animationType="fade"> | ||
| <TouchableOpacity activeOpacity={1} onPress={this.dialogHandler} style={styles.overlay}> | ||
| <View style={styles.picker} testID="picker-test"> | ||
| { | ||
| items.map((item: { label: string, value: string }, i: number) => ( | ||
| <List> | ||
| <ListItem key={i} title={item.label} onPress={this.onPressHandler(i)} /> | ||
| </List> | ||
|
|
||
| ) | ||
| ) | ||
| } | ||
|
|
||
| </View> | ||
|
|
||
| </TouchableOpacity> | ||
| </Modal> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| dialogHandler = () => { | ||
| this.setState({ modalVisible: !this.state.modalVisible }); | ||
| } | ||
|
|
||
| render() { | ||
| const { mode } = this.props; | ||
| return ( | ||
| <> | ||
| {mode ? this.renderPicker() : this.renderDropdownPicker()} | ||
| </> | ||
| ); | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.