Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Navigation } from 'react-native-navigation';
- class SFNavigation {
- static createRootStackWithDrawer(componentName: string, drawerName: string, componentId: string) {
- return {
- root: {
- sideMenu: {
- left: {
- component: {
- name: drawerName,
- },
- },
- center: {
- stack: {
- children: [
- {
- component: {
- id: componentId,
- name: componentName,
- },
- },
- ],
- },
- },
- },
- },
- };
- }
- static createRootStack(componentName: string, componentId: string) {
- return {
- root: {
- stack: {
- children: [{
- component: {
- id: componentId,
- name: componentName,
- },
- }],
- },
- },
- };
- }
- static createScreenId(): string {
- const randomString = Math.random().toString(36).substr(2);
- const now = Date.now();
- return `SfScreenId-${randomString}.${now}`;
- }
- private componentIds: string[] = [];
- private drawerComponent: string = '';
- constructor() {
- Navigation.events().registerAppLaunchedListener(() => {
- Navigation.setDefaultOptions({
- topBar: {
- visible: false,
- height: 0,
- },
- });
- });
- }
- public setDrawerComponent(componentName: string) {
- this.drawerComponent = componentName;
- }
- public pop(componentId?: string) {
- const id = componentId || this.getLastComponentId();
- Navigation.pop(id);
- this.componentIds.pop();
- }
- public push(componentName: string, componentId?: string) {
- const id = componentId || SFNavigation.createScreenId();
- const currentId = this.getLastComponentId();
- Navigation.push(currentId, {
- component: {
- id,
- name: componentName,
- },
- });
- this.componentIds.push(id);
- }
- public resetTo(componentName: string, withDrawer: boolean = false) {
- if (withDrawer && !!this.drawerComponent)
- return this.resetWithDrawer(componentName);
- return this.resetWithoutDrawer(componentName);
- }
- private resetWithDrawer(componentName: string, drawerComponent?: string) {
- const id = SFNavigation.createScreenId();
- this.componentIds = [id];
- const drawer = drawerComponent || this.drawerComponent;
- Navigation.setRoot(
- SFNavigation.createRootStackWithDrawer(
- componentName,
- drawer,
- id,
- ));
- }
- private resetWithoutDrawer(componentName: string) {
- const id = SFNavigation.createScreenId();
- this.componentIds = [id];
- Navigation.setRoot(SFNavigation.createRootStack(componentName, id));
- }
- private getLastComponentId(): string {
- return this.componentIds[this.componentIds.length - 1] || '';
- }
- }
- const nav: SFNavigation = new SFNavigation();
- export default nav;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement