Skip to content

Commit 7c72fb1

Browse files
authored
feat(vue-hooks): add type annotations to multiple files to enhance type safety (#3302)
1 parent d234cba commit 7c72fb1

File tree

6 files changed

+42
-30
lines changed

6 files changed

+42
-30
lines changed

packages/vue-hooks/src/useRect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const isWindow = (val) => val === window
2-
const makeDOMRect = (width, height) => ({
1+
const isWindow = (val: any) => val === window
2+
const makeDOMRect = (width: number, height: number) => ({
33
top: 0,
44
left: 0,
55
width,
@@ -8,7 +8,7 @@ const makeDOMRect = (width, height) => ({
88
bottom: height
99
})
1010

11-
export const useRect = (unref) => (elOrRef) => {
11+
export const useRect = (unref: any) => (elOrRef: any) => {
1212
const el = unref(elOrRef)
1313

1414
if (isWindow(el)) {

packages/vue-hooks/src/useTouch.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
const TAP_OFFSET = 5
22

3-
const getDirection = (x, y) => {
3+
const getDirection = (x: number, y: number) => {
44
if (x > y) return 'horizontal'
55
if (y > x) return 'vertical'
66
return ''
77
}
88

9-
const touchEvent = (event) => event.touches[0]
9+
const touchEvent = (event: TouchEvent) => event.touches[0]
1010

11-
export const useTouch = (ref) => () => {
11+
export const useTouch = (ref: any) => () => {
1212
const startX = ref(0)
1313
const startY = ref(0)
1414
const deltaX = ref(0)
@@ -30,14 +30,14 @@ export const useTouch = (ref) => () => {
3030
isTap.value = true
3131
}
3232

33-
const start = (event) => {
33+
const start = (event: TouchEvent) => {
3434
reset()
3535
const touch = touchEvent(event)
3636
startX.value = touch.clientX
3737
startY.value = touch.clientY
3838
}
3939

40-
const move = (event) => {
40+
const move = (event: TouchEvent) => {
4141
const touch = touchEvent(event)
4242
// safari back will set clientX to negative number
4343
deltaX.value = (touch.clientX < 0 ? 0 : touch.clientX) - startX.value

packages/vue-hooks/src/vue-emitter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
*/
1313

1414
// 全局未引用 ,待移除
15-
export default (vm) => {
16-
const broadcast = (vm, componentName, eventName, params) => {
17-
vm.$children.forEach((child) => {
15+
export default (vm: any) => {
16+
const broadcast = (vm: any, componentName: string, eventName: string, params: any) => {
17+
vm.$children.forEach((child: any) => {
1818
const name = child.$options.componentName
1919

2020
if (name === componentName) {
@@ -26,7 +26,7 @@ export default (vm) => {
2626
}
2727

2828
return {
29-
dispatch(componentName, eventName, params) {
29+
dispatch(componentName: string, eventName: string, params: any) {
3030
let parent = vm.$parent || vm.$root
3131
let name = parent.$options.componentName
3232

@@ -42,7 +42,7 @@ export default (vm) => {
4242
parent.$emit.apply(parent, [eventName].concat(params))
4343
}
4444
},
45-
broadcast(componentName, eventName, params) {
45+
broadcast(componentName: string, eventName: string, params: any) {
4646
broadcast(vm, componentName, eventName, params)
4747
}
4848
}

packages/vue-hooks/src/vue-popper.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,11 @@
1111
*/
1212

1313
import { PopupManager, PopperJS, on, off, isDisplayNone } from '@opentiny/utils'
14-
15-
// todo
16-
import type { ISharedRenderlessFunctionParams } from 'types/shared.type'
17-
14+
import type { ISharedRenderlessFunctionParams } from '../types/shared.type'
1815
import { isServer } from '@opentiny/utils'
1916

2017
export interface IPopperState {
21-
popperJS: Popper
18+
popperJS: PopperJS | null
2219
appended: boolean
2320
popperElm: HTMLElement
2421
showPopper: boolean
@@ -48,10 +45,10 @@ const getReference = ({ state, props, vm, slots }: Pick<IPopperInputParams, 'sta
4845
return reference
4946
}
5047

51-
const getReferMaxZIndex = (reference) => {
48+
const getReferMaxZIndex = (reference: HTMLElement) => {
5249
if (!reference || !reference.nodeType) return
5350

54-
let getZIndex = (dom) => parseInt(window.getComputedStyle(dom).zIndex, 10) || 0
51+
let getZIndex = (dom: HTMLElement) => parseInt(window.getComputedStyle(dom).zIndex, 10) || 0
5552
let max = getZIndex(reference)
5653
let z
5754

@@ -90,14 +87,14 @@ export const userPopper = (options: IPopperInputParams) => {
9087
toRefs,
9188
popperVmRef
9289
} = options
93-
const state = reactive<IPopperState>({
90+
const state = reactive({
9491
popperJS: null as any,
9592
appended: false, // arrow 是否添加
9693
popperElm: null as any,
9794
showPopper: props.manual ? Boolean(props.modelValue) : false,
9895
referenceElm: null as any,
9996
currentPlacement: ''
100-
})
97+
}) as IPopperState
10198

10299
/** 创建箭头函数 */
103100
const appendArrow = (el: HTMLElement) => {
@@ -123,11 +120,11 @@ export const userPopper = (options: IPopperInputParams) => {
123120
}
124121
}
125122

126-
const nextZIndex = (reference) => {
123+
const nextZIndex = (reference: HTMLElement) => {
127124
return props.zIndex === 'relative' ? getReferMaxZIndex(reference) : PopupManager.nextZIndex()
128125
}
129126

130-
const createPopper = (dom) => {
127+
const createPopper = (dom: HTMLElement) => {
131128
if (isServer) {
132129
return
133130
}
@@ -183,7 +180,7 @@ export const userPopper = (options: IPopperInputParams) => {
183180
/** 第一次 updatePopper 的时候,才真正执行创建
184181
* popperElmOrTrue===true的场景仅在select组件动态更新面版时,不更新zIndex
185182
*/
186-
const updatePopper = (popperElmOrTrue?: HTMLElement) => {
183+
const updatePopper = (popperElmOrTrue?: HTMLElement | boolean) => {
187184
if (popperElmOrTrue && popperElmOrTrue !== true) {
188185
state.popperElm = popperElmOrTrue
189186
}
@@ -230,7 +227,7 @@ export const userPopper = (options: IPopperInputParams) => {
230227
// 注意: 一直以来,state.showPopper 为false时,并未调用doDestroy. 像popover只是依赖这个值来 给reference元素 v-show一下
231228
watch(
232229
() => state.showPopper,
233-
(val) => {
230+
(val: boolean) => {
234231
if (props.disabled) {
235232
return
236233
}

packages/vue-hooks/src/vue-popup.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import { merge, PopupManager, addClass } from '@opentiny/utils'
1414

1515
// todo
16-
import type { ISharedRenderlessFunctionParams } from 'types/shared.type'
16+
import type { ISharedRenderlessFunctionParams } from '../types/shared.type'
1717
import { isServer } from '@opentiny/utils'
1818

1919
let idSeed = 1
@@ -53,7 +53,7 @@ const setWatchFn = ({
5353

5454
watch(
5555
() => props.visible,
56-
(val) => {
56+
(val: boolean) => {
5757
if (val) {
5858
if (vm._opening) {
5959
return
@@ -182,10 +182,10 @@ const closeFn =
182182
*/
183183
export const usePopup = (options: IPopupInputParams) => {
184184
const { api, nextTick, onBeforeUnmount, onMounted, props, reactive, toRefs, vm, watch } = options
185-
const state = reactive<IPopupState>({
185+
const state = reactive({
186186
opened: false,
187187
rendered: false
188-
})
188+
}) as IPopupState
189189

190190
setWatchFn({ onMounted, onBeforeUnmount, watch, vm, api, props, state, nextTick })
191191

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface ISharedRenderlessFunctionParams<T> {
2+
vm: any
3+
props: any
4+
slots: any
5+
emit: Function
6+
parent: any
7+
nextTick: Function
8+
watch: Function
9+
reactive: Function
10+
toRefs: Function
11+
onBeforeUnmount: Function
12+
onMounted: Function
13+
onDeactivated: Function
14+
popperVmRef: any
15+
}

0 commit comments

Comments
 (0)