質問編集履歴
1
スタイルについての質問に変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
テキストラインの開始位置を揃えたまま中央寄せしたい
|
body
CHANGED
@@ -1,236 +1,63 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
|
3
|
+
テキストラインの開始位置を揃えたまま全体を中央に寄せたいです
|
3
|
-
|
4
|
+
金額の最後に「円」がある為、少し左にずれてしまっております。
|
4
|
-
この
|
5
|
+
こちらの左のズレをなくし、「内容」と「金額」の開始ラインを揃えたいです。
|
5
|
-
|
6
|
+
アドバイスをいただけると幸いです。
|
6
7
|
|
8
|
+
以下現在のイメージです
|
7
|
-
|
9
|
+

|
8
|
-
保存したいデータは、incomeItemsとexpenseItemsです。
|
9
|
-
uidを使って紐付けをしたいと考えています。
|
10
10
|
|
11
|
-
是非アドバイスをいただけますと幸いです。
|
12
|
-
|
13
11
|
### 該当のソースコード
|
14
|
-
firebaseとデータやり取り関係のないコンポーネントは省略してます。
|
15
12
|
|
16
|
-
|
13
|
+
```HTML
|
14
|
+
<form className="add-form">
|
15
|
+
<select onChange={typeHandler}>
|
16
|
+
<option value="inc">+</option>
|
17
|
+
<option value="exp">-</option>
|
18
|
+
</select>
|
19
|
+
<div className="add-text">
|
20
|
+
<label>内容</label>
|
21
|
+
<input type="text" value={inputText} onChange={inputTextHandler} />
|
22
|
+
</div>
|
23
|
+
<div className="add-amount">
|
24
|
+
<label>金額</label>
|
25
|
+
<input type="number" value={inputAmount} onChange={inputAmountHandler}/>
|
26
|
+
<div>円</div>
|
27
|
+
</div>
|
28
|
+
<button className="add-btn" type="submit" onClick={submitItemHandler}>追加</button>
|
29
|
+
</form>
|
30
|
+
```
|
17
31
|
|
18
|
-
```
|
32
|
+
```CSS
|
19
|
-
import React, {useState, useEffect, useContext, useMemo, useCallback} from 'react';
|
20
|
-
import { auth, db } from "../firebase/Firebase";
|
21
|
-
|
33
|
+
.add-form {
|
22
|
-
|
34
|
+
background: var(--gray-color);
|
23
|
-
import { IncomeExpense } from './IncomeExpense';
|
24
|
-
|
35
|
+
}
|
25
|
-
import { ItemsList } from './ItemsList';
|
26
|
-
import { AuthContext } from '../auth/AuthProvider';
|
27
36
|
|
37
|
+
.add-text {
|
28
|
-
|
38
|
+
text-align: center;
|
39
|
+
}
|
29
40
|
|
30
|
-
const [inputText, setInputText] = useState("");
|
31
|
-
|
41
|
+
.add-amount {
|
32
|
-
const [incomeItems, setIncomeItems] = useState([]);
|
33
|
-
|
42
|
+
text-align: center;
|
34
|
-
|
43
|
+
}
|
35
44
|
|
45
|
+
.add-form label {
|
46
|
+
display: inline-block;
|
47
|
+
margin: 10px 0;
|
48
|
+
padding-right: 5px;
|
49
|
+
}
|
36
50
|
|
37
|
-
const { currentUser } = useContext(AuthContext)
|
38
|
-
console.log(currentUser.uid)
|
39
|
-
|
40
|
-
return (
|
41
|
-
<div>
|
42
|
-
<Header />
|
43
|
-
<Balance
|
44
|
-
incomeItems={incomeItems}
|
45
|
-
expenseItems={expenseItems}
|
46
|
-
/>
|
47
|
-
<IncomeExpense
|
48
|
-
incomeItems={incomeItems}
|
49
|
-
expenseItems={expenseItems}
|
50
|
-
/>
|
51
|
-
<AddItem
|
52
|
-
inputText={inputText}
|
53
|
-
setInputText={setInputText}
|
54
|
-
|
51
|
+
.add-amount > div {
|
55
|
-
setInputAmount={setInputAmount}
|
56
|
-
incomeItems={incomeItems}
|
57
|
-
setIncomeItems={setIncomeItems}
|
58
|
-
expenseItems={expenseItems}
|
59
|
-
setExpenseItems={setExpenseItems}
|
60
|
-
type={type}
|
61
|
-
|
52
|
+
display: inline-block;
|
62
|
-
/>
|
63
|
-
<ItemsList
|
64
|
-
incomeItems={incomeItems}
|
65
|
-
setIncomeItems={setIncomeItems}
|
66
|
-
expenseItems={expenseItems}
|
67
|
-
setExpenseItems={setExpenseItems}
|
68
|
-
/>
|
69
|
-
<button onClick={() => auth.signOut()}>log out</button>
|
70
|
-
</div>
|
71
|
-
)
|
72
53
|
}
|
73
54
|
|
74
|
-
export default Home;
|
75
|
-
```
|
76
|
-
|
77
|
-
AddItems.js
|
78
|
-
```JavaScript
|
79
|
-
import React, { useState } from 'react';
|
80
|
-
|
81
|
-
export const AddItem = ({ inputText, setInputText, inputAmount, setInputAmount, incomeItems, setIncomeItems, expenseItems, setExpenseItems, type, setType}) => {
|
82
|
-
|
83
|
-
|
84
|
-
const typeHandler = (e) => {
|
85
|
-
setType(e.target.value)
|
86
|
-
console.log(type)
|
87
|
-
}
|
88
|
-
|
89
|
-
const inputTextHandler = (e) => {
|
90
|
-
setInputText(e.target.value);
|
91
|
-
console.log(inputText)
|
92
|
-
};
|
93
|
-
|
94
|
-
const inputAmountHandler = (e) => {
|
95
|
-
setInputAmount(parseInt(e.target.value))
|
96
|
-
console.log(inputAmount)
|
97
|
-
}
|
98
|
-
|
99
|
-
const submitItemHandler = (e) => {
|
100
|
-
e.preventDefault();
|
101
|
-
if ( type === 'inc' ) {
|
102
|
-
setIncomeItems([
|
103
|
-
...incomeItems, {text: inputText, amount: inputAmount, id: Math.random() * 1000 }
|
104
|
-
]);
|
105
|
-
} else if ( type === 'exp' ) {
|
106
|
-
setExpenseItems([
|
107
|
-
...expenseItems, {text: inputText, amount:inputAmount, id: Math.random() * 1000 }
|
108
|
-
]);
|
109
|
-
}
|
110
|
-
console.log(incomeItems)
|
111
|
-
}
|
112
|
-
|
113
|
-
return (
|
114
|
-
<>
|
115
|
-
<form >
|
116
|
-
<select onChange={typeHandler}>
|
117
|
-
<option value="inc">+</option>
|
118
|
-
|
55
|
+
form input[type="text"] ,input[type="number"] {
|
56
|
+
height: 40px;
|
57
|
+
width: 300px;
|
58
|
+
border: 1px solid #A9A9A9;
|
119
|
-
|
59
|
+
background: #fafafa;
|
120
|
-
<label>Text</label>
|
121
|
-
<input type="text" placeholder="Text" value={inputText} onChange={inputTextHandler} />
|
122
|
-
<label>Amount</label>
|
123
|
-
<input type="number" placeholder="Value" value={inputAmount} onChange={inputAmountHandler} />
|
124
|
-
<button type="submit" onClick={submitItemHandler}>Add</button>
|
125
|
-
|
60
|
+
border-radius: 3px;
|
126
|
-
</>
|
127
|
-
)
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
```
|
132
|
-
|
133
|
-
AuthProvider.js
|
134
|
-
```JavaScript
|
135
|
-
import React, { useEffect, useState } from "react";
|
136
|
-
import { auth } from "../firebase/Firebase";
|
137
|
-
|
138
|
-
const AuthContext = React.createContext()
|
139
|
-
|
140
|
-
const AuthProvider = ({ children }) => {
|
141
|
-
const [currentUser, setCurrentUser] = useState(null);
|
142
|
-
|
143
|
-
//サインアップ後認証情報を更新
|
144
|
-
const signup = async (email, password, history) => {
|
145
|
-
try {
|
146
|
-
await auth.createUserWithEmailAndPassword(email, password);
|
147
|
-
auth.onAuthStateChanged(user => setCurrentUser(user));
|
148
|
-
history.push("/");
|
149
|
-
} catch (error) {
|
150
|
-
|
61
|
+
color: #393939;
|
151
|
-
}
|
152
|
-
};
|
153
|
-
|
154
|
-
//ログインさせる
|
155
|
-
const login = async (email, password, history) => {
|
156
|
-
try {
|
157
|
-
await auth.signInWithEmailAndPassword(email,password);
|
158
|
-
auth.onAuthStateChanged(user => setCurrentUser(user));
|
159
|
-
history.push("/");
|
160
|
-
} catch (error) {
|
161
|
-
alert(error);
|
162
|
-
}
|
163
|
-
}
|
164
|
-
|
165
|
-
//初回アクセス時に認証済みかチェック
|
166
|
-
useEffect(() => {
|
167
|
-
auth.onAuthStateChanged(setCurrentUser);
|
168
|
-
}, []);
|
169
|
-
|
170
|
-
return (
|
171
|
-
<AuthContext.Provider value={{ signup, login, currentUser}}>
|
172
|
-
{children}
|
173
|
-
</AuthContext.Provider>
|
174
|
-
)
|
175
62
|
}
|
176
|
-
|
177
|
-
export {AuthContext, AuthProvider}
|
178
|
-
```
|
179
|
-
|
180
|
-
Login.js
|
181
|
-
```JavaScript
|
182
|
-
import React, { useContext } from "react";
|
183
|
-
import { withRouter } from "react-router";
|
184
|
-
import { AuthContext } from "./AuthProvider";
|
185
|
-
import { app } from "../firebase/Firebase";
|
186
|
-
import firebase from "firebase/app";
|
187
|
-
import "firebase/auth";
|
188
|
-
|
189
|
-
const Login = ({ history }) => {
|
190
|
-
const { login } = useContext(AuthContext);
|
191
|
-
//AuthContextからlogin関数を受け取る
|
192
|
-
|
193
|
-
const handleSubmit = event => {
|
194
|
-
event.preventDefault();
|
195
|
-
const { email, password } = event.target.elements;
|
196
|
-
login(email.value, password.value, history);
|
197
|
-
};
|
198
|
-
|
199
|
-
|
200
|
-
return (
|
201
|
-
<div>
|
202
|
-
<h1>Log in</h1>
|
203
|
-
<form onSubmit={handleSubmit}>
|
204
|
-
<label>
|
205
|
-
Email
|
206
|
-
<input name="email" type="email" placeholder="Email" />
|
207
|
-
</label>
|
208
|
-
<label>
|
209
|
-
Password
|
210
|
-
<input name="password" type="password" placeholder="password"/>
|
211
|
-
</label>
|
212
|
-
<button type="submit">Log in</button>
|
213
|
-
</form>
|
214
|
-
</div>
|
215
|
-
);
|
216
|
-
};
|
217
|
-
|
218
|
-
export default withRouter(Login);
|
219
|
-
```
|
220
|
-
|
221
|
-
PrivateRoute.js
|
222
|
-
```
|
223
|
-
import React, { useContext } from "react";
|
224
|
-
import { Route } from "react-router-dom";
|
225
|
-
import { AuthContext } from "./AuthProvider";
|
226
|
-
import Login from "./Login";
|
227
|
-
|
228
|
-
const PrivateRoute = ({ component: RouteComponent, ...options}) => {
|
229
|
-
const { currentUser } = useContext(AuthContext);
|
230
|
-
const Component = currentUser ? RouteComponent : Login;
|
231
|
-
|
232
|
-
return <Route {...options} component={Component} />;
|
233
|
-
};
|
234
|
-
|
235
|
-
export default PrivateRoute;
|
236
63
|
```
|