Skip to content

Commit 7900837

Browse files
authored
Add typescript/#42 (#47)
* add deps + command * add directory * setup typescript * remove js files * add types for components * move typings to types folder * refactor * especify files in ts config * add scripts and filter npm files * delete files * add components types * move interfaces * add type definition for components * naming instance
1 parent 826ef99 commit 7900837

22 files changed

+590
-107
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
coverage
3-
yarn.lock
3+
yarn.lock
4+
dist

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Block, Card, Button, Text, Input } from "./src";
2-
import * as Utils from "./src/utils";
1+
import { Block, Button, Card, Input, Text } from "./src";
32
import * as theme from "./src/theme";
3+
import * as Utils from "./src/utils";
44

55
export { Block, Card, Button, Text, Input, Utils, theme };

jest.config.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,16 @@ module.exports = {
77
coveragePathIgnorePatterns: ["node_modules", "src/utils"],
88
moduleDirectories: ["node_modules"],
99
transform: {
10-
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
10+
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js",
11+
"\\.(ts|tsx)$": "ts-jest"
1112
},
1213
setupFiles: ["<rootDir>/jest.setup.js"],
13-
moduleFileExtensions: ["js", "jsx"],
14-
transformIgnorePatterns: []
14+
moduleFileExtensions: ["js", "jsx", "ts", "tsx"],
15+
transformIgnorePatterns: [],
16+
globals: {
17+
"ts-jest": {
18+
tsConfig: "tsconfig.jest.json"
19+
}
20+
},
21+
testPathIgnorePatterns: ["dist/"]
1522
};

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@
99
],
1010
"homepage": "http://expo-ui-kit.com/",
1111
"description": "Expo.io UI Kit for React-Native",
12-
"main": "index.js",
12+
"main": "dist/index.js",
13+
"types": "dist/index.d.ts",
1314
"author": "React-UI-Kit <[email protected]> (https://react-ui-kit.com)",
1415
"license": "MIT",
1516
"scripts": {
1617
"lint": "eslint src",
1718
"test": "jest"
1819
},
1920
"devDependencies": {
21+
"@types/enzyme": "^3.10.5",
22+
"@types/enzyme-adapter-react-16": "^1.0.6",
23+
"@types/jest": "^25.2.1",
24+
"@types/react": "^16.9.34",
25+
"@types/react-native": "^0.62.2",
26+
"@types/react-test-renderer": "^16.9.2",
2027
"enzyme": "^3.10.0",
2128
"enzyme-adapter-react-16": "^1.14.0",
2229
"eslint": "^6.5.0",
@@ -28,9 +35,11 @@
2835
"react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
2936
"react-native-testing-library": "^1.13.0",
3037
"react-test-renderer": "^16.13.1",
38+
"ts-jest": "^25.3.1",
3139
"typescript": "^3.8.3"
3240
},
3341
"eslintConfig": {
3442
"extends": "universe/native"
35-
}
43+
},
44+
"dependencies": {}
3645
}

src/Block.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ import { getSpacing, mergeTheme, parseSpacing } from "./utils";
115115
* </Block>
116116
*/
117117

118-
const Block = props => {
119-
const getSpacings = type => {
118+
const Block = (props) => {
119+
const getSpacings = (type) => {
120120
const {
121121
margin,
122122
marginTop,
@@ -203,8 +203,7 @@ const Block = props => {
203203
safe,
204204
style,
205205
children,
206-
scroll,
207-
...rest
206+
scroll
208207
} = props;
209208

210209
const excludeProps = [
@@ -258,7 +257,7 @@ const Block = props => {
258257
shadowRadius: elevation
259258
},
260259
space && { justifyContent: `space-${space}` },
261-
card && { borderRadius: SIZES.border },
260+
card && { borderRadius: SIZES.radius },
262261
radius && { borderRadius: radius },
263262
// color shortcuts
264263
primary && { backgroundColor: COLORS.primary },

src/Button.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
TouchableWithoutFeedback
88
} from "react-native";
99
import expoTheme from "./theme";
10-
import { getSpacing, mergeTheme, parseSpacing, rgba } from "./utils";
10+
import { getSpacing, mergeTheme, parseSpacing, rgba } from "./utils/index";
1111

1212
/**
1313
* https://facebook.github.io/react-native/docs/touchableopacity
@@ -56,7 +56,13 @@ import { getSpacing, mergeTheme, parseSpacing, rgba } from "./utils";
5656
*
5757
*/
5858

59-
function Button(props) {
59+
export const ButtonInstance = ({
60+
Touchable = TouchableOpacity,
61+
children,
62+
...props
63+
}) => <Touchable {...props}>{children}</Touchable>;
64+
65+
const Button = (props) => {
6066
const getSpacings = (type) => {
6167
const {
6268
margin,
@@ -106,7 +112,6 @@ function Button(props) {
106112
];
107113
}
108114
};
109-
110115
const {
111116
disabled,
112117
opacity,
@@ -134,8 +139,7 @@ function Button(props) {
134139
withoutFeedback,
135140
theme,
136141
style,
137-
children,
138-
...rest
142+
children
139143
} = props;
140144

141145
const excludeProps = [
@@ -205,7 +209,7 @@ function Button(props) {
205209
buttonStyles.backgroundColor = "transparent";
206210
}
207211

208-
const ButtonType = highlight
212+
const Touchable = highlight
209213
? TouchableHighlight
210214
: nativeFeedback
211215
? TouchableNativeFeedback
@@ -214,15 +218,16 @@ function Button(props) {
214218
: TouchableOpacity;
215219

216220
return (
217-
<ButtonType
221+
<ButtonInstance
218222
{...extraProps}
219223
disabled={disabled}
224+
Touchable={Touchable}
220225
activeOpacity={opacity}
221-
style={buttonStyles}>
222-
{children}
223-
</ButtonType>
226+
style={buttonStyles}
227+
children={children}
228+
/>
224229
);
225-
}
230+
};
226231

227232
Button.defaultProps = {
228233
color: null,
@@ -232,7 +237,6 @@ Button.defaultProps = {
232237
margin: null,
233238
padding: null,
234239
flex: 0,
235-
height: false,
236240
transparent: false,
237241
primary: false,
238242
secondary: false,

src/Card.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react";
22
import { StyleSheet } from "react-native";
33
import Block from "./Block";
44
import expoTheme from "./theme";
5-
import { mergeTheme, rgba } from "./utils";
5+
import { mergeTheme, rgba } from "./utils/index";
66

77
/**
88
* https://facebook.github.io/react-native/docs/view
@@ -48,7 +48,7 @@ import { mergeTheme, rgba } from "./utils";
4848
*
4949
*/
5050

51-
const Card = props => {
51+
const Card = (props) => {
5252
const {
5353
color,
5454
radius,

src/Input.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import React, { useReducer } from "react";
22
import { StyleSheet, TextInput } from "react-native";
3-
43
import expoTheme from "./theme";
5-
import { mergeTheme, rgba } from "./utils";
4+
import { mergeTheme, rgba } from "./utils/index";
65

76
/**
87
* https://facebook.github.io/react-native/docs/textinput
@@ -168,7 +167,7 @@ Input.defaultProps = {
168167
pattern: null,
169168
onFocus: null,
170169
onBlur: null,
171-
onChange: null,
170+
onChangeText: null,
172171
onValidation: null,
173172
placeholder: null,
174173
autoCorrect: false,

src/Text.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from "react";
22
import { Animated, StyleSheet, Text } from "react-native";
33
import expoTheme from "./theme";
4-
import { getSpacing, mergeTheme, parseSpacing } from "./utils";
4+
import { getSpacing, mergeTheme, parseSpacing } from "./utils/index";
55

66
/**
77
* Usage:
@@ -66,8 +66,8 @@ import { getSpacing, mergeTheme, parseSpacing } from "./utils";
6666
* - <Text animated>will render Animated.Text</Text>
6767
*/
6868

69-
const Typography = props => {
70-
const getSpacings = type => {
69+
const Typography = (props) => {
70+
const getSpacings = (type) => {
7171
const {
7272
margin,
7373
marginTop,

src/__tests__/Block.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ describe("<Block />", () => {
421421

422422
const style = StyleSheet.flatten(component.props.style);
423423
expect(instance.props.card).toEqual(true);
424-
expect(style.borderRadius).toEqual(SIZES.border);
424+
425+
expect(style.borderRadius).toEqual(SIZES.radius);
425426
});
426427

427428
it("radius={4}", () => {
@@ -497,7 +498,7 @@ describe("<Block />", () => {
497498

498499
expect(instance.props.theme).toEqual(customTheme);
499500

500-
let style = StyleSheet.flatten(component.props.style);
501+
const style = StyleSheet.flatten(component.props.style);
501502

502503
expect(style.backgroundColor).toEqual("red");
503504
});

src/__tests__/Button.test.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import { shallow } from "enzyme";
22
import React from "react";
3-
import { StyleSheet } from "react-native";
3+
import {
4+
StyleSheet,
5+
TouchableHighlight,
6+
TouchableNativeFeedback,
7+
TouchableWithoutFeedback
8+
} from "react-native";
49
import renderer from "react-test-renderer";
5-
6-
import Button from "../Button";
10+
import Button, { RenderButton } from "../Button";
711
import Text from "../Text";
812
import { SIZES } from "../theme";
913
import { rgba } from "../utils";
1014

1115
describe("<Button />", () => {
1216
it("render default TouchableOpacity", () => {
1317
const button = shallow(<Button />);
18+
const buttonType = shallow(<RenderButton />);
1419

1520
const component = button;
1621

@@ -24,7 +29,8 @@ describe("<Button />", () => {
2429
backgroundColor: "#4630EB",
2530
justifyContent: "center"
2631
});
27-
expect(component.name()).toEqual("TouchableOpacity");
32+
33+
expect(buttonType.name()).toEqual("TouchableOpacity");
2834
});
2935

3036
it("transparent", () => {
@@ -329,9 +335,12 @@ describe("<Button />", () => {
329335

330336
it("ButtonType: nativeFeedback", () => {
331337
const component = shallow(<Button nativeFeedback />);
338+
const buttonType = shallow(
339+
<RenderButton Touchable={TouchableNativeFeedback} />
340+
);
332341

333342
expect(component.props().nativeFeedback).toEqual(true);
334-
expect(component.name()).toEqual("DummyTouchableNativeFeedback");
343+
expect(buttonType.name()).toEqual("DummyTouchableNativeFeedback");
335344
});
336345

337346
/**
@@ -346,8 +355,14 @@ describe("<Button />", () => {
346355
</Button>
347356
);
348357

358+
const buttonType = shallow(
359+
<RenderButton Touchable={TouchableHighlight}>
360+
<Text />
361+
</RenderButton>
362+
);
363+
349364
expect(component.props().highlight).toEqual(true);
350-
expect(component.name()).toEqual("TouchableHighlight");
365+
expect(buttonType.name()).toEqual("TouchableHighlight");
351366
});
352367

353368
it("ButtonType: withoutFeedback", () => {
@@ -356,9 +371,14 @@ describe("<Button />", () => {
356371
<Text />
357372
</Button>
358373
);
374+
const buttonType = shallow(
375+
<RenderButton Touchable={TouchableWithoutFeedback}>
376+
<Text />
377+
</RenderButton>
378+
);
359379

360380
expect(component.props().withoutFeedback).toEqual(true);
361-
expect(component.name()).toEqual("TouchableWithoutFeedback");
381+
expect(buttonType.name()).toEqual("TouchableWithoutFeedback");
362382
});
363383

364384
it("onPress", () => {

0 commit comments

Comments
 (0)