react-native/Examples/UIExplorer/UIExplorerApp.ios.js
Christine Abernathy 09958618c5 Adding emulator, simulator UIExplorer example across docs
Summary:Current docs show an Appetize.io example for AlertIOS doc. This pull request adds that feature across all applicable iOS and Android docs. So if a doc has an example in UIExplorer, it shows up in the top right and clicking to Play should navigate to the relevant example.

The changes here also touched NavigationExperimental to fix a typo that prevented iOS deep link from working. Code was also added to help support Android deep links but there's an outstanding issue (a race condition) around how Android deep links trigger getInitialURL in NavigationRootContainer that prevents this from fully working.

For adding the docs, a few things were done outside this pull request:

1/ Release builds for UIExplorer Android and iOS apps were uploaded to Appetize.io. The Appetize.io info (public key to run the build) is embedded in the docs.
2/ The iOS build was generated by making a few changes to get a local bundle. The current UIExplorer set up doesn't support "react-native run-ios".

Regarding the Appetize bu
Closes https://github.com/facebook/react-native/pull/6306

Differential Revision: D3129651

Pulled By: bestander

fb-gh-sync-id: d296d64db8236faa36f35484bb6b362990caf934
fbshipit-source-id: d296d64db8236faa36f35484bb6b362990caf934
2016-04-04 06:05:23 -07:00

211 lines
6.4 KiB
JavaScript

/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* The examples provided by Facebook are for non-commercial testing and
* evaluation purposes only.
*
* Facebook reserves all rights not expressly granted.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL
* FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @providesModule UIExplorerApp
* @flow
*/
'use strict';
const React = require('react-native');
const UIExplorerList = require('./UIExplorerList.ios');
const UIExplorerExampleList = require('./UIExplorerExampleList');
const UIExplorerNavigationReducer = require('./UIExplorerNavigationReducer');
const UIExplorerStateTitleMap = require('./UIExplorerStateTitleMap');
const URIActionMap = require('./URIActionMap');
const {
AppRegistry,
NavigationExperimental,
SnapshotViewIOS,
StyleSheet,
View,
} = React;
const {
CardStack: NavigationCardStack,
Header: NavigationHeader,
RootContainer: NavigationRootContainer,
} = NavigationExperimental;
import type { NavigationSceneRendererProps } from 'NavigationTypeDefinition';
import type { UIExplorerNavigationState } from './UIExplorerNavigationReducer';
import type { UIExplorerExample } from './UIExplorerList.ios';
type Props = {
exampleFromAppetizeParams: string,
};
type State = {
initialExampleUri: ?string,
};
class UIExplorerApp extends React.Component {
_navigationRootRef: ?NavigationRootContainer;
_renderNavigation: Function;
_renderOverlay: Function;
_renderScene: Function;
_renderCard: Function;
_renderTitleComponent: Function;
_handleOpenInitialExample: Function;
state: State;
constructor(props: Props) {
super(props);
this._handleOpenInitialExample = this._handleOpenInitialExample.bind(this);
this.state = {
initialExampleUri: props.exampleFromAppetizeParams,
};
}
componentWillMount() {
this._renderNavigation = this._renderNavigation.bind(this);
this._renderOverlay = this._renderOverlay.bind(this);
this._renderScene = this._renderScene.bind(this);
this._renderTitleComponent = this._renderTitleComponent.bind(this);
}
componentDidMount() {
// There's a race condition if we try to navigate to the specified example
// from the initial props at the same time the navigation logic is setting
// up the initial navigation state. This hack adds a delay to avoid this
// scenario. So after the initial example list is shown, we then transition
// to the initial example.
setTimeout(this._handleOpenInitialExample, 500);
}
render() {
return (
<NavigationRootContainer
persistenceKey="UIExplorerState"
reducer={UIExplorerNavigationReducer}
ref={navRootRef => { this._navigationRootRef = navRootRef; }}
renderNavigation={this._renderNavigation}
linkingActionMap={URIActionMap}
/>
);
}
_handleOpenInitialExample() {
if (this.state.initialExampleUri) {
const exampleAction = URIActionMap(this.state.initialExampleUri);
if (exampleAction && this._navigationRootRef) {
this._navigationRootRef.handleNavigation(exampleAction);
}
}
this.setState({initialExampleUri: null});
}
_renderNavigation(navigationState: UIExplorerNavigationState, onNavigate: Function) {
if (!navigationState) {
return null;
}
if (navigationState.externalExample) {
var Component = UIExplorerList.Modules[navigationState.externalExample];
return (
<Component
onExampleExit={() => {
onNavigate(NavigationRootContainer.getBackAction());
}}
/>
);
}
const {stack} = navigationState;
return (
<NavigationCardStack
navigationState={stack}
style={styles.container}
renderOverlay={this._renderOverlay}
renderScene={this._renderScene}
/>
);
}
_renderOverlay(props: NavigationSceneRendererProps): ReactElement {
return (
<NavigationHeader
{...props}
renderTitleComponent={this._renderTitleComponent}
/>
);
}
_renderTitleComponent(props: NavigationSceneRendererProps): ReactElement {
return (
<NavigationHeader.Title>
{UIExplorerStateTitleMap(props.scene.navigationState)}
</NavigationHeader.Title>
);
}
_renderScene(props: NavigationSceneRendererProps): ?ReactElement {
const state = props.scene.navigationState;
if (state.key === 'AppList') {
return (
<UIExplorerExampleList
list={UIExplorerList}
style={styles.exampleContainer}
{...state}
/>
);
}
const Example = UIExplorerList.Modules[state.key];
if (Example) {
const Component = UIExplorerExampleList.makeRenderable(Example);
return (
<View style={styles.exampleContainer}>
<Component />
</View>
);
}
return null;
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
exampleContainer: {
flex: 1,
paddingTop: NavigationHeader.HEIGHT,
},
});
AppRegistry.registerComponent('SetPropertiesExampleApp', () => require('./SetPropertiesExampleApp'));
AppRegistry.registerComponent('RootViewSizeFlexibilityExampleApp', () => require('./RootViewSizeFlexibilityExampleApp'));
AppRegistry.registerComponent('UIExplorerApp', () => UIExplorerApp);
// Register suitable examples for snapshot tests
UIExplorerList.ComponentExamples.concat(UIExplorerList.APIExamples).forEach((Example: UIExplorerExample) => {
const ExampleModule = Example.module;
if (ExampleModule.displayName) {
var Snapshotter = React.createClass({
render: function() {
var Renderable = UIExplorerExampleList.makeRenderable(ExampleModule);
return (
<SnapshotViewIOS>
<Renderable />
</SnapshotViewIOS>
);
},
});
AppRegistry.registerComponent(ExampleModule.displayName, () => Snapshotter);
}
});
module.exports = UIExplorerApp;