开始我们的 React 待办事项清单
假设我们被要求使用 React 创建一个概念验证——一个允许用户添加、编辑和删除他们想要完成的任务的应用程序,并且还可以标记任务为已完成而无需删除它们。本文将引导您完成此类应用程序的基本结构和样式,为各个组件的定义和交互性做好准备,这些将在稍后添加。
注意:如果您需要将您的代码与我们的版本进行比较,您可以在我们的 todo-react 代码库 中找到此示例 React 应用的完整版本。要查看运行中的版本,请访问 https://mdn.github.io/todo-react/。
| 先决条件 |
熟悉核心 HTML、CSS 和 JavaScript 语言,了解 终端/命令行。 |
|---|---|
| 目标 | 介绍我们的待办事项列表案例研究,并准备好基本的 App 结构和样式。 |
我们应用程序的用户故事
在软件开发中,用户故事是从用户角度出发的一个可操作的目标。在我们开始工作之前定义用户故事将有助于我们集中精力。我们的应用程序应满足以下故事
作为用户,我可以
- 读取任务列表。
- 使用鼠标或键盘添加任务。
- 使用鼠标或键盘将任何任务标记为已完成。
- 使用鼠标或键盘删除任何任务。
- 使用鼠标或键盘编辑任何任务。
- 查看特定子集的任务:所有任务、仅活动任务或仅已完成的任务。
我们将逐一解决这些故事。
项目准备工作
Vite 为我们提供了一些我们项目中根本不会使用的代码。以下终端命令将删除它,为我们的新项目腾出空间。确保您从应用程序的根目录开始!
# Move into the src directory
cd src
# Delete the App.css file and the React logo provided by Vite
rm App.css assets/react.svg
# Empty the contents of App.jsx and index.css
echo -n > App.jsx && echo -n > index.css
# Move back up to the root of the project
cd ..
注意:如果您停止了服务器以执行上面提到的终端任务,则需要使用 npm run dev 重新启动它。
项目启动代码
作为此项目的起点,我们将提供两样东西:一个 App() 函数来替换您刚刚删除的函数,以及一些用于为您的应用程序设置样式的 CSS。
JSX 代码
将以下代码段复制到剪贴板,然后将其粘贴到 App.jsx 中
function App(props) {
return (
<div className="todoapp stack-large">
<h1>TodoMatic</h1>
<form>
<h2 className="label-wrapper">
<label htmlFor="new-todo-input" className="label__lg">
What needs to be done?
</label>
</h2>
<input
type="text"
id="new-todo-input"
className="input input__lg"
name="text"
autoComplete="off"
/>
<button type="submit" className="btn btn__primary btn__lg">
Add
</button>
</form>
<div className="filters btn-group stack-exception">
<button type="button" className="btn toggle-btn" aria-pressed="true">
<span className="visually-hidden">Show </span>
<span>all</span>
<span className="visually-hidden"> tasks</span>
</button>
<button type="button" className="btn toggle-btn" aria-pressed="false">
<span className="visually-hidden">Show </span>
<span>Active</span>
<span className="visually-hidden"> tasks</span>
</button>
<button type="button" className="btn toggle-btn" aria-pressed="false">
<span className="visually-hidden">Show </span>
<span>Completed</span>
<span className="visually-hidden"> tasks</span>
</button>
</div>
<h2 id="list-heading">3 tasks remaining</h2>
<ul
role="list"
className="todo-list stack-large stack-exception"
aria-labelledby="list-heading">
<li className="todo stack-small">
<div className="c-cb">
<input id="todo-0" type="checkbox" defaultChecked />
<label className="todo-label" htmlFor="todo-0">
Eat
</label>
</div>
<div className="btn-group">
<button type="button" className="btn">
Edit <span className="visually-hidden">Eat</span>
</button>
<button type="button" className="btn btn__danger">
Delete <span className="visually-hidden">Eat</span>
</button>
</div>
</li>
<li className="todo stack-small">
<div className="c-cb">
<input id="todo-1" type="checkbox" />
<label className="todo-label" htmlFor="todo-1">
Sleep
</label>
</div>
<div className="btn-group">
<button type="button" className="btn">
Edit <span className="visually-hidden">Sleep</span>
</button>
<button type="button" className="btn btn__danger">
Delete <span className="visually-hidden">Sleep</span>
</button>
</div>
</li>
<li className="todo stack-small">
<div className="c-cb">
<input id="todo-2" type="checkbox" />
<label className="todo-label" htmlFor="todo-2">
Repeat
</label>
</div>
<div className="btn-group">
<button type="button" className="btn">
Edit <span className="visually-hidden">Repeat</span>
</button>
<button type="button" className="btn btn__danger">
Delete <span className="visually-hidden">Repeat</span>
</button>
</div>
</li>
</ul>
</div>
);
}
export default App;
现在打开 index.html 并将 <title> 元素的文本更改为 TodoMatic。这样,它将与应用程序顶部的 <h1> 匹配。
<title>TodoMatic</title>
浏览器刷新后,您应该会看到类似以下内容
它看起来很丑,并且还不能正常工作,但这没关系——我们稍后会对其进行样式设置。首先,考虑一下我们拥有的 JSX,以及它如何与我们的用户故事相对应。
- 我们有一个
<form>元素,其中包含一个<input type="text">用于编写新任务,以及一个提交表单的按钮。 - 我们有一组按钮,将用于过滤我们的任务。
- 我们有一个标题,告诉我们还有多少任务。
- 我们有 3 个任务,排列在一个无序列表中。每个任务都是一个列表项 (
<li>),并且有用于编辑和删除它的按钮,以及一个复选框用于将其标记为已完成。
表单将允许我们创建任务;按钮将允许我们过滤它们;标题和列表是我们读取它们的方式。编辑任务的 UI 目前缺失。没关系——我们稍后会编写它。
辅助功能特性
您可能会注意到这里有一些不寻常的标记。例如
<button type="button" className="btn toggle-btn" aria-pressed="true">
<span className="visually-hidden">Show </span>
<span>all</span>
<span className="visually-hidden"> tasks</span>
</button>
这里,aria-pressed 告诉辅助技术(如屏幕阅读器)按钮可以处于两种状态之一:pressed 或 unpressed。可以将其视为 on 和 off 的类似物。设置值为 "true" 表示按钮默认处于按下状态。
类 visually-hidden 目前没有任何效果,因为我们还没有包含任何 CSS。但是,一旦我们设置了样式,任何具有此类的元素都将对有视力用户隐藏,但仍可供辅助技术用户使用——这是因为这些文字对有视力用户来说是不必要的;它们是为了为那些没有额外视觉上下文来帮助他们的辅助技术用户提供有关按钮功能的更多信息。
在下面,您可以找到我们的 <ul> 元素
<ul
role="list"
className="todo-list stack-large stack-exception"
aria-labelledby="list-heading">
…
</ul>
role 属性帮助辅助技术解释标签表示哪种类型的元素。<ul> 默认被视为列表,但我们即将添加的样式将破坏此功能。此角色将“列表”含义恢复到 <ul> 元素。如果您想了解更多有关这为什么必要的信息,您可以查看 Scott O'Hara 的文章“修复列表”。
aria-labelledby 属性告诉辅助技术我们正在将列表标题视为描述其下方列表目的的标签。建立这种关联使列表具有更具信息性的上下文,这可以帮助辅助技术用户更好地理解列表的目的。
最后,列表项中的标签和输入有一些 JSX 独有的属性
<input id="todo-0" type="checkbox" defaultChecked />
<label className="todo-label" htmlFor="todo-0">
Eat
</label>
<input /> 标签中的 defaultChecked 属性告诉 React 初始选中此复选框。如果我们使用 checked(就像我们在普通 HTML 中一样),React 会将一些警告记录到我们的浏览器控制台中,这些警告与处理复选框上的事件有关,我们希望避免这种情况。暂时不用太担心这一点——我们将在稍后处理事件时介绍它。
htmlFor 属性对应于 HTML 中使用的 for 属性。我们不能在 JSX 中将 for 作为属性使用,因为 for 是一个保留字,所以 React 使用 htmlFor 代替。
关于 JSX 中的布尔属性
上一节中的 defaultChecked 属性是一个布尔属性——一个值可以是 true 或 false 的属性。与 HTML 一样,如果布尔属性存在则为 true,如果不存在则为 false;表达式右侧的赋值是可选的。您可以通过在花括号中传递它来显式设置其值——例如,defaultChecked={true} 或 defaultChecked={false}。
因为 JSX 是 JavaScript,所以布尔属性有一个需要注意的陷阱:编写 defaultChecked="false" 将设置 "false" 的字符串值,而不是布尔值。非空字符串是 真值,因此 React 会认为 defaultChecked 为 true 并默认选中复选框。这不是我们想要的,所以我们应该避免这种情况。
如果您愿意,您可以使用之前可能见过的另一个属性来练习编写布尔属性,hidden,它可以阻止元素在页面上呈现。尝试将 hidden 添加到 App.jsx 中的 <h1> 元素以查看会发生什么,然后尝试将其值显式设置为 {false}。再次注意,编写 hidden="false" 会导致真值,因此 <h1> 将隐藏。完成后不要忘记删除此代码。
注意:我们之前代码片段中使用的 aria-pressed 属性的值为 "true",因为 aria-pressed 不是像 checked 那样真正的布尔属性。
实现我们的样式
将以下 CSS 代码粘贴到 src/index.css 中
/* Resets */
*,
*::before,
*::after {
box-sizing: border-box;
}
*:focus-visible {
outline: 3px dashed #228bec;
outline-offset: 0;
}
html {
font: 62.5% / 1.15 sans-serif;
}
h1,
h2 {
margin-bottom: 0;
}
ul {
list-style: none;
padding: 0;
}
button {
-moz-osx-font-smoothing: inherit;
-webkit-font-smoothing: inherit;
appearance: none;
background: transparent;
border: none;
color: inherit;
font: inherit;
line-height: normal;
margin: 0;
overflow: visible;
padding: 0;
width: auto;
}
button::-moz-focus-inner {
border: 0;
}
button,
input,
optgroup,
select,
textarea {
font-family: inherit;
font-size: 100%;
line-height: 1.15;
margin: 0;
}
button,
input {
overflow: visible;
}
input[type="text"] {
border-radius: 0;
}
body {
background-color: #f5f5f5;
color: #4d4d4d;
font:
1.6rem/1.25 Arial,
sans-serif;
margin: 0 auto;
max-width: 68rem;
width: 100%;
}
@media screen and (min-width: 620px) {
body {
font-size: 1.9rem;
line-height: 1.31579;
}
}
/* End resets */
/* Global styles */
.form-group > input[type="text"] {
display: inline-block;
margin-top: 0.4rem;
}
.btn {
border: 0.2rem solid #4d4d4d;
cursor: pointer;
padding: 0.8rem 1rem 0.7rem;
text-transform: capitalize;
}
.btn.toggle-btn {
border-color: #d3d3d3;
border-width: 1px;
}
.btn.toggle-btn[aria-pressed="true"] {
border-color: #4d4d4d;
text-decoration: underline;
}
.btn__danger {
background-color: #ca3c3c;
border-color: #bd2130;
color: #fff;
}
.btn__filter {
border-color: lightgrey;
}
.btn__primary {
background-color: #000;
color: #fff;
}
.btn-group {
display: flex;
justify-content: space-between;
}
.btn-group > * {
flex: 1 1 49%;
}
.btn-group > * + * {
margin-left: 0.8rem;
}
.label-wrapper {
flex: 0 0 100%;
margin: 0;
text-align: center;
}
.visually-hidden {
clip: rect(1px 1px 1px 1px);
clip: rect(1px, 1px, 1px, 1px);
height: 1px;
overflow: hidden;
position: absolute !important;
white-space: nowrap;
width: 1px;
}
[class*="stack"] > * {
margin-bottom: 0;
margin-top: 0;
}
.stack-small > * + * {
margin-top: 1.25rem;
}
.stack-large > * + * {
margin-top: 2.5rem;
}
@media screen and (min-width: 550px) {
.stack-small > * + * {
margin-top: 1.4rem;
}
.stack-large > * + * {
margin-top: 2.8rem;
}
}
.stack-exception {
margin-top: 1.2rem;
}
/* End global styles */
/* General app styles */
.todoapp {
background: #fff;
box-shadow:
0 2px 4px 0 rgb(0 0 0 / 20%),
0 2.5rem 5rem 0 rgb(0 0 0 / 10%);
margin: 2rem 0 4rem 0;
padding: 1rem;
position: relative;
}
@media screen and (min-width: 550px) {
.todoapp {
padding: 4rem;
}
}
.todoapp > * {
margin-left: auto;
margin-right: auto;
max-width: 50rem;
}
.todoapp > form {
max-width: 100%;
}
.todoapp > h1 {
display: block;
margin: 0;
margin-bottom: 1rem;
max-width: 100%;
text-align: center;
}
.label__lg {
line-height: 1.01567;
font-weight: 300;
margin-bottom: 1rem;
padding: 0.8rem;
text-align: center;
}
.input__lg {
border: 2px solid #000;
padding: 2rem;
}
.input__lg:focus-visible {
border-color: #4d4d4d;
box-shadow: inset 0 0 0 2px;
}
[class*="__lg"] {
display: inline-block;
font-size: 1.9rem;
width: 100%;
}
[class*="__lg"]:not(:last-child) {
margin-bottom: 1rem;
}
@media screen and (min-width: 620px) {
[class*="__lg"] {
font-size: 2.4rem;
}
}
/* End general app styles */
/* Todo item styles */
.todo {
display: flex;
flex-direction: row;
flex-wrap: wrap;
}
.todo > * {
flex: 0 0 100%;
}
.todo-text {
border: 2px solid #565656;
min-height: 4.4rem;
padding: 0.4rem 0.8rem;
width: 100%;
}
.todo-text:focus-visible {
box-shadow: inset 0 0 0 2px;
}
/* End todo item styles */
/* Checkbox styles */
.c-cb {
-webkit-font-smoothing: antialiased;
box-sizing: border-box;
clear: left;
display: block;
font-family: Arial, sans-serif;
font-size: 1.6rem;
font-weight: 400;
line-height: 1.25;
min-height: 44px;
padding-left: 40px;
position: relative;
}
.c-cb > label::before,
.c-cb > input[type="checkbox"] {
box-sizing: border-box;
height: 44px;
left: -2px;
top: -2px;
width: 44px;
}
.c-cb > input[type="checkbox"] {
-webkit-font-smoothing: antialiased;
cursor: pointer;
margin: 0;
opacity: 0;
position: absolute;
z-index: 1;
}
.c-cb > label {
cursor: pointer;
display: inline-block;
font-family: inherit;
font-size: inherit;
line-height: inherit;
margin-bottom: 0;
padding: 8px 15px 5px;
touch-action: manipulation;
}
.c-cb > label::before {
background: transparent;
border: 2px solid currentcolor;
content: "";
position: absolute;
}
.c-cb > input[type="checkbox"]:focus-visible + label::before {
border-width: 4px;
outline: 3px dashed #228bec;
}
.c-cb > label::after {
background: transparent;
border: solid;
border-width: 0 0 5px 5px;
border-top-color: transparent;
box-sizing: content-box;
content: "";
height: 7px;
left: 9px;
opacity: 0;
position: absolute;
top: 11px;
transform: rotate(-45deg);
width: 18px;
}
.c-cb > input[type="checkbox"]:checked + label::after {
opacity: 1;
}
/* End checkbox styles */
保存并查看浏览器,您的应用程序现在应该具有合理的样式。