Skip to content

Commit 6f345d8

Browse files
committed
feat: Typescript porting
- Port source code to typescript
1 parent bee1cd2 commit 6f345d8

28 files changed

+2286
-2163
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.DS_Store
33
*.DS_Store
44
**.DS_Store
5+
build
56

67
# npm
78
node_modules

package.json

+15-6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"build:es": "cross-env BABEL_ENV=es NODE_ENV=production babel src -d es --ignore '**/*.spec.js','__snapshots__' --copy-files --source-maps",
2525
"build:css": "node-sass src/_BaseTable.scss ./styles.css --output-style expanded",
2626
"build": "npm run build:js && npm run build:es && npm run build:css",
27+
"build:typescript": "tsc",
2728
"format": "prettier --write 'src/**/*.{js,scss}'",
2829
"prebuild": "npm run clean",
2930
"precommit": "lint-staged",
@@ -44,11 +45,18 @@
4445
},
4546
"dependencies": {
4647
"@babel/runtime": "^7.0.0",
48+
"@types/classnames": "^2.2.9",
49+
"@types/memoize-one": "^4.1.1",
50+
"@types/react": "^16.9.2",
51+
"@types/react-test-renderer": "^16.9.0",
52+
"@types/react-virtualized-auto-sizer": "^1.0.0",
53+
"@types/react-window": "^1.8.1",
4754
"classnames": "^2.2.5",
4855
"memoize-one": "^5.0.0",
4956
"prop-types": "^15.7.0",
5057
"react-virtualized-auto-sizer": "^1.0.2",
51-
"react-window": "^1.8.2"
58+
"react-window": "^1.8.2",
59+
"typescript": "^3.5.3"
5260
},
5361
"peerDependencies": {
5462
"react": "^16.0.0",
@@ -62,9 +70,9 @@
6270
"@babel/plugin-transform-runtime": "^7.0.0",
6371
"@babel/preset-env": "^7.0.0",
6472
"@babel/preset-react": "^7.0.0",
73+
"@types/jest": "^24.0.18",
6574
"babel-core": "^7.0.0-bridge.0",
6675
"babel-eslint": "^9.0.0",
67-
"babel-jest": "^23.4.2",
6876
"babel-plugin-syntax-trailing-function-commas": "^6.22.0",
6977
"cross-env": "^5.2.0",
7078
"eslint": "^5.6.0",
@@ -76,23 +84,24 @@
7684
"eslint-plugin-prettier": "^2.6.2",
7785
"eslint-plugin-react": "^7.11.1",
7886
"husky": "^0.14.3",
79-
"jest": "^23.5.0",
87+
"jest": "^23.6.0",
8088
"lerna": "^3.2.1",
8189
"lint-staged": "^7.2.2",
8290
"node-sass": "^4.9.3",
8391
"prettier": "^1.14.2",
8492
"react": "^16.4.2",
8593
"react-dom": "^16.4.2",
8694
"react-test-renderer": "^16.4.2",
87-
"rimraf": "^2.6.2"
95+
"rimraf": "^2.6.2",
96+
"ts-jest": "^24.0.2"
8897
},
8998
"jest": {
9099
"roots": [
91100
"<rootDir>/src"
92101
],
93-
"testRegex": ".*.spec\\.js$",
102+
"testRegex": ".*.spec\\.tsx?$",
94103
"transform": {
95-
"^.+\\.jsx?$": "babel-jest"
104+
"^.+\\.tsx?$": "ts-jest"
96105
},
97106
"transformIgnorePatterns": [
98107
"<rootDir>/node_modules/"

src/AutoResizer.js src/AutoResizer.tsx

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import React from 'react';
2-
import PropTypes from 'prop-types';
32
import AutoSizer from 'react-virtualized-auto-sizer';
43

54
/**
65
* Decorator component that automatically adjusts the width and height of a single child
76
*/
8-
const AutoResizer = ({ className, width, height, children, onResize }) => {
7+
const AutoResizer: React.FunctionComponent<AutoResizerProps> = ({ className, width, height, children, onResize }) => {
98
const disableWidth = typeof width === 'number';
109
const disableHeight = typeof height === 'number';
1110

@@ -29,29 +28,31 @@ const AutoResizer = ({ className, width, height, children, onResize }) => {
2928
);
3029
};
3130

32-
AutoResizer.propTypes = {
31+
32+
type ChildrenArgs = {width: number, height: number};
33+
export interface AutoResizerProps {
3334
/**
3435
* Class name for the component
3536
*/
36-
className: PropTypes.string,
37+
className?: string;
3738
/**
3839
* the width of the component, will be the container's width if not set
3940
*/
40-
width: PropTypes.number,
41+
width?: number;
4142
/**
4243
* the height of the component, will be the container's width if not set
4344
*/
44-
height: PropTypes.number,
45+
height?: number;
4546
/**
4647
* A callback function to render the children component
4748
* The handler is of the shape of `({ width, height }) => node`
4849
*/
49-
children: PropTypes.func.isRequired,
50+
children: (args: ChildrenArgs) => React.ReactNode;
5051
/**
5152
* A callback function when the size of the table container changed if the width and height are not set
5253
* The handler is of the shape of `({ width, height }) => *`
5354
*/
54-
onResize: PropTypes.func,
55+
onResize?: (args: ChildrenArgs) => void;
5556
};
5657

5758
export default AutoResizer;

src/BaseTable.spec.js src/BaseTable.spec.tsx

+19-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
22
import renderer from 'react-test-renderer';
33

4-
import BaseTable from './BaseTable';
4+
import BaseTable, { IBaseTableProps } from './BaseTable';
55

6-
const RENDERER = () => null;
6+
type TRenderer = () => null;
7+
const RENDERER: TRenderer = () => null;
78

89
const columns = [
910
{
@@ -33,7 +34,22 @@ const data = [
3334
},
3435
];
3536

36-
const Table = props => <BaseTable width={100} height={100} data={data} columns={columns} {...props} />;
37+
type DefaultPropsKeys = keyof typeof BaseTable.defaultProps;
38+
interface TableProps extends Partial<typeof BaseTable.defaultProps> {};
39+
interface MinusDefaultKeys extends Partial<Omit<IBaseTableProps<any>, DefaultPropsKeys>> {};
40+
41+
const Table: React.FunctionComponent<
42+
TableProps &
43+
MinusDefaultKeys> = ({width, height, data, columns, ...restProps}) => (
44+
<BaseTable width={width} height={height} {...restProps} />
45+
);
46+
47+
Table.defaultProps = {
48+
width: 100,
49+
height: 100,
50+
data,
51+
columns
52+
};
3753

3854
describe('Table', function() {
3955
test('renders correctly', () => {

0 commit comments

Comments
 (0)