Skip to content

Commit

Permalink
fix(list): make list item clickable if click event is used (jd-solank…
Browse files Browse the repository at this point in the history
…i#86)

Co-authored-by: JD Solanki <jdsolanki0001@gmail.com>
  • Loading branch information
ManUtopiK and jd-solanki committed Dec 11, 2022
1 parent dec14d2 commit 14499d4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/demos/list/DemoListVModelSupport.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const slotSelection = ref(0)
:value="index"
:disable="item.disable"
:is-active="slotSelection === index"
@click="item => handleListItemClick(item)"
@click="handleListItemClick(item, index)"
/>
</template>
<template #after>
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/components/list.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Just like `avatar-append`, you can also use `icon-append` to render the action b
<!-- 👉 `v-model` Support -->
::::card `v-model` Support

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

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.

Expand Down
12 changes: 3 additions & 9 deletions packages/anu-vue/src/components/list-item/AListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ export const AListItem = defineComponent({
default: false,
},
},
emits: ['click', 'click:icon', 'click:avatar', 'click:iconAppend', 'click:avatarAppend'],
setup(props, { slots, emit }) {
emits: ['click:icon', 'click:avatar', 'click:iconAppend', 'click:avatarAppend'],
setup(props, { slots, emit, attrs }) {
const { getLayerClasses } = useLayer()

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

// Handle list item click
const handleListItemClick = () => {
emit('click', props.value)
}

// useLayer
const { styles, classes } = getLayerClasses(
computed(() => props.isActive ? props.color || 'primary' : undefined),
Expand All @@ -93,13 +88,12 @@ export const AListItem = defineComponent({
class={[
'a-list-item',
{ 'opacity-50 pointer-events-none': props.disable },
props.value !== undefined
props.value !== undefined || attrs.onClick
? [...classes.value, 'cursor-pointer']
: '',
'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',
]}
data-color={props.color}
onClick={handleListItemClick}
style={[...styles.value]}
>
{/* 👉 Slot: prepend */}
Expand Down
9 changes: 4 additions & 5 deletions packages/anu-vue/src/components/list/AList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ export const AList = defineComponent({
})

// const isActive = computed(() => options.value[itemIndex].isSelected)
const handleListItemClick = (item: any) => {
const emitValue = item.value ?? item
emit('update:modelValue', emitValue)
selectListItem(emitValue)
const handleListItemClick = (item: ListItem, index: number) => {
selectListItem(item.value || index)
emit('update:modelValue', value.value)
}

// 👉 Return
Expand All @@ -103,7 +102,7 @@ export const AList = defineComponent({
avatarAppend={props.avatarAppend}
iconAppend={props.iconAppend}
is-active={options.value[index].isSelected}
onClick={handleListItemClick}
onClick={item.value || (props.modelValue !== undefined) ? () => handleListItemClick(item, index) : null}
v-slots={{
prepend: slots.prepend ? () => slots.prepend?.({ item, index }) : null,
item: slots.item ? () => slots.item?.({ item, index }) : null,
Expand Down

0 comments on commit 14499d4

Please sign in to comment.