Dylan Baker 3 роки тому
джерело
коміт
cd05055086
3 змінених файлів з 85 додано та 41 видалено
  1. 10
    1
      src/Board.tsx
  2. 22
    8
      src/Controls.tsx
  3. 53
    32
      src/index.css

+ 10
- 1
src/Board.tsx Переглянути файл

@@ -7,6 +7,10 @@ const DEFAULT_WIDTH = 20;
7 7
 const DEFAULT_HEIGHT = 10;
8 8
 const DEFAULT_SPEED = 500;
9 9
 
10
+const INITIAL_GRID = Array(DEFAULT_HEIGHT).fill(
11
+  Array(DEFAULT_WIDTH).fill(false),
12
+);
13
+
10 14
 interface State {
11 15
   grid: boolean[][];
12 16
   running: boolean;
@@ -21,7 +25,7 @@ class Board extends React.Component<{}, State> {
21 25
   constructor(props: {}) {
22 26
     super(props);
23 27
     this.state = {
24
-      grid: Array(DEFAULT_HEIGHT).fill(Array(DEFAULT_WIDTH).fill(false)),
28
+      grid: [...INITIAL_GRID],
25 29
       running: false,
26 30
       speed: DEFAULT_SPEED,
27 31
       width: DEFAULT_WIDTH,
@@ -41,6 +45,10 @@ class Board extends React.Component<{}, State> {
41 45
     if (this.timer) clearInterval(this.timer);
42 46
   };
43 47
 
48
+  reset = () => {
49
+    this.setState({ grid: [...INITIAL_GRID] });
50
+  };
51
+
44 52
   toggleCell = (x: number, y: number) => {
45 53
     const newGrid = [...this.state.grid];
46 54
     const newRow = [...newGrid[y]];
@@ -104,6 +112,7 @@ class Board extends React.Component<{}, State> {
104 112
           height={height}
105 113
           updateDimensions={this.updateDimensions}
106 114
           updateSpeed={this.updateSpeed}
115
+          reset={this.reset}
107 116
         />
108 117
         {grid.map((row, y) => (
109 118
           <div className="row" key={y}>

+ 22
- 8
src/Controls.tsx Переглянути файл

@@ -5,6 +5,7 @@ interface Props {
5 5
   speed: number;
6 6
   width: number;
7 7
   height: number;
8
+  reset: () => void;
8 9
   toggle: () => void;
9 10
   updateSpeed: (speed: number) => void;
10 11
   updateDimensions: (dimensions: { width?: number; height?: number }) => void;
@@ -18,6 +19,7 @@ const Controls = (props: Props) => {
18 19
     updateSpeed,
19 20
     width,
20 21
     height,
22
+    reset,
21 23
     updateDimensions,
22 24
   } = props;
23 25
 
@@ -33,10 +35,11 @@ const Controls = (props: Props) => {
33 35
 
34 36
   return (
35 37
     <div className="controls">
36
-      <div className="controls__row --width">
38
+      <div className="controls__section dimensions">
37 39
         <label>
38 40
           Width{' '}
39 41
           <input
42
+            className="number-field"
40 43
             type="number"
41 44
             value={width}
42 45
             onChange={(e) =>
@@ -47,6 +50,7 @@ const Controls = (props: Props) => {
47 50
         <label>
48 51
           Height{' '}
49 52
           <input
53
+            className="number-field"
50 54
             type="number"
51 55
             value={height}
52 56
             onChange={(e) =>
@@ -55,14 +59,24 @@ const Controls = (props: Props) => {
55 59
           />
56 60
         </label>
57 61
       </div>
58
-      <div className="controls__row">
59
-        <button onClick={toggle}>{running ? 'Pause' : 'Play'}</button>
60
-        <div className="speed">
61
-          <button onClick={goSlower}>Slower</button>
62
-          <button disabled={speed <= 100} onClick={goFaster}>
63
-            Faster
62
+      <div className="controls__section speed">
63
+        <button className="button" onClick={goSlower}>
64
+          Slower
65
+        </button>
66
+        <button className="button" disabled={speed <= 100} onClick={goFaster}>
67
+          Faster
68
+        </button>
69
+      </div>
70
+      <div className="controls__section play-reset">
71
+        <div className="reset">
72
+          <button className="button" onClick={reset}>
73
+            Reset
74
+          </button>
75
+        </div>
76
+        <div className="play">
77
+          <button className="button" onClick={toggle}>
78
+            {running ? 'Pause' : 'Play'}
64 79
           </button>
65
-          <span>{speed}ms</span>
66 80
         </div>
67 81
       </div>
68 82
     </div>

+ 53
- 32
src/index.css Переглянути файл

@@ -55,63 +55,84 @@ code {
55 55
 }
56 56
 
57 57
 .controls {
58
+  background: black;
59
+  color: white;
58 60
   display: flex;
59
-  flex-direction: column;
61
+  left: 0;
62
+  padding: 1em;
63
+  position: absolute;
64
+  top: 0;
60 65
   width: 100%;
61 66
 }
62 67
 
63
-.controls button {
64
-  background: transparent;
65
-  border: 1px solid black;
66
-  border-right: none;
68
+.button {
69
+  background: black;
70
+  border: 1px solid white;
71
+  color: white;
67 72
   cursor: pointer;
68
-  flex: 1;
69 73
   font-size: 18px;
70
-  padding: 0.25em 0;
71
-}
72
-
73
-.controls button:last-child,
74
-.controls .speed button:last-of-type {
75
-  border-right: 1px solid black;
74
+  margin-right: 1em;
75
+  padding: 0.25em 0.5em;
76 76
 }
77 77
 
78
-.controls button:not([disabled]):hover {
79
-  background: black;
80
-  color: white;
78
+.button:not([disabled]):hover {
79
+  background: white;
80
+  color: black;
81 81
 }
82 82
 
83
-.controls .controls__row {
83
+.controls__section {
84
+  align-items: center;
84 85
   display: flex;
85
-  margin: 1em 0;
86
+  margin: 0.5em 0;
86 87
 }
87 88
 
88
-.controls .speed {
89
+.speed {
89 90
   align-items: center;
90 91
   display: flex;
91 92
   flex: 1;
92
-  justify-content: center;
93
+  justify-content: flex-end;
93 94
 }
94 95
 
95
-.controls .speed button,
96
-.controls .speed span {
97
-  flex: 1;
96
+.button[disabled] {
97
+  cursor: not-allowed;
98
+  opacity: 0.6;
98 99
 }
99 100
 
100
-.controls .speed span {
101
-  font-weight: bold;
102
-  text-align: center;
101
+.controls .play {
102
+  text-align: right;
103 103
 }
104 104
 
105
-.controls .speed button[disabled] {
106
-  cursor: not-allowed;
107
-  opacity: 0.6;
105
+.play .button {
106
+  background: #347b20;
108 107
 }
109 108
 
110
-.--width label {
111
-  flex: 1;
109
+.play .button:hover {
110
+  background: #4c9c36;
111
+  color: white;
112 112
 }
113 113
 
114
-.--width input {
115
-  border: 1px solid black;
114
+.reset .button {
115
+  background: #7b2020;
116
+}
117
+
118
+.reset .button:hover {
119
+  background: #9c3636;
120
+  color: white;
121
+}
122
+
123
+.number-field {
116 124
   font-size: 16px;
125
+  display: inline-block;
126
+  margin-left: 0.25em;
127
+  text-align: center;
128
+  width: 50px;
129
+}
130
+
131
+.dimensions label {
132
+  margin-right: 1em;
133
+}
134
+
135
+.play-reset {
136
+  flex: 1;
137
+  justify-content: flex-end;
117 138
 }

Завантаження…
Відмінити
Зберегти