React ToDo 应用程序入门

假设我们接到任务,要用 React 创建一个概念验证应用程序——一个允许用户添加、编辑和删除他们想要完成的任务,并可以在不删除任务的情况下将其标记为已完成的应用程序。本文将引导您了解此类应用程序的基本结构和样式,为单个组件的定义和交互性做好准备,我们稍后会添加这些内容。

注意:如果您需要对照我们的版本检查您的代码,可以在我们的 todo-react 仓库中找到示例 React 应用程序代码的最终版本。要查看正在运行的实时版本,请访问 https://mdn.github.io/todo-react/

预备知识 熟悉核心 HTMLCSSJavaScript 语言,以及 终端/命令行
学习成果 熟悉我们的待办事项列表案例研究,并完成基本的 App 结构和样式。

我们应用程序的用户故事

在软件开发中,用户故事是从用户的角度来看的一个可操作的目标。在我们开始工作之前定义用户故事将有助于我们集中精力。我们的应用程序应该满足以下故事:

作为用户,我可以

  • 阅读任务列表。
  • 使用鼠标或键盘添加任务。
  • 使用鼠标或键盘将任何任务标记为已完成。
  • 使用鼠标或键盘删除任何任务。
  • 使用鼠标或键盘编辑任何任务。
  • 查看特定子集的任务:所有任务、仅活动任务或仅已完成任务。

我们将逐一解决这些故事。

项目前的准备工作

Vite 给了我们一些我们完全不会用于项目中的代码。以下终端命令将删除它,为我们的新项目腾出空间。请确保您从应用程序的根目录开始!

bash
# 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

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> 匹配。

html
<title>TodoMatic</title>

当您的浏览器刷新时,您应该会看到类似以下内容:

todo-matic app, unstyled, showing a jumbled mess of labels, inputs, and buttons

它很丑,并且还没有功能,但这没关系——我们稍后会进行样式设置。首先,考虑我们拥有的 JSX,以及它如何与我们的用户故事对应:

  • 我们有一个 <form> 元素,其中包含一个 <input type="text"> 用于编写新任务,以及一个提交表单的按钮。
  • 我们有一个按钮数组,将用于过滤任务。
  • 我们有一个标题,告诉我们还剩下多少任务。
  • 我们有 3 个任务,排列在一个无序列表中。每个任务都是一个列表项 (<li>),并具有编辑和删除它的按钮,以及一个用于标记为已完成的复选框。

表单将允许我们创建任务;按钮将允许我们过滤任务;标题和列表是我们阅读任务的方式。用于编辑任务的 UI 目前明显缺失。没关系——我们稍后会编写它。

辅助功能

您可能会注意到这里有一些不寻常的标记。例如:

jsx
<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 告诉辅助技术(如屏幕阅读器)按钮可以处于两种状态之一:pressedunpressed。将它们视为 onoff 的模拟。将值设置为 "true" 意味着按钮默认被按下。

visually-hidden 类目前没有任何效果,因为我们还没有包含任何 CSS。但是,一旦我们设置好样式,任何具有此类的元素都将对有视力的用户隐藏,但仍可供辅助技术用户使用——这是因为有视力的用户不需要这些词;它们用于为没有额外视觉上下文的辅助技术用户提供有关按钮功能的更多信息。

往下看,您可以找到我们的 <ul> 元素

html
<ul
  role="list"
  className="todo-list stack-large stack-exception"
  aria-labelledby="list-heading">
  …
</ul>

role 属性帮助辅助技术解释标签代表何种元素。<ul> 默认被视为列表,但我们即将添加的样式会破坏该功能。此角色将恢复 <ul> 元素的“列表”含义。如果您想了解为什么这是必要的,可以查看 Scott O'Hara 的文章,“Fixing Lists”

aria-labelledby 属性告诉辅助技术,我们将列表标题视为描述其下方列表用途的标签。这种关联为列表提供了更具信息性的上下文,这有助于辅助技术用户更好地理解列表的用途。

最后,我们列表项中的标签和输入具有 JSX 特有的一些属性

jsx
<div className="c-cb">
  <input id="todo-0" type="checkbox" defaultChecked />
  <label className="todo-label" htmlFor="todo-0">
    Eat
  </label>
</div>

<input /> 标签中的 defaultChecked 属性告诉 React 初始勾选此复选框。如果像在常规 HTML 中那样使用 checked,React 会在我们的浏览器控制台中记录一些关于处理复选框事件的警告,这是我们希望避免的。现在不用太担心这个问题——我们稍后在处理事件时会涉及它。

htmlFor 属性对应于 HTML 中使用的 for 属性。我们不能在 JSX 中将 for 用作属性,因为 for 是保留字,所以 React 改用 htmlFor

关于 JSX 中布尔属性的注意事项

上一节中的 defaultChecked 属性是一个布尔属性——其值是 truefalse 的属性。与 HTML 中一样,布尔属性如果存在则为 true,如果不存在则为 false;表达式右侧的赋值是可选的。您可以通过在大括号中传递它来显式设置其值——例如,defaultChecked={true}defaultChecked={false}

由于 JSX 是 JavaScript,因此布尔属性有一个需要注意的陷阱:写入 defaultChecked="false" 将设置一个 "false"字符串值,而不是布尔值。非空字符串是真值,因此 React 会认为 defaultCheckedtrue,并默认勾选复选框。这不是我们想要的,所以我们应该避免它。

如果您愿意,可以使用您可能以前见过的另一个属性 hidden 来练习编写布尔属性,它阻止元素在页面上渲染。尝试将 hidden 添加到 App.jsx 中的 <h1> 元素中,看看会发生什么,然后尝试显式将其值设置为 {false}。再次注意,写入 hidden="false" 会导致一个真值,因此 <h1> *将*隐藏。完成时不要忘记删除此代码。

注意:我们早期代码片段中使用的 aria-pressed 属性的值为 "true",因为 aria-pressed 并非像 checked 那样是真正的布尔属性。

实现我们的样式

将以下 CSS 代码粘贴到 src/index.css

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: whitesmoke;
  color: #4d4d4d;
  font:
    1.6rem/1.25 "Arial",
    sans-serif;
  margin: 0 auto;
  max-width: 68rem;
  width: 100%;
}
@media screen and (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: lightgray;
  border-width: 1px;
}
.btn.toggle-btn[aria-pressed="true"] {
  border-color: #4d4d4d;
  text-decoration: underline;
}
.btn__danger {
  background-color: #ca3c3c;
  border-color: #bd2130;
  color: white;
}
.btn__filter {
  border-color: lightgrey;
}
.btn__primary {
  background-color: black;
  color: white;
}
.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);
  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 (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: white;
  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 (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 black;
  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 (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: normal;
  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 */

保存并回到您的浏览器,您的应用程序现在应该具有合理的样式了。

总结

现在我们的待办事项列表应用程序看起来更像一个真正的应用程序了!问题是:它实际上什么也做不了。我们将在下一章开始解决这个问题!