-
Notifications
You must be signed in to change notification settings - Fork 957
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix(theme): pass wrapped component displayName to consumer components * feat(playground): react-navigation integration * refactor(ui): move sample component to playground
- Loading branch information
1 parent
0e38a4f
commit e6d030c
Showing
13 changed files
with
193 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +0,0 @@ | ||
import { StyledComponent } from '@rk-kit/theme'; | ||
import { Sample, Props } from './sample/sample.component'; | ||
|
||
const StyledSample = StyledComponent<Sample, Props>(Sample); | ||
|
||
export { | ||
StyledSample as Sample, | ||
Props as SampleProps, | ||
}; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export { | ||
withNavigation, | ||
Props as NavigatorProps, | ||
RouteType, | ||
} from './navigation.component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import { | ||
// @ts-ignore: createAppContainer is not exported | ||
createAppContainer, | ||
createStackNavigator, | ||
NavigationRouteConfigMap, | ||
} from 'react-navigation'; | ||
import hoistNonReactStatics from 'hoist-non-react-statics'; | ||
|
||
export interface RouteType { | ||
name: string; | ||
} | ||
|
||
export interface Props { | ||
routes?: RouteType[]; | ||
} | ||
|
||
const ROUTE_INITIAL = 'root'; | ||
|
||
export const withNavigation = <P extends object>(Root: React.ComponentType<P>, | ||
routes: NavigationRouteConfigMap) => { | ||
|
||
type WrapperProps = Props & P; | ||
|
||
class RootWrapper extends React.Component<WrapperProps> { | ||
|
||
getComponentName = (Component: React.ComponentType) => Component.displayName || Component.name; | ||
|
||
createCustomProps = (): Props => { | ||
const toRoute = (key: string): RouteType => ({ name: key }); | ||
return { | ||
routes: Object.keys(routes).filter(key => { | ||
const rootComponentName = this.getComponentName(Root); | ||
const componentName = this.getComponentName(routes[key]); | ||
return rootComponentName !== componentName; | ||
}).map(toRoute), | ||
}; | ||
}; | ||
|
||
render() { | ||
return ( | ||
<Root | ||
{...this.createCustomProps()} | ||
{...this.props} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
const routeConfig: NavigationRouteConfigMap = { | ||
[ROUTE_INITIAL]: RootWrapper, | ||
...routes, | ||
}; | ||
const stackConfig: NavigationRouteConfigMap = { | ||
initialRouteName: ROUTE_INITIAL, | ||
}; | ||
|
||
hoistNonReactStatics(RootWrapper, Root); | ||
|
||
// FIXME: | ||
// @ts-ignore: createAppContainer is not exported | ||
return createAppContainer(createStackNavigator(routeConfig, stackConfig)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { StyledComponent } from '@rk-kit/theme'; | ||
import { Sample, Props } from './sample.component'; | ||
|
||
const StyledSample = StyledComponent<Sample, Props>(Sample); | ||
|
||
export { | ||
StyledSample as Sample, | ||
Props as SampleProps, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from 'react'; | ||
import { NavigationScreenProps } from 'react-navigation'; | ||
import { | ||
FlatList, | ||
Text, | ||
TouchableOpacity, | ||
ListRenderItemInfo, | ||
} from 'react-native'; | ||
import { | ||
withStyles, | ||
ThemeType, | ||
ThemedComponentProps, | ||
} from '@rk-kit/theme'; | ||
import { | ||
NavigatorProps, | ||
RouteType, | ||
} from '../../navigation'; | ||
|
||
type Props = NavigatorProps & ThemedComponentProps & NavigationScreenProps; | ||
|
||
class Home extends React.Component<Props> { | ||
|
||
static navigationOptions = { | ||
title: 'Home', | ||
}; | ||
|
||
onItemPress = (route: RouteType) => { | ||
this.props.navigation.navigate(route.name); | ||
}; | ||
|
||
extractItemKey = (item: RouteType, index: number) => `${index}`; | ||
|
||
renderItem = (info: ListRenderItemInfo<any>) => ( | ||
<TouchableOpacity | ||
style={this.props.themedStyle.itemContainer} | ||
key={info.index} | ||
onPress={() => this.onItemPress(info.item)}> | ||
<Text style={this.props.themedStyle.itemText}>{info.item.name}</Text> | ||
</TouchableOpacity> | ||
); | ||
|
||
render() { | ||
return ( | ||
<FlatList | ||
keyExtractor={this.extractItemKey} | ||
style={this.props.themedStyle.container} | ||
data={this.props.routes} | ||
renderItem={this.renderItem} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
export const HomeScreen = withStyles(Home, (theme: ThemeType) => ({ | ||
container: { | ||
flex: 1, | ||
paddingHorizontal: 16, | ||
paddingVertical: 16, | ||
}, | ||
itemContainer: { | ||
paddingVertical: 8, | ||
}, | ||
itemText: { | ||
fontSize: 20, | ||
fontWeight: '500', | ||
}, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { HomeScreen } from './home.component'; | ||
export { SampleScreen } from './sample.component'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from 'react'; | ||
import { View } from 'react-native'; | ||
import { NavigationScreenProps } from 'react-navigation'; | ||
import { | ||
withStyles, | ||
ThemeType, | ||
ThemedComponentProps, | ||
} from '@rk-kit/theme'; | ||
import { Sample as SampleComponent } from '../component'; | ||
|
||
type Props = & ThemedComponentProps & NavigationScreenProps; | ||
|
||
class Sample extends React.Component<Props> { | ||
|
||
static navigationOptions = { | ||
title: 'Sample', | ||
}; | ||
|
||
render() { | ||
return ( | ||
<View style={this.props.themedStyle.container}> | ||
<SampleComponent variant='dark success'/> | ||
</View> | ||
); | ||
} | ||
} | ||
|
||
export const SampleScreen = withStyles(Sample, (theme: ThemeType) => ({ | ||
container: { | ||
flex: 1, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
})); |