-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React Native - On Device UI #1413
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React, { PropTypes } from 'react'; | ||
import { View } from 'react-native'; | ||
import style from './style'; | ||
import StoryListView from '../StoryListView'; | ||
import StoryView from '../StoryView'; | ||
|
||
export default function OnDeviceUI(props) { | ||
const { | ||
stories, | ||
events, | ||
url | ||
} = props; | ||
|
||
return ( | ||
<View style={style.main}> | ||
<View style={style.leftPanel}> | ||
<StoryListView stories={stories} events={events} /> | ||
</View> | ||
<View style={style.rightPanel}> | ||
<View style={style.preview}> | ||
<StoryView url={url} events={events} /> | ||
</View> | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
OnDeviceUI.propTypes = { | ||
stories: PropTypes.shape({ | ||
dumpStoryBook: PropTypes.func.isRequired, | ||
on: PropTypes.func.isRequired, | ||
emit: PropTypes.func.isRequired, | ||
removeListener: PropTypes.func.isRequired, | ||
}).isRequired, | ||
events: PropTypes.shape({ | ||
on: PropTypes.func.isRequired, | ||
emit: PropTypes.func.isRequired, | ||
removeListener: PropTypes.func.isRequired, | ||
}).isRequired, | ||
url: PropTypes.string.isRequired, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { StyleSheet } from 'react-native'; | ||
|
||
export default { | ||
main: { | ||
flex: 1, | ||
flexDirection: 'row', | ||
paddingTop: 20, | ||
backgroundColor: 'rgba(247, 247, 247, 1)', | ||
}, | ||
leftPanel: { | ||
flex: 1, | ||
maxWidth: 250, | ||
paddingHorizontal: 8, | ||
paddingBottom: 8, | ||
}, | ||
rightPanel: { | ||
flex: 1, | ||
backgroundColor: 'rgba(255, 255, 255, 1)', | ||
borderWidth: StyleSheet.hairlineWidth, | ||
borderColor: 'rgba(236, 236, 236, 1)', | ||
borderRadius: 4, | ||
marginBottom: 8, | ||
marginHorizontal: 8, | ||
}, | ||
preview: { | ||
...StyleSheet.absoluteFillObject, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import React, { Component, PropTypes } from 'react'; | ||
import { SectionList, View, Text, TouchableOpacity } from 'react-native'; | ||
import style from './style'; | ||
|
||
const SectionHeader = ({ title, selected }) => ( | ||
<View key={title} style={style.header}> | ||
<Text style={[style.headerText, selected && style.headerTextSelected]}> | ||
{title} | ||
</Text> | ||
</View> | ||
); | ||
|
||
SectionHeader.propTypes = { | ||
title: PropTypes.string.isRequired, | ||
selected: PropTypes.bool.isRequired, | ||
}; | ||
|
||
const ListItem = ({ title, selected, onPress }) => ( | ||
<TouchableOpacity | ||
key={title} | ||
style={style.item} | ||
onPress={onPress} | ||
> | ||
<Text style={[style.itemText, selected && style.itemTextSelected]}> | ||
{title} | ||
</Text> | ||
</TouchableOpacity> | ||
); | ||
|
||
ListItem.propTypes = { | ||
title: PropTypes.string.isRequired, | ||
onPress: PropTypes.func.isRequired, | ||
selected: PropTypes.bool.isRequired, | ||
}; | ||
|
||
export default class StoryListView extends Component { | ||
constructor(props, ...args) { | ||
super(props, ...args); | ||
this.state = { | ||
sections: [], | ||
selectedKind: null, | ||
selectedStory: null, | ||
}; | ||
|
||
this.storyAddedHandler = this.handleStoryAdded.bind(this); | ||
this.storyChangedHandler = this.handleStoryChanged.bind(this); | ||
this.changeStoryHandler = this.changeStory.bind(this); | ||
|
||
this.props.stories.on('storyAdded', this.storyAddedHandler); | ||
this.props.events.on('story', this.storyChangedHandler); | ||
} | ||
|
||
componentDidMount() { | ||
this.handleStoryAdded(); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.props.stories.removeListener('storyAdded', this.storiesHandler); | ||
this.props.events.removeListener('story', this.storyChangedHandler); | ||
} | ||
|
||
handleStoryAdded() { | ||
if (this.props.stories) { | ||
const data = this.props.stories.dumpStoryBook(); | ||
this.setState({ | ||
sections: data.map((section) => ({ | ||
key: section.kind, | ||
title: section.kind, | ||
data: section.stories.map((story) => ({ | ||
key: story, | ||
kind: section.kind, | ||
name: story | ||
})) | ||
})) | ||
}); | ||
} | ||
} | ||
|
||
handleStoryChanged(storyFn, selection) { | ||
const { kind, story } = selection; | ||
this.setState({ | ||
selectedKind: kind, | ||
selectedStory: story | ||
}); | ||
} | ||
|
||
changeStory(kind, story) { | ||
this.props.events.emit('setCurrentStory', { kind, story }); | ||
} | ||
|
||
render() { | ||
return ( | ||
<SectionList | ||
style={style.list} | ||
renderItem={({ item }) => ( | ||
<ListItem | ||
title={item.name} | ||
selected={item.kind === this.state.selectedKind && item.name === this.state.selectedStory} | ||
onPress={() => this.changeStory(item.kind, item.name)} | ||
/> | ||
)} | ||
renderSectionHeader={({ section }) => ( | ||
<SectionHeader | ||
title={section.title} | ||
selected={section.title === this.state.selectedKind} | ||
/> | ||
)} | ||
sections={this.state.sections} | ||
stickySectionHeadersEnabled={false} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
StoryListView.propTypes = { | ||
stories: PropTypes.shape({ | ||
dumpStoryBook: PropTypes.func.isRequired, | ||
on: PropTypes.func.isRequired, | ||
emit: PropTypes.func.isRequired, | ||
removeListener: PropTypes.func.isRequired, | ||
}).isRequired, | ||
events: PropTypes.shape({ | ||
on: PropTypes.func.isRequired, | ||
emit: PropTypes.func.isRequired, | ||
removeListener: PropTypes.func.isRequired, | ||
}).isRequired, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export default { | ||
list: { | ||
flex: 1, | ||
maxWidth: 250, | ||
}, | ||
header: { | ||
paddingTop: 24, | ||
paddingBottom: 4, | ||
}, | ||
headerText: { | ||
fontSize: 16, | ||
}, | ||
headerTextSelected: { | ||
fontWeight: 'bold', | ||
}, | ||
item: { | ||
paddingVertical: 4, | ||
paddingHorizontal: 16, | ||
}, | ||
itemText: { | ||
fontSize: 14, | ||
}, | ||
itemTextSelected: { | ||
fontWeight: 'bold', | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,7 +25,7 @@ | |
2D02E4BC1E0B4A80006451C7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.m */; }; | ||
2D02E4BD1E0B4A84006451C7 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; }; | ||
2D02E4BF1E0B4AB3006451C7 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; }; | ||
2D02E4C21E0B4AEC006451C7 /* libRCTAnimation-tvOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5E9157351DD0AC6500FF2AA8 /* libRCTAnimation-tvOS.a */; }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know we probably don't need this right now, but since all the other There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a change which Xcode made when I changed the example to be universal rather than iPhone only. Looking at it there are two targets, one for the phone and another for tvOS. The tvOS one includes I think for now we should go with what Xcode has changed it to to avoid commit noise in the future. It can always be fixed if there is an issue if/when tvOS support is added to Storybook. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the explanation! Sounds good 👍 |
||
2D02E4C21E0B4AEC006451C7 /* libRCTAnimation.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5E9157351DD0AC6500FF2AA8 /* libRCTAnimation.a */; }; | ||
2D02E4C31E0B4AEC006451C7 /* libRCTImage-tvOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3DAD3E841DF850E9000B6D8A /* libRCTImage-tvOS.a */; }; | ||
2D02E4C41E0B4AEC006451C7 /* libRCTLinking-tvOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3DAD3E881DF850E9000B6D8A /* libRCTLinking-tvOS.a */; }; | ||
2D02E4C51E0B4AEC006451C7 /* libRCTNetwork-tvOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3DAD3E8C1DF850E9000B6D8A /* libRCTNetwork-tvOS.a */; }; | ||
|
@@ -289,7 +289,7 @@ | |
buildActionMask = 2147483647; | ||
files = ( | ||
2D02E4C91E0B4AEC006451C7 /* libReact.a in Frameworks */, | ||
2D02E4C21E0B4AEC006451C7 /* libRCTAnimation-tvOS.a in Frameworks */, | ||
2D02E4C21E0B4AEC006451C7 /* libRCTAnimation.a in Frameworks */, | ||
2D02E4C31E0B4AEC006451C7 /* libRCTImage-tvOS.a in Frameworks */, | ||
2D02E4C41E0B4AEC006451C7 /* libRCTLinking-tvOS.a in Frameworks */, | ||
2D02E4C51E0B4AEC006451C7 /* libRCTNetwork-tvOS.a in Frameworks */, | ||
|
@@ -419,7 +419,7 @@ | |
isa = PBXGroup; | ||
children = ( | ||
5E9157331DD0AC6500FF2AA8 /* libRCTAnimation.a */, | ||
5E9157351DD0AC6500FF2AA8 /* libRCTAnimation-tvOS.a */, | ||
5E9157351DD0AC6500FF2AA8 /* libRCTAnimation.a */, | ||
); | ||
name = Products; | ||
sourceTree = "<group>"; | ||
|
@@ -804,10 +804,10 @@ | |
remoteRef = 5E9157321DD0AC6500FF2AA8 /* PBXContainerItemProxy */; | ||
sourceTree = BUILT_PRODUCTS_DIR; | ||
}; | ||
5E9157351DD0AC6500FF2AA8 /* libRCTAnimation-tvOS.a */ = { | ||
5E9157351DD0AC6500FF2AA8 /* libRCTAnimation.a */ = { | ||
isa = PBXReferenceProxy; | ||
fileType = archive.ar; | ||
path = "libRCTAnimation-tvOS.a"; | ||
path = libRCTAnimation.a; | ||
remoteRef = 5E9157341DD0AC6500FF2AA8 /* PBXContainerItemProxy */; | ||
sourceTree = BUILT_PRODUCTS_DIR; | ||
}; | ||
|
@@ -1006,6 +1006,7 @@ | |
"-lc++", | ||
); | ||
PRODUCT_NAME = ReactNativeVanilla; | ||
TARGETED_DEVICE_FAMILY = "1,2"; | ||
VERSIONING_SYSTEM = "apple-generic"; | ||
}; | ||
name = Debug; | ||
|
@@ -1023,6 +1024,7 @@ | |
"-lc++", | ||
); | ||
PRODUCT_NAME = ReactNativeVanilla; | ||
TARGETED_DEVICE_FAMILY = "1,2"; | ||
VERSIONING_SYSTEM = "apple-generic"; | ||
}; | ||
name = Release; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how
removeStory
works/becomes called, but do we want to add an emitter for that as well for ourStoryListView
to subscribe to?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hadn't spotted the remove method. I can't think of a situation when that would be used...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neither can I - I'm not familiar enough with that part; cc @ndelangen