Skip to content

Commit 55bd477

Browse files
committed
fix(list,useSelection): properly pass value to list item component
1 parent 73c38fb commit 55bd477

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

docs/guide/components/list.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,44 @@ When you override the list item via CSS variable, it's up to you to handle `--a-
3535
For in library example you can check `.a-list-items-pill` class styles.
3636
:::
3737

38+
:::details Make list items clickable
39+
If you want to make list item clickable (have cursor pointer), You can pass `value` property other than `undefined`.
40+
41+
```ts{2}
42+
const items = [
43+
{ text: 'Donut jujubes', value: null }, // value other than `undefined`
44+
{ text: 'Sesame snaps' },
45+
{ text: 'I love jelly' },
46+
{ text: 'Cake gummi' },
47+
]
48+
```
49+
50+
With above items, first item will be clickable (have cursor pointer) and rest of the items will have default pointer.
51+
52+
In case if you want to make all the items clickable without adding `value` property to each item, you can use `:model-value="null"` prop to `AList`.
53+
54+
```vue{13}
55+
<script lang="ts" setup>
56+
const items = [
57+
{ text: 'Donut jujubes' },
58+
{ text: 'Sesame snaps' },
59+
{ text: 'I love jelly' },
60+
{ text: 'Cake gummi' },
61+
]
62+
</script>
63+
64+
<template>
65+
<ACard>
66+
<AList
67+
:model-value="null"
68+
:items="items"
69+
/>
70+
</ACard>
71+
</template>
72+
```
73+
74+
:::
75+
3876
::::
3977

4078
:::::
@@ -84,8 +122,6 @@ Just like `avatar-append`, you can also use `icon-append` to render the action b
84122

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

87-
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.
88-
89125
If you are using `AListItem` in default slot of `AList` you can use `handleListItemClick` slot prop function to select the item value. `handleListItemClick` accepts option item as parameter.
90126

91127
::::code DemoListVModelSupport

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function handleListItemClick(item: AListPropItems[number]) {
6868
:variant="props.variant"
6969
:states="props.states"
7070
:is-active="options[index]?.isSelected as unknown as boolean"
71-
:value="props.modelValue !== undefined ? options[index] : undefined"
71+
:value="props.modelValue !== undefined || (typeof item === 'object' ? item.value : undefined)"
7272
v-on="{
7373
click: props['onClick:item'] || (props.modelValue !== undefined)
7474
? () => { handleListItemClick(item) }

packages/anu-vue/src/composables/useSelection.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export function useSelection<const Item, Multi extends boolean, InitialValue ext
4444
) as ReturnValue<Item, Multi>['value']
4545

4646
const select = (option: Item) => {
47-
console.log('selecting...')
48-
4947
// If multiple selection is enabled
5048
if (_multi.value) {
5149
// If value is not set (Means previously multi was false) => Initialize new set and assign it to value
@@ -88,25 +86,19 @@ export function useSelection<const Item, Multi extends boolean, InitialValue ext
8886
}
8987
}
9088

89+
export function extractItemValueFromItemOption<T>(item: T): T extends { value: infer V } ? V : T {
90+
return isObject(item) && 'value' in item
91+
? item.value
92+
: item as any
93+
}
94+
9195
export function calculateSelectionItems<T>(items: MaybeRefOrGetter<T[]>) {
9296
return computed(() => {
9397
const _items = toRef(items)
9498

9599
if (_items.value.length === 0)
96100
return []
97101

98-
const firstItem = _items.value[0]
99-
if (isObject(firstItem)) {
100-
if ('value' in firstItem)
101-
return _items.value.map(item => (item as { value: T }).value)
102-
}
103-
104-
return _items.value
102+
return _items.value.map(extractItemValueFromItemOption)
105103
})
106104
}
107-
108-
export function extractItemValueFromItemOption<T>(item: T): T extends { value: infer V } ? V : T {
109-
return isObject(item)
110-
? ('value' in item ? item.value : item)
111-
: item as any
112-
}

0 commit comments

Comments
 (0)