Skip to content

Commit 94f0bd1

Browse files
committed
fix: move type statement of handlers to child component to use them.
1 parent e395b8f commit 94f0bd1

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

src/components/EditTodoForm.vue

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
<script lang="ts">
1515
import { defineComponent, PropType, Ref, ref, onMounted } from "vue";
1616
import type Todo from "@/models/todo";
17-
import type {
18-
HandleCancelEdit,
19-
HandleSubmitEditedTodo,
20-
} from "@/pages/Index.vue";
17+
18+
export type HandleCancelEdit = () => void;
19+
export type HandleSubmitEditedTodo = (editedTodo: Todo) => void;
2120
2221
export default defineComponent({
2322
props: {

src/components/TodoItem.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
import { defineComponent, PropType } from "vue";
2626
import { FontAwesomeIcon } from "@/plugins/font-awesome";
2727
import Todo from "@/models/todo";
28-
import type { HandleClickEdit, HandleClickRemove, HandleToggleCompleted } from "@/pages/Index.vue";
28+
29+
export type HandleToggleCompleted = (todo: Todo) => void;
30+
export type HandleClickEdit = (todo: Todo) => void;
31+
export type HandleClickRemove = (todo: Todo) => void;
2932
3033
export default defineComponent({
3134
components: { fa: FontAwesomeIcon },
@@ -47,7 +50,7 @@ export default defineComponent({
4750
type: Function as PropType<HandleClickRemove>,
4851
required: true
4952
}
50-
},
53+
}
5154
});
5255
</script>
5356

src/components/TodoList.vue

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ import Todo from "@/models/todo";
2424
import TodoItem from "@/components/TodoItem.vue";
2525
import EditTodoForm from "@/components/EditTodoForm.vue";
2626
27+
import type {
28+
HandleToggleCompleted,
29+
HandleClickEdit,
30+
HandleClickRemove,
31+
} from "@/components/TodoItem.vue";
32+
import type {
33+
HandleCancelEdit,
34+
HandleSubmitEditedTodo,
35+
} from "@/components/EditTodoForm.vue";
36+
2737
export default defineComponent({
2838
components: { TodoItem, EditTodoForm },
2939
@@ -33,29 +43,27 @@ export default defineComponent({
3343
required: true
3444
},
3545
todoIdBeingEdited: {
36-
// nullable のため required は false
3746
type: String as PropType<string | null>,
38-
required: false
47+
default: undefined
3948
},
40-
// 以下の props は子に渡すだけなので、PropType をわざわざ書く必要はない?
4149
handleToggleCompleted: {
42-
type: Function,
50+
type: Function as PropType<HandleToggleCompleted>,
4351
required: true
4452
},
4553
handleClickEdit: {
46-
type: Function,
54+
type: Function as PropType<HandleClickEdit>,
4755
required: true
4856
},
4957
handleClickRemove: {
50-
type: Function,
58+
type: Function as PropType<HandleClickRemove>,
5159
required: true
5260
},
5361
handleSubmitEditedTodo: {
54-
type: Function,
62+
type: Function as PropType<HandleSubmitEditedTodo>,
5563
required: true
5664
},
5765
handleCancelEdit: {
58-
type: Function,
66+
type: Function as PropType<HandleCancelEdit>,
5967
required: true
6068
}
6169
},

src/pages/Index.vue

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,42 @@ import { defineComponent, Ref, ref } from "vue";
2121
import CreateTodoForm from "@/components/CreateTodoForm.vue";
2222
import TodoList from "@/components/TodoList.vue";
2323
import useTodos from "@/composables/use-todos";
24-
import Todo from "@/models/todo";
2524
26-
import { HandleSubmitNewTodo } from "@/components/CreateTodoForm.vue";
27-
28-
export type HandleToggleCompleted = (todo: Todo) => void;
29-
export type HandleClickEdit = (todo: Todo) => void;
30-
export type HandleClickRemove = (todo: Todo) => void;
31-
export type HandleCancelEdit = () => void;
32-
export type HandleSubmitEditedTodo = (editedTodo: Todo) => void;
25+
import type { HandleSubmitNewTodo } from "@/components/CreateTodoForm.vue";
26+
import type {
27+
HandleToggleCompleted,
28+
HandleClickEdit,
29+
HandleClickRemove,
30+
} from "@/components/TodoItem.vue";
31+
import type {
32+
HandleCancelEdit,
33+
HandleSubmitEditedTodo,
34+
} from "@/components/EditTodoForm.vue";
3335
3436
export default defineComponent({
3537
components: { CreateTodoForm, TodoList },
3638
3739
setup() {
3840
const { todos, createTodo, updateTodo, removeTodo } = useTodos();
3941
40-
const handleSubmitNewTodo: HandleSubmitNewTodo = title => {
42+
const handleSubmitNewTodo: HandleSubmitNewTodo = (title) => {
4143
createTodo({ title: title.value, completed: false });
4244
title.value = "";
4345
};
4446
4547
const todoIdBeingEdited: Ref<string | null> = ref(null);
46-
const handleToggleCompleted: HandleToggleCompleted = todo => {
48+
const handleToggleCompleted: HandleToggleCompleted = (todo) => {
4749
const { id, title, completed } = todo;
4850
updateTodo({ id, title, completed: !completed });
4951
};
50-
const handleClickEdit: HandleClickEdit = todo => {
52+
const handleClickEdit: HandleClickEdit = (todo) => {
5153
todoIdBeingEdited.value = todo.id;
5254
};
53-
const handleClickRemove: HandleClickRemove = todo => {
55+
const handleClickRemove: HandleClickRemove = (todo) => {
5456
const { id } = todo;
5557
removeTodo({ id });
5658
};
57-
const handleSubmitEditedTodo: HandleSubmitEditedTodo = editedTodo => {
59+
const handleSubmitEditedTodo: HandleSubmitEditedTodo = (editedTodo) => {
5860
const { id, title, completed } = editedTodo;
5961
updateTodo({ id, title, completed });
6062
todoIdBeingEdited.value = null;
@@ -72,8 +74,8 @@ export default defineComponent({
7274
handleClickEdit,
7375
handleClickRemove,
7476
handleSubmitEditedTodo,
75-
handleCancelEdit
77+
handleCancelEdit,
7678
};
77-
}
79+
},
7880
});
7981
</script>

0 commit comments

Comments
 (0)