From d211359aeba005f30643d66d0dd1d212db1307e4 Mon Sep 17 00:00:00 2001 From: Georgiy Kassabli Date: Wed, 20 May 2015 08:33:16 -0700 Subject: [PATCH] Added ability to set custom accessibility tap handler to React Native --- Libraries/Components/View/View.js | 6 ++++++ .../ReactNative/ReactNativeViewAttributes.js | 1 + React/Modules/RCTUIManager.m | 3 +++ React/Views/RCTView.h | 5 +++-- React/Views/RCTView.m | 10 ++++++++++ React/Views/RCTViewManager.m | 16 ++++++++++++---- 6 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Libraries/Components/View/View.js b/Libraries/Components/View/View.js index 568ae43fd..c6a279a22 100644 --- a/Libraries/Components/View/View.js +++ b/Libraries/Components/View/View.js @@ -99,6 +99,12 @@ var View = React.createClass({ PropTypes.arrayOf(PropTypes.oneOf(AccessibilityTraits)), ]), + /** + * When `accessible` is true, the system will try to invoke this function + * when the user performs accessibility tap gesture. + */ + onAcccessibilityTap: PropTypes.func, + /** * When `accessible` is true, the system will invoke this function when the * user performs the magic tap gesture. diff --git a/Libraries/ReactNative/ReactNativeViewAttributes.js b/Libraries/ReactNative/ReactNativeViewAttributes.js index 7fca115df..50b839e1d 100644 --- a/Libraries/ReactNative/ReactNativeViewAttributes.js +++ b/Libraries/ReactNative/ReactNativeViewAttributes.js @@ -22,6 +22,7 @@ ReactNativeViewAttributes.UIView = { accessibilityTraits: true, testID: true, onLayout: true, + onAccessibilityTap: true, onMagicTap: true, }; diff --git a/React/Modules/RCTUIManager.m b/React/Modules/RCTUIManager.m index 51b37635a..e9734c204 100644 --- a/React/Modules/RCTUIManager.m +++ b/React/Modules/RCTUIManager.m @@ -1309,6 +1309,9 @@ RCT_EXPORT_METHOD(clearJSResponder) @"topLoadingError": @{ @"registrationName": @"onLoadingError" }, + @"topAccessibilityTap": @{ + @"registrationName": @"onAccessibilityTap" + }, @"topMagicTap": @{ @"registrationName": @"onMagicTap" }, diff --git a/React/Views/RCTView.h b/React/Views/RCTView.h index 7335605eb..51d060ca3 100644 --- a/React/Views/RCTView.h +++ b/React/Views/RCTView.h @@ -16,11 +16,12 @@ @protocol RCTAutoInsetsProtocol; @class RCTView; -typedef void (^RCTViewMagicTapHandler)(RCTView *view); +typedef void (^RCTViewEventHandler)(RCTView *view); @interface RCTView : UIView -@property (nonatomic, copy) RCTViewMagicTapHandler magicTapHandler; +@property (nonatomic, copy) RCTViewEventHandler accessibilityTapHandler; +@property (nonatomic, copy) RCTViewEventHandler magicTapHandler; /** * Used to control how touch events are processed. diff --git a/React/Views/RCTView.m b/React/Views/RCTView.m index 675b3b8be..d6394f2c6 100644 --- a/React/Views/RCTView.m +++ b/React/Views/RCTView.m @@ -167,6 +167,16 @@ static NSString *RCTRecursiveAccessibilityLabel(UIView *view) } } +- (BOOL)accessibilityActivate +{ + if (self.accessibilityTapHandler) { + self.accessibilityTapHandler(self); + return YES; + } else { + return NO; + } +} + - (BOOL)accessibilityPerformMagicTap { if (self.magicTapHandler) { diff --git a/React/Views/RCTViewManager.m b/React/Views/RCTViewManager.m index eff68f05f..8230e398d 100644 --- a/React/Views/RCTViewManager.m +++ b/React/Views/RCTViewManager.m @@ -172,18 +172,26 @@ RCT_CUSTOM_VIEW_PROPERTY(borderWidth, CGFloat, RCTView) view.layer.borderWidth = json ? [RCTConvert CGFloat:json] : defaultView.layer.borderWidth; } } +RCT_CUSTOM_VIEW_PROPERTY(onAccessibilityTap, BOOL, RCTView) +{ + view.accessibilityTapHandler = [self eventHandlerWithName:@"topAccessibilityTap" json:json]; +} RCT_CUSTOM_VIEW_PROPERTY(onMagicTap, BOOL, RCTView) { - RCTViewMagicTapHandler handler = nil; + view.magicTapHandler = [self eventHandlerWithName:@"topMagicTap" json:json]; +} + +- (RCTViewEventHandler)eventHandlerWithName:(NSString *)eventName json:(id)json +{ + RCTViewEventHandler handler = nil; if ([RCTConvert BOOL:json]) { __weak RCTViewManager *weakSelf = self; handler = ^(RCTView *tappedView) { NSDictionary *body = @{ @"target": tappedView.reactTag }; - [weakSelf.bridge.eventDispatcher sendInputEventWithName:@"topMagicTap" body:body]; + [weakSelf.bridge.eventDispatcher sendInputEventWithName:eventName body:body]; }; } - - view.magicTapHandler = handler; + return handler; } #define RCT_VIEW_BORDER_PROPERTY(SIDE) \