Skip to content

Commit 289e50c

Browse files
committed
create standalone component
1 parent 579bfb6 commit 289e50c

File tree

7 files changed

+64
-50
lines changed

7 files changed

+64
-50
lines changed

public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
work correctly both with client-side routing and a non-root public URL.
2020
Learn how to configure a non-root public URL by running `npm run build`.
2121
-->
22-
<title>React App</title>
22+
<title>Simple react json viewer</title>
2323
</head>
2424
<body>
2525
<noscript>You need to enable JavaScript to run this app.</noscript>

src/App.css

-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +0,0 @@
1-
.app {
2-
text-align: center;
3-
}
4-
5-
.json-container {
6-
margin-left: 130px;
7-
}

src/App.js

+3-40
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,16 @@
11
import React from 'react';
22
import './App.css';
3-
import {Input} from './input/Input';
4-
import {Json} from "./json/Json";
3+
import {JsonViewer} from "./json/JsonViewer";
54

65

76
export class App extends React.Component {
8-
constructor() {
9-
super();
10-
this.state = {json: {}};
11-
}
12-
13-
dataLoaded = t => {
14-
this.setState(() => ({json: t, error: false, loading: false}));
15-
};
16-
17-
onError = () => {
18-
this.setState(() => ({json: null, error: true, loading: false}));
19-
};
20-
21-
onLoading = () => {
22-
this.setState(() => ({loading: true}));
23-
};
24-
25-
renderData() {
26-
if (this.state.loading) {
27-
return null;
28-
}
29-
30-
return this.state.error
31-
? <div className="error">Error loading</div>
32-
: <Json className="json-container" json={this.state.json}/>;
33-
}
34-
35-
renderLoader() {
36-
if (!this.state.loading) {
37-
return null;
38-
}
39-
return <span>Loading....</span>;
40-
}
417

428
render() {
439
return (
44-
<div className="app">
45-
<Input onDataLoaded={this.dataLoaded} onError={this.onError} onLoading={this.onLoading}/>
46-
{this.renderLoader()}
47-
{this.renderData()}
48-
</div>
10+
<JsonViewer/>
4911
);
5012
}
13+
5114
}
5215

5316
export default App;

src/input/Input.js

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ export class Input extends React.Component {
3333
}
3434

3535
render() {
36-
console.log(styles);
3736
return (
3837
<form className={styles.form} onSubmit={this.handleSubmit}>
3938
<input className={styles.input} type="text" value={this.state.value} onChange={this.handleChange} />

src/json/Json.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class Json extends React.Component {
2828
const {folded} = this.state;
2929

3030
const dotsClass = classnames({[styles.folded]: !folded});
31-
console.log(dotsClass);
31+
3232
if (!json) {
3333
return null;
3434
}

src/json/Json.module.css

+9
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@
3838
.folded {
3939
display: none;
4040
}
41+
42+
43+
.component {
44+
text-align: center;
45+
}
46+
47+
.container {
48+
margin-left: 130px;
49+
}

src/json/JsonViewer.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react';
2+
import styles from './Json.module.css';
3+
import {Input} from "../input/Input";
4+
import {Json} from "./Json";
5+
6+
export class JsonViewer extends React.Component {
7+
constructor() {
8+
super();
9+
this.state = {json: {}};
10+
}
11+
12+
dataLoaded = t => {
13+
this.setState(() => ({json: t, error: false, loading: false}));
14+
};
15+
16+
onError = () => {
17+
this.setState(() => ({json: null, error: true, loading: false}));
18+
};
19+
20+
onLoading = () => {
21+
this.setState(() => ({loading: true}));
22+
};
23+
24+
renderData() {
25+
if (this.state.loading) {
26+
return null;
27+
}
28+
29+
return this.state.error
30+
? <div>Error loading</div>
31+
: <Json className={styles.container} json={this.state.json}/>;
32+
}
33+
34+
renderLoader() {
35+
if (!this.state.loading) {
36+
return null;
37+
}
38+
return <span>Loading....</span>;
39+
}
40+
41+
render() {
42+
return (
43+
<div className={styles.component}>
44+
<Input onDataLoaded={this.dataLoaded} onError={this.onError} onLoading={this.onLoading}/>
45+
{this.renderLoader()}
46+
{this.renderData()}
47+
</div>
48+
);
49+
}
50+
}

0 commit comments

Comments
 (0)