Compare commits

...

1 Commits

Author SHA1 Message Date
Anselm
71f68b0f60 Try using react-hook-form 2024-08-09 15:09:33 +01:00
4 changed files with 55 additions and 59 deletions

View File

@@ -27,6 +27,7 @@
"qrcode": "^1.5.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hook-form": "^7.52.2",
"react-router": "^6.16.0",
"react-router-dom": "^6.16.0",
"tailwind-merge": "^1.14.0",

View File

@@ -6,7 +6,7 @@ import {
} from "react-router-dom";
import "./index.css";
import { createJazzReactContext, PasskeyAuth } from "jazz-react";
import { createJazzReactContext, DemoAuth, PasskeyAuth } from "jazz-react";
import {
Button,
@@ -31,9 +31,8 @@ import { TodoAccount, TodoProject } from "./1_schema.ts";
const appName = "Jazz Todo List Example";
const auth = PasskeyAuth<TodoAccount>({
const auth = DemoAuth<TodoAccount>({
appName,
Component: PrettyAuthUI,
accountSchema: TodoAccount,
});
const Jazz = createJazzReactContext<TodoAccount>({

View File

@@ -19,6 +19,7 @@ import uniqolor from "uniqolor";
import { useParams } from "react-router";
import { ID } from "jazz-tools";
import { useCoState } from "./2_main";
import { useForm } from "react-hook-form";
/** Walkthrough: Reactively rendering a todo project as a table,
* adding and editing tasks
@@ -76,68 +77,50 @@ export function ProjectTodoTable() {
</h1>
<InviteButton value={project} valueHint="project" />
</div>
<Table>
<TableHeader>
<TableRow>
<TableHead className="w-[40px]">Done</TableHead>
<TableHead>Task</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{project?.tasks?.map(
(task) => task && <TaskRow key={task.id} task={task} />,
)}
<NewTaskInputRow
createTask={createTask}
disabled={!project}
/>
</TableBody>
</Table>
{project?.tasks?.map(
(task) => task && <TaskRow key={task.id} task={task} />,
)}
<NewTaskInputRow createTask={createTask} disabled={!project} />
</div>
);
}
export function TaskRow({ task }: { task: Task | undefined }) {
const { register, handleSubmit, formState } = useForm({
values: task && { text: task.text, done: task.done },
});
return (
<TableRow>
<TableCell>
<Checkbox
className="mt-1"
checked={task?.done}
onCheckedChange={(checked) => {
// Tick or untick the task
// Task is also immutable, but this will update all queries
// that include this task as a reference
if (task) task.done = !!checked;
}}
/>
</TableCell>
<TableCell>
<div className="flex flex-row justify-between items-center gap-2">
{task?.text ? (
<span className={task?.done ? "line-through" : ""}>
{task.text}
</span>
) : (
<Skeleton className="mt-1 w-[200px] h-[1em] rounded-full" />
)}
{
// Here we see for the first time how we can access edit history
// for a CoValue, and use it to display who created the task.
task?._edits.text?.by?.profile?.name ? (
<span
className="rounded-full py-0.5 px-2 text-xs"
style={uniqueColoring(task._edits.text.by.id)}
>
{task._edits.text.by.profile.name}
</span>
) : (
<Skeleton className="mt-1 w-[50px] h-[1em] rounded-full" />
)
}
</div>
</TableCell>
</TableRow>
<form
onSubmit={handleSubmit((newValues) => task?.applyDiff(newValues))}
className="flex gap-2 items-center"
>
<Checkbox className="mt-1" {...register("done")} />
{task?.text ? (
// <span className={task?.done ? "line-through" : ""}>
// {task.text}
// </span>
<input type="text" {...register("text")} />
) : (
<Skeleton className="mt-1 w-[200px] h-[1em] rounded-full" />
)}
{formState.isDirty && <input type="submit" value="Save" />}
{
// Here we see for the first time how we can access edit history
// for a CoValue, and use it to display who created the task.
task?._edits.text?.by?.profile?.name ? (
<span
className="rounded-full py-0.5 px-2 text-xs"
style={uniqueColoring(task._edits.text.by.id)}
>
{task._edits.text.by.profile.name}
</span>
) : (
<Skeleton className="mt-1 w-[50px] h-[1em] rounded-full" />
)
}
</form>
);
}

13
pnpm-lock.yaml generated
View File

@@ -368,6 +368,9 @@ importers:
react-dom:
specifier: ^18.2.0
version: 18.2.0(react@18.2.0)
react-hook-form:
specifier: ^7.52.2
version: 7.52.2(react@18.2.0)
react-router:
specifier: ^6.16.0
version: 6.21.0(react@18.2.0)
@@ -3760,6 +3763,12 @@ packages:
peerDependencies:
react: ^18.2.0
react-hook-form@7.52.2:
resolution: {integrity: sha512-pqfPEbERnxxiNMPd0bzmt1tuaPcVccywFDpyk2uV5xCIBphHV5T8SVnX9/o3kplPE1zzKt77+YIoq+EMwJp56A==}
engines: {node: '>=18.0.0'}
peerDependencies:
react: ^16.8.0 || ^17 || ^18 || ^19
react-is@18.2.0:
resolution: {integrity: sha512-xWGDIW6x921xtzPkhiULtthJHoJvBbF3q26fzloPCK0hsvxtPVelvftw3zjbHWSkR2km9Z+4uxbDDK/6Zw9B8w==}
@@ -8027,6 +8036,10 @@ snapshots:
react: 18.2.0
scheduler: 0.23.0
react-hook-form@7.52.2(react@18.2.0):
dependencies:
react: 18.2.0
react-is@18.2.0: {}
react-router-dom@6.21.0(react-dom@18.2.0(react@18.2.0))(react@18.2.0):