Files
react-native/packages/rn-tester/js/examples/PopupMenuAndroid/PopupMenuAndroidExample.js
T
Ramanpreet Nara 35308a73a5 Open source PopupMenuAndroid
Summary:
In React Native 0.75, we will remove UIManager.showPopupMenu(), UIManager.dismissPopupMenu().

To replace that API, we are introducing this <PopupMenuAndroid> component. This component works in both Fabric and Paper!

For the usage, please see PopupMenuAndroidExample.js.

Changelog: [Android][Added] - Introduce PopupMenuAndroid to replace UIManager.showPopupMenu()

Reviewed By: mdvacca

Differential Revision: D52712758

fbshipit-source-id: a87628a168d64fabbcc4d0f7b694fa639a927448
2024-01-29 18:54:14 -08:00

70 lines
1.7 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
import type {Node} from 'react';
import type {PopupMenuAndroidInstance} from 'react-native/Libraries/Components/PopupMenuAndroid/PopupMenuAndroid';
import * as React from 'react';
import {Button, PopupMenuAndroid, StyleSheet, Text, View} from 'react-native';
type Fruit = 'Apple' | 'Pear' | 'Banana' | 'Orange' | 'Kiwi';
const PopupMenu = () => {
const popupRef = React.useRef<?PopupMenuAndroidInstance>();
const [fruit, setFruit] = React.useState<?Fruit>();
const fruits: Array<Fruit> = ['Apple', 'Pear', 'Banana', 'Orange', 'Kiwi'];
const items = fruits.map(item => ({
label: item,
onPress: () => {
setFruit(item);
},
}));
return (
<View style={styles.container}>
{fruit ? <Text>Selected {fruit}</Text> : null}
<PopupMenuAndroid
instanceRef={popupRef}
menuItems={items.map(({label}) => label)}
onSelectionChange={selection => items[selection].onPress()}>
<Button
title="Show PopupMenu!"
onPress={() => {
popupRef.current?.show();
}}
/>
</PopupMenuAndroid>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
padding: 16,
},
});
exports.title = 'PopupMenuAndroid';
// TODO(T175446328): Publish oss documentation for PopupMenuAndroid
exports.description = 'PopupMenuAndroid Example';
exports.examples = [
{
title: 'PopupMenu Example',
render(): Node {
return <PopupMenu />;
},
},
];