-
-
Notifications
You must be signed in to change notification settings - Fork 141
/
StoryView.tsx
53 lines (45 loc) 路 1.44 KB
/
StoryView.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import React, { useState, useEffect } from 'react';
import { StoreItem } from '@storybook/client-api';
import { Text, View, StyleSheet } from 'react-native';
import { StoryContext } from '@storybook/addons';
interface Props {
story?: StoreItem;
}
const styles = StyleSheet.create({
container: { flex: 1 },
helpContainer: {
flex: 1,
paddingHorizontal: 16,
paddingVertical: 16,
alignItems: 'center',
justifyContent: 'center',
},
});
const StoryView = ({ story }: Props) => {
const [context, setContext] = useState<StoryContext | undefined>(undefined);
const id = story?.id;
useEffect(() => {
const loadContext = async () => {
if (story && story.unboundStoryFn && story.applyLoaders) {
setContext(await story.applyLoaders());
}
};
loadContext();
}, [story, id]);
if (story && story.unboundStoryFn) {
const { unboundStoryFn } = story;
const StoryComponent = (context && context.id === story.id) ? unboundStoryFn : null;
return (
<View key={id} testID={id} style={styles.container}>
{/* We need to get the result by the method of rendering a component, otherwise there will be errors if the react hooks are used */}
{ StoryComponent && <StoryComponent {...context} /> }
</View>
);
}
return (
<View style={styles.helpContainer}>
<Text>Please open navigator and select a story to preview.</Text>
</View>
);
};
export default StoryView;