|
| 1 | +import { NSVElement, NativeScriptProps, registerElement } from 'react-nativescript'; |
| 2 | +import { warn } from 'react-nativescript/dist/shared/logger'; |
| 3 | +import { BottomNavigation, SelectedIndexChangedEventData, TabContentItem, TabStrip } from '../'; |
| 4 | +import { TabNavigationBaseAttributes } from '@nativescript-community/ui-material-core/tab-navigation-base/tab-navigation-base/react'; |
| 5 | + |
| 6 | +// Global compile-time constant (for some reason not exported by RNS itself) |
| 7 | +// eslint-disable-next-line no-var |
| 8 | +declare var __DEV__: boolean; |
| 9 | + |
| 10 | +// ui/bottom-navigation/bottom-navigation.d.ts |
| 11 | +export type BottomNavigationAttributes = TabNavigationBaseAttributes & { |
| 12 | + android?: any; |
| 13 | + ios?: any; |
| 14 | + items?: TabContentItem[]; |
| 15 | + onSelectedIndexChanged?: (args: SelectedIndexChangedEventData) => void; |
| 16 | + selectedIndex?: number; |
| 17 | + tabStrip?: TabStrip; |
| 18 | +}; |
| 19 | + |
| 20 | +declare global { |
| 21 | + // eslint-disable-next-line @typescript-eslint/prefer-namespace-keyword |
| 22 | + module JSX { |
| 23 | + interface IntrinsicElements { |
| 24 | + bottomNavigation: NativeScriptProps<BottomNavigationAttributes, BottomNavigation>; |
| 25 | + } |
| 26 | + interface ElementChildrenAttribute { |
| 27 | + children: {}; |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +let installed: boolean = false; |
| 33 | + |
| 34 | +export function registerBottomNavigation(): void { |
| 35 | + if (installed) { |
| 36 | + return; |
| 37 | + } |
| 38 | + registerElement( |
| 39 | + 'bottomNavigation', |
| 40 | + // @ts-ignore I can assure that this really does extend ViewBase |
| 41 | + () => BottomNavigation, |
| 42 | + { |
| 43 | + // TODO: share the same NodeOps for both BottomNavigation and Tabs; they're identical as they both extend TabNavigationBase. |
| 44 | + nodeOps: { |
| 45 | + insert(child: NSVElement, parent: NSVElement<BottomNavigation>, atIndex?: number): void { |
| 46 | + const bottomNavigation = parent.nativeView; |
| 47 | + |
| 48 | + if (child.nodeRole === 'tabStrip') { |
| 49 | + if (child.nativeView instanceof TabStrip) { |
| 50 | + bottomNavigation.tabStrip = child.nativeView; |
| 51 | + } else { |
| 52 | + if (__DEV__) { |
| 53 | + warn(`Unable to add child "${child.nativeView.constructor.name}" as the tabStrip of <bottomNavigation> as it is not an instance of TabStrip.`); |
| 54 | + } |
| 55 | + } |
| 56 | + } else if (child.nodeRole === 'items') { |
| 57 | + if (child.nativeView instanceof TabContentItem === false) { |
| 58 | + if (__DEV__) { |
| 59 | + warn(`Unable to add child "${child.nativeView.constructor.name}" to the items of <bottomNavigation> as it is not an instance of TabContentItem.`); |
| 60 | + } |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + const items = bottomNavigation.items || []; // Annoyingly, it's the consumer's responsibility to ensure there's an array there! |
| 65 | + |
| 66 | + if (typeof atIndex === 'undefined' || atIndex === items.length) { |
| 67 | + bottomNavigation.items = items.concat(child.nativeView as TabContentItem); |
| 68 | + } else { |
| 69 | + bottomNavigation.items = items.slice().splice(atIndex, 0, child.nativeView as TabContentItem); |
| 70 | + } |
| 71 | + } else if (child.nodeRole === 'item') { |
| 72 | + if (__DEV__) { |
| 73 | + warn(`Unable to add child "${child.nativeView.constructor.name}" to <bottomNavigation> as it had the nodeRole "item"; please correct it to "items".`); |
| 74 | + } |
| 75 | + } else { |
| 76 | + if (__DEV__) { |
| 77 | + warn( |
| 78 | + `Unable to add child "${child.nativeView.constructor.name}" to <bottomNavigation> as it does not have a nodeRole specified; ` + |
| 79 | + 'please set a nodeRole of "tabStrip", or "items".' |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + }, |
| 84 | + remove(child: NSVElement, parent: NSVElement<BottomNavigation>): void { |
| 85 | + const tabs = parent.nativeView; |
| 86 | + |
| 87 | + if (child.nodeRole === 'tabStrip') { |
| 88 | + tabs.tabStrip = null; // Anything falsy should work. |
| 89 | + } else if (child.nodeRole === 'items') { |
| 90 | + tabs.items = (tabs.items || []).filter((i) => i !== child.nativeView); |
| 91 | + } else if (child.nodeRole === 'item') { |
| 92 | + if (__DEV__) { |
| 93 | + warn(`Unable to remove child "${child.nativeView.constructor.name}" from <bottomNavigation> as it had the nodeRole "item"; please correct it to "items".`); |
| 94 | + } |
| 95 | + } else { |
| 96 | + if (__DEV__) { |
| 97 | + warn( |
| 98 | + `Unable to remove child "${child.nativeView.constructor.name}" from <bottomNavigation> as it does not have a nodeRole specified; ` + |
| 99 | + 'please set a nodeRole of "tabStrip", or "items"' |
| 100 | + ); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + ); |
| 107 | + |
| 108 | + installed = true; |
| 109 | +} |
0 commit comments