Skip to content

Commit 14499d4

Browse files
fix(list): make list item clickable if click event is used (#86)
Co-authored-by: JD Solanki <jdsolanki0001@gmail.com>
1 parent dec14d2 commit 14499d4

File tree

4 files changed

+9
-16
lines changed

4 files changed

+9
-16
lines changed

docs/demos/list/DemoListVModelSupport.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const slotSelection = ref(0)
4646
:value="index"
4747
:disable="item.disable"
4848
:is-active="slotSelection === index"
49-
@click="item => handleListItemClick(item)"
49+
@click="handleListItemClick(item, index)"
5050
/>
5151
</template>
5252
<template #after>

docs/guide/components/list.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Just like `avatar-append`, you can also use `icon-append` to render the action b
5252
<!-- 👉 `v-model` Support -->
5353
::::card `v-model` Support
5454

55-
`AList` also support `v-model`. Use any value other than `null` to enable the `v-model` support.
55+
`AList` also support `v-model`. Use any value other than `undefined` to enable the `v-model` support.
5656

5757
If you use `items` prop on `AList` and don't provide `value` property to each list item, `AList` will emit list item's index as selected value.
5858

packages/anu-vue/src/components/list-item/AListItem.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ export const AListItem = defineComponent({
6363
default: false,
6464
},
6565
},
66-
emits: ['click', 'click:icon', 'click:avatar', 'click:iconAppend', 'click:avatarAppend'],
67-
setup(props, { slots, emit }) {
66+
emits: ['click:icon', 'click:avatar', 'click:iconAppend', 'click:avatarAppend'],
67+
setup(props, { slots, emit, attrs }) {
6868
const { getLayerClasses } = useLayer()
6969

7070
// ℹ️ Reduce the size of title to 1rem. We did the same in ACard as well.
@@ -74,11 +74,6 @@ export const AListItem = defineComponent({
7474
else
7575
_titleProp.value.classes += ' uno-layer-base-text-base'
7676

77-
// Handle list item click
78-
const handleListItemClick = () => {
79-
emit('click', props.value)
80-
}
81-
8277
// useLayer
8378
const { styles, classes } = getLayerClasses(
8479
computed(() => props.isActive ? props.color || 'primary' : undefined),
@@ -93,13 +88,12 @@ export const AListItem = defineComponent({
9388
class={[
9489
'a-list-item',
9590
{ 'opacity-50 pointer-events-none': props.disable },
96-
props.value !== undefined
91+
props.value !== undefined || attrs.onClick
9792
? [...classes.value, 'cursor-pointer']
9893
: '',
9994
'flex items-center gap-$a-list-item-gap m-$a-list-item-margin p-$a-list-item-padding min-h-$a-list-item-min-height',
10095
]}
10196
data-color={props.color}
102-
onClick={handleListItemClick}
10397
style={[...styles.value]}
10498
>
10599
{/* 👉 Slot: prepend */}

packages/anu-vue/src/components/list/AList.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ export const AList = defineComponent({
7676
})
7777

7878
// const isActive = computed(() => options.value[itemIndex].isSelected)
79-
const handleListItemClick = (item: any) => {
80-
const emitValue = item.value ?? item
81-
emit('update:modelValue', emitValue)
82-
selectListItem(emitValue)
79+
const handleListItemClick = (item: ListItem, index: number) => {
80+
selectListItem(item.value || index)
81+
emit('update:modelValue', value.value)
8382
}
8483

8584
// 👉 Return
@@ -103,7 +102,7 @@ export const AList = defineComponent({
103102
avatarAppend={props.avatarAppend}
104103
iconAppend={props.iconAppend}
105104
is-active={options.value[index].isSelected}
106-
onClick={handleListItemClick}
105+
onClick={item.value || (props.modelValue !== undefined) ? () => handleListItemClick(item, index) : null}
107106
v-slots={{
108107
prepend: slots.prepend ? () => slots.prepend?.({ item, index }) : null,
109108
item: slots.item ? () => slots.item?.({ item, index }) : null,

0 commit comments

Comments
 (0)