forked from vitest-dev/vitest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(useTemplateRefsList): new function (vitest-dev#645)
Co-authored-by: aaronz <aaron.bjym1011@outlook.com> Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
5bbea75
commit 57e4c8d
Showing
8 changed files
with
199 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<template> | ||
<span v-for="i of count" :key="i" :ref="refs.set" class="mr-2"> | ||
{{ i }} | ||
</span> | ||
<br> | ||
<button @click="count += 1"> | ||
Inc | ||
</button> | ||
<button :disabled="count <= 0" @click="count -= 1"> | ||
Dec | ||
</button> | ||
<note>Open the console to see the output</note> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { ref, watch, nextTick } from 'vue' | ||
import { useTemplateRefsList } from '@vueuse/core' | ||
const count = ref(5) | ||
const refs = useTemplateRefsList<HTMLDivElement>() | ||
watch(refs, async() => { | ||
await nextTick() | ||
console.log([...refs.value]) | ||
}, { | ||
deep: true, | ||
flush: 'post', | ||
}) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
category: Component | ||
--- | ||
|
||
# useTemplateRefsList | ||
|
||
Shorthand for binding refs to template elements and components inside `v-for`. | ||
|
||
> This function only works for Vue 3.x. | ||
## Usage | ||
|
||
```html | ||
<template> | ||
<div v-for="i of 5" :key="i" :ref="refs.set"></div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { onUpdated } from 'vue' | ||
import { useTemplateRefsList } from '@vueuse/core' | ||
const refs = useTemplateRefsList<HTMLDivElement>() | ||
onUpdated(() => { | ||
console.log(refs) | ||
}) | ||
</script> | ||
``` | ||
|
||
<!--FOOTER_STARTS--> | ||
|
||
|
||
## Source | ||
|
||
[Source](https://github.com/vueuse/vueuse/blob/main/packages/core/useTemplateRefsList/index.ts) • [Demo](https://github.com/vueuse/vueuse/blob/main/packages/core/useTemplateRefsList/demo.vue) • [Docs](https://github.com/vueuse/vueuse/blob/main/packages/core/useTemplateRefsList/index.md) | ||
|
||
|
||
<!--FOOTER_ENDS--> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { defineComponent, h, isVue2, nextTick, ref, toRefs } from 'vue-demi' | ||
import { mount } from '../../.test' | ||
import { useTemplateRefsList } from '.' | ||
|
||
if (isVue2) { | ||
// Vue 2 is not supported | ||
it('stub', () => {}) | ||
} | ||
else { | ||
const Component1 = defineComponent({ | ||
setup() { | ||
const list = ref([1, 2, 3]) | ||
const refs = useTemplateRefsList() | ||
return { list, refs } | ||
}, | ||
render() { | ||
return h( | ||
'div', | ||
this.list.map(item => h('div', { | ||
ref: this.refs.set, | ||
id: `div${item}`, | ||
})), | ||
) | ||
}, | ||
}) | ||
|
||
interface ChildAPI { | ||
foo: () => string | ||
} | ||
|
||
const Child = defineComponent({ | ||
props: { | ||
id: { | ||
type: Number, | ||
required: true, | ||
}, | ||
}, | ||
setup(props) { | ||
const { id } = toRefs(props) | ||
const foo = () => { | ||
return `foo${id.value}` | ||
} | ||
return { foo } | ||
}, | ||
render() { | ||
return h('div') | ||
}, | ||
}) | ||
|
||
const testCom2 = defineComponent({ | ||
setup() { | ||
const list = ref([1, 2, 3]) | ||
const refs = useTemplateRefsList<ChildAPI>() | ||
return { list, refs } | ||
}, | ||
render() { | ||
return h( | ||
'div', | ||
this.list.map(item => h(Child, { | ||
ref: this.refs.set, | ||
id: item, | ||
})), | ||
) | ||
}, | ||
}) | ||
|
||
describe('useTemplateRefsList', () => { | ||
it('should be defined', () => { | ||
expect(useTemplateRefsList).toBeDefined() | ||
}) | ||
|
||
it('ref all 3 divs', () => { | ||
const vm = mount(Component1) | ||
|
||
expect(vm.refs).toBeDefined() | ||
expect(vm.refs.length).toBe(3) | ||
expect(vm.refs[0]).toBe(vm.$el.querySelector('#div1')) | ||
expect(vm.refs[1]).toBe(vm.$el.querySelector('#div2')) | ||
expect(vm.refs[2]).toBe(vm.$el.querySelector('#div3')) | ||
}) | ||
|
||
it('v-for source update', async() => { | ||
const vm = mount(Component1) | ||
|
||
vm.list = [1, 2, 3, 4] | ||
await nextTick() | ||
|
||
expect(vm.refs.length).toBe(4) | ||
expect(vm.refs[3]).toBe(vm.$el.querySelector('#div4')) | ||
|
||
vm.list = [1] | ||
await nextTick() | ||
|
||
expect(vm.refs.length).toBe(1) | ||
}) | ||
|
||
it('call child component methods', async() => { | ||
const vm = mount(testCom2) | ||
|
||
expect(vm.refs[0].foo()).toBe('foo1') | ||
expect(vm.refs[1].foo()).toBe('foo2') | ||
expect(vm.refs[2].foo()).toBe('foo3') | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { onBeforeUpdate, Ref, ref } from 'vue-demi' | ||
|
||
export type TemplateRefsList<T> = T[] & { | ||
set(el: Object | null): void | ||
} | ||
|
||
export function useTemplateRefsList<T = Element>(): Readonly<Ref<Readonly<TemplateRefsList<T>>>> { | ||
const refs = ref<unknown>([]) as Ref<TemplateRefsList<T>> | ||
refs.value.set = (el: Object | null) => { | ||
if (el) | ||
refs.value.push(el as T) | ||
} | ||
onBeforeUpdate(() => { | ||
refs.value.length = 0 | ||
}) | ||
return refs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters