React Native – How to use native layout

Action menu

Action menu component

Create new folder “app/helpers/action-menu”

index.ios.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import { ActionSheetIOS } from 'react-native'
export default ActionSheetIOS
import { ActionSheetIOS } from 'react-native' export default ActionSheetIOS
import { ActionSheetIOS } from 'react-native'

export default ActionSheetIOS

index.android.js

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import DialogAndroid from 'react-native-dialogs'
export default {
async showActionSheetWithOptions (options, callback) {
const { title = null, message = null, options: buttons = [], cancelButtonIndex = false, destructiveButtonIndex = false } = options
let positiveText = null
if (cancelButtonIndex !== false) {
positiveText = buttons[cancelButtonIndex]
}
let negativeText = null
if (destructiveButtonIndex !== false) {
negativeText = buttons[destructiveButtonIndex]
}
const items = buttons.map((item, index) => ({
index,
label: item
})).filter(({ index }) => index !== cancelButtonIndex && index !== destructiveButtonIndex)
const { action, selectedItem } = await DialogAndroid.showPicker(title, message, {
negativeColor: 'red',
negativeText,
positiveText,
items
})
if (action === 'actionNegative') {
callback(destructiveButtonIndex)
} else if (selectedItem) {
callback(selectedItem.index) // Send the index just like on IOS
}
}
}
import DialogAndroid from 'react-native-dialogs' export default { async showActionSheetWithOptions (options, callback) { const { title = null, message = null, options: buttons = [], cancelButtonIndex = false, destructiveButtonIndex = false } = options let positiveText = null if (cancelButtonIndex !== false) { positiveText = buttons[cancelButtonIndex] } let negativeText = null if (destructiveButtonIndex !== false) { negativeText = buttons[destructiveButtonIndex] } const items = buttons.map((item, index) => ({ index, label: item })).filter(({ index }) => index !== cancelButtonIndex && index !== destructiveButtonIndex) const { action, selectedItem } = await DialogAndroid.showPicker(title, message, { negativeColor: 'red', negativeText, positiveText, items }) if (action === 'actionNegative') { callback(destructiveButtonIndex) } else if (selectedItem) { callback(selectedItem.index) // Send the index just like on IOS } } }
import DialogAndroid from 'react-native-dialogs'

export default {
  async showActionSheetWithOptions (options, callback) {
    const { title = null, message = null, options: buttons = [], cancelButtonIndex = false, destructiveButtonIndex = false } = options

    let positiveText = null
    if (cancelButtonIndex !== false) {
      positiveText = buttons[cancelButtonIndex]
    }

    let negativeText = null
    if (destructiveButtonIndex !== false) {
      negativeText = buttons[destructiveButtonIndex]
    }

    const items = buttons.map((item, index) => ({
      index,
      label: item
    })).filter(({ index }) => index !== cancelButtonIndex && index !== destructiveButtonIndex)

    const { action, selectedItem } = await DialogAndroid.showPicker(title, message, {
      negativeColor: 'red',
      negativeText,
      positiveText,
      items
    })

    if (action === 'actionNegative') {
      callback(destructiveButtonIndex)
    } else if (selectedItem) {
      callback(selectedItem.index) // Send the index just like on IOS
    }

  }
}

Usage

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import ActionMenu from 'app/helpers/action-menu'
...
ActionMenu.showActionSheetWithOptions({
title: 'Select an option',
options: ['Option 1', 'Option 2', 'Cancel'],
cancelButtonIndex: 2
}, (index) => {
if (index === 0) {
// Option 1
} else if (index === 1) {
// Option 2
}
})
import ActionMenu from 'app/helpers/action-menu' ... ActionMenu.showActionSheetWithOptions({ title: 'Select an option', options: ['Option 1', 'Option 2', 'Cancel'], cancelButtonIndex: 2 }, (index) => { if (index === 0) { // Option 1 } else if (index === 1) { // Option 2 } })
  import ActionMenu from 'app/helpers/action-menu'
  
  ...
  
  ActionMenu.showActionSheetWithOptions({
    title: 'Select an option',
    options: ['Option 1', 'Option 2', 'Cancel'],
    cancelButtonIndex: 2
  }, (index) => {
    if (index === 0) {
      // Option 1
    } else if (index === 1) {
      // Option 2
    }
  })

Be the first to comment

Leave a Reply

Your email address will not be published.


*