Skip to content
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

refactor(playground): update playground to use typescript #246

Merged
merged 1 commit into from
Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions src/playground/src/app.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,19 @@ interface State {
theme: ThemeType;
}

export default class App extends React.Component<any, State> {
type Props = any;

constructor(props) {
super(props);
this.state = {
mapping: mapping,
styles: style,
theme: theme,
};
}
export default class App extends React.Component<Props, State> {

public state: State = {
mapping: mapping,
styles: style,
theme: theme,
};

render() {
const { RadioScreen: RootScreen, ...nestedScreens } = Screens;
const Router = withNavigation(RootScreen, nestedScreens);
public render(): React.ReactNode {
const { RadioScreen: RootScreen, ...screens } = Screens;
const Router: React.ComponentClass = withNavigation(RootScreen, screens);

return (
<StyleProvider styles={this.state.styles} theme={this.state.theme} mapping={mapping}>
Expand Down
31 changes: 19 additions & 12 deletions src/playground/src/navigation/navigation.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,38 @@ export interface Props {

const ROUTE_INITIAL = 'root';

export const withNavigation = <P extends object>(Root: React.ComponentType<P>,
routes: NavigationRouteConfigMap) => {
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;
private getComponentName = (Component: React.ComponentType): string => {
return Component.displayName || Component.name;
};

private isComponentRoute = (name: string): boolean => {
const rootRouteName: string = this.getComponentName(Root);
const componentRouteName: string = this.getComponentName(routes[name]);

return componentRouteName !== rootRouteName;
};

createCustomProps = (): Props => {
const toRoute = (key: string): RouteType => ({ name: key });
private createRoute = (name: string): RouteType => {
return {
routes: Object.keys(routes).filter(key => {
const rootComponentName = this.getComponentName(Root);
const componentName = this.getComponentName(routes[key]);
return rootComponentName !== componentName;
}).map(toRoute),
name: name,
};
};

render() {
private createRoutes = (): RouteType[] => {
return Object.keys(routes).filter(this.isComponentRoute).map(this.createRoute);
};

public render(): React.ReactNode {
return (
<Root
{...this.createCustomProps()}
{...this.props}
routes={this.createRoutes()}
/>
);
}
Expand Down
25 changes: 14 additions & 11 deletions src/playground/src/ui/screen/home.component.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import {
ThemedComponentProps,
ThemeType,
withStyles,
} from '@kitten/theme';
import React from 'react';
import { NavigationScreenProps } from 'react-navigation';
import {
FlatList,
ListRenderItemInfo,
Text,
TouchableOpacity,
ListRenderItemInfo,
TouchableOpacityProps,
} from 'react-native';
import {
withStyles,
ThemeType,
ThemedComponentProps,
} from '@kitten/theme';
import { NavigationScreenProps } from 'react-navigation';
import {
NavigatorProps,
RouteType,
Expand All @@ -24,13 +25,15 @@ class Home extends React.Component<Props> {
title: 'Home',
};

onItemPress = (route: RouteType) => {
private onItemPress = (route: RouteType) => {
this.props.navigation.navigate(route.name);
};

extractItemKey = (item: RouteType, index: number) => `${index}`;
private extractItemKey = (item: RouteType, index: number): string => {
return index.toString();
};

renderItem = (info: ListRenderItemInfo<any>) => (
private renderItem = (info: ListRenderItemInfo<RouteType>): React.ReactElement<TouchableOpacityProps> => (
<TouchableOpacity
style={this.props.themedStyle.itemContainer}
key={info.index}
Expand All @@ -39,7 +42,7 @@ class Home extends React.Component<Props> {
</TouchableOpacity>
);

render() {
public render(): React.ReactNode {
return (
<FlatList
keyExtractor={this.extractItemKey}
Expand Down
14 changes: 6 additions & 8 deletions src/playground/src/ui/screen/radio.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ interface State {
isRadio2Checked: boolean;
isRadio3Checked: boolean;
isRadio4Checked: boolean;
variant: string;
}

class Radio extends React.Component<Props, State> {
Expand All @@ -27,31 +26,30 @@ class Radio extends React.Component<Props, State> {
title: 'Radio',
};

state: State = {
public state: State = {
isRadio1Checked: false,
isRadio2Checked: true,
isRadio3Checked: false,
isRadio4Checked: true,
variant: 'default',
};

onRadio1Change = (selected: boolean) => {
private onRadio1Change = (selected: boolean) => {
this.setState({ isRadio1Checked: !selected });
};

onRadio2Change = (selected: boolean) => {
private onRadio2Change = (selected: boolean) => {
this.setState({ isRadio2Checked: !selected });
};

onRadio3Change = (selected: boolean) => {
private onRadio3Change = (selected: boolean) => {
this.setState({ isRadio3Checked: !selected });
};

onRadio4Change = (selected: boolean) => {
private onRadio4Change = (selected: boolean) => {
this.setState({ isRadio4Checked: !selected });
};

render() {
public render(): React.ReactNode {
return (
<View style={this.props.themedStyle.container}>
<View style={this.props.themedStyle.containerSection}>
Expand Down
4 changes: 2 additions & 2 deletions src/playground/src/ui/screen/radioGroup.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class RadioGroup extends React.Component<Props, State> {
title: 'Radio Group',
};

state: State = {
public state: State = {
selectedIndexGroup1: 0,
selectedIndexGroup2: 0,
selectedIndexGroup3: 0,
Expand All @@ -52,7 +52,7 @@ class RadioGroup extends React.Component<Props, State> {
});
};

render() {
public render(): React.ReactNode {
return (
<View style={this.props.themedStyle.container}>
<View style={this.props.themedStyle.containerSection}>
Expand Down