-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathindex.tsx
56 lines (52 loc) · 1.82 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import * as Render from 'hyperview/src/services/render';
import type { Props } from './types';
import { useBottomTabBarContext } from '../../Contexts';
import { useCallback } from 'react';
/**
* Component used by Hyperview to render a custom bottom tab bar.
* It retrieves the arguments needed by Hyperview render service
* from BottomTabBarContext. This works in tandem with the custom Hyperview
* element <navigation:bottom-tab-bar>
*/
export const BottomTabBar = (navProps: Props): JSX.Element | null => {
const { id, state, navigation } = navProps;
// id is provided by Hyperview, and represents a tab navigator id
const { getElementProps, setElement } = useBottomTabBarContext();
// Props are the props received by the component backing the custom
// Hyperview element <navigation:bottom-tab-bar>
const props = getElementProps?.(id);
const { onUpdate } = props || {};
const onUpdateCustom = useCallback(
(href, action, currentElement, opts) => {
if (action === 'swap' && opts?.newElement) {
if (currentElement.parentNode) {
const newElement = currentElement.parentNode as Element;
newElement.replaceChild(opts.newElement, currentElement);
setElement?.(id, newElement);
} else {
console.warn('Parent node is null. Cannot replace child element.');
}
} else {
onUpdate?.(href, action, currentElement, opts);
}
},
[id, setElement, onUpdate],
);
if (!props) {
return null;
}
return (Render.renderChildren(
props.element,
props.stylesheets,
onUpdateCustom,
{
...props.options,
onSelect: (route: string | null | undefined) => {
if (route) {
navigation.navigate(route);
}
},
targetId: state.routes[state.index].name,
},
) as unknown) as JSX.Element;
};