Skip to content

Commit d4c98b3

Browse files
authored
feat: allow action sheet items to be disabled (#149)
* feat: allow action sheet items to be disabled * fix: corrected action sheet param * chore: specify dep on RN >=0.65.0
1 parent cc0a5f1 commit d4c98b3

5 files changed

+66
-50
lines changed

example/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"dependencies": {
1616
"@expo/react-native-action-sheet": "^3.10.0",
1717
"@react-native-community/masked-view": "0.1.10",
18-
"@react-navigation/native": "^5.1.1",
19-
"@react-navigation/stack": "^5.2.3",
18+
"@react-navigation/native": "^6.0.2",
19+
"@react-navigation/stack": "^6.0.7",
2020
"expo": "^42.0.0",
2121
"expo-status-bar": "~1.0.4",
2222
"react": "16.13.1",

example/yarn.lock

+34-33
Original file line numberDiff line numberDiff line change
@@ -2302,40 +2302,46 @@
23022302
resolved "https://registry.yarnpkg.com/@react-native-community/masked-view/-/masked-view-0.1.10.tgz#5dda643e19e587793bc2034dd9bf7398ad43d401"
23032303
integrity sha512-rk4sWFsmtOw8oyx8SD3KSvawwaK7gRBSEIy2TAwURyGt+3TizssXP1r8nx3zY+R7v2vYYHXZ+k2/GULAT/bcaQ==
23042304

2305-
"@react-navigation/core@^5.16.1":
2306-
version "5.16.1"
2307-
resolved "https://registry.yarnpkg.com/@react-navigation/core/-/core-5.16.1.tgz#e0d308bd9bbd930114ce55c4151806b6d7907f69"
2308-
integrity sha512-3AToC7vPNeSNcHFLd1h71L6u34hfXoRAS1CxF9Fc4uC8uOrVqcNvphpeFbE0O9Bw6Zpl0BnMFl7E5gaL3KGzNA==
2305+
"@react-navigation/core@^6.0.1":
2306+
version "6.0.1"
2307+
resolved "https://registry.yarnpkg.com/@react-navigation/core/-/core-6.0.1.tgz#4424ee1f83e2e305ef80d27c2778fb82fba852a0"
2308+
integrity sha512-mVdvBDYdz8uzLQHokmVdX/xC4rS7NIkD1FN/yaGdovVzYApAhM+UGd3w1zskjyCSyXaVHHOwV59ZGVew+84xfQ==
23092309
dependencies:
2310-
"@react-navigation/routers" "^5.7.4"
2310+
"@react-navigation/routers" "^6.0.1"
23112311
escape-string-regexp "^4.0.0"
2312-
nanoid "^3.1.15"
2313-
query-string "^6.13.6"
2312+
nanoid "^3.1.23"
2313+
query-string "^7.0.0"
23142314
react-is "^16.13.0"
23152315

2316-
"@react-navigation/native@^5.1.1":
2317-
version "5.9.8"
2318-
resolved "https://registry.yarnpkg.com/@react-navigation/native/-/native-5.9.8.tgz#ac76ee6390ea7ce807486ca5c38d903e23433a97"
2319-
integrity sha512-DNbcDHXQPSFDLn51kkVVJjT3V7jJy2GztNYZe/2bEg29mi5QEcHHcpifjMCtyFKntAOWzKlG88UicIQ17UEghg==
2316+
"@react-navigation/elements@^1.1.0":
2317+
version "1.1.0"
2318+
resolved "https://registry.yarnpkg.com/@react-navigation/elements/-/elements-1.1.0.tgz#483155ccf5a8f18d015db283bed34cc2255e2e9e"
2319+
integrity sha512-jZncciZPGuoP6B6f+Wpf6MYSSYy86B2HJDbFTCtT5xZV0w6V9GgCeqvSTOEAxifZrmKl8uDxsr0GrIxgQE8NxA==
2320+
2321+
"@react-navigation/native@^6.0.2":
2322+
version "6.0.2"
2323+
resolved "https://registry.yarnpkg.com/@react-navigation/native/-/native-6.0.2.tgz#6bdb3cfafb6a9cfb603c1555dd61faafca35b7c2"
2324+
integrity sha512-HDqEwgvQ4Cu16vz8jQ55lfyNK9CGbECI1wM9cPOcUa+gkOQEDZ/95VFfFjGGflXZs3ybPvGXlMC4ZAyh1CcO6w==
23202325
dependencies:
2321-
"@react-navigation/core" "^5.16.1"
2326+
"@react-navigation/core" "^6.0.1"
23222327
escape-string-regexp "^4.0.0"
2323-
nanoid "^3.1.15"
2328+
nanoid "^3.1.23"
23242329

2325-
"@react-navigation/routers@^5.7.4":
2326-
version "5.7.4"
2327-
resolved "https://registry.yarnpkg.com/@react-navigation/routers/-/routers-5.7.4.tgz#8b5460e841a0c64f6c9a5fbc2a1eb832432d4fb0"
2328-
integrity sha512-0N202XAqsU/FlE53Nmh6GHyMtGm7g6TeC93mrFAFJOqGRKznT0/ail+cYlU6tNcPA9AHzZu1Modw1eoDINSliQ==
2330+
"@react-navigation/routers@^6.0.1":
2331+
version "6.0.1"
2332+
resolved "https://registry.yarnpkg.com/@react-navigation/routers/-/routers-6.0.1.tgz#ae50f07c776c18bd9fdc87f17e9f3afc3fd59d19"
2333+
integrity sha512-5ctB49rmtTRQuTSBVgqMsEzBUjPP2ByUzBjNivA7jmvk+PDCl4oZsiR8KAm/twhxe215GYThfi2vUWXKAg6EEQ==
23292334
dependencies:
2330-
nanoid "^3.1.15"
2335+
nanoid "^3.1.23"
23312336

2332-
"@react-navigation/stack@^5.2.3":
2333-
version "5.14.9"
2334-
resolved "https://registry.yarnpkg.com/@react-navigation/stack/-/stack-5.14.9.tgz#49c7b9316e6fb456e9766c901e0d607862f0ea7d"
2335-
integrity sha512-DuvrT9P+Tz8ezZLQYxORZqOGqO+vEufaxlW1hSLw1knLD4jNxkz8TJDXtfKwaz//9gb43UhTNccNM02vm7iPqQ==
2337+
"@react-navigation/stack@^6.0.7":
2338+
version "6.0.7"
2339+
resolved "https://registry.yarnpkg.com/@react-navigation/stack/-/stack-6.0.7.tgz#305e66129d0b425802bb6ed4f24dbd2fb56ab74f"
2340+
integrity sha512-hxwhRZbn6zD2rInhItBeHTCPYzmurz+/8/MhtRevBEdLG0+61dik8Y+evg/mu6AsOU0WrDakTsLcHdf/9zkXzw==
23362341
dependencies:
2342+
"@react-navigation/elements" "^1.1.0"
23372343
color "^3.1.3"
2338-
react-native-iphone-x-helper "^1.3.0"
2344+
warn-once "^0.1.0"
23392345

23402346
"@types/glob@^7.1.1":
23412347
version "7.1.3"
@@ -5270,7 +5276,7 @@ nan@^2.12.1:
52705276
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
52715277
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==
52725278

5273-
nanoid@^3.1.15:
5279+
nanoid@^3.1.23:
52745280
version "3.1.25"
52755281
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.1.25.tgz#09ca32747c0e543f0e1814b7d3793477f9c8e152"
52765282
integrity sha512-rdwtIXaXCLFAQbnfqDRnI6jaRHp9fTcYBjtFKE8eezcZ7LuLjhUaQGNeMXf1HmRoCH32CLz6XwX0TtxEOS/A3Q==
@@ -5779,10 +5785,10 @@ pump@^3.0.0:
57795785
end-of-stream "^1.1.0"
57805786
once "^1.3.1"
57815787

5782-
query-string@^6.13.6:
5783-
version "6.14.1"
5784-
resolved "https://registry.yarnpkg.com/query-string/-/query-string-6.14.1.tgz#7ac2dca46da7f309449ba0f86b1fd28255b0c86a"
5785-
integrity sha512-XDxAeVmpfu1/6IjyT/gXHOl+S0vQ9owggJ30hhWKdHAsNPOcasn5o9BW0eejZqL2e4vMjhAxoW3jVHcD6mbcYw==
5788+
query-string@^7.0.0:
5789+
version "7.0.1"
5790+
resolved "https://registry.yarnpkg.com/query-string/-/query-string-7.0.1.tgz#45bd149cf586aaa582dffc7ec7a8ad97dd02f75d"
5791+
integrity sha512-uIw3iRvHnk9to1blJCG3BTc+Ro56CBowJXKmNNAm3RulvPBzWLRqKSiiDk+IplJhsydwtuNMHi8UGQFcCLVfkA==
57865792
dependencies:
57875793
decode-uri-component "^0.2.0"
57885794
filter-obj "^1.1.0"
@@ -5848,11 +5854,6 @@ react-native-gesture-handler@~1.10.2:
58485854
invariant "^2.2.4"
58495855
prop-types "^15.7.2"
58505856

5851-
react-native-iphone-x-helper@^1.3.0:
5852-
version "1.3.1"
5853-
resolved "https://registry.yarnpkg.com/react-native-iphone-x-helper/-/react-native-iphone-x-helper-1.3.1.tgz#20c603e9a0e765fd6f97396638bdeb0e5a60b010"
5854-
integrity sha512-HOf0jzRnq2/aFUcdCJ9w9JGzN3gdEg0zFE4FyYlp4jtidqU03D5X7ZegGKfT1EWteR0gPBGp9ye5T5FvSWi9Yg==
5855-
58565857
react-native-paper@^3.6.0:
58575858
version "3.6.0"
58585859
resolved "https://registry.yarnpkg.com/react-native-paper/-/react-native-paper-3.6.0.tgz#9f8452387bc6e6dae84db90c9c4715af5c7bd142"

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"invariant": ">=2"
2323
},
2424
"peerDependencies": {
25-
"@react-navigation/native": ">=5"
25+
"@react-navigation/native": ">=5",
26+
"react-native": ">=0.65.0"
2627
},
2728
"keywords": [
2829
"react-navigation",

src/__tests__/overflowMenuPressHandlers.test.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ describe('overflowMenuPressHandlers', () => {
156156
expect(ActionSheetIOS.showActionSheetWithOptions).toHaveBeenCalledWith(
157157
{
158158
cancelButtonIndex: 0,
159-
// disabledButtonsIndices: [1],
160-
destructiveButtonIndex: [2],
161-
options: ['Cancel', 'one', 'three'],
159+
disabledButtonIndices: [2],
160+
destructiveButtonIndex: [3],
161+
options: ['Cancel', 'one', 'two', 'three'],
162162
},
163163
expect.any(Function) // press callback
164164
);

src/overflowMenuPressHandlers.js

+25-11
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,40 @@ export const overflowMenuPressHandlerActionSheet = ({
6767
cancelButtonLabel = 'Cancel',
6868
}: OnOverflowMenuPressParams) => {
6969
checkParams(hiddenButtons);
70-
const enabledButtons = hiddenButtons.filter((it) => it.disabled !== true);
71-
let actionTitles = enabledButtons.map((btn) => btn.title);
70+
let actionTitles = hiddenButtons.map((btn) => btn.title);
7271
actionTitles.unshift(cancelButtonLabel);
73-
const destructiveActions: Array<number> = enabledButtons.reduce((acc, btn, index) => {
74-
if (btn.destructive) {
75-
acc.push(index + 1);
76-
}
77-
return acc;
78-
}, []);
72+
73+
const disabledButtonIndices: Array<number> = (() => {
74+
let result = [];
75+
hiddenButtons.forEach((it, index) => {
76+
if (it.disabled === true) {
77+
result.push(index + 1);
78+
}
79+
});
80+
return result;
81+
})();
82+
83+
const destructiveButtonIndex: Array<number> = (() => {
84+
let result = [];
85+
hiddenButtons.forEach((it, index) => {
86+
if (it.destructive === true) {
87+
result.push(index + 1);
88+
}
89+
});
90+
return result;
91+
})();
7992

8093
ActionSheetIOS.showActionSheetWithOptions(
94+
// $FlowFixMe - typings are old
8195
{
8296
options: actionTitles,
8397
cancelButtonIndex: 0,
84-
// $FlowFixMe
85-
destructiveButtonIndex: destructiveActions,
98+
disabledButtonIndices,
99+
destructiveButtonIndex,
86100
},
87101
(buttonIndex: number) => {
88102
if (buttonIndex > 0) {
89-
enabledButtons[buttonIndex - 1].onPress();
103+
hiddenButtons[buttonIndex - 1].onPress();
90104
}
91105
}
92106
);

0 commit comments

Comments
 (0)