Added ability to set custom accessibility tap handler to React Native

This commit is contained in:
Georgiy Kassabli 2015-05-20 08:33:16 -07:00
parent 32666f0aa2
commit d211359aeb
6 changed files with 35 additions and 6 deletions

View File

@ -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.

View File

@ -22,6 +22,7 @@ ReactNativeViewAttributes.UIView = {
accessibilityTraits: true,
testID: true,
onLayout: true,
onAccessibilityTap: true,
onMagicTap: true,
};

View File

@ -1309,6 +1309,9 @@ RCT_EXPORT_METHOD(clearJSResponder)
@"topLoadingError": @{
@"registrationName": @"onLoadingError"
},
@"topAccessibilityTap": @{
@"registrationName": @"onAccessibilityTap"
},
@"topMagicTap": @{
@"registrationName": @"onMagicTap"
},

View File

@ -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.

View File

@ -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) {

View File

@ -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) \