Skip to content

Commit

Permalink
refactor react components
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaozixiao committed Dec 13, 2021
1 parent 0403d2f commit 1e4d942
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
9 changes: 7 additions & 2 deletions todo-web/src/components/FilterButton.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
function FilterButton(props) {
return (
<button type="button" className="btn toggle-btn" aria-pressed="true">
<button
type="button"
className="btn toggle-btn"
aria-pressed={props.isPressed}
onClick={() => props.setFilter(props.name)}
>
<span className="visually-hidden">Show </span>
<span>all </span>
<span>{props.name}</span>
<span className="visually-hidden"> tasks</span>
</button>
);
Expand Down
14 changes: 13 additions & 1 deletion todo-web/src/components/Todo.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import React, { useState } from "react";
import React, { useEffect, useRef, useState } from "react";

export default function Todo(props) {
const [isEditing, setEditing] = useState(false);
const [newName, setNewName] = useState('');
const editFieldRef = useRef(null);
const editButtonRef = useRef(null);
useEffect(() => {
if (isEditing) {
editFieldRef.current.focus();
} else {
editButtonRef.current.focus();
}
}, [isEditing]);
function handleChange(e) {
setNewName(e.target.value);
}
Expand All @@ -22,7 +31,9 @@ export default function Todo(props) {
id={props.id}
className="todo-text"
type="text"
value={newName}
onChange={handleChange}
ref={editFieldRef}
/>
</div>
<div className="btn-group">
Expand Down Expand Up @@ -59,6 +70,7 @@ export default function Todo(props) {
type="button"
className="btn"
onClick={() => setEditing(true)}
ref={editButtonRef}
>
Edit <span className="visually-hidden">{props.name}</span>
</button>
Expand Down
6 changes: 0 additions & 6 deletions todo-web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';

const DATA = [
{ id: "todo-0", name: "Eat", completed: true },
{ id: "todo-1", name: "Sleep", completed: false },
{ id: "todo-2", name: "Repeat", completed: false }
];

ReactDOM.render(<App />, document.getElementById("root"));

// If you want to start measuring performance in your app, pass a function
Expand Down

0 comments on commit 1e4d942

Please sign in to comment.