-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathoverflowMenuPressHandlers.ts
131 lines (119 loc) · 3.37 KB
/
overflowMenuPressHandlers.ts
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import type * as React from 'react';
import {
Platform,
ActionSheetIOS,
UIManager,
findNodeHandle,
View,
} from 'react-native';
import { HiddenItem, HiddenItemProps } from '../HeaderItems';
import { OverflowMenuContextType } from './OverflowMenuContext';
import inspectElements, { type PropsExtractor } from 'react-to-imperative';
type OverflowButtonDescriptors = ReadonlyArray<{
title: string;
onPress?: HiddenItemProps['onPress'];
destructive?: boolean;
disabled?: boolean;
}>;
export const extractOverflowButtonData = (
hiddenButtons: React.ReactNode
): OverflowButtonDescriptors => {
return inspectElements(hiddenButtons, extractHiddenItemProps);
};
export const extractHiddenItemProps: PropsExtractor<HiddenItemProps> = ({
props,
type,
}) => {
if (type === HiddenItem) {
return props;
}
return true;
};
export type OnOverflowMenuPressParams = {
hiddenButtons: OverflowButtonDescriptors;
overflowButtonRef: null | View;
cancelButtonLabel?: string;
children: React.ReactNode;
} & OverflowMenuContextType;
export const overflowMenuPressHandlerActionSheet = ({
hiddenButtons,
cancelButtonLabel = 'Cancel',
}: OnOverflowMenuPressParams) => {
let actionTitles = hiddenButtons.map((btn) => btn.title);
actionTitles.unshift(cancelButtonLabel);
const disabledButtonIndices: Array<number> = (() => {
let result: number[] = [];
hiddenButtons.forEach((it, index) => {
if (it.disabled) {
result.push(index + 1);
}
});
return result;
})();
const destructiveButtonIndex: Array<number> = (() => {
let result: number[] = [];
hiddenButtons.forEach((it, index) => {
if (it.destructive) {
result.push(index + 1);
}
});
return result;
})();
ActionSheetIOS.showActionSheetWithOptions(
{
options: actionTitles,
cancelButtonIndex: 0,
disabledButtonIndices,
destructiveButtonIndex,
},
(buttonIndex: number) => {
if (buttonIndex > 0) {
hiddenButtons[buttonIndex - 1]?.onPress?.();
}
}
);
};
export const overflowMenuPressHandlerPopupMenu = ({
hiddenButtons,
overflowButtonRef,
}: OnOverflowMenuPressParams) => {
const enabledButtons = hiddenButtons.filter((it) => it.disabled !== true);
const presenter = UIManager.showPopupMenu;
const node = findNodeHandle(overflowButtonRef);
if (!presenter || !node) {
console.warn(
'could not present overflow menu using showPopupMenu(). Note this is only available on Android.'
);
return;
}
presenter(
node,
enabledButtons.map((btn) => btn.title),
() => console.debug('overflowBtn error'),
(eventName: string, index?: number) => {
if (eventName !== 'itemSelected' || typeof index !== 'number') {
return;
}
enabledButtons[index]?.onPress?.();
}
);
};
export const overflowMenuPressHandlerDropdownMenu = ({
children,
overflowButtonRef,
presentMenu,
}: OnOverflowMenuPressParams) => {
if (overflowButtonRef) {
overflowButtonRef.measureInWindow((x, y, width) => {
presentMenu({ elements: children, x: x + width, y });
});
} else {
console.error('overflowButtonRef is null, cannot show overflow menu');
}
};
export const defaultOnOverflowMenuPress: (
params: OnOverflowMenuPressParams
) => void = Platform.select({
ios: overflowMenuPressHandlerActionSheet,
default: overflowMenuPressHandlerDropdownMenu,
});