Skip to content

Commit

Permalink
NavigatorIOS System Icon Support
Browse files Browse the repository at this point in the history
Summary:
Continuation on #3959 (started by cancan101). Rebased to latest master, adds support for left system icon, fixes issues mentioned in previous PR.

<img width="432" alt="screen shot 2016-09-05 at 13 54 41" src="https://cloud.githubusercontent.com/assets/7275322/18248478/5b7aa25c-7370-11e6-8c51-01a2b7fd1030.png">
Closes #9742

Differential Revision: D3836850

Pulled By: javache

fbshipit-source-id: ef9f6e42211ed18bf5f3dc1eb7a8b1318d939674
  • Loading branch information
jacobp100 authored and Facebook Github Bot 2 committed Sep 9, 2016
1 parent 7b18b2c commit 9e6e573
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 1 deletion.
35 changes: 35 additions & 0 deletions Examples/UIExplorer/js/NavigatorIOSExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ class NavigatorIOSExamplePage extends React.Component {
}
});
})}
{this._renderRow('Custom Right System Button', () => {
this.props.navigator.push({
title: NavigatorIOSExample.title,
component: EmptyPage,
rightButtonSystemIcon: 'bookmarks',
onRightButtonPress: () => this.props.navigator.pop(),
passProps: {
text: 'This page has a right system button in the nav bar',
}
});
})}
{this._renderRow('Custom Left & Right Icons', () => {
this.props.navigator.push({
title: NavigatorIOSExample.title,
Expand All @@ -114,6 +125,30 @@ class NavigatorIOSExamplePage extends React.Component {
}
});
})}
{this._renderRow('Custom Left & Right System Icons', () => {
this.props.navigator.push({
title: NavigatorIOSExample.title,
component: EmptyPage,
leftButtonSystemIcon: 'cancel',
onLeftButtonPress: () => this.props.navigator.pop(),
rightButtonSystemIcon: 'search',
onRightButtonPress: () => {
AlertIOS.alert(
'Bar Button Action',
'Recognized a tap on the bar button icon',
[
{
text: 'OK',
onPress: () => console.log('Tapped OK'),
},
]
);
},
passProps: {
text: 'This page has an icon for the right button in the nav bar',
}
});
})}
{this._renderRow('Pop', () => {
this.props.navigator.pop();
})}
Expand Down
49 changes: 49 additions & 0 deletions Libraries/Components/Navigation/NavigatorIOS.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var invariant = require('fbjs/lib/invariant');
var logError = require('logError');
var requireNativeComponent = require('requireNativeComponent');

const keyMirror = require('fbjs/lib/keyMirror');

var TRANSITIONER_REF = 'transitionerRef';

var PropTypes = React.PropTypes;
Expand All @@ -50,6 +52,34 @@ class NavigatorTransitionerIOS extends React.Component {
}
}

const SystemIconLabels = {
done: true,
cancel: true,
edit: true,
save: true,
add: true,
compose: true,
reply: true,
action: true,
organize: true,
bookmarks: true,
search: true,
refresh: true,
stop: true,
camera: true,
trash: true,
play: true,
pause: true,
rewind: true,
'fast-forward': true,
undo: true,
redo: true,
'page-curl': true,
};
const SystemIcons = keyMirror(SystemIconLabels);

type SystemButtonType = $Enum<typeof SystemIconLabels>;

type Route = {
component: Function,
title: string,
Expand All @@ -59,9 +89,11 @@ type Route = {
backButtonIcon?: Object,
leftButtonTitle?: string,
leftButtonIcon?: Object,
leftButtonSystemIcon?: SystemButtonType;
onLeftButtonPress?: Function,
rightButtonTitle?: string,
rightButtonIcon?: Object,
rightButtonSystemIcon?: SystemButtonType;
onRightButtonPress?: Function,
wrapperStyle?: any,
};
Expand Down Expand Up @@ -336,6 +368,16 @@ var NavigatorIOS = React.createClass({
*/
leftButtonTitle: PropTypes.string,

/**
* If set, the left header button will appear with this system icon
*
* Supported icons are `done`, `cancel`, `edit`, `save`, `add`,
* `compose`, `reply`, `action`, `organize`, `bookmarks`, `search`,
* `refresh`, `stop`, `camera`, `trash`, `play`, `pause`, `rewind`,
* `fast-forward`, `undo`, `redo`, and `page-curl`
*/
leftButtonSystemIcon: PropTypes.oneOf(Object.keys(SystemIcons)),

/**
* This function will be invoked when the left navigation bar item is
* pressed.
Expand All @@ -353,6 +395,13 @@ var NavigatorIOS = React.createClass({
*/
rightButtonTitle: PropTypes.string,

/**
* If set, the right header button will appear with this system icon
*
* See leftButtonSystemIcon for supported icons
*/
rightButtonSystemIcon: PropTypes.oneOf(Object.keys(SystemIcons)),

/**
* This function will be invoked when the right navigation bar item is
* pressed.
Expand Down
2 changes: 2 additions & 0 deletions React/Views/RCTNavItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
@property (nonatomic, strong) UIImage *titleImage;
@property (nonatomic, strong) UIImage *leftButtonIcon;
@property (nonatomic, copy) NSString *leftButtonTitle;
@property (nonatomic, assign) UIBarButtonSystemItem leftButtonSystemIcon;
@property (nonatomic, strong) UIImage *rightButtonIcon;
@property (nonatomic, copy) NSString *rightButtonTitle;
@property (nonatomic, assign) UIBarButtonSystemItem rightButtonSystemIcon;
@property (nonatomic, strong) UIImage *backButtonIcon;
@property (nonatomic, copy) NSString *backButtonTitle;
@property (nonatomic, assign) BOOL navigationBarHidden;
Expand Down
33 changes: 33 additions & 0 deletions React/Views/RCTNavItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ - (UIImageView *)titleImageView
}
}

-(instancetype)init
{
if (self = [super init]) {
_leftButtonSystemIcon = NSNotFound;
_rightButtonSystemIcon = NSNotFound;
}
return self;
}

- (void)setBackButtonTitle:(NSString *)backButtonTitle
{
_backButtonTitle = backButtonTitle;
Expand Down Expand Up @@ -68,6 +77,12 @@ - (void)setLeftButtonIcon:(UIImage *)leftButtonIcon
_leftButtonItem = nil;
}

- (void)setLeftButtonSystemIcon:(UIBarButtonSystemItem)leftButtonSystemIcon
{
_leftButtonSystemIcon = leftButtonSystemIcon;
_leftButtonItem = nil;
}

- (UIBarButtonItem *)leftButtonItem
{
if (!_leftButtonItem) {
Expand All @@ -84,6 +99,12 @@ - (UIBarButtonItem *)leftButtonItem
style:UIBarButtonItemStylePlain
target:self
action:@selector(handleLeftButtonPress)];

} else if (_leftButtonSystemIcon != NSNotFound) {
_leftButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:_leftButtonSystemIcon
target:self
action:@selector(handleLeftButtonPress)];
} else {
_leftButtonItem = nil;
}
Expand All @@ -110,6 +131,12 @@ - (void)setRightButtonIcon:(UIImage *)rightButtonIcon
_rightButtonItem = nil;
}

- (void)setRightButtonSystemIcon:(UIBarButtonSystemItem)rightButtonSystemIcon
{
_rightButtonSystemIcon = rightButtonSystemIcon;
_rightButtonItem = nil;
}

- (UIBarButtonItem *)rightButtonItem
{
if (!_rightButtonItem) {
Expand All @@ -126,6 +153,12 @@ - (UIBarButtonItem *)rightButtonItem
style:UIBarButtonItemStylePlain
target:self
action:@selector(handleRightButtonPress)];

} else if (_rightButtonSystemIcon != NSNotFound) {
_rightButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:_rightButtonSystemIcon
target:self
action:@selector(handleRightButtonPress)];
} else {
_rightButtonItem = nil;
}
Expand Down
9 changes: 8 additions & 1 deletion React/Views/RCTNavItemManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@

#import "RCTViewManager.h"

@interface RCTNavItemManager : RCTViewManager
#import "RCTConvert.h"

@interface RCTConvert (BarButtonSystemItem)

+ (UIBarButtonSystemItem)UIBarButtonSystemItem:(id)json;

@end

@interface RCTNavItemManager : RCTViewManager

@end
33 changes: 33 additions & 0 deletions React/Views/RCTNavItemManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,37 @@
#import "RCTConvert.h"
#import "RCTNavItem.h"

@implementation RCTConvert (BarButtonSystemItem)

RCT_ENUM_CONVERTER(UIBarButtonSystemItem, (@{
@"done": @(UIBarButtonSystemItemDone),
@"cancel": @(UIBarButtonSystemItemCancel),
@"edit": @(UIBarButtonSystemItemEdit),
@"save": @(UIBarButtonSystemItemSave),
@"add": @(UIBarButtonSystemItemAdd),
@"flexible-space": @(UIBarButtonSystemItemFlexibleSpace),
@"fixed-space": @(UIBarButtonSystemItemFixedSpace),
@"compose": @(UIBarButtonSystemItemCompose),
@"reply": @(UIBarButtonSystemItemReply),
@"action": @(UIBarButtonSystemItemAction),
@"organize": @(UIBarButtonSystemItemOrganize),
@"bookmarks": @(UIBarButtonSystemItemBookmarks),
@"search": @(UIBarButtonSystemItemSearch),
@"refresh": @(UIBarButtonSystemItemRefresh),
@"stop": @(UIBarButtonSystemItemStop),
@"camera": @(UIBarButtonSystemItemCamera),
@"trash": @(UIBarButtonSystemItemTrash),
@"play": @(UIBarButtonSystemItemPlay),
@"pause": @(UIBarButtonSystemItemPause),
@"rewind": @(UIBarButtonSystemItemRewind),
@"fast-forward": @(UIBarButtonSystemItemFastForward),
@"undo": @(UIBarButtonSystemItemUndo),
@"redo": @(UIBarButtonSystemItemRedo),
@"page-curl": @(UIBarButtonSystemItemPageCurl)
}), NSNotFound, integerValue);

@end

@implementation RCTNavItemManager

RCT_EXPORT_MODULE()
Expand All @@ -36,9 +67,11 @@ - (UIView *)view

RCT_EXPORT_VIEW_PROPERTY(leftButtonTitle, NSString)
RCT_EXPORT_VIEW_PROPERTY(leftButtonIcon, UIImage)
RCT_EXPORT_VIEW_PROPERTY(leftButtonSystemIcon, UIBarButtonSystemItem)

RCT_EXPORT_VIEW_PROPERTY(rightButtonIcon, UIImage)
RCT_EXPORT_VIEW_PROPERTY(rightButtonTitle, NSString)
RCT_EXPORT_VIEW_PROPERTY(rightButtonSystemIcon, UIBarButtonSystemItem)

RCT_EXPORT_VIEW_PROPERTY(onLeftButtonPress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onRightButtonPress, RCTBubblingEventBlock)
Expand Down

0 comments on commit 9e6e573

Please sign in to comment.