8000 feat(useCycleList): add `go` function (#3615) · vueuse/vueuse@2ae3639 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Commit 2ae3639

Browse files
lanxizhuantfu
andauthored
feat(useCycleList): add go function (#3615)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 3fd9434 commit 2ae3639

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

packages/core/useCycleList/index.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Cycle through a list of items.
1313
```ts
1414
import { useCycleList } from '@vueuse/core'
1515

16-
const { state, next, prev } = useCycleList([
16+
const { state, next, prev, go } = useCycleList([
1717
'Dog',
1818
'Cat',
1919
'Lizard',
@@ -29,4 +29,8 @@ console.log(state.value) // 'Dog'
2929
prev()
3030

3131
console.log(state.value) // 'Seal'
32+
33+
go(3)
34+
35+
console.log(state.value) // 'Shark'
3236
```

packages/core/useCycleList/index.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ describe('useCycleList', () => {
3333
it('should work with ref', () => {
3434
const list = ref(['foo', 'bar', 'fooBar'])
3535

36-
const { state, next, prev, index } = useCycleList(list)
36+
const { state, next, prev, index, go } = useCycleList(list)
3737

3838
expect(state.value).toBe('foo')
3939
expect(index.value).toBe(0)
@@ -57,6 +57,16 @@ describe('useCycleList', () => {
5757

5858
expect(state.value).toBe('foo')
5959
expect(index.value).toBe(0)
60+
61+
go(1)
62+
63+
expect(state.value).toBe('bar')
64+
expect(index.value).toBe(1)
65+
66+
go(-1)
67+
68+
expect(state.value).toBe('fooBar')
69+
expect(index.value).toBe(2)
6070
})
6171

6272
describe('when list empty', () => {

packages/core/useCycleList/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ export function useCycleList<T>(list: MaybeRefOrGetter<T[]>, options?: UseCycleL
8080
index,
8181
next,
8282
prev,
83+
go: set,
8384
}
8485
}
8586

@@ -88,4 +89,8 @@ export interface UseCycleListReturn<T> {
8889
index: Ref<number>
8990
next: (n?: number) => T
9091
prev: (n?: number) => T
92+
/**
93+
* Go to a specific index
94+
*/
95+
go: (i: number) => T
9196
}

0 commit comments

Comments
 (0)
0