Skip to content

Instantly share code, notes, and snippets.

@mosluce
Created July 30, 2018 13:46
Show Gist options
  • Save mosluce/0e83f79432ed9d8a2e1dc8b94c26d3a0 to your computer and use it in GitHub Desktop.
Save mosluce/0e83f79432ed9d8a2e1dc8b94c26d3a0 to your computer and use it in GitHub Desktop.
import React, { PureComponent } from "react";
import {
View,
Text,
TouchableWithoutFeedback,
Picker,
PickerItemProps,
Modal,
Animated,
TouchableWithoutFeedbackProps,
} from "react-native";
type SelectProps = {
options: PickerItemProps[],
};
const TouchableContainer = (props: TouchableWithoutFeedbackProps) => {
return (
<TouchableWithoutFeedback {...props}>
<View {...props}>{props.children}</View>
</TouchableWithoutFeedback>
)
}
class Select extends PureComponent<SelectProps> {
constructor() {
super();
this.state = {
value: 0,
modalVisible: false,
animatedHeight: new Animated.Value(0),
};
this.setModelVisible = this.setModelVisible.bind(this);
this.onValueChange = this.onValueChange.bind(this);
}
setModelVisible(visible: boolean) {
if (visible) {
this.setState({
modalVisible: visible,
});
Animated.timing(
this.state.animatedHeight,
{
toValue: 275,
duration: 250
}
).start();
} else {
Animated.timing(
this.state.animatedHeight,
{
toValue: 0,
duration: 250
}
).start(() => this.setState({ modalVisible: false }));
}
}
onValueChange(value: any) {
console.log(123);
this.setState({ value });
}
render() {
const { options } = this.props;
const { value, modalVisible, animatedHeight } = this.state;
const text = options[0].label;
return (
<TouchableContainer onPress={() => this.setModelVisible(true)}>
<Text>{text}</Text>
<Modal
transparent={true}
supportedOrientations={['landscape', 'portrait']}
animationType="none"
visible={modalVisible}
onRequestClose={() => this.setModelVisible(false)}
>
<View style={{ flex: 1 }}>
<TouchableContainer
onPress={() => this.setModelVisible(false)}
style={{
backgroundColor: '#00000077',
flex: 1,
flexDirection: 'row',
alignItems: 'flex-end',
}}
>
<TouchableContainer
style={{ flex: 1, backgroundColor: 'white' }}
>
<Animated.View
style={{
height: animatedHeight,
}}
>
<Picker selectedValue={value} onValueChange={(value) => this.onValueChange(value)}>
{options.map((o, i) => <Picker.Item key={i} {...o} />)}
</Picker>
</Animated.View>
</TouchableContainer>
</TouchableContainer>
</View>
</Modal>
</TouchableContainer>
)
}
}
Select.defaultProps = {
options: [{
label: 'X',
value: 1
}, {
label: 'Y',
value: 2
}, {
label: 'Z',
value: 3
}]
}
export default Select;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment