Skip to content
This repository was archived by the owner on Nov 5, 2023. It is now read-only.

Commit 465c2a8

Browse files
authored
feat: new getKey prop (#256)
1 parent 6d9b9b6 commit 465c2a8

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ npm install vue-virtual-scroll-grid
4141
| `scrollBehavior` | The behavior of `scrollTo`. Default value is `smooth` | `smooth` | `auto` | Optional, a string to be `smooth` or `auto`, defaults to `smooth` |
4242
| `scrollTo` | Scroll to a specific item by index | `number` | Optional, an integer from 0 to the `length` prop - 1, defaults to 0 |
4343
| `tag` | The HTML tag used as container element. Default value is `div` | `string` | Optional, any valid HTML tag, defaults to `div` |
44+
| `getKey` | The `:key` used on each grid item. Auto-generated, but overwritable via function | `(internalItem: InternalItem) => number \| string` <sup>1</sup>| Optional, any valid Function that returns a `string` or `number` |
45+
46+
<sup>1</sup>
47+
```ts
48+
interface InternalItem {
49+
index: number;
50+
value: unknown | undefined;
51+
style?: { transform: string; gridArea: string };
52+
}
53+
```
4454

4555
Example:
4656

src/Grid.vue

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<template
1919
v-for="internalItem in buffer"
20-
:key="keyPrefix + '.' + internalItem.index"
20+
:key="getKey ? getKey(internalItem) : keyPrefix + '.' + internalItem.index"
2121
>
2222
<slot
2323
v-if="internalItem.value === undefined"
@@ -52,7 +52,7 @@ import {
5252
fromWindowScroll,
5353
useObservable,
5454
} from "./utilites";
55-
import { PageProvider, pipeline, ScrollAction } from "./pipeline";
55+
import { InternalItem, PageProvider, pipeline, ScrollAction } from "./pipeline";
5656
import { once } from "ramda";
5757
import { VueInstance } from "@vueuse/core";
5858
@@ -112,6 +112,11 @@ export default defineComponent({
112112
required: false,
113113
default: "div",
114114
},
115+
getKey: {
116+
type: Function as PropType<(internalItem: InternalItem) => number | string>,
117+
required: false,
118+
default: undefined,
119+
},
115120
},
116121
setup(props) {
117122
// template refs

0 commit comments

Comments
 (0)