Skip to content

Commit 91d5a71

Browse files
committed
Example library is added. Need to test
1 parent bc489dd commit 91d5a71

File tree

8 files changed

+511
-2
lines changed

8 files changed

+511
-2
lines changed

.eslintrc.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
module.exports = {
2+
root: true,
3+
extends: [
4+
"@react-native-community",
5+
"airbnb-typescript",
6+
"prettier",
7+
"prettier/@typescript-eslint",
8+
"prettier/react"
9+
],
210
parser: "babel-eslint",
3-
extends: "airbnb",
411
plugins: ["react", "react-native"],
512
env: {
613
jest: true,
@@ -11,7 +18,7 @@ module.exports = {
1118
"react/jsx-filename-extension": [
1219
"error",
1320
{
14-
extensions: [".js", ".jsx"]
21+
extensions: [".js", ".jsx", ".tsx", ".ts"]
1522
}
1623
],
1724
// for post defining style object in react-native

lib/FunctionalComponent/index.tsx

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React from "react";
2+
import { Button, StyleSheet, Text, View } from "react-native";
3+
4+
export interface Props {
5+
name: string;
6+
enthusiasmLevel?: number;
7+
}
8+
9+
interface State {
10+
enthusiasmLevel: number;
11+
}
12+
13+
export class Hello extends React.Component<Props, State> {
14+
constructor(props: Props) {
15+
super(props);
16+
17+
if ((props.enthusiasmLevel || 0) <= 0) {
18+
throw new Error("You could be a little more enthusiastic. :D");
19+
}
20+
21+
this.state = {
22+
enthusiasmLevel: props.enthusiasmLevel || 1
23+
};
24+
}
25+
26+
onIncrement = () =>
27+
this.setState({ enthusiasmLevel: this.state.enthusiasmLevel + 1 });
28+
onDecrement = () =>
29+
this.setState({ enthusiasmLevel: this.state.enthusiasmLevel - 1 });
30+
getExclamationMarks = (numChars: number) => Array(numChars + 1).join("!");
31+
32+
render() {
33+
return (
34+
<View style={styles.root}>
35+
<Text style={styles.greeting}>
36+
Hello{" "}
37+
{this.props.name +
38+
this.getExclamationMarks(this.state.enthusiasmLevel)}
39+
</Text>
40+
41+
<View style={styles.buttons}>
42+
<View style={styles.button}>
43+
<Button
44+
title="-"
45+
onPress={this.onDecrement}
46+
accessibilityLabel="decrement"
47+
color="red"
48+
/>
49+
</View>
50+
51+
<View style={styles.button}>
52+
<Button
53+
title="+"
54+
onPress={this.onIncrement}
55+
accessibilityLabel="increment"
56+
color="blue"
57+
/>
58+
</View>
59+
</View>
60+
</View>
61+
);
62+
}
63+
}
64+
65+
// styles
66+
const styles = StyleSheet.create({
67+
root: {
68+
alignItems: "center",
69+
alignSelf: "center"
70+
},
71+
buttons: {
72+
flexDirection: "row",
73+
minHeight: 70,
74+
alignItems: "stretch",
75+
alignSelf: "center",
76+
borderWidth: 5
77+
},
78+
button: {
79+
flex: 1,
80+
paddingVertical: 0
81+
},
82+
greeting: {
83+
color: "#999",
84+
fontWeight: "bold"
85+
}
86+
});

lib/StatefulComponent/Size.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
S: 11,
3+
M: 14,
4+
L: 16,
5+
XL: 22,
6+
XXL: 28
7+
};

lib/StatefulComponent/Text.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from "react";
2+
import { Text as RNText, TextStyle } from "react-native";
3+
import Size from "./Size";
4+
5+
export interface TextProps {
6+
size?: "S" | "M" | "L" | "XL" | "XXL"; // size name
7+
style?: TextStyle;
8+
bold?: boolean;
9+
color?: string;
10+
children: React.ReactNode;
11+
}
12+
13+
const getSize: { [key: string]: number } = Size;
14+
15+
const checkSize = (size: string): number => {
16+
return getSize[size] || 0;
17+
};
18+
19+
const Text = ({
20+
size = "M",
21+
children,
22+
style,
23+
bold,
24+
color,
25+
...rest
26+
}: TextProps) => (
27+
<RNText
28+
{...rest}
29+
style={{
30+
...style,
31+
fontSize: checkSize(size),
32+
fontWeight: bold ? "700" : "400",
33+
color: color || "black"
34+
}}
35+
>
36+
{children}
37+
</RNText>
38+
);
39+
40+
export default Text;

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,11 @@
2222
"peerDependencies": {
2323
"react": ">= 16.x.x",
2424
"react-native": ">= 0.55.x"
25+
},
26+
"devDependencies": {
27+
"@types/react": "^16.9.17",
28+
"@types/react-native": "^0.60.28",
29+
"react-native-typescript-transformer": "^1.2.13",
30+
"typescript": "^3.7.4"
2531
}
2632
}

rn-cli.config.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
getTransformModulePath() {
3+
return require.resolve("react-native-typescript-transformer");
4+
},
5+
getSourceExts() {
6+
return ["ts", "tsx"];
7+
}
8+
};

tsconfig.json

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"compilerOptions": {
3+
/* Basic Options */
4+
// "incremental": true, /* Enable incremental compilation */
5+
"target": "es6" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */,
6+
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
7+
"lib": [
8+
"es6"
9+
] /* Specify library files to be included in the compilation. */,
10+
11+
"allowJs": true /* Allow javascript files to be compiled. */,
12+
"allowSyntheticDefaultImports": true,
13+
// "checkJs": true, /* Report errors in .js files. */
14+
"jsx": "react-native" /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */,
15+
// "declaration": true, /* Generates corresponding '.d.ts' file. */
16+
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
17+
// "sourceMap": true, /* Generates corresponding '.map' file. */
18+
// "outFile": "./", /* Concatenate and emit output to single file. */
19+
// "outDir": "./", /* Redirect output structure to the directory. */
20+
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
21+
// "composite": true, /* Enable project compilation */
22+
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
23+
// "removeComments": true, /* Do not emit comments to output. */
24+
"noEmit": true /* Do not emit outputs. */,
25+
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
26+
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
27+
"isolatedModules": true /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */,
28+
29+
/* Strict Type-Checking Options */
30+
"strict": true /* Enable all strict type-checking options. */,
31+
"noImplicitAny": false /* Raise error on expressions and declarations with an implied 'any' type. */,
32+
// "strictNullChecks": true, /* Enable strict null checks. */
33+
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
34+
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
35+
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
36+
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
37+
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
38+
39+
/* Additional Checks */
40+
// "noUnusedLocals": true, /* Report errors on unused locals. */
41+
// "noUnusedParameters": true, /* Report errors on unused parameters. */
42+
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
43+
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
44+
45+
/* Module Resolution Options */
46+
"moduleResolution": "node" /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */,
47+
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
48+
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
49+
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
50+
// "typeRoots": [], /* List of folders to include type definitions from. */
51+
// "types": [], /* Type declaration files to be included in compilation. */
52+
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
53+
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
54+
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
55+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
56+
57+
/* Source Map Options */
58+
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
59+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
60+
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
61+
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
62+
63+
/* Experimental Options */
64+
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
65+
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
66+
67+
"noResolve": false,
68+
/* Advanced Options */
69+
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
70+
},
71+
"exclude": [
72+
"node_modules",
73+
"babel.config.js",
74+
"metro.config.js",
75+
"jest.config.js",
76+
"__tests__/**/*-test.ts"
77+
]
78+
}

0 commit comments

Comments
 (0)