Skip to content

Commit

Permalink
refactor: migrate codebase to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed May 15, 2019
1 parent b15e96d commit 01ba66e
Show file tree
Hide file tree
Showing 45 changed files with 1,216 additions and 1,948 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ jobs:
- persist_to_workspace:
root: .
paths: .
lint-and-flow:
lint-and-typecheck:
<<: *defaults
steps:
- attach_workspace:
at: ~/project
- run: |
yarn run lint
yarn run flow
yarn run typescript
unit-tests:
<<: *defaults
steps:
Expand All @@ -55,6 +55,6 @@ workflows:
build-and-test:
jobs:
- install-dependencies
- lint-and-flow:
- lint-and-typecheck:
requires:
- install-dependencies
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
node_modules/
flow-typed/

# generated by bob
lib/
5 changes: 0 additions & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@

"env": {
"react-native-globals/all": true
},

"rules": {
"flowtype/no-dupe-keys": "off",
"flowtype/no-existential-type": "off"
}
}
127 changes: 0 additions & 127 deletions .flowconfig

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

# VSCode
.vscode/
tsconfig.json
jsconfig.json

# Xcode
Expand Down Expand Up @@ -48,3 +47,6 @@ buck-out/
\.buckd/
android/app/libs
android/keystores/debug.keystore

# generated by bob
lib/
4 changes: 0 additions & 4 deletions .prettierrc

This file was deleted.

6 changes: 6 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ A cross-platform Tab View component for React Native.
- Supports both top and bottom tab bars
- Follows Material Design spec
- Highly customizable
- Fully typed with [Flow](https://flow.org/)
- Fully typed with [TypeScript](https://typescriptlang.org)

## Demo

Expand Down Expand Up @@ -567,10 +567,10 @@ On Android, enabling `removeClippedSubviews` can improve memory usage. This opti

While developing, you can run the [example app](/example/README.md) to test your changes.

Make sure your code passes Flow and ESLint. Run the following to verify:
Make sure your code passes TypeScript and ESLint. Run the following to verify:

```sh
yarn flow
yarn typescript
yarn lint
```

Expand Down
2 changes: 1 addition & 1 deletion example/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"plugins": [
["module-resolver", {
"alias": {
"react-native-tab-view": "../src/index.js"
"react-native-tab-view": "../src/index"
}
}]
]
Expand Down
1 change: 1 addition & 0 deletions example/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './src/App.tsx';
2 changes: 1 addition & 1 deletion example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "tabviewexample",
"version": "0.0.1",
"private": true,
"main": "src/App.js",
"main": "index.js",
"scripts": {
"start": "expo start",
"android": "expo android",
Expand Down
69 changes: 35 additions & 34 deletions example/src/App.js → example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* @flow */

import { registerRootComponent, Asset, KeepAwake } from 'expo';
import * as React from 'react';
import {
Expand All @@ -21,16 +19,24 @@ import CustomTabBarExample from './CustomTabBarExample';
import CoverflowExample from './CoverflowExample';

type State = {
title: string,
index: number,
restoring: boolean,
title: string;
index: number;
restoring: boolean;
};

type ExampleComponentType = React.ComponentType<{}> & {
title: string;
tintColor?: string;
backgroundColor?: string;
statusBarStyle?: 'light-content' | 'dark-content';
appbarElevation?: number;
};

YellowBox.ignoreWarnings(['bind():']);

const PERSISTENCE_KEY = 'index_persistence';

const EXAMPLE_COMPONENTS = [
const EXAMPLE_COMPONENTS: ExampleComponentType[] = [
ScrollableTabBarExample,
TabBarIconExample,
CustomIndicatorExample,
Expand All @@ -47,7 +53,7 @@ export default class ExampleList extends React.Component<{}, State> {

componentDidMount() {
if (process.env.NODE_ENV !== 'production') {
this._restoreNavigationState();
this.restoreNavigationState();
}

[
Expand All @@ -62,21 +68,21 @@ export default class ExampleList extends React.Component<{}, State> {
].map(image => Asset.fromModule(image).downloadAsync());
}

_persistNavigationState = async (currentIndex: number) => {
private persistNavigationState = async (currentIndex: number) => {
if (process.env.NODE_ENV !== 'production') {
await AsyncStorage.setItem(PERSISTENCE_KEY, JSON.stringify(currentIndex));
}
};

_restoreNavigationState = async () => {
private restoreNavigationState = async () => {
this.setState({
restoring: true,
});

const savedIndexString = await AsyncStorage.getItem(PERSISTENCE_KEY);

try {
const savedIndex = JSON.parse(savedIndexString);
const savedIndex = JSON.parse(savedIndexString || '');

if (
Number.isFinite(savedIndex) &&
Expand All @@ -96,30 +102,28 @@ export default class ExampleList extends React.Component<{}, State> {
});
};

_handleNavigate = index => {
private handleNavigate = (index: number) => {
this.setState({
index,
});
this._persistNavigationState(index);
this.persistNavigationState(index);
};

_handleNavigateBack = () => {
this._handleNavigate(-1);
private handleNavigateBack = () => {
this.handleNavigate(-1);
};

_renderItem = (component, i) => {
return (
<TouchableOpacity
key={i}
style={styles.touchable}
onPress={() => this._handleNavigate(i)}
>
<Text style={styles.item}>
{i + 1}. {component.title}
</Text>
</TouchableOpacity>
);
};
private renderItem = (component: ExampleComponentType, i: number) => (
<TouchableOpacity
key={i}
style={styles.touchable}
onPress={() => this.handleNavigate(i)}
>
<Text style={styles.item}>
{i + 1}. {component.title}
</Text>
</TouchableOpacity>
);

render() {
if (this.state.restoring) {
Expand All @@ -138,7 +142,7 @@ export default class ExampleList extends React.Component<{}, State> {
? ExampleComponent.tintColor
: 'white';
const appbarElevation =
ExampleComponent && Number.isFinite(ExampleComponent.appbarElevation)
ExampleComponent && typeof ExampleComponent.appbarElevation === 'number'
? ExampleComponent.appbarElevation
: 4;
const statusBarStyle =
Expand All @@ -151,10 +155,7 @@ export default class ExampleList extends React.Component<{}, State> {
return (
<View style={styles.container}>
<StatusBar
barStyle={
/* $FlowFixMe */
Platform.OS === 'ios' ? statusBarStyle : 'light-content'
}
barStyle={Platform.OS === 'ios' ? statusBarStyle : 'light-content'}
/>
<KeepAwake />
<View
Expand All @@ -175,7 +176,7 @@ export default class ExampleList extends React.Component<{}, State> {
{index > -1 ? (
<TouchableOpacity
style={styles.button}
onPress={this._handleNavigateBack}
onPress={this.handleNavigateBack}
>
<Ionicons
name={
Expand All @@ -192,7 +193,7 @@ export default class ExampleList extends React.Component<{}, State> {
{index > -1 ? <View style={styles.button} /> : null}
</View>
{index === -1 ? (
<ScrollView>{EXAMPLE_COMPONENTS.map(this._renderItem)}</ScrollView>
<ScrollView>{EXAMPLE_COMPONENTS.map(this.renderItem)}</ScrollView>
) : ExampleComponent ? (
<ExampleComponent />
) : null}
Expand Down
Loading

0 comments on commit 01ba66e

Please sign in to comment.