Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit todo items #126

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/renderer/src/AppContext.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const appStateReducer = (state, action) => {
if (i.key === action.item.key) {
return Object.assign({}, i, {
status: action.item.status,
text: action.item.text
});
}
return i;
Expand Down Expand Up @@ -108,4 +109,4 @@ export function AppStateProvider({ children }) {
<AppContext.Provider value={value}>{children}</AppContext.Provider>
</div>
);
}
}
46 changes: 42 additions & 4 deletions src/renderer/src/components/Item.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { useState, useRef } from "react";
import { useAppReducer } from "../AppContext.jsx";
import styles from "./Item.module.css";

// Individual todo item
function Item({ item }) {
const dispatch = useAppReducer();

const [isEditMode, setIsEditMode] = useState(false);
const editValueRef = useRef(item.text);

let text = item.text;
let paused = item.status === "paused";
let completed = item.status === "completed";
Expand All @@ -12,6 +17,13 @@ function Item({ item }) {
dispatch({ type: "DELETE_ITEM", item });
}

function editItem() {
const editedValue = editValueRef.current.value === "" ? text : editValueRef.current.value;
const editedItem = { ...item, text: editedValue };
dispatch({ type: "UPDATE_ITEM", item: editedItem });
setIsEditMode(false);
}

function pauseItem() {
const pausedItem = { ...item, status: "paused" };
dispatch({ type: "UPDATE_ITEM", item: pausedItem });
Expand All @@ -29,13 +41,37 @@ function Item({ item }) {

return (
<div className={styles.item} tabIndex="0">
<div className={styles.itemname}>{text}</div>
<div
{isEditMode && (
<form
className={styles.editform}
onSubmit={() => editItem()}>
<input
ref={editValueRef}
defaultValue={text}
autoFocus
onBlur={() => editItem()}
/>
</form>
)}
{!isEditMode && (
<div
className={styles.itemname}
onDoubleClick={() => setIsEditMode(true)}
>
{text}
</div>
)}
{!isEditMode && (
<div
className={`${styles.buttons} ${
completed ? styles.completedButtons : ""
}`}
>
{completed && <button className={styles.empty} tabIndex="0"></button>}
<button
className={styles.edit}
onClick={() => setIsEditMode(true)}
tabIndex="0"
></button>
<button
className={styles.delete}
onClick={deleteItem}
Expand All @@ -62,9 +98,11 @@ function Item({ item }) {
tabIndex="0"
></button>
)}
{completed && <button className={styles.empty} tabIndex="0"></button>}
</div>
)}
</div>
);
}

export default Item;
export default Item;
27 changes: 25 additions & 2 deletions src/renderer/src/components/Item.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,25 @@
}
}

.editform {
width: 100%;
input {
width: 100%;
height: 24px;
padding: 0 10px;
background: var(--input-color);
border: none;
border-radius: 3px;
box-sizing: border-box;
color: var(--font-color);
outline: none;
}
}

.buttons {
display: flex;
justify-content: space-between;
width: 100px;
width: 140px;

button {
position: relative;
Expand Down Expand Up @@ -86,6 +101,14 @@
}
}

&.edit {
width: 30px;
background: no-repeat url("../img/edit.svg");
&:after {
background: var(--blue);
}
}

&::after {
display: block;
content: "";
Expand All @@ -110,4 +133,4 @@
}
}
}
}
}
6 changes: 6 additions & 0 deletions src/renderer/src/img/edit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.