react课程6-eat-n-split

Last updated on August 29, 2024 am

本节课构建的是一个小巧的账单小组件。

如图所示,一共可以被分为三个部分:朋友列表;添加朋友表;分账单表。

image-20240819194647578

代码

=>css

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
:root {
--color-lightest: #fff4e6;
--color-light: #ffe8cc;
--color-medium: #ffa94d;
--color-dark: #ff922b;
}

* {
margin: 0;
padding: 0;
box-sizing: border-box;
}

html {
font-size: 62.5%;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen,
Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}

body {
height: 100vh;
color: #495057;
display: flex;
align-items: center;
justify-content: center;
}

.app {
min-height: 66vh;
display: grid;
grid-template-columns: 34rem 44rem;
column-gap: 4rem;
align-items: start;
}

.button {
background-color: var(--color-medium);
color: #343a40;
padding: 0.8rem 1.2rem;
border: none;
border-radius: 7px;
font-weight: bold;
cursor: pointer;
transition: 0.3s;
}

.button:hover {
background-color: var(--color-dark);
}

.sidebar ul {
list-style: none;
display: flex;
flex-direction: column;
gap: 0.4rem;
font-size: 1.4rem;
margin-bottom: 2rem;
}

.sidebar li {
display: grid;
grid-template-columns: 4.8rem 1fr auto;
align-items: center;
column-gap: 1.6rem;

padding: 1.2rem;
border-radius: 7px;
transition: 0.5s;
}

.selected,
.sidebar li:hover {
background-color: var(--color-lightest);
}

.sidebar li img {
border-radius: 50%;
width: 100%;
grid-row: span 2;
}

.sidebar li h3 {
grid-row: 1;
grid-column: 2;
}

.sidebar li p {
grid-row: 2;
grid-column: 2;
}

.sidebar li .button {
grid-row: span 2;
grid-column: 3;
}

.sidebar > .button {
float: right;
margin-right: 1.2rem;
}

.green {
color: #66a80f;
}

.red {
color: #e03131;
}

form {
font-size: 1.6rem;
display: grid;
align-items: center;
gap: 1.2rem;

background-color: var(--color-lightest);
border-radius: 7px;
}

.form-add-friend {
grid-template-columns: 1fr 1.5fr;
margin-bottom: 1.6rem;
padding: 1.2rem;
}
.form-split-bill {
grid-template-columns: 1fr 12rem;
padding: 3.2rem 4rem;
}

label {
font-weight: 500;
}

label::first-letter {
display: inline-block;
margin-right: 4px;
font-size: 1.8rem;
}

input,
select {
font-family: inherit;
color: inherit;
font-size: 1.5rem;
padding: 0.7rem;
text-align: center;
border: 1px solid var(--color-light);
border-radius: 4px;
transition: 0.3s;
}

input:focus,
select:focus {
outline: none;
border: 1px solid var(--color-dark);
}

form .button {
margin-top: 0.6rem;
grid-column: 2;
}

form h2 {
grid-column: 1 / -1;
font-size: 2.2rem;
text-transform: uppercase;
letter-spacing: -0.5px;
margin-bottom: 1.6rem;
}

=>js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { useState } from "react";

const initialFriends = [
{
id: 118836,
name: "Clark",
image: "https://i.pravatar.cc/48?u=118836",
balance: -7,
},
{
id: 933372,
name: "Sarah",
image: "https://i.pravatar.cc/48?u=933372",
balance: 20,
},
{
id: 499476,
name: "Anthony",
image: "https://i.pravatar.cc/48?u=499476",
balance: 0,
},
];

function Button({ children, onClick }) {
return (
<button className="button" onClick={onClick}>
{children}
</button>
);
}

export default function App() {
const [showAddFriend, setShowAddFriend] = useState(false);
const [friends, setFriends] = useState(initialFriends);
const [selectedFriend, setSelectFriend] = useState(null);

function handleAddFriend(friend) {
setFriends((friends) => [...friends, friend]);
setShowAddFriend(false);
}
function handleSelection(friend) {
setSelectFriend((cur) => (cur?.id === friend.id ? null : friend));
setShowAddFriend(false);
}
function handleSplitBill(value) {
setFriends((friends) =>
friends.map((friend) =>
friend.id === selectedFriend.id
? { ...friend, balance: friend.balance + value }
: friend
)
);
setSelectFriend(null);
}

return (
<div className="app">
<div className="sidebar">
<FriendList
friends={friends}
onSelection={handleSelection}
selectedFriend={selectedFriend}
/>

{showAddFriend && <FormAddFriend onAddFriends={handleAddFriend} />}

<Button onClick={() => setShowAddFriend((e) => !e)}>
{showAddFriend ? "Close" : "Add friend"}
</Button>
</div>

{selectedFriend && (
<FormSplitBill
selectedFriend={selectedFriend}
onSplitBill={handleSplitBill}
key={selectedFriend.id}
/>
)}
</div>
);
}

function FriendList({ friends, onSelection, selectedFriend }) {
return (
<ul>
{friends.map((friend) => (
<Friend
friend={friend}
key={friend.id}
onSelection={onSelection}
selectedFriend={selectedFriend}
/>
))}
</ul>
);
}

function Friend({ friend, onSelection, selectedFriend }) {
const isSelected = selectedFriend?.id === friend.id;

return (
<li className={isSelected ? "selected" : ""}>
<img src={friend.image} alt={friend.name} />
<h3>{friend.name}</h3>
{friend.balance < 0 && (
<p className="red">
You owe {friend.name} {Math.abs(friend.balance)}$
</p>
)}
{friend.balance > 0 && (
<p className="green">
{friend.name} owes you {friend.balance}$
</p>
)}
{friend.balance === 0 && <p>You and your friend are even</p>}
<Button onClick={() => onSelection(friend)}>
{isSelected ? "Close" : "Select"}
</Button>
</li>
);
}

function FormAddFriend({ onAddFriends }) {
const [name, setName] = useState("");
const [image, setImage] = useState("https://i.pravatar.cc/48");

function handleSubmit(e) {
e.preventDefault();
if (!name || !image) return;
const id = crypto.randomUUID();

const newFriend = {
id,
name,
image: `${image}?=${id}`,
balance: 0,
};

onAddFriends(newFriend);

setName("");
setImage("https://i.pravatar.cc/48");
}

return (
<form className="form-add-friend" onSubmit={handleSubmit}>
<label>👧🏻Friend name</label>
<input
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
/>

<label>🧣Image URL</label>
<input
type="text"
value={image}
onChange={(e) => setImage(e.target.value)}
/>
<Button>Add</Button>
</form>
);
}

function FormSplitBill({ selectedFriend, onSplitBill }) {
const [bill, setBill] = useState("");
const [paidByUser, setPaidByUser] = useState("");
const paidByFriend = bill ? bill - paidByUser : "";
const [whoIsPaying, setWhoIsPaying] = useState("user");

function handleSubmit(e) {
e.preventDefault();
if (!bill || !paidByUser) return;
onSplitBill(whoIsPaying === "user" ? paidByFriend : -paidByUser);
}

return (
<form className="form-split-bill" onSubmit={handleSubmit}>
<h2>Split a Bill With {selectedFriend.name}</h2>
<label>💜Bill Value</label>
<input
type="text"
value={bill}
onChange={(e) => setBill(Number(e.target.value))}
/>

<label>🔆Your expense</label>
<input
type="text"
value={paidByUser}
onChange={(e) => setPaidByUser(Number(e.target.value))}
/>

<label>☀️{selectedFriend.name}'s expense</label>
<input type="text" value={paidByFriend} disabled />

<label>🛎️Who</label>
<select
value={whoIsPaying}
onChange={(e) => setWhoIsPaying(e.target.value)}
>
<option value="user">You</option>
<option value="friend">{selectedFriend.name}</option>
</select>

<Button>Split bill</Button>
</form>
);
}


react课程6-eat-n-split
http://example.com/2024/08/16/react课程6-eat-n-split/
Author
Yaodeer
Posted on
August 16, 2024
Licensed under