-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
74 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import Vue from 'vue'; | ||
import { computed, onMounted } from 'vue-function-api'; | ||
import useRef from '../useRef'; | ||
import renderHook from '../util/renderHook'; | ||
|
||
describe('useRef', () => { | ||
it('should be defined', () => { | ||
expect(useRef).toBeDefined(); | ||
}); | ||
|
||
it('should return element when passing ref name', () => { | ||
renderHook(() => { | ||
const app = useRef<HTMLDivElement>('app'); | ||
const style = computed(() => (app.value ? app.value.style : undefined)); | ||
|
||
expect(app.value).toBeUndefined(); | ||
expect(style.value).toBeUndefined(); | ||
|
||
onMounted(async () => { | ||
await Vue.nextTick(); | ||
expect(app.value).toBeDefined(); | ||
expect(style.value).toBeDefined(); | ||
expect(style.value.width).toBe('1280px'); | ||
expect(style.value.height).toBe('800px'); | ||
}); | ||
}); | ||
}); | ||
|
||
it('should return element when passing function', () => { | ||
renderHook((_, { refs }) => { | ||
const nav = useRef<HTMLDivElement>(() => refs.nav); | ||
const style = computed(() => (nav.value ? nav.value.style : undefined)); | ||
|
||
expect(nav.value).toBeUndefined(); | ||
expect(style.value).toBeUndefined(); | ||
|
||
onMounted(async () => { | ||
await Vue.nextTick(); | ||
expect(nav.value).toBeDefined(); | ||
expect(style.value).toBeDefined(); | ||
expect(style.value.width).toBe('100%'); | ||
}); | ||
}); | ||
}); | ||
}); |
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,26 @@ | ||
import Vue from 'vue'; | ||
import { value, onMounted } from 'vue-function-api'; | ||
import { getRuntimeVM } from './util/runtime'; | ||
|
||
export type Ref = Vue | Element | Vue[] | Element[]; | ||
|
||
export default function useRef<T extends Ref>(target: string | (() => Ref)) { | ||
const ref = value<T>(undefined!); | ||
|
||
onMounted(async () => { | ||
await Vue.nextTick(); | ||
switch (typeof target) { | ||
case 'string': | ||
const { $refs } = getRuntimeVM(); // eslint-disable-line no-case-declarations | ||
ref.value = $refs[target] as T; | ||
break; | ||
case 'function': | ||
ref.value = target() as T; | ||
break; | ||
default: | ||
throw new TypeError('Target must be string or function.'); | ||
} | ||
}); | ||
|
||
return ref; | ||
} |
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