Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Add new task autofocus feature on task creation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicodeck committed Feb 9, 2024
1 parent 42b5379 commit 15d82e3
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/components/TodoList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import {
} from "react-native";
import TaskLine from "./TaskLine";
import Ionicons from "@expo/vector-icons/Ionicons";
import { TodoListsAtom } from "@/state";
import { atom, useAtom } from "jotai";
import { TodoListsAtom, lastCreatedTaskKeyAtom } from "@/state";
import { atom, useAtom, useAtomValue } from "jotai";
import { useEffect, useState } from "react";

interface TodoListProps {
todoListKey: string;
Expand All @@ -20,6 +21,10 @@ const completedTasksContainerIsOpenAtom = atom(false);
export default function TodoList({ todoListKey }: TodoListProps) {
const [todoLists, setTodoLists] = useAtom(TodoListsAtom);

const [justAddedANewTask, setJustAddedANewTask] = useState(false);

const lastCreatedTaskKey = useAtomValue(lastCreatedTaskKeyAtom);

const tasks =
todoListKey && todoLists[todoListKey] ? todoLists[todoListKey].tasks : {};

Expand Down Expand Up @@ -47,6 +52,7 @@ export default function TodoList({ todoListKey }: TodoListProps) {
type: "AddNewTask",
todoListKey: todoListKey,
});
setJustAddedANewTask(true);
};

const completedTasksList = () => {
Expand All @@ -63,14 +69,25 @@ export default function TodoList({ todoListKey }: TodoListProps) {
);
};

useEffect(() => {
if (justAddedANewTask) {
setJustAddedANewTask(false);
}
}, [justAddedANewTask]);

return (
<View style={{ flex: 1 }}>
<ScrollView>
{countOfNotCompletedTasks > 0 ? (
Object.keys(tasks).map((key) => {
if (!tasks[key].isCompleted) {
return (
<TaskLine key={key} todoListKey={todoListKey} taskKey={key} />
<TaskLine
key={key}
todoListKey={todoListKey}
taskKey={key}
isAutoFocus={lastCreatedTaskKey === key && justAddedANewTask}
/>
);
}
})
Expand Down

0 comments on commit 15d82e3

Please sign in to comment.