Skip to content

Instantly share code, notes, and snippets.

@kamleshchandnani
Created February 6, 2017 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamleshchandnani/43d5e54eacba9d75709198d237904e4d to your computer and use it in GitHub Desktop.
Save kamleshchandnani/43d5e54eacba9d75709198d237904e4d to your computer and use it in GitHub Desktop.
react-native context-menu/stackoverflow-menu
import React, { Component, PropTypes } from 'react'
import { View, UIManager, findNodeHandle, TouchableOpacity } from 'react-native'
import { Icon } from 'native-base';
const ICON_SIZE = 24
export default class ContextMenu extends Component {
static propTypes = {
// array of strings, will be list items of Menu
actions: PropTypes.arrayOf(PropTypes.string).isRequired,
onPress: PropTypes.func.isRequired
}
constructor(props) {
super(props)
this.state = {
icon: null
}
}
onError() {
console.log('Popup Error')
}
onPress = () => {
console.log('icon ', this.state.icon)
if (this.state.icon) {
UIManager.showPopupMenu(
findNodeHandle(this.state.icon),
this.props.actions,
this.onError,
this.props.onPress
)
}
}
render() {
return (
<View>
<TouchableOpacity onPress={this.onPress}>
<Icon
name='md-more'
size={ICON_SIZE}
color={'grey'}
ref={this.onRef} />
</TouchableOpacity>
</View>
)
}
onRef = icon => {
//calback with icon component as reference
console.log(icon);
if (!this.state.icon) {
this.setState({ icon })
}
}
}
import ContextMenu from './ContextMenu';
import React,{Component} from 'react';
import {View} from 'react-native';
export default class ContextMenuUsage extends Component{
render () {
return (
<View>
<ContextMenu actions={['Edit', 'Remove']} onPress={this.onPopupEvent} />
</View>
)
}
onPopupEvent = (eventName, index) => {
if (eventName !== 'itemSelected') return
if (index === 0) this.onEdit()
else this.onRemove()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment