fix: List item UI improvements
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<script setup lang="ts">
|
||||
import BaseModal from '@/components/BaseModal.vue'
|
||||
|
||||
withDefaults(
|
||||
defineProps<{
|
||||
title: string
|
||||
description: string
|
||||
confirmLabel?: string
|
||||
}>(),
|
||||
{
|
||||
confirmLabel: 'Delete',
|
||||
},
|
||||
)
|
||||
|
||||
const emit = defineEmits<{
|
||||
close: []
|
||||
confirm: []
|
||||
}>()
|
||||
|
||||
function handleClose() {
|
||||
emit('close')
|
||||
}
|
||||
|
||||
function handleConfirm() {
|
||||
emit('confirm')
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BaseModal :title="title" title-id="confirm-delete-modal-title" @close="handleClose">
|
||||
<p class="modal-description">{{ description }}</p>
|
||||
|
||||
<template #footer>
|
||||
<button type="button" class="btn btn-secondary cancel-btn" @click="handleClose">
|
||||
Cancel
|
||||
</button>
|
||||
<button type="button" class="btn btn-danger confirm-delete-btn" @click="handleConfirm">
|
||||
{{ confirmLabel }}
|
||||
</button>
|
||||
</template>
|
||||
</BaseModal>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.modal-description {
|
||||
font-size: 0.85rem;
|
||||
color: var(--c-text-soft);
|
||||
margin-bottom: 1.25rem;
|
||||
line-height: 1.4;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,31 @@
|
||||
<script setup lang="ts">
|
||||
import type { LocalListItem } from '@/database/db'
|
||||
import ConfirmDeleteModal from '@/components/ConfirmDeleteModal.vue'
|
||||
|
||||
const props = defineProps<{
|
||||
item: LocalListItem
|
||||
}>()
|
||||
|
||||
const emit = defineEmits<{
|
||||
close: []
|
||||
confirm: []
|
||||
}>()
|
||||
|
||||
function handleClose() {
|
||||
emit('close')
|
||||
}
|
||||
|
||||
function handleConfirm() {
|
||||
emit('confirm')
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ConfirmDeleteModal
|
||||
:title="`Delete "${props.item.title}"?`"
|
||||
description="Are you sure you want to delete this item? This action cannot be undone."
|
||||
confirm-label="Delete item"
|
||||
@close="handleClose"
|
||||
@confirm="handleConfirm"
|
||||
/>
|
||||
</template>
|
||||
@@ -1,8 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
import type { LocalList } from '@/database/db'
|
||||
import BaseModal from '@/components/BaseModal.vue'
|
||||
import ConfirmDeleteModal from '@/components/ConfirmDeleteModal.vue'
|
||||
|
||||
defineProps<{
|
||||
const props = defineProps<{
|
||||
list: LocalList
|
||||
}>()
|
||||
|
||||
@@ -21,32 +21,11 @@ function handleConfirm() {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BaseModal
|
||||
:title="`Delete "${list.name}"?`"
|
||||
title-id="delete-modal-title"
|
||||
<ConfirmDeleteModal
|
||||
:title="`Delete "${props.list.name}"?`"
|
||||
description="Are you sure you want to delete this list? This action cannot be undone and all items in this list will be deleted."
|
||||
confirm-label="Delete list"
|
||||
@close="handleClose"
|
||||
>
|
||||
<p class="modal-description">
|
||||
Are you sure you want to delete this list? This action cannot be undone and all items in this
|
||||
list will be deleted.
|
||||
</p>
|
||||
|
||||
<template #footer>
|
||||
<button type="button" class="btn btn-secondary cancel-btn" @click="handleClose">
|
||||
Cancel
|
||||
</button>
|
||||
<button type="button" class="btn btn-danger confirm-delete-btn" @click="handleConfirm">
|
||||
Delete list
|
||||
</button>
|
||||
</template>
|
||||
</BaseModal>
|
||||
@confirm="handleConfirm"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.modal-description {
|
||||
font-size: 0.85rem;
|
||||
color: var(--c-text-soft);
|
||||
margin-bottom: 1.25rem;
|
||||
line-height: 1.4;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -3,6 +3,7 @@ import { ref } from 'vue'
|
||||
import type { LocalListItem } from '@/database/db'
|
||||
import { useListsStore } from '@/stores/lists'
|
||||
import { useDismissableMenu } from '@/composables/useDismissableMenu'
|
||||
import DeleteListItemModal from '@/components/DeleteListItemModal.vue'
|
||||
|
||||
const props = defineProps<{ item: LocalListItem }>()
|
||||
|
||||
@@ -16,6 +17,7 @@ const editedTitle = ref(props.item.title)
|
||||
// diffing against the live (possibly just-changed) prop and overwriting the
|
||||
// remote edit with the untouched original text.
|
||||
const originalTitle = ref(props.item.title)
|
||||
const showDeleteModal = ref(false)
|
||||
const {
|
||||
isOpen: isMenuOpen,
|
||||
containerRef: menuContainerRef,
|
||||
@@ -27,6 +29,7 @@ function toggleCompleted() {
|
||||
}
|
||||
|
||||
function startEditing() {
|
||||
isMenuOpen.value = false
|
||||
editedTitle.value = props.item.title
|
||||
originalTitle.value = props.item.title
|
||||
isEditing.value = true
|
||||
@@ -35,15 +38,22 @@ function startEditing() {
|
||||
function saveTitle() {
|
||||
const title = editedTitle.value.trim()
|
||||
if (title && title !== originalTitle.value) {
|
||||
listsStore.updateListItem(props.item.id, { title })
|
||||
listsStore.updateListItemTitle(props.item.id, title)
|
||||
}
|
||||
isEditing.value = false
|
||||
}
|
||||
|
||||
function handleDelete(event: Event) {
|
||||
event.preventDefault()
|
||||
event.stopPropagation()
|
||||
function handleOpenDelete() {
|
||||
isMenuOpen.value = false
|
||||
showDeleteModal.value = true
|
||||
}
|
||||
|
||||
function handleCloseDelete() {
|
||||
showDeleteModal.value = false
|
||||
}
|
||||
|
||||
function handleConfirmDelete() {
|
||||
showDeleteModal.value = false
|
||||
listsStore.deleteListItem(props.item.id)
|
||||
}
|
||||
</script>
|
||||
@@ -68,7 +78,7 @@ function handleDelete(event: Event) {
|
||||
@keyup.escape="isEditing = false"
|
||||
@blur="saveTitle"
|
||||
/>
|
||||
<span v-else class="title" @click="startEditing">{{ item.title }}</span>
|
||||
<span v-else class="title" @click="toggleCompleted">{{ item.title }}</span>
|
||||
|
||||
<span v-if="item.pendingSync" class="pending-dot" title="Not yet synced"></span>
|
||||
|
||||
@@ -90,11 +100,25 @@ function handleDelete(event: Event) {
|
||||
</button>
|
||||
|
||||
<div v-if="isMenuOpen" class="submenu-dropdown card" role="menu">
|
||||
<button type="button" class="submenu-item" role="menuitem" @click="startEditing">
|
||||
<svg
|
||||
class="submenu-icon"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
>
|
||||
<path d="M17 3a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4L17 3z" />
|
||||
</svg>
|
||||
<span>Edit title</span>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="submenu-item submenu-item-danger"
|
||||
role="menuitem"
|
||||
@click="handleDelete"
|
||||
@click="handleOpenDelete"
|
||||
>
|
||||
<svg
|
||||
class="submenu-icon"
|
||||
@@ -116,6 +140,13 @@ function handleDelete(event: Event) {
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<DeleteListItemModal
|
||||
v-if="showDeleteModal"
|
||||
:item="item"
|
||||
@close="handleCloseDelete"
|
||||
@confirm="handleConfirmDelete"
|
||||
/>
|
||||
</li>
|
||||
</template>
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import ConfirmDeleteModal from '../ConfirmDeleteModal.vue'
|
||||
|
||||
describe('ConfirmDeleteModal', () => {
|
||||
it('renders title, description, and default confirm label', () => {
|
||||
const wrapper = mount(ConfirmDeleteModal, {
|
||||
props: {
|
||||
title: 'Delete "Groceries"?',
|
||||
description: 'Are you sure?',
|
||||
},
|
||||
})
|
||||
|
||||
expect(wrapper.text()).toContain('Delete "Groceries"?')
|
||||
expect(wrapper.text()).toContain('Are you sure?')
|
||||
expect(wrapper.find('.confirm-delete-btn').text()).toBe('Delete')
|
||||
expect(wrapper.find('.cancel-btn').text()).toBe('Cancel')
|
||||
})
|
||||
|
||||
it('renders a custom confirm label', () => {
|
||||
const wrapper = mount(ConfirmDeleteModal, {
|
||||
props: {
|
||||
title: 'Delete "Apples"?',
|
||||
description: 'Are you sure?',
|
||||
confirmLabel: 'Delete item',
|
||||
},
|
||||
})
|
||||
|
||||
expect(wrapper.find('.confirm-delete-btn').text()).toBe('Delete item')
|
||||
})
|
||||
|
||||
it('emits confirm event when Delete button is clicked', async () => {
|
||||
const wrapper = mount(ConfirmDeleteModal, {
|
||||
props: {
|
||||
title: 'Delete "Groceries"?',
|
||||
description: 'Are you sure?',
|
||||
},
|
||||
})
|
||||
|
||||
await wrapper.find('.confirm-delete-btn').trigger('click')
|
||||
expect(wrapper.emitted('confirm')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('emits close event when Cancel button is clicked', async () => {
|
||||
const wrapper = mount(ConfirmDeleteModal, {
|
||||
props: {
|
||||
title: 'Delete "Groceries"?',
|
||||
description: 'Are you sure?',
|
||||
},
|
||||
})
|
||||
|
||||
await wrapper.find('.cancel-btn').trigger('click')
|
||||
expect(wrapper.emitted('close')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('emits close event when close icon button is clicked', async () => {
|
||||
const wrapper = mount(ConfirmDeleteModal, {
|
||||
props: {
|
||||
title: 'Delete "Groceries"?',
|
||||
description: 'Are you sure?',
|
||||
},
|
||||
})
|
||||
|
||||
await wrapper.find('.close-btn').trigger('click')
|
||||
expect(wrapper.emitted('close')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('emits close event when clicking overlay background', async () => {
|
||||
const wrapper = mount(ConfirmDeleteModal, {
|
||||
props: {
|
||||
title: 'Delete "Groceries"?',
|
||||
description: 'Are you sure?',
|
||||
},
|
||||
})
|
||||
|
||||
await wrapper.find('.modal-overlay').trigger('click')
|
||||
expect(wrapper.emitted('close')).toBeTruthy()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,50 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import DeleteListItemModal from '../DeleteListItemModal.vue'
|
||||
import type { LocalListItem } from '@/database/db'
|
||||
|
||||
describe('DeleteListItemModal', () => {
|
||||
const sampleItem: LocalListItem = {
|
||||
id: 'item-1',
|
||||
list_id: 'list-1',
|
||||
title: 'Apples',
|
||||
is_completed: false,
|
||||
created_at: '2026-08-21T00:00:00.000Z',
|
||||
modified_at: '2026-08-21T00:00:00.000Z',
|
||||
}
|
||||
|
||||
it('renders modal with item title and confirmation prompt', () => {
|
||||
const wrapper = mount(DeleteListItemModal, {
|
||||
props: {
|
||||
item: sampleItem,
|
||||
},
|
||||
})
|
||||
|
||||
expect(wrapper.text()).toContain('Delete "Apples"?')
|
||||
expect(wrapper.text()).toContain('Are you sure you want to delete this item?')
|
||||
expect(wrapper.find('.confirm-delete-btn').text()).toBe('Delete item')
|
||||
expect(wrapper.find('.cancel-btn').text()).toBe('Cancel')
|
||||
})
|
||||
|
||||
it('emits confirm event when Delete button is clicked', async () => {
|
||||
const wrapper = mount(DeleteListItemModal, {
|
||||
props: {
|
||||
item: sampleItem,
|
||||
},
|
||||
})
|
||||
|
||||
await wrapper.find('.confirm-delete-btn').trigger('click')
|
||||
expect(wrapper.emitted('confirm')).toBeTruthy()
|
||||
})
|
||||
|
||||
it('emits close event when Cancel button is clicked', async () => {
|
||||
const wrapper = mount(DeleteListItemModal, {
|
||||
props: {
|
||||
item: sampleItem,
|
||||
},
|
||||
})
|
||||
|
||||
await wrapper.find('.cancel-btn').trigger('click')
|
||||
expect(wrapper.emitted('close')).toBeTruthy()
|
||||
})
|
||||
})
|
||||
@@ -47,7 +47,39 @@ describe('ListItemRow', () => {
|
||||
expect(wrapper.find('.submenu-dropdown').exists()).toBe(false)
|
||||
})
|
||||
|
||||
it('deletes item when Delete item is clicked in submenu', async () => {
|
||||
it('toggles completed state when the title text is clicked', async () => {
|
||||
const wrapper = mount(ListItemRow, {
|
||||
props: {
|
||||
item: sampleItem,
|
||||
},
|
||||
})
|
||||
|
||||
const listsStore = useListsStore()
|
||||
const toggleSpy = vi.spyOn(listsStore, 'setListItemCompleted').mockResolvedValue()
|
||||
|
||||
await wrapper.find('.title').trigger('click')
|
||||
|
||||
expect(toggleSpy).toHaveBeenCalledWith('item-1', true)
|
||||
})
|
||||
|
||||
it('opens the title editor via the Edit title submenu item', async () => {
|
||||
const wrapper = mount(ListItemRow, {
|
||||
props: {
|
||||
item: sampleItem,
|
||||
},
|
||||
})
|
||||
|
||||
await wrapper.find('.menu-trigger-btn').trigger('click')
|
||||
const editBtn = wrapper.find('.submenu-item:not(.submenu-item-danger)')
|
||||
expect(editBtn.text()).toContain('Edit title')
|
||||
|
||||
await editBtn.trigger('click')
|
||||
|
||||
expect(wrapper.find('.title-input').exists()).toBe(true)
|
||||
expect(wrapper.find('.submenu-dropdown').exists()).toBe(false)
|
||||
})
|
||||
|
||||
it('opens a confirmation modal when Delete item is clicked in submenu', async () => {
|
||||
const wrapper = mount(ListItemRow, {
|
||||
props: {
|
||||
item: sampleItem,
|
||||
@@ -61,10 +93,42 @@ describe('ListItemRow', () => {
|
||||
await wrapper.find('.menu-trigger-btn').trigger('click')
|
||||
expect(wrapper.find('.submenu-dropdown').exists()).toBe(true)
|
||||
|
||||
// 2nd click: delete item
|
||||
// 2nd click: opens confirmation modal, doesn't delete yet
|
||||
await wrapper.find('.submenu-item-danger').trigger('click')
|
||||
|
||||
expect(deleteSpy).toHaveBeenCalledWith('item-1')
|
||||
expect(wrapper.find('.submenu-dropdown').exists()).toBe(false)
|
||||
expect(deleteSpy).not.toHaveBeenCalled()
|
||||
|
||||
const modal = wrapper.findComponent({ name: 'DeleteListItemModal' })
|
||||
expect(modal.exists()).toBe(true)
|
||||
expect(modal.text()).toContain('Delete "Apples"?')
|
||||
|
||||
// Confirm deletion in modal
|
||||
await modal.find('.confirm-delete-btn').trigger('click')
|
||||
|
||||
expect(deleteSpy).toHaveBeenCalledWith('item-1')
|
||||
expect(wrapper.findComponent({ name: 'DeleteListItemModal' }).exists()).toBe(false)
|
||||
})
|
||||
|
||||
it('cancels item deletion when cancel is clicked in confirmation modal', async () => {
|
||||
const wrapper = mount(ListItemRow, {
|
||||
props: {
|
||||
item: sampleItem,
|
||||
},
|
||||
})
|
||||
|
||||
const listsStore = useListsStore()
|
||||
const deleteSpy = vi.spyOn(listsStore, 'deleteListItem').mockResolvedValue()
|
||||
|
||||
await wrapper.find('.menu-trigger-btn').trigger('click')
|
||||
await wrapper.find('.submenu-item-danger').trigger('click')
|
||||
|
||||
const modal = wrapper.findComponent({ name: 'DeleteListItemModal' })
|
||||
expect(modal.exists()).toBe(true)
|
||||
|
||||
await modal.find('.cancel-btn').trigger('click')
|
||||
|
||||
expect(deleteSpy).not.toHaveBeenCalled()
|
||||
expect(wrapper.findComponent({ name: 'DeleteListItemModal' }).exists()).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user