Skip to content

Commit b4bd6ea

Browse files
committed
fix checkbox onchange
1 parent 5e2eee6 commit b4bd6ea

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

client/src/demo/Demo.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,22 @@ const Demo = () => {
4848
setNewTodo(e.target.value);
4949
};
5050

51-
const onCompleteTodo = (e: any, sqlId: number) => {
51+
const onCompleteTodo = (
52+
e: React.ChangeEvent<HTMLInputElement>,
53+
sqlId: number
54+
) => {
5255
const { id, checked } = e.target;
5356
const newTodos = todos;
5457
const todo = todos.find((todo) => todo.id === sqlId);
5558

56-
if (todo) {
57-
todo[id as keyof Todo] = checked as never;
59+
if (todo && id === 'completed') {
60+
onUpdateTodo(sqlId, checked, id).then((res) => {
61+
if (res?.status === 200) {
62+
todo[id] = checked;
63+
setTodos([...newTodos]);
64+
}
65+
});
5866
}
59-
60-
onUpdateTodo(sqlId, checked, id).then((res) => {
61-
if (res?.status === 200) {
62-
setTodos([...newTodos]);
63-
}
64-
});
6567
};
6668

6769
const onSubmitCreateTodo = async (e: React.FormEvent) => {
@@ -246,7 +248,7 @@ const Demo = () => {
246248
<TableCell style={{ textAlign: 'center' }}>
247249
<Checkbox
248250
checked={todo.completed}
249-
onClick={(e) => onCompleteTodo(e, todo.id)}
251+
onChange={(e) => onCompleteTodo(e, todo.id)}
250252
id="completed"
251253
/>
252254
</TableCell>

0 commit comments

Comments
 (0)