質問編集履歴

1

スタイルについての質問に変更

2020/09/17 01:02

投稿

Kana-T
Kana-T

スコア5

test CHANGED
@@ -1 +1 @@
1
- Firebaseを使ってデータ保存/取り出しをしたい
1
+ テキストライン開始位置揃えたまま中央寄せしたい
test CHANGED
@@ -2,470 +2,124 @@
2
2
 
3
3
 
4
4
 
5
- React使って家計簿アプリ作って
5
+ テキストラインの開始位置揃えたまま全体中央に寄せた
6
6
 
7
- 家計簿をログインのユーザーごとデータ保存/取り出たいのでが、
7
+ 金額最後「円」がある為、少左にずれてまっておりま
8
8
 
9
+ こちらの左のズレをなくし、「内容」と「金額」の開始ラインを揃えたいです。
10
+
9
- 実装方法詰まっています。
11
+ アドバイスをいただけると幸いです。
10
12
 
11
13
 
12
14
 
13
- ログン機能は出来上がり、console.log(currentUser.uid)uidが表示されるのを確認しました。
15
+ 以下現在のメージ
14
16
 
15
- 保存したいデタは、incomeItemsとexpenseItemsです。
17
+ ![イメジ説明](704250979460ec3545ae23c621972b37.png)
16
-
17
- uidを使って紐付けをしたいと考えています。
18
-
19
-
20
-
21
- 是非アドバイスをいただけますと幸いです。
22
18
 
23
19
 
24
20
 
25
21
  ### 該当のソースコード
26
22
 
23
+
24
+
25
+ ```HTML
26
+
27
+ <form className="add-form">
28
+
29
+ <select onChange={typeHandler}>
30
+
31
+ <option value="inc">+</option>
32
+
33
+ <option value="exp">-</option>
34
+
35
+ </select>
36
+
37
+ <div className="add-text">
38
+
27
- firebaseとデータやり取り関係のないコンポーネントは省略してます。
39
+ <label>内容</label>
40
+
41
+ <input type="text" value={inputText} onChange={inputTextHandler} />
42
+
43
+ </div>
44
+
45
+ <div className="add-amount">
46
+
47
+ <label>金額</label>
48
+
49
+ <input type="number" value={inputAmount} onChange={inputAmountHandler}/>
50
+
51
+ <div>円</div>
52
+
53
+ </div>
54
+
55
+ <button className="add-btn" type="submit" onClick={submitItemHandler}>追加</button>
56
+
57
+ </form>
58
+
59
+ ```
28
60
 
29
61
 
30
62
 
31
- Home.js
63
+ ```CSS
32
64
 
65
+ .add-form {
33
66
 
34
-
35
- ```JavaScript
36
-
37
- import React, {useState, useEffect, useContext, useMemo, useCallback} from 'react';
38
-
39
- import { auth, db } from "../firebase/Firebase";
40
-
41
- import { Header } from './Header';
42
-
43
- import { Balance } from './Balance';
44
-
45
- import { IncomeExpense } from './IncomeExpense';
46
-
47
- import { AddItem } from './AddItem';
48
-
49
- import { ItemsList } from './ItemsList';
50
-
51
- import { AuthContext } from '../auth/AuthProvider';
52
-
53
-
54
-
55
- function Home (props) {
67
+ background: var(--gray-color);
56
-
57
-
58
-
59
- const [inputText, setInputText] = useState("");
60
-
61
- const [inputAmount, setInputAmount] = useState(0);
62
-
63
- const [incomeItems, setIncomeItems] = useState([]);
64
-
65
- const [expenseItems, setExpenseItems] = useState([]);
66
-
67
- const [type, setType] = useState("inc");
68
-
69
-
70
-
71
-
72
-
73
- const { currentUser } = useContext(AuthContext)
74
-
75
- console.log(currentUser.uid)
76
-
77
-
78
-
79
- return (
80
-
81
- <div>
82
-
83
- <Header />
84
-
85
- <Balance
86
-
87
- incomeItems={incomeItems}
88
-
89
- expenseItems={expenseItems}
90
-
91
- />
92
-
93
- <IncomeExpense
94
-
95
- incomeItems={incomeItems}
96
-
97
- expenseItems={expenseItems}
98
-
99
- />
100
-
101
- <AddItem
102
-
103
- inputText={inputText}
104
-
105
- setInputText={setInputText}
106
-
107
- inputAmount={inputAmount}
108
-
109
- setInputAmount={setInputAmount}
110
-
111
- incomeItems={incomeItems}
112
-
113
- setIncomeItems={setIncomeItems}
114
-
115
- expenseItems={expenseItems}
116
-
117
- setExpenseItems={setExpenseItems}
118
-
119
- type={type}
120
-
121
- setType={setType}
122
-
123
- />
124
-
125
- <ItemsList
126
-
127
- incomeItems={incomeItems}
128
-
129
- setIncomeItems={setIncomeItems}
130
-
131
- expenseItems={expenseItems}
132
-
133
- setExpenseItems={setExpenseItems}
134
-
135
- />
136
-
137
- <button onClick={() => auth.signOut()}>log out</button>
138
-
139
- </div>
140
-
141
- )
142
68
 
143
69
  }
144
70
 
145
71
 
146
72
 
147
- export default Home;
73
+ .add-text {
148
74
 
149
- ```
150
-
151
-
152
-
153
- AddItems.js
154
-
155
- ```JavaScript
156
-
157
- import React, { useState } from 'react';
158
-
159
-
160
-
161
- export const AddItem = ({ inputText, setInputText, inputAmount, setInputAmount, incomeItems, setIncomeItems, expenseItems, setExpenseItems, type, setType}) => {
162
-
163
-
164
-
165
-
166
-
167
- const typeHandler = (e) => {
168
-
169
- setType(e.target.value)
170
-
171
- console.log(type)
172
-
173
- }
174
-
175
-
176
-
177
- const inputTextHandler = (e) => {
178
-
179
- setInputText(e.target.value);
180
-
181
- console.log(inputText)
182
-
183
- };
184
-
185
-
186
-
187
- const inputAmountHandler = (e) => {
188
-
189
- setInputAmount(parseInt(e.target.value))
190
-
191
- console.log(inputAmount)
192
-
193
- }
194
-
195
-
196
-
197
- const submitItemHandler = (e) => {
198
-
199
- e.preventDefault();
200
-
201
- if ( type === 'inc' ) {
202
-
203
- setIncomeItems([
204
-
205
- ...incomeItems, {text: inputText, amount: inputAmount, id: Math.random() * 1000 }
206
-
207
- ]);
208
-
209
- } else if ( type === 'exp' ) {
210
-
211
- setExpenseItems([
75
+ text-align: center;
212
-
213
- ...expenseItems, {text: inputText, amount:inputAmount, id: Math.random() * 1000 }
214
-
215
- ]);
216
-
217
- }
218
-
219
- console.log(incomeItems)
220
-
221
- }
222
-
223
-
224
-
225
- return (
226
-
227
- <>
228
-
229
- <form >
230
-
231
- <select onChange={typeHandler}>
232
-
233
- <option value="inc">+</option>
234
-
235
- <option value="exp" >-</option>
236
-
237
- </select>
238
-
239
- <label>Text</label>
240
-
241
- <input type="text" placeholder="Text" value={inputText} onChange={inputTextHandler} />
242
-
243
- <label>Amount</label>
244
-
245
- <input type="number" placeholder="Value" value={inputAmount} onChange={inputAmountHandler} />
246
-
247
- <button type="submit" onClick={submitItemHandler}>Add</button>
248
-
249
- </form>
250
-
251
- </>
252
-
253
- )
254
-
255
-
256
-
257
- }
258
-
259
-
260
-
261
- ```
262
-
263
-
264
-
265
- AuthProvider.js
266
-
267
- ```JavaScript
268
-
269
- import React, { useEffect, useState } from "react";
270
-
271
- import { auth } from "../firebase/Firebase";
272
-
273
-
274
-
275
- const AuthContext = React.createContext()
276
-
277
-
278
-
279
- const AuthProvider = ({ children }) => {
280
-
281
- const [currentUser, setCurrentUser] = useState(null);
282
-
283
-
284
-
285
- //サインアップ後認証情報を更新
286
-
287
- const signup = async (email, password, history) => {
288
-
289
- try {
290
-
291
- await auth.createUserWithEmailAndPassword(email, password);
292
-
293
- auth.onAuthStateChanged(user => setCurrentUser(user));
294
-
295
- history.push("/");
296
-
297
- } catch (error) {
298
-
299
- alert(error);
300
-
301
- }
302
-
303
- };
304
-
305
-
306
-
307
- //ログインさせる
308
-
309
- const login = async (email, password, history) => {
310
-
311
- try {
312
-
313
- await auth.signInWithEmailAndPassword(email,password);
314
-
315
- auth.onAuthStateChanged(user => setCurrentUser(user));
316
-
317
- history.push("/");
318
-
319
- } catch (error) {
320
-
321
- alert(error);
322
-
323
- }
324
-
325
- }
326
-
327
-
328
-
329
- //初回アクセス時に認証済みかチェック
330
-
331
- useEffect(() => {
332
-
333
- auth.onAuthStateChanged(setCurrentUser);
334
-
335
- }, []);
336
-
337
-
338
-
339
- return (
340
-
341
- <AuthContext.Provider value={{ signup, login, currentUser}}>
342
-
343
- {children}
344
-
345
- </AuthContext.Provider>
346
-
347
- )
348
76
 
349
77
  }
350
78
 
351
79
 
352
80
 
353
- export {AuthContext, AuthProvider}
81
+ .add-amount {
354
82
 
83
+ text-align: center;
84
+
355
- ```
85
+ }
356
86
 
357
87
 
358
88
 
359
- Login.js
89
+ .add-form label {
360
90
 
361
- ```JavaScript
91
+ display: inline-block;
362
92
 
363
- import React, { useContext } from "react";
93
+ margin: 10px 0;
364
94
 
365
- import { withRouter } from "react-router";
95
+ padding-right: 5px;
366
96
 
367
- import { AuthContext } from "./AuthProvider";
97
+ }
368
-
369
- import { app } from "../firebase/Firebase";
370
-
371
- import firebase from "firebase/app";
372
-
373
- import "firebase/auth";
374
98
 
375
99
 
376
100
 
377
- const Login = ({ history }) => {
101
+ .add-amount > div {
378
102
 
379
- const { login } = useContext(AuthContext);
103
+ display: inline-block;
380
104
 
381
- //AuthContextからlogin関数を受け取る
105
+ }
382
106
 
383
107
 
384
108
 
385
- const handleSubmit = event => {
109
+ form input[type="text"] ,input[type="number"] {
386
110
 
387
- event.preventDefault();
111
+ height: 40px;
388
112
 
389
- const { email, password } = event.target.elements;
113
+ width: 300px;
390
114
 
391
- login(email.value, password.value, history);
115
+ border: 1px solid #A9A9A9;
392
116
 
393
- };
117
+ background: #fafafa;
394
118
 
119
+ border-radius: 3px;
395
120
 
121
+ color: #393939;
396
122
 
397
-
398
-
399
- return (
400
-
401
- <div>
402
-
403
- <h1>Log in</h1>
404
-
405
- <form onSubmit={handleSubmit}>
406
-
407
- <label>
408
-
409
- Email
410
-
411
- <input name="email" type="email" placeholder="Email" />
412
-
413
- </label>
414
-
415
- <label>
416
-
417
- Password
418
-
419
- <input name="password" type="password" placeholder="password"/>
420
-
421
- </label>
422
-
423
- <button type="submit">Log in</button>
424
-
425
- </form>
426
-
427
- </div>
428
-
429
- );
430
-
431
- };
123
+ }
432
-
433
-
434
-
435
- export default withRouter(Login);
436
124
 
437
125
  ```
438
-
439
-
440
-
441
- PrivateRoute.js
442
-
443
- ```
444
-
445
- import React, { useContext } from "react";
446
-
447
- import { Route } from "react-router-dom";
448
-
449
- import { AuthContext } from "./AuthProvider";
450
-
451
- import Login from "./Login";
452
-
453
-
454
-
455
- const PrivateRoute = ({ component: RouteComponent, ...options}) => {
456
-
457
- const { currentUser } = useContext(AuthContext);
458
-
459
- const Component = currentUser ? RouteComponent : Login;
460
-
461
-
462
-
463
- return <Route {...options} component={Component} />;
464
-
465
- };
466
-
467
-
468
-
469
- export default PrivateRoute;
470
-
471
- ```