Skip to content

Commit a6c102a

Browse files
Testing env and error handling improved.
1 parent 2da514b commit a6c102a

File tree

3 files changed

+103
-9
lines changed

3 files changed

+103
-9
lines changed

src/app.scss

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ body,
77
flex: 1 0 auto;
88
}
99

10+
.error {
11+
background: lighten($color: red, $amount: 45);
12+
padding: 15px;
13+
margin: 10px;
14+
margin-right: 12px;
15+
border: 1px solid lighten($color: red, $amount: 25);
16+
font-family: "Open Sans", sans-serif;
17+
font-size: 18px;
18+
width: 100%;
19+
20+
> * {
21+
padding-top: 5px;
22+
}
23+
24+
.disclaimer {
25+
font-style: italic;
26+
font-size: 16px;
27+
}
28+
29+
.message {
30+
font-weight: bold;
31+
}
32+
33+
.component-stack {
34+
font-size: 16px;
35+
36+
pre {
37+
padding-top: 3px;
38+
}
39+
}
40+
}
41+
1042
.forms,
1143
.store,
1244
.store-result {
@@ -16,9 +48,28 @@ body,
1648
.forms {
1749
flex: 1 0 auto;
1850
font-family: "Open Sans", sans-serif;
51+
font-size: 16px;
1952

2053
input {
54+
font-size: 16px;
2155
font-family: "Open Sans", sans-serif;
56+
padding-left: 4px;
57+
width: 98%;
58+
}
59+
60+
button {
61+
font-size: 16px;
62+
background: lightgrey;
63+
border: 1px solid #ccc;
64+
color: black;
65+
padding: 5px 10px;
66+
border-radius: 5px;
67+
width: 100%;
68+
margin-top: 5px;
69+
70+
&:hover {
71+
background: lighten($color: lightgrey, $amount: 5);
72+
}
2273
}
2374
}
2475

src/app.tsx

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect } from "react";
1+
import React, { useState, useEffect, ErrorInfo } from "react";
22
import ReactDOM from "react-dom";
33
import { Test } from "./components";
44
import { TextField } from "./components/text-field";
@@ -10,7 +10,7 @@ import "./reset.scss";
1010
import "./app.scss";
1111

1212
const store = new GroupStoreMutable();
13-
const App = () => {
13+
const Test = () => {
1414
const forceUpdate = useForceUpdate();
1515
useEffect(() => {
1616
const listener = () => {
@@ -34,18 +34,22 @@ const App = () => {
3434
<div className="forms">
3535
<GroupContext.Provider value={groupContext}>
3636
<div>
37+
<button onClick={() => setShow(!show)}>
38+
{!show ? "Mount" : "Unmount"}
39+
</button>
3740
<label>
38-
Imported:
41+
Last name
3942
{show ? (
4043
<TextField
41-
name="firstName"
42-
initialValue="Test"
44+
name="lastName"
45+
initialValue="Smith"
4346
/>
4447
) : null}
4548
</label>
46-
<button onClick={() => setShow(!show)}>
47-
{!show ? "Mount" : "Unmount"}
48-
</button>
49+
<label>
50+
First name
51+
<TextField name="firstName" initialValue="John" />
52+
</label>
4953
</div>
5054
</GroupContext.Provider>
5155
</div>
@@ -72,4 +76,43 @@ const App = () => {
7276
);
7377
};
7478

79+
interface State {
80+
error?: Error;
81+
info?: ErrorInfo;
82+
}
83+
84+
class App extends React.Component {
85+
public state: State = {};
86+
87+
public componentDidCatch(error: Error, info: ErrorInfo) {
88+
this.setState(() => ({
89+
error: error,
90+
info: info
91+
}));
92+
}
93+
94+
public render(): JSX.Element {
95+
const { error } = this.state;
96+
if (error != null) {
97+
const info = this.state.info!;
98+
console.warn(error.message);
99+
console.log(info);
100+
return (
101+
<div className="error">
102+
<p className="disclaimer">
103+
Oops! An error has occurred! Here’s what we know…
104+
</p>
105+
<p className="message">{error.message}</p>
106+
<p className="component-stack">
107+
Component stack:
108+
<pre>{info.componentStack.substr(1)}</pre>
109+
</p>
110+
</div>
111+
);
112+
}
113+
114+
return <Test />;
115+
}
116+
}
117+
75118
ReactDOM.render(<App />, document.getElementById("root"));

src/stores/group-store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class GroupStoreMutable extends TinyEmitter<Callback> {
3232
initialValue: string
3333
): void {
3434
if (this.fields[id] != null) {
35-
throw new Error(`Field with id ${id} is already registered.`);
35+
throw new Error(`Field with an id '${id}' is already registered.`);
3636
}
3737

3838
const newField: Field = {

0 commit comments

Comments
 (0)