Skip to content

Commit e31c73f

Browse files
authored
feat: add imperative setScrollEnabled method (#119)
* feat: add imperative setScrollEnabled method * Update ios/ReactViewPagerManager.m Co-Authored-By: troZee <[email protected]> * Update ios/ReactViewPagerManager.m Co-Authored-By: troZee <[email protected]> * fix: github bad manual merge
1 parent 25c02f9 commit e31c73f

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

android/src/main/java/com/reactnativecommunity/viewpager/ReactViewPagerManager.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class ReactViewPagerManager extends ViewGroupManager<ReactViewPager> {
3030

3131
private static final int COMMAND_SET_PAGE = 1;
3232
private static final int COMMAND_SET_PAGE_WITHOUT_ANIMATION = 2;
33+
private static final int COMMAND_SET_SCROLL_ENABLED = 3;
3334

3435
@Override
3536
public String getName() {
@@ -70,7 +71,9 @@ public Map<String,Integer> getCommandsMap() {
7071
"setPage",
7172
COMMAND_SET_PAGE,
7273
"setPageWithoutAnimation",
73-
COMMAND_SET_PAGE_WITHOUT_ANIMATION);
74+
COMMAND_SET_PAGE_WITHOUT_ANIMATION,
75+
"setScrollEnabled",
76+
COMMAND_SET_SCROLL_ENABLED);
7477
}
7578

7679
@Override
@@ -89,6 +92,10 @@ public void receiveCommand(
8992
viewPager.setCurrentItemFromJs(args.getInt(0), false);
9093
return;
9194
}
95+
case COMMAND_SET_SCROLL_ENABLED: {
96+
viewPager.setScrollEnabled(args.getBoolean(0));
97+
return;
98+
}
9299
default:
93100
throw new IllegalArgumentException(String.format(
94101
"Unsupported command %d received by %s.",

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ PODS:
182182
- React-cxxreact (= 0.61.4)
183183
- React-jsi (= 0.61.4)
184184
- React-jsinspector (0.61.4)
185-
- react-native-viewpager (2.0.2):
185+
- react-native-viewpager (3.1.0):
186186
- React
187187
- React-RCTActionSheet (0.61.4):
188188
- React-Core/RCTActionSheetHeaders (= 0.61.4)
@@ -326,7 +326,7 @@ SPEC CHECKSUMS:
326326
React-jsi: ca921f4041505f9d5197139b2d09eeb020bb12e8
327327
React-jsiexecutor: 8dfb73b987afa9324e4009bdce62a18ce23d983c
328328
React-jsinspector: d15478d0a8ada19864aa4d1cc1c697b41b3fa92f
329-
react-native-viewpager: 88f4269c19d626d2ccd5f7c7f5f7f76422ddb9e3
329+
react-native-viewpager: 66c02e8a18d2d11eef08c84ed9a406b149791751
330330
React-RCTActionSheet: 7369b7c85f99b6299491333affd9f01f5a130c22
331331
React-RCTAnimation: d07be15b2bd1d06d89417eb0343f98ffd2b099a7
332332
React-RCTBlob: 8e0b23d95c9baa98f6b0e127e07666aaafd96c34

ios/ReactViewPagerManager.m

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ - (void) goToPage
3232
}];
3333
}
3434

35+
- (void) changeScrollEnabled
36+
: (nonnull NSNumber *)reactTag enabled
37+
: (BOOL)enabled {
38+
[self.bridge.uiManager addUIBlock:^(
39+
RCTUIManager *uiManager,
40+
NSDictionary<NSNumber *, UIView *> *viewRegistry) {
41+
ReactNativePageView *view = (ReactNativePageView *)viewRegistry[reactTag];
42+
if (!view || ![view isKindOfClass:[ReactNativePageView class]]) {
43+
RCTLogError(@"Cannot find ReactNativePageView with tag #%@", reactTag);
44+
return;
45+
}
46+
[view shouldScroll:enabled];
47+
}];
48+
}
49+
3550
RCT_EXPORT_METHOD(setPage
3651
: (nonnull NSNumber *)reactTag index
3752
: (nonnull NSNumber *)index) {
@@ -44,6 +59,13 @@ - (void) goToPage
4459
[self goToPage:reactTag index:index animated:false];
4560
}
4661

62+
RCT_EXPORT_METHOD(setScrollEnabled
63+
: (nonnull NSNumber *)reactTag enabled
64+
: (nonnull NSNumber *)enabled) {
65+
BOOL isEnabled = [enabled boolValue];
66+
[self changeScrollEnabled:reactTag enabled:isEnabled];
67+
}
68+
4769
RCT_CUSTOM_VIEW_PROPERTY(scrollEnabled, BOOL, ReactNativePageView) {
4870
[view shouldScroll:[RCTConvert BOOL:json]];
4971
}

js/ViewPager.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ class ViewPager extends React.Component<ViewPagerProps> {
146146
);
147147
};
148148

149+
/**
150+
* A helper function to enable/disable scroll imperatively
151+
* The recommended way is using the scrollEnabled prop, however, there might be a case where a
152+
* imperative solution is more useful (e.g. for not blocking an animation)
153+
*/
154+
setScrollEnabled = (scrollEnabled: boolean) => {
155+
UIManager.dispatchViewManagerCommand(
156+
ReactNative.findNodeHandle(this),
157+
getViewManagerConfig(VIEW_MANAGER_NAME).Commands.setScrollEnabled,
158+
[scrollEnabled],
159+
);
160+
};
161+
149162
render() {
150163
return (
151164
<NativeViewPager

0 commit comments

Comments
 (0)