GithubHelp home page GithubHelp logo

carautenbach / react-native-default-menu Goto Github PK

View Code? Open in Web Editor NEW

This project forked from souvik-ghosh/react-native-default-menu

0.0 0.0 0.0 1.7 MB

Menu component for showing default OS menus. Uses ActionSheetIOS for IOS and UIManager for Android.

JavaScript 100.00%

react-native-default-menu's Introduction

react-native-default-menu

npm Version License

Menu component for showing default OS menus. Uses ActionSheetIOS for IOS and UIManager for Android

Installation

npm i react-native-default-menu

or

yarn add react-native-default-menu

Demo

Usage

...
import Menu from 'react-native-default-menu';
...
const options = ['Edit', 'Remove'];
...
onPopupEvent = (eventName, index) => {
  // on IOS it returns the option name as first argument
  // on Android it returns 'itemSelected' or 'dismissed' as first argument
  // the second argument is the index of the selected option. If cancelled, it returns undefined as index
  console.log('index', index);
  let optionName;
  if (index >= 0) optionName = options[index]; // get option name from 'options' array
  console.log('selected option', optionName);
};
...
render() {
  return(
    <View>
      <Menu options={options} onPress={this.onPopupEvent}>
        <Text>click me</Text>
      </Menu>
    </View>
   )
}

Complete Example

import React, { Component } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Menu from 'react-native-default-menu';
import Icon from 'react-native-vector-icons/MaterialIcons';

const options = ['Edit', 'Remove'];

export default class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      eventName: 'none',
      index: 'none',
      selectedOption: 'none',
    };
  }

  onPopupEvent = (eventName, index) => {
    let selectedOption = 'none';
    if (index >= 0) selectedOption = options[index];
    this.setState({ eventName, index, selectedOption });
  };

  render() {
    return (
      <View style={styles.container}>
        <View style={{ padding: 20 }}>
          <Menu options={options} onPress={this.onPopupEvent} style={{height: 30, width: 30}}>
            <Icon name={'more-vert'} size={24} color={'grey'} />
          </Menu>
        </View>
        <View style={{ padding: 15 }}>
          <Text style={styles.text}>{`event: ${this.state.eventName}`}</Text>
          <Text style={styles.text}>{`index: ${this.state.index}`}</Text>
          <Text style={styles.text}>{`selected option: ${this.state.selectedOption}`}</Text>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    backgroundColor: '#ecf0f1',
    padding: 8,
  },
  text: { fontSize: 30 },
});

react-native-default-menu's People

Contributors

souvik-ghosh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.