Skip to content

Commit

Permalink
feat: 优化组件 emit 实现
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyh2001 committed Jun 20, 2023
1 parent 1db89dd commit c36b40f
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 157 deletions.
2 changes: 1 addition & 1 deletion packages/fighting-design/_hooks/use-input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export interface UseInputReturn {
*/
export const useInput = (
prop: Partial<UseInputProps>,
modelValue: Ref<string | number | undefined>
modelValue: Ref<string | number>
): UseInputReturn => {
const { run } = useRun()

Expand Down
7 changes: 3 additions & 4 deletions packages/fighting-design/_hooks/use-offset/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ref, onMounted, onUnmounted } from 'vue'
import { EMIT_UPDATE } from '../../_tokens'
import type { Ref } from 'vue'
import type { SliderProps } from '../../slider'

Expand All @@ -22,13 +21,13 @@ export interface UseOffsetReturn {
* @author Tyh2001 <https://github.com/Tyh2001>
* @param { Object } el 小球元素节点
* @param { Object } step 步长
* @param { Function } emit 回调方法
* @param { Object } modelValue 绑定值
* @returns { Object } 包括:偏移距离、距离左侧的偏移量、设置偏移量方法
*/
export const useOffset = (
el: Ref<HTMLDivElement | undefined>,
prop: SliderProps,
emit: (event: 'update:modelValue', val: number) => void
modelValue: Ref<number>
): UseOffsetReturn => {
/** 距离左侧的偏移量 */
const offsetLeft = ref<number>(0)
Expand Down Expand Up @@ -118,7 +117,7 @@ export const useOffset = (

offset.value = value

emit(EMIT_UPDATE, value)
modelValue.value = value
}

return { offset, offsetLeft, setPosition }
Expand Down
13 changes: 2 additions & 11 deletions packages/fighting-design/_hooks/use-tabs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from 'vue'
import { useChildren } from '../../_utils'
import { useRun } from '../../_hooks'
import { EMIT_UPDATE } from '../../_tokens'
import { TABS_PROPS_KEY } from '../../tabs/src/props'
import type { ComponentInternalInstance, ComputedRef, Ref } from 'vue'
import type { TabsModelValue, TabsProps, TabsNavInstance, TabsEdit } from '../../tabs'
Expand Down Expand Up @@ -40,14 +39,6 @@ export type TabsProvide = {
activeName: Ref<TabsModelValue>
} & UseChildrenReturn<TabsPane>

/**
* setActiveName 回调类型
*
* @param { string } event 回调事件名
* @param { string | number } val 回调参数
*/
export type SetActiveNameEmit = (event: 'update:modelValue', val: string | number) => void

/**
* 设置绑定值回调类型
*
Expand All @@ -63,7 +54,7 @@ export type SetActiveName = (name: TabsModelValue) => void
* @param { Function } emit 回调函数
* @returns { Object }
*/
export const useTabs = (prop: TabsProps, emit: SetActiveNameEmit): UseTabsReturn => {
export const useTabs = (prop: TabsProps, modelValue: Ref<string | number>): UseTabsReturn => {
const { run } = useRun()
/** 获取当前组件实例 */
const instance = getCurrentInstance() as ComponentInternalInstance
Expand All @@ -80,7 +71,7 @@ export const useTabs = (prop: TabsProps, emit: SetActiveNameEmit): UseTabsReturn
const setActiveName = (name: TabsModelValue): void => {
activeName.value = name
/** 回调更新绑定值 */
emit(EMIT_UPDATE, name)
modelValue.value = name
}

/**
Expand Down
52 changes: 5 additions & 47 deletions packages/fighting-design/_hooks/use-visible/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import { watch, ref } from 'vue'
import { useRun } from '..'
import { EMIT_VISIBLE } from '../../_tokens'
import type { Ref } from 'vue'

/**
* useVisible 返回值类型接口
*
* @param { Object } isVisible 展示状态
* @param { Function } closeVisible 关闭方法
* @param { Function } maskClose 遮罩层关闭方法
*/
export interface UseVisibleReturn {
isVisible: Ref<boolean>
closeVisible: () => void
maskClose: () => void
}

/**
* 回调函数类型
*
* @param { String } event 回调事件名
* @param { Boolean } val 回调参数
*/
export type UseVisibleEmit = (event: 'update:visible', val: boolean) => void

/**
* 控制 dialog 和 drawer 组件显示隐藏方法
*
Expand All @@ -33,58 +22,27 @@ export type UseVisibleEmit = (event: 'update:visible', val: boolean) => void
* @returns { Object }
*/
export const useVisible = (
prop: { visible: boolean, maskClose: boolean },
emit: UseVisibleEmit,
prop: { maskClose: boolean },
visible: Ref<boolean>,
callback?: Function
): UseVisibleReturn => {
const { run } = useRun()

/** 是否展示 */
const isVisible = ref<boolean>(prop.visible)

/**
* 关闭
*
* @param { Object } [evt] 事件对象
*/
const closeVisible = (evt?: MouseEvent): void => {
emit(EMIT_VISIBLE, false)
visible.value = false
run(callback, evt)
}

/** 监视绑定值的变化,如果为假则关闭 */
watch(
(): boolean => isVisible.value,
/**
* @param { boolean } newVal 最新值
*/
(newVal: boolean): void => {
if (!newVal) {
closeVisible()
}
}
)

/** 监视数据更新绑定值 */
watch(
(): boolean => prop.visible,
/**
* @param { boolean } newVal 最新值
*/
(newVal: boolean): void => {
isVisible.value = newVal
}
)

/** 点击遮罩层关闭 */
const maskClose = (): void => {
if (!prop.maskClose) return
closeVisible()
}

return {
isVisible,
closeVisible,
maskClose
}
return { closeVisible, maskClose }
}
21 changes: 4 additions & 17 deletions packages/fighting-design/date-picker/src/date-picker.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@
import { FInput } from '../../input'
import { FTrigger } from '../../trigger'
import { FCalendar } from '../../calendar'
import { EMIT_DATE } from '../../_tokens'
import { useModel } from '../../_hooks'
import { addZero, warning, isFunction } from '../../_utils'
import { FIconCalendar } from '../../_svg'
import type { TriggerInstance } from '../../trigger'
defineOptions({ name: 'FDatePicker' })
const prop = defineProps(Props)
const emit = defineEmits([EMIT_DATE])
const dateModelValue = defineModel<string>('date', { required: true, default: '' })
/** 传递给日历组件的当前时间 */
const date = new Date()
const { keyword } = useModel<string>(
(): string => prop.date,
(val: string): void => {
emit(EMIT_DATE, val)
}
)
/** trigger 组件实例 */
const triggerInstance = ref<TriggerInstance>()
Expand Down Expand Up @@ -58,14 +49,10 @@
)
}
// keyword.value = `${year}/${prop.addZero ? addZero(month) : month}/${
// prop.addZero ? addZero(date) : date
// }`
setDateFun = (): void => {
/** 将绑定值设置为格式化后的日期 */
// keyword.value = formatDate
keyword.value = `${year}/${prop.addZero ? addZero(month) : month}/${
dateModelValue.value = `${year}/${prop.addZero ? addZero(month) : month}/${
prop.addZero ? addZero(date) : date
}`
}
Expand Down Expand Up @@ -95,7 +82,7 @@
setDateFun = (): void => {
/** 将绑定值设置为格式化后的日期 */
keyword.value = formatDate
dateModelValue.value = formatDate
}
}
Expand All @@ -120,7 +107,7 @@
<f-trigger ref="triggerInstance" trigger="click" :disabled="disabled">
<!-- 输入框 -->
<f-input
v-model="keyword"
v-model="dateModelValue"
autocomplete="off"
readonly
:disabled="disabled"
Expand Down
9 changes: 4 additions & 5 deletions packages/fighting-design/dialog/src/dialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import { FCloseBtn } from '../../close-btn'
import { useTransition } from '../../_hooks'
import { useVisible, useList } from '../../_hooks'
import { EMIT_VISIBLE } from '../../_tokens'
defineOptions({ name: 'FDialog' })
const prop = defineProps(Props)
const emit = defineEmits([EMIT_VISIBLE])
const visible = defineModel<boolean>('visible', { required: true, default: false })
const { styles, classes } = useList(prop, 'dialog')
const { isVisible, closeVisible, maskClose } = useVisible(prop, emit)
const { closeVisible, maskClose } = useVisible(prop, visible)
const { handleOpen, handleOpenEnd, handleClose, handleCloseEnd } = useTransition(prop)
/** 样式列表 */
Expand All @@ -34,7 +33,7 @@
@after-leave="handleCloseEnd"
>
<div
v-show="isVisible"
v-show="visible"
role="dialog"
aria-modal="true"
tabindex="-1"
Expand All @@ -47,7 +46,7 @@
<!-- 主内容 -->
<div class="f-dialog__wrapper" @click.self="maskClose">
<transition name="f-dialog__container-trans">
<div v-show="isVisible" class="f-dialog__container">
<div v-show="visible" class="f-dialog__container">
<!-- 头部 -->
<header v-if="showHeader" class="f-dialog__header">
<!-- 头部前缀 -->
Expand Down
9 changes: 4 additions & 5 deletions packages/fighting-design/drawer/src/drawer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
import { FCloseBtn } from '../../close-btn'
import { useTransition } from '../../_hooks'
import { useVisible, useList } from '../../_hooks'
import { EMIT_VISIBLE } from '../../_tokens'
defineOptions({ name: 'FDrawer' })
const prop = defineProps(Props)
const emit = defineEmits([EMIT_VISIBLE])
const visible = defineModel<boolean>('visible', { required: true, default: false })
const { styles, classes } = useList(prop, 'drawer')
const { isVisible, closeVisible, maskClose } = useVisible(prop, emit)
const { closeVisible, maskClose } = useVisible(prop, visible)
const { handleOpen, handleOpenEnd, handleClose, handleCloseEnd } = useTransition(prop)
/** 样式列表 */
Expand All @@ -34,7 +33,7 @@
@after-leave="handleCloseEnd"
>
<div
v-show="isVisible"
v-show="visible"
role="dialog"
aria-modal="true"
tabindex="-1"
Expand All @@ -47,7 +46,7 @@
<!-- 主内容 -->
<div class="f-drawer__wrapper" @click.self="maskClose">
<transition name="f-drawer__container-trans">
<div v-show="isVisible" class="f-drawer__container">
<div v-show="visible" class="f-drawer__container">
<!-- 头部 -->
<header v-if="showHeader" class="f-drawer__header">
<slot name="header">
Expand Down
5 changes: 2 additions & 3 deletions packages/fighting-design/image-preview/src/image-preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
FIconZoomOut
} from '../../_svg'
import { useOperationImg, useRun } from '../../_hooks'
import { EMIT_VISIBLE } from '../../_tokens'
defineOptions({ name: 'FImagePreview' })
const prop = defineProps(Props)
const emit = defineEmits([EMIT_VISIBLE])
const visible = defineModel<boolean>('visible', { required: true, default: false })
const { run } = useRun()
const {
Expand Down Expand Up @@ -112,7 +111,7 @@
* @param { Object } evt 事件对象
*/
const handelClose = (evt: MouseEvent): void => {
emit(EMIT_VISIBLE, false)
visible.value = false
run(prop.onClose, evt)
}
</script>
Expand Down
Loading

0 comments on commit c36b40f

Please sign in to comment.