Skip to content

Commit

Permalink
Fix react native server (#7187)
Browse files Browse the repository at this point in the history
Fix react native server
  • Loading branch information
shilman committed Jul 31, 2019
1 parent cbac4f3 commit 9774325
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 236 deletions.
5 changes: 1 addition & 4 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ lib/cli/test
scripts/storage
*.bundle.js
*.js.map
*.d.ts

!.remarkrc.js
!.babelrc.js
Expand All @@ -18,7 +19,3 @@ scripts/storage
!.jest.config.js
!.storybook

REACT_NATIVE
examples-native
react-native
ondevice-*
20 changes: 12 additions & 8 deletions addons/ondevice-actions/src/components/ActionLogger/Inspect.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable react/no-array-index-key */
/* eslint-disable no-nested-ternary */
import React from 'react';
import { Button, View, Text } from 'react-native';

Expand All @@ -22,6 +24,7 @@ const theme = {

class Inspect extends React.Component<{ name?: string; value: any }, { expanded: boolean }> {
state = { expanded: false };

render() {
const { name, value } = this.props;
const { expanded } = this.state;
Expand Down Expand Up @@ -52,7 +55,7 @@ class Inspect extends React.Component<{ name?: string; value: any }, { expanded:
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
{toggle}
{nameComponent}
<Text>{': ' + (value.length === 0 ? '[]' : expanded ? '[' : '[...]')}</Text>
<Text>{`: ${value.length === 0 ? '[]' : expanded ? '[' : '[...]'}`}</Text>
</View>
{expanded ? (
<View style={{ marginLeft: 40 }}>
Expand All @@ -62,7 +65,7 @@ class Inspect extends React.Component<{ name?: string; value: any }, { expanded:
</View>
))}
<View style={{ marginLeft: 20 }}>
<Text>{']'}</Text>
<Text>]</Text>
</View>
</View>
) : null}
Expand All @@ -71,13 +74,13 @@ class Inspect extends React.Component<{ name?: string; value: any }, { expanded:
}
return (
<View>
<Text>{'['}</Text>
<Text>[</Text>
{value.map((v, i) => (
<View key={i} style={{ marginLeft: 20 }}>
<Inspect value={v} />
</View>
))}
<Text>{']'}</Text>
<Text>]</Text>
</View>
);
}
Expand All @@ -89,7 +92,7 @@ class Inspect extends React.Component<{ name?: string; value: any }, { expanded:
{toggle}
{nameComponent}
<Text>
{': ' + (Object.keys(value).length === 0 ? '{}' : expanded ? '{' : '{...}')}
{`: ${Object.keys(value).length === 0 ? '{}' : expanded ? '{' : '{...}'}`}
</Text>
</View>
{expanded ? (
Expand Down Expand Up @@ -124,7 +127,7 @@ class Inspect extends React.Component<{ name?: string; value: any }, { expanded:
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
{toggle}
{nameComponent}
<Text>{': '}</Text>
<Text>: </Text>
<Value value={value} />
</View>
);
Expand All @@ -147,7 +150,7 @@ function Value({ value }: { value: any }) {
if (value instanceof RegExp) {
return (
<Text style={{ color: theme.OBJECT_VALUE_REGEXP_COLOR }}>
{'/' + value.source + '/' + value.flags}
{`/${value.source}/${value.flags}`}
</Text>
);
}
Expand All @@ -166,8 +169,9 @@ function Value({ value }: { value: any }) {
);
case 'function':
return <Text style={{ color: theme.OBJECT_VALUE_FUNCTION_PREFIX_COLOR }}>[Function]</Text>;
default:
return <Text>{JSON.stringify(value)}</Text>;
}
return <Text>{JSON.stringify(value)}</Text>;
}

export default Inspect;
9 changes: 5 additions & 4 deletions addons/ondevice-backgrounds/src/BackgroundPanel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react/prop-types, react/destructuring-assignment, import/no-extraneous-dependencies */
import React, { Component } from 'react';
import { View, Text } from 'react-native';
import Events from '@storybook/core-events';
Expand Down Expand Up @@ -36,10 +37,6 @@ const Instructions = () => (
);

export default class BackgroundPanel extends Component {
setBackgroundFromSwatch = background => {
this.props.channel.emit(Constants.UPDATE_BACKGROUND, background);
};

componentDidMount() {
this.props.channel.on(Events.SELECT_STORY, this.onStorySelected);
}
Expand All @@ -48,6 +45,10 @@ export default class BackgroundPanel extends Component {
this.props.channel.removeListener(Events.SELECT_STORY, this.onStorySelected);
}

setBackgroundFromSwatch = background => {
this.props.channel.emit(Constants.UPDATE_BACKGROUND, background);
};

onStorySelected = selection => {
this.setState({ selection });
};
Expand Down
4 changes: 2 additions & 2 deletions addons/ondevice-backgrounds/src/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export default class Container extends React.Component {
channel.removeListener(Constants.UPDATE_BACKGROUND, this.onBackgroundChange);
}

onBackgroundChange = (background) => {
onBackgroundChange = background => {
this.setState({ background });
}
};

render() {
const { background } = this.state;
Expand Down
8 changes: 4 additions & 4 deletions addons/ondevice-knobs/src/PropForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PropTypes from 'prop-types';
import { View } from 'react-native';
import PropField from './PropField';

export default class propForm extends React.Component {
export default class PropForm extends React.Component {
makeChangeHandler(name, type) {
return value => {
const { onFieldChange } = this.props;
Expand Down Expand Up @@ -38,13 +38,13 @@ export default class propForm extends React.Component {
}
}

propForm.displayName = 'propForm';
PropForm.displayName = 'PropForm';

propForm.defaultProps = {
PropForm.defaultProps = {
knobs: [],
};

propForm.propTypes = {
PropForm.propTypes = {
knobs: PropTypes.arrayOf(
PropTypes.shape({
name: PropTypes.string,
Expand Down
3 changes: 2 additions & 1 deletion addons/ondevice-notes/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// eslint-disable-next-line no-undef
if (__DEV__) {
console.log("import '@storybook/addon-ondevice-notes/register' to register the notes addon");
}
}
7 changes: 3 additions & 4 deletions addons/ondevice-notes/src/register.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/* eslint-disable react/prop-types */
/* eslint-disable react/destructuring-assignment */
/* eslint-disable import/no-extraneous-dependencies */
import React from 'react';
import { View } from 'react-native';
import Markdown from 'react-native-simple-markdown';
Expand All @@ -7,10 +10,6 @@ import Events from '@storybook/core-events';
export const PARAM_KEY = `notes`;

class Notes extends React.Component {
setBackgroundFromSwatch = background => {
this.props.channel.emit(Constants.UPDATE_BACKGROUND, background);
};

componentDidMount() {
this.props.channel.on(Events.SELECT_STORY, this.onStorySelected);
}
Expand Down
9 changes: 1 addition & 8 deletions app/react-native-server/src/client/manager/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ export default class ReactProvider extends Provider {
<Consumer filter={mapper} pure>
{({ storiesHash, storyId, api, viewMode }) => {
if (storiesHash[storyId]) {
const { kind, story } = storiesHash[storyId];
api.emit(Events.SET_CURRENT_STORY, { kind, story });
api.emit(Events.SET_CURRENT_STORY, { storyId });
}
return viewMode === 'story' ? <PreviewHelp /> : null;
}}
Expand All @@ -62,12 +61,6 @@ export default class ReactProvider extends Provider {

handleAPI(api) {
addons.loadAddons(api);
api.on(Events.STORY_CHANGED, () => {
api.emit(Events.SET_CURRENT_STORY, this.selection);
});
api.on(Events.GET_CURRENT_STORY, () => {
api.emit(Events.SET_CURRENT_STORY, this.selection);
});
api.emit(Events.GET_STORIES);
}
}
1 change: 1 addition & 0 deletions app/react-native/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prefer-destructuring */
import Preview from './preview';

const preview = new Preview();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ type Props = {
// To avoid issues we use absolute positioned element with predefined screen size
export default class AbsolutePositionedKeyboardAwareView extends PureComponent<Props> {
keyboardDidShowListener: EmitterSubscription;

keyboardDidHideListener: EmitterSubscription;

keyboardOpen: boolean;

componentWillMount() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NAVIGATOR, PREVIEW, ADDONS } from './navigation/constants';
import { Animated } from 'react-native';
import { NAVIGATOR, PREVIEW, ADDONS } from './navigation/constants';

const PREVIEW_SCALE = 0.3;

Expand Down
65 changes: 4 additions & 61 deletions app/react-native/src/preview/components/OnDeviceUI/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
TouchableOpacityProps,
} from 'react-native';
import styled from '@emotion/native';
import Events from '@storybook/core-events';
import addons from '@storybook/addons';
import Channel from '@storybook/channels';
import StoryListView from '../StoryListView';
Expand Down Expand Up @@ -36,14 +35,11 @@ interface OnDeviceUIProps {
url?: string;
tabOpen?: number;
isUIHidden?: boolean;
getInitialStory?: (...args: any[]) => any;
shouldDisableKeyboardAvoidingView?: boolean;
keyboardAvoidingViewVerticalOffset?: number;
}

interface OnDeviceUIState {
selection: any;
storyFn: any;
tabOpen: number;
slideBetweenAnimation: boolean;
previewWidth: number;
Expand All @@ -64,6 +60,7 @@ const Preview: typeof TouchableOpacity = styled.TouchableOpacity`

export default class OnDeviceUI extends PureComponent<OnDeviceUIProps, OnDeviceUIState> {
animatedValue: Animated.Value;

channel: Channel;

constructor(props: OnDeviceUIProps) {
Expand All @@ -72,33 +69,13 @@ export default class OnDeviceUI extends PureComponent<OnDeviceUIProps, OnDeviceU
this.state = {
tabOpen,
slideBetweenAnimation: false,
selection: {},
storyFn: null,
previewWidth: 0,
previewHeight: 0,
};
this.animatedValue = new Animated.Value(tabOpen);
this.channel = addons.getChannel();
}

async componentWillMount() {
const { getInitialStory } = this.props;
if (getInitialStory) {
const story = await getInitialStory();
this.setState({
selection: story || {},
storyFn: story ? story.storyFn : null,
});
}
this.channel.on(Events.SELECT_STORY, this.handleStoryChange);
this.channel.on(Events.FORCE_RE_RENDER, this.forceReRender);
}

componentWillUnmount() {
this.channel.removeListener(Events.SELECT_STORY, this.handleStoryChange);
this.channel.removeListener(Events.FORCE_RE_RENDER, this.forceReRender);
}

onLayout = ({ previewWidth, previewHeight }: PreviewDimens) => {
this.setState({ previewWidth, previewHeight });
};
Expand All @@ -107,24 +84,6 @@ export default class OnDeviceUI extends PureComponent<OnDeviceUIProps, OnDeviceU
this.handleToggleTab(PREVIEW);
};

forceReRender = () => {
this.forceUpdate();
};

handleStoryChange = (selection: any) => {
const { selection: prevSelection } = this.state;
if (selection.kind === prevSelection.kind && selection.story === prevSelection.story) {
this.handleToggleTab(PREVIEW);
}
this.setState({
selection: {
kind: selection.kind,
story: selection.story,
},
storyFn: selection.storyFn,
});
};

handleToggleTab = (newTabOpen: number) => {
const { tabOpen } = this.state;
if (newTabOpen === tabOpen) {
Expand Down Expand Up @@ -155,14 +114,7 @@ export default class OnDeviceUI extends PureComponent<OnDeviceUIProps, OnDeviceU
keyboardAvoidingViewVerticalOffset,
} = this.props;

const {
tabOpen,
slideBetweenAnimation,
selection,
storyFn,
previewWidth,
previewHeight,
} = this.state;
const { tabOpen, slideBetweenAnimation, previewWidth, previewHeight } = this.state;

const previewWrapperStyles = [
{ flex: 1 },
Expand Down Expand Up @@ -190,21 +142,12 @@ export default class OnDeviceUI extends PureComponent<OnDeviceUIProps, OnDeviceU
disabled={tabOpen === PREVIEW}
onPress={this.handleOpenPreview}
>
<StoryView
url={url}
selection={selection}
storyFn={storyFn}
listenToEvents={false}
/>
<StoryView url={url} onDevice stories={stories} />
</Preview>
</Animated.View>
</Animated.View>
<Panel style={getNavigatorPanelPosition(this.animatedValue, previewWidth)}>
<StoryListView
stories={stories}
selectedKind={selection.kind}
selectedStory={selection.story}
/>
<StoryListView stories={stories} />
</Panel>
<Panel style={getAddonPanelPosition(this.animatedValue, previewWidth)}>
<Addons />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react/destructuring-assignment */
import React, { PureComponent } from 'react';
import { View, SafeAreaView, StyleSheet } from 'react-native';
import GestureRecognizer, { GestureRecognizerConfig } from 'react-native-swipe-gestures';
Expand Down
Loading

0 comments on commit 9774325

Please sign in to comment.