-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathNavigationButton.tsx
More file actions
86 lines (80 loc) · 2.43 KB
/
NavigationButton.tsx
File metadata and controls
86 lines (80 loc) · 2.43 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import React, { Component } from 'react';
import { Button, TouchableOpacity } from 'react-native';
import { Navigation, OptionsTopBarButton } from 'react-native-navigation';
import { events } from '../Stores/EventsStore';
interface ButtonProps {
button: OptionsTopBarButton;
componentId: string;
}
export const NavigationButton = class extends Component<ButtonProps> {
ref = undefined;
render() {
const { button, componentId } = this.props;
if (button.component) return this.renderButtonComponent();
return (
<Button
testID={button.testID}
key={button.id}
title={button.text || ''}
disabled={button.enabled === false}
onPress={() =>
button.enabled !== false &&
events.invokeNavigationButtonPressed({
buttonId: button.id,
componentId,
})
}
/>
);
}
renderButtonComponent() {
const { button, componentId } = this.props;
// @ts-ignore
const buttonComponentId = button.component!.componentId;
// @ts-ignore
const ComponentClass = Navigation.mock.store.getComponentClassForName(button.component.name);
if (!ComponentClass) {
throw new Error(`Cannot find registered component for: ${button.component?.name}`);
}
const ButtonComponent = ComponentClass();
const props = Navigation.mock.store.getPropsForId(buttonComponentId);
return (
<TouchableOpacity
onPress={() => {
if (this.ref) {
this.invokeOnClick(
// @ts-ignore
(this.ref!._reactInternalFiber || this.ref!._reactInternals).return.stateNode
);
}
events.invokeNavigationButtonPressed({
buttonId: button.id,
componentId: componentId,
});
}}
testID={button.testID}
>
<ButtonComponent
key={buttonComponentId}
{...props}
componentId={buttonComponentId}
ref={(ref: any) => (this.ref = ref)}
/>
</TouchableOpacity>
);
}
invokeOnClick(stateNode: any) {
if (stateNode.children) {
// @ts-ignore
stateNode.children.forEach((instance) => {
if (
instance.internalInstanceHandle &&
instance.internalInstanceHandle.stateNode.props.onClick
) {
instance.internalInstanceHandle.stateNode.props.onClick();
}
this.invokeOnClick(instance);
});
}
}
};