Skip to content

Commit

Permalink
export ctx + update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
sodik82 committed Apr 27, 2018
1 parent 519f977 commit a2a4f59
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion doc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## MenuProvider

It provides methods to handle popup menus imperatively. The same methods are exposed to the child context with name `menuActions`.
It provides methods to handle popup menus imperatively. The same methods are exposed to the context property `menuActions`. This can be retrieved by HOC `withMenuContext` that adds `ctx` property to your component. Then simply call `props.ctx.menuActions.method()`.

**Note:** It is important that `<MenuProvider />` is on the top of the component hierarchy (e.g. `ScrollView` should be inside of `MenuProvider`) and wraps all `<Menu />` components.
This is needed in order to solve z-index issues.
Expand Down
16 changes: 14 additions & 2 deletions examples/MenuMethodsExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ import Menu, {
MenuProvider,
MenuOptions,
MenuOption,
MenuTrigger
MenuTrigger,
withMenuContext,
} from 'react-native-popup-menu';

const Openner = (props) => (
<TouchableOpacity style={{ paddingTop: 50 }}
onPress={() => props.ctx.menuActions.openMenu('menu-1')}>
<Text>Open menu from context</Text>
</TouchableOpacity>
);

const ContextOpenner = withMenuContext(Openner);

export default class ControlledExample extends Component {

onOptionSelect(value) {
Expand All @@ -28,7 +38,8 @@ export default class ControlledExample extends Component {
render() {
return (
<MenuProvider style={{flexDirection: 'column', padding: 30}}>
<Menu onSelect={value => this.onOptionSelect(value)} ref={this.onRef}>
<Menu onSelect={value => this.onOptionSelect(value)}
name="menu-1" ref={this.onRef}>
<MenuTrigger text='Select option'/>
<MenuOptions>
<MenuOption value={1} text='One' />
Expand All @@ -38,6 +49,7 @@ export default class ControlledExample extends Component {
<TouchableOpacity style={{ paddingTop: 50 }} onPress={() => this.openMenu()}>
<Text>Open menu from outside</Text>
</TouchableOpacity>
<ContextOpenner />
</MenuProvider>
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import './polyfills';
import { deprecatedComponent } from './helpers'

import Menu from './Menu';
import MenuProvider from './MenuProvider';
import MenuProvider, { withCtx } from './MenuProvider';
import MenuOption from './MenuOption';
import MenuOptions from './MenuOptions';
import MenuTrigger from './MenuTrigger';
Expand All @@ -27,4 +27,5 @@ export {
MenuOptions,
MenuTrigger,
renderers,
withCtx as withMenuContext,
};

0 comments on commit a2a4f59

Please sign in to comment.