Skip to content

Commit 28362ef

Browse files
MykolaGolubyevtsiq-nick
authored andcommitted
migration to typesript
1 parent 91ff8ef commit 28362ef

25 files changed

+11025
-2336
lines changed

README.md

+1-2,138
Large diffs are not rendered by default.

package-lock.json

+10,686
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+36-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,43 @@
11
{
22
"name": "react-components-viewer",
33
"version": "0.1.0",
4-
"private": true,
5-
"dependencies": {
6-
"react": "^15.6.1",
7-
"react-dom": "^15.6.1"
4+
"private": false,
5+
"main": "build/lib/components/index.js",
6+
"types": "build/lib/components/index.d.ts",
7+
"engines": {
8+
"node": ">=8.0.0"
89
},
9-
"devDependencies": {
10-
"react-scripts": "1.0.10"
10+
"files": [
11+
"build/lib"
12+
],
13+
"dependencies": {
14+
"react": "^16.2.0",
15+
"react-dom": "^16.2.0",
16+
"react-scripts-ts": "2.13.0"
1117
},
1218
"scripts": {
13-
"start": "react-scripts start",
14-
"build": "react-scripts build",
15-
"test": "react-scripts test --env=jsdom",
16-
"eject": "react-scripts eject"
19+
"start-js": "react-scripts-ts start",
20+
"start": "npm-run-all -p watch-sass-to-css start-js",
21+
"build-js": "react-scripts-ts build",
22+
"build": "npm-run-all -p build-sass-to-css build-js",
23+
"build-lib": "tsc && npm run build-sass-to-css && npm run copy-css-to-lib",
24+
"test": "react-scripts-ts test --env=jsdom",
25+
"eject": "react-scripts-ts eject",
26+
"copy-css-to-lib": "npm-run-all -s copy-base-css copy-component-css",
27+
"copy-base-css": "cpx \"./src/**/*.css\" ./build/lib/",
28+
"copy-component-css": "cpx \"./src/components/**/*.css\" ./build/lib/components",
29+
"build-sass-to-css": "node-sass-chokidar src/ -o src/",
30+
"watch-sass-to-css": "node-sass-chokidar src/ -o src/ --watch --recursive"
31+
},
32+
"devDependencies": {
33+
"@types/jest": "^22.1.0",
34+
"@types/node": "^9.4.0",
35+
"@types/react": "^16.0.35",
36+
"@types/react-dom": "^16.0.3",
37+
"cpx": "^1.5.0",
38+
"node-sass-chokidar": "0.0.3",
39+
"npm-run-all": "^4.1.2",
40+
"typescript": "^2.6.2",
41+
"webpack": "^3.10.0"
1742
}
18-
}
43+
}

src/App.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, {Component} from 'react';
22

3-
import Registry from './registry/Registry';
4-
import Header from './registry/Header';
5-
import ComponentsViewer from './viewer/ComponentsViewer';
3+
import Registry from './components/registry/Registry';
4+
import Header from './components/registry/Header';
5+
import ComponentsViewer from './components/viewer/ComponentsViewer';
66

77
const TestComponent = ({label}) => <div>TC: {label}</div>;
88
const AnotherTestComponent = ({label}) => <div>Another TC: {label}</div>;

src/Demo.tsx

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import * as React from 'react';
2+
import { Component } from 'react';
3+
4+
import Registry from './components/registry/Registry';
5+
import { Header } from './components/registry/Header';
6+
import { ComponentsViewer } from './components/viewer/ComponentsViewer';
7+
8+
const TestComponent = ({label}: {label: string}) => <div>TC: {label}</div>;
9+
const AnotherTestComponent = ({label}: {label: string}) => <div>Another TC: {label}</div>;
10+
11+
const registry = new Registry();
12+
13+
registry.registerAsGrid('Links', {
14+
'simple states': <Header label="header one"/>,
15+
'state-one': <AnotherTestComponent label="1"/>,
16+
'state-two': <AnotherTestComponent label="2"/>,
17+
'complex states': <Header label="header two"/>,
18+
'state-three': <AnotherTestComponent label="3"/>
19+
});
20+
21+
registry.registerAsGrid('Buttons', {
22+
'disabled': <TestComponent label="1"/>,
23+
'enabled': <TestComponent label="2"/>
24+
});
25+
26+
class Demo extends Component {
27+
render() {
28+
return (
29+
<ComponentsViewer registry={registry}/>
30+
);
31+
}
32+
}
33+
34+
export default Demo;

src/components/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import GridLayout from 'components/layouts/GridLayout';
2+
import { Header } from 'components/registry/Header';
3+
import Registry from 'components/registry/Registry';
4+
import ComponentWithDescription from 'components/viewer/ComponentWithDescription';
5+
import ComponentDemo from 'components/viewer/ComponentDemo';
6+
import { ComponentsViewer } from 'components/viewer/ComponentsViewer';
7+
8+
export {
9+
GridLayout,
10+
Registry,
11+
Header,
12+
ComponentDemo,
13+
ComponentsViewer,
14+
ComponentWithDescription
15+
};
File renamed without changes.

src/components/layouts/GridLayout.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as React from 'react';
2+
import './GridLayout.css';
3+
4+
export interface LayoutProps {
5+
children: JSX.Element[];
6+
}
7+
8+
const GridLayout = ({children}: LayoutProps) => {
9+
return (
10+
<div className="grid-layout">
11+
{children}
12+
</div>
13+
);
14+
};
15+
16+
export default GridLayout;
File renamed without changes.

src/components/registry/Header.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import './Header.css';
2+
3+
import * as React from 'react';
4+
5+
export interface Props {
6+
label: string;
7+
}
8+
9+
const Header = ({label}: Props) => <div className="header">{label}</div>;
10+
11+
export {Header};

src/components/registry/Registry.tsx

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as React from 'react';
2+
3+
import ComponentDemo from '../viewer/ComponentDemo';
4+
import GridLayout from '../layouts/GridLayout';
5+
6+
class Registry {
7+
_componentsDemo: {[name: string]: JSX.Element} = {};
8+
9+
registerAsGrid(demoName: string, instancesWithDescription: {[name: string]: JSX.Element}) {
10+
this._componentsDemo[demoName] = (
11+
<ComponentDemo
12+
name={demoName}
13+
layoutComponent={GridLayout}
14+
instancesWithDescription={instancesWithDescription}
15+
/>
16+
);
17+
}
18+
19+
findComponentsDemoByName(name: string) {
20+
return this._componentsDemo[name];
21+
}
22+
23+
get demoNames() {
24+
return Object.keys(this._componentsDemo);
25+
}
26+
}
27+
28+
export default Registry;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
1-
import React, {Component} from 'react';
1+
import * as React from 'react';
2+
import { Component } from 'react';
3+
24
import ComponentWithDescription from './ComponentWithDescription';
35

4-
import Header from '../registry/Header';
6+
import { Header } from '../registry/Header';
7+
import { LayoutProps } from '../layouts/GridLayout';
8+
9+
export interface Props {
10+
name: string;
11+
layoutComponent: React.ComponentClass<LayoutProps> | React.StatelessComponent<LayoutProps>;
12+
instancesWithDescription: {[name: string]: JSX.Element };
13+
}
514

6-
class ComponentDemo extends Component {
15+
class ComponentDemo extends Component<Props> {
716
render() {
817
const {layoutComponent, instancesWithDescription} = this.props;
9-
1018
const Layout = layoutComponent;
19+
1120
const layoutItems = Object.keys(instancesWithDescription).map(description =>
1221
createComponent(description, instancesWithDescription[description]));
1322

@@ -17,19 +26,22 @@ class ComponentDemo extends Component {
1726
{layoutItems}
1827
</Layout>
1928
</div>
20-
)
29+
);
2130
}
2231
}
2332

24-
function createComponent(description, componentInstance) {
33+
function createComponent(description: string, componentInstance: JSX.Element) {
2534
if (componentInstance.type === Header) {
2635
return React.cloneElement(componentInstance, {key: description, label: description});
2736
}
2837

29-
return <ComponentWithDescription key={description}
30-
description={description}
31-
componentInstance={componentInstance}/>
32-
38+
return (
39+
<ComponentWithDescription
40+
key={description}
41+
description={description}
42+
componentInstance={componentInstance}
43+
/>
44+
);
3345
}
3446

3547
export default ComponentDemo;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import React from 'react';
1+
import * as React from 'react';
22

3-
import './ComponentWithDescription.css'
3+
import './ComponentWithDescription.css';
44

5-
const ComponentWithDescription = ({description, componentInstance}) => {
5+
export interface Props {
6+
description: string;
7+
componentInstance: JSX.Element;
8+
}
9+
10+
const ComponentWithDescription = ({description, componentInstance}: Props) => {
611
return (
712
<div className="component-with-description">
813
<div className="description">
@@ -13,7 +18,7 @@ const ComponentWithDescription = ({description, componentInstance}) => {
1318
{componentInstance}
1419
</div>
1520
</div>
16-
)
21+
);
1722
};
1823

1924
export default ComponentWithDescription;
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
1-
import React, {Component} from 'react';
1+
import * as React from 'react';
2+
import { Component } from 'react';
23

34
import './ComponentsViewer.css';
5+
import Registry from '../registry/Registry';
46

57
const queryParamNames = {
6-
demoName: "demo"
8+
demoName: 'demo'
79
};
810

9-
class ComponentsViewer extends Component {
10-
constructor(props) {
11+
export interface Props {
12+
registry: Registry;
13+
}
14+
15+
export interface State {
16+
selectedDemoName: string;
17+
}
18+
19+
class ComponentsViewer extends Component<Props, State> {
20+
constructor(props: Props) {
1121
super(props);
1222
const {registry} = this.props;
1323

1424
this.state = {selectedDemoName: registry.demoNames[0]};
1525
}
1626

17-
selectDemo = (demoName) => {
27+
selectDemo = (demoName: string) => {
1828
this.setState({selectedDemoName: demoName});
19-
window.history.pushState({}, null, `?${queryParamNames.demoName}=` + demoName);
20-
};
29+
window.history.pushState({}, '', `?${queryParamNames.demoName}=` + demoName);
30+
}
2131

2232
render() {
2333
const {registry} = this.props;
@@ -30,21 +40,21 @@ class ComponentsViewer extends Component {
3040
<div className="toc">
3141
{registry.demoNames.map(name => {
3242
const isSelected = selectedDemoName === name;
33-
const className = "name" + (isSelected ? " selected" : "");
43+
const className = 'name' + (isSelected ? ' selected' : '');
3444

3545
return (
3646
<div key={name} className={className} onClick={() => this.selectDemo(name)}>
3747
{name}
3848
</div>
39-
)
49+
);
4050
})}
4151

4252
</div>
4353
<div className="preview">
4454
{componentsDemo}
4555
</div>
4656
</div>
47-
)
57+
);
4858
}
4959

5060
componentDidMount() {
@@ -56,4 +66,4 @@ class ComponentsViewer extends Component {
5666
}
5767
}
5868

59-
export default ComponentsViewer;
69+
export {ComponentsViewer};

src/index.js

-7
This file was deleted.

src/index.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as React from 'react';
2+
import * as ReactDOM from 'react-dom';
3+
import Demo from './Demo';
4+
5+
ReactDOM.render(
6+
<Demo />,
7+
document.getElementById('root') as HTMLElement
8+
);

src/layouts/GridLayout.js

-12
This file was deleted.

0 commit comments

Comments
 (0)