提交 e1729eff 作者: 方治民

feat: 优化 Hook Basic 示例解包处理,减少重复代码

上级 2e3d7fbc
...@@ -72,9 +72,9 @@ ...@@ -72,9 +72,9 @@
</view> </view>
<!-- 自定义内容 --> <!-- 自定义内容 -->
<view class="content flex-center"> <scroll-view scroll-y class="content flex-center">
<slot></slot> <slot></slot>
</view> </scroll-view>
<!-- 底部安全区 --> <!-- 底部安全区 -->
<fui-safe-area /> <fui-safe-area />
......
import { getBasicWidgetInstanceFunctions, getBooleanOrDefault, registerFactory } from '../../utils' import { getBooleanOrDefault, registerWidgetFactory, unpackWidgetInstance } from '../../utils'
import type { BottomBarInstance, BottomBarProps } from './types' import type { BottomBarInstance, BottomBarProps } from './types'
// 组件名称 // 组件名称
...@@ -13,14 +13,13 @@ export function useBottomBarWidget<T extends BottomBarInstance, P extends Bottom ...@@ -13,14 +13,13 @@ export function useBottomBarWidget<T extends BottomBarInstance, P extends Bottom
props: P, props: P,
): [(instance: T) => void, BottomBarInstance] { ): [(instance: T) => void, BottomBarInstance] {
const instanceRef = ref<T>() const instanceRef = ref<T>()
const register = registerFactory(instanceRef, props) const register = registerWidgetFactory(instanceRef, props)
const { get, setProps, toggleShow } = getBasicWidgetInstanceFunctions(instanceRef, name) const { get, ...fns } = unpackWidgetInstance(instanceRef, name)
return [ return [
register, register,
{ {
setProps, ...fns,
toggleShow,
toggleExpand: (expand?: boolean) => get()?.toggleExpand(getBooleanOrDefault(expand)), toggleExpand: (expand?: boolean) => get()?.toggleExpand(getBooleanOrDefault(expand)),
height: computed(() => instanceRef?.value?.height) as unknown as ComputedRef<string>, height: computed(() => instanceRef?.value?.height) as unknown as ComputedRef<string>,
}, },
......
import { getBasicWidgetInstanceFunctions, getBooleanOrDefault, registerFactory } from '../../utils' import { getBooleanOrDefault, registerWidgetFactory, unpackWidgetInstance } from '../../utils'
import type { LegendInstance, LegendProps, Option } from './types' import type { LegendInstance, LegendProps, Option } from './types'
// 组件名称 // 组件名称
...@@ -13,14 +13,13 @@ export function useLegendWidget<T extends LegendInstance, P extends LegendProps> ...@@ -13,14 +13,13 @@ export function useLegendWidget<T extends LegendInstance, P extends LegendProps>
props: P, props: P,
): [(instance: T) => void, LegendInstance] { ): [(instance: T) => void, LegendInstance] {
const instanceRef = ref<T>() const instanceRef = ref<T>()
const register = registerFactory(instanceRef, props) const register = registerWidgetFactory(instanceRef, props)
const { get, setProps, toggleShow } = getBasicWidgetInstanceFunctions(instanceRef, name) const { get, ...fns } = unpackWidgetInstance(instanceRef, name)
return [ return [
register, register,
{ {
setProps, ...fns,
toggleShow,
setTitle: (title: string) => get()?.setTitle(title), setTitle: (title: string) => get()?.setTitle(title),
setOption: (option: Option) => get()?.setOption(option), setOption: (option: Option) => get()?.setOption(option),
toggleExpand: (expand?: boolean) => get()?.toggleExpand(getBooleanOrDefault(expand)), toggleExpand: (expand?: boolean) => get()?.toggleExpand(getBooleanOrDefault(expand)),
......
export type * from './src/types' export type * from './src/types'
export * from './src/hook' export * from './src/hook'
export { default as SwitchControlWidget } from './src/SwitchControl.vue' export { default as SwitchWidget } from './src/Switch.vue'
...@@ -20,9 +20,7 @@ ...@@ -20,9 +20,7 @@
const data = reactive({ const data = reactive({
show: props.show, show: props.show,
bottom: props.bottom, bottom: props.bottom,
// 往前
prev: () => {}, prev: () => {},
// 往后
next: () => {}, next: () => {},
}) })
......
import { getBasicWidgetInstanceFunctions, registerFactory } from '../../utils' import { registerWidgetFactory, unpackWidgetInstance } from '../../utils'
import type { SwitchControlInstance, SwitchControlProps } from './types' import type { SwitchControlInstance, SwitchControlProps } from './types'
// 组件名称 // 组件名称
...@@ -9,18 +9,17 @@ export const name = 'SwitchControlWidget' ...@@ -9,18 +9,17 @@ export const name = 'SwitchControlWidget'
* @param props 组件参数 * @param props 组件参数
* @returns 组件响应式数据 * @returns 组件响应式数据
*/ */
export function useSwitchControlWidget<T extends SwitchControlInstance, P extends SwitchControlProps>( export function useSwitchWidget<T extends SwitchControlInstance, P extends SwitchControlProps>(
props: P, props: P,
): [(instance: T) => void, SwitchControlInstance] { ): [(instance: T) => void, SwitchControlInstance] {
const instanceRef = ref<T>() const instanceRef = ref<T>()
const register = registerFactory(instanceRef, props) const register = registerWidgetFactory(instanceRef, props)
const { get, setProps, toggleShow } = getBasicWidgetInstanceFunctions(instanceRef, name) const { get, ...fns } = unpackWidgetInstance(instanceRef, name)
return [ return [
register, register,
{ {
setProps, ...fns,
toggleShow,
prev: () => get()?.prev(), prev: () => get()?.prev(),
next: () => get()?.next(), next: () => get()?.next(),
}, },
......
import type { Dayjs } from 'dayjs' import type { Dayjs } from 'dayjs'
import { getBasicWidgetInstanceFunctions, registerFactory } from '../../utils' import { registerWidgetFactory, unpackWidgetInstance } from '../../utils'
import type { TimeBarInstance, TimeBarProps } from './types' import type { TimeBarInstance, TimeBarProps } from './types'
// 组件名称 // 组件名称
...@@ -15,14 +15,13 @@ export function useTimeBarWidget<T extends TimeBarInstance, P extends TimeBarPro ...@@ -15,14 +15,13 @@ export function useTimeBarWidget<T extends TimeBarInstance, P extends TimeBarPro
props: P, props: P,
): [(instance: T) => void, TimeBarInstance] { ): [(instance: T) => void, TimeBarInstance] {
const instanceRef = ref<T>() const instanceRef = ref<T>()
const register = registerFactory(instanceRef, props) const register = registerWidgetFactory(instanceRef, props)
const { get, setProps, toggleShow } = getBasicWidgetInstanceFunctions(instanceRef, name) const { get, ...fns } = unpackWidgetInstance(instanceRef, name)
return [ return [
register, register,
{ {
setProps, ...fns,
toggleShow,
getTime: () => get()?.getTime(), getTime: () => get()?.getTime(),
setTime: (time: Dayjs[]) => get()?.setTime(time), setTime: (time: Dayjs[]) => get()?.setTime(time),
getCheckedOption: () => get()?.getCheckedOption(), getCheckedOption: () => get()?.getCheckedOption(),
......
import { getBasicWidgetInstanceFunctions, getBooleanOrDefault, registerFactory } from '../../utils' import { getBooleanOrDefault, registerWidgetFactory, unpackWidgetInstance } from '../../utils'
import type { ToolBoxInstance, ToolBoxProps } from './types' import type { ToolBoxInstance, ToolBoxProps } from './types'
// 组件名称 // 组件名称
...@@ -13,14 +13,13 @@ export function useToolBoxWidget<T extends ToolBoxInstance, P extends ToolBoxPro ...@@ -13,14 +13,13 @@ export function useToolBoxWidget<T extends ToolBoxInstance, P extends ToolBoxPro
props: P, props: P,
): [(instance: T) => void, ToolBoxInstance] { ): [(instance: T) => void, ToolBoxInstance] {
const instanceRef = ref<T>() const instanceRef = ref<T>()
const register = registerFactory(instanceRef, props) const register = registerWidgetFactory(instanceRef, props)
const { get, setProps, toggleShow } = getBasicWidgetInstanceFunctions(instanceRef, name) const { get, ...fns } = unpackWidgetInstance(instanceRef, name)
return [ return [
register, register,
{ {
setProps, ...fns,
toggleShow,
toggleExpand: (expand?: boolean) => get()?.toggleExpand(getBooleanOrDefault(expand)), toggleExpand: (expand?: boolean) => get()?.toggleExpand(getBooleanOrDefault(expand)),
}, },
] ]
......
...@@ -35,7 +35,7 @@ export function getBooleanOrDefault(value: any, def = null): boolean { ...@@ -35,7 +35,7 @@ export function getBooleanOrDefault(value: any, def = null): boolean {
* @param props 组件属性 * @param props 组件属性
* @param callback 回调函数 * @param callback 回调函数
*/ */
export function registerFactory<T extends BasicWidgetInstance<P>, P>( export function registerWidgetFactory<T extends BasicWidgetInstance<P>, P>(
instanceRef: Ref<Nullable<T>>, instanceRef: Ref<Nullable<T>>,
props: P, props: P,
callback?: (instance: T) => void, callback?: (instance: T) => void,
...@@ -65,7 +65,7 @@ export function registerFactory<T extends BasicWidgetInstance<P>, P>( ...@@ -65,7 +65,7 @@ export function registerFactory<T extends BasicWidgetInstance<P>, P>(
* @param name 组件名称 * @param name 组件名称
* @returns 组件实例 * @returns 组件实例
*/ */
export function getInstance<T extends BasicWidgetInstance>(instanceRef: Ref<Nullable<T>>, name?: string): T { export function getWidgetInstance<T extends BasicWidgetInstance>(instanceRef: Ref<Nullable<T>>, name?: string): T {
const instance = unref(instanceRef) const instance = unref(instanceRef)
if (!instance) { if (!instance) {
console.warn(`The ${name || 'widget'} instance has not been obtained`) console.warn(`The ${name || 'widget'} instance has not been obtained`)
...@@ -80,14 +80,14 @@ export function getInstance<T extends BasicWidgetInstance>(instanceRef: Ref<Null ...@@ -80,14 +80,14 @@ export function getInstance<T extends BasicWidgetInstance>(instanceRef: Ref<Null
* @returns 组件实例函数 * @returns 组件实例函数
* @example * @example
* ```ts * ```ts
* const { get, setProps, toggleShow } = getBasicWidgetInstanceFunctions<BasicWidgetInstance, BasicWidgetProps>(instanceRef, 'basic widget') * const { get, setProps, toggleShow } = unpackWidgetInstance<BasicWidgetInstance, BasicWidgetProps>(instanceRef, 'basic widget')
* ``` * ```
*/ */
export function getBasicWidgetInstanceFunctions<T extends BasicWidgetInstance, P extends BasicWidgetProps>( export function unpackWidgetInstance<T extends BasicWidgetInstance, P extends BasicWidgetProps>(
instanceRef: Ref<Nullable<T>>, instanceRef: Ref<Nullable<T>>,
name?: string, name?: string,
) { ) {
const get = () => getInstance(instanceRef, name) const get = () => getWidgetInstance(instanceRef, name)
return { return {
get, get,
......
...@@ -5,10 +5,10 @@ ...@@ -5,10 +5,10 @@
import { useShare } from '@/hooks/page/useShare' import { useShare } from '@/hooks/page/useShare'
import type { MapboxConfig } from '@/components/Map/Mapbox' import type { MapboxConfig } from '@/components/Map/Mapbox'
import { LegendWidget, useLegendWidget } from '@/components/Map/Widgets/Legend' import { LegendWidget, useLegendWidget } from '@/components/Map/Widgets/Legend'
import { SwitchWidget, useSwitchWidget } from '@/components/Map/Widgets/Switch'
import { TimeBarWidget, formatTime, useTimeBarWidget } from '@/components/Map/Widgets/TimeBar' import { TimeBarWidget, formatTime, useTimeBarWidget } from '@/components/Map/Widgets/TimeBar'
import { ToolBoxWidget, useToolBoxWidget } from '@/components/Map/Widgets/ToolBox' import { ToolBoxWidget, useToolBoxWidget } from '@/components/Map/Widgets/ToolBox'
import { BottomBarWidget, useBottomBarWidget } from '@/components/Map/Widgets/BottomBar' import { BottomBarWidget, useBottomBarWidget } from '@/components/Map/Widgets/BottomBar'
import { SwitchControlWidget, useSwitchControlWidget } from '@/components/Map/Widgets/SwitchControl'
useShare() useShare()
...@@ -197,7 +197,7 @@ ...@@ -197,7 +197,7 @@
}) })
// 前后切换小部件 // 前后切换小部件
const [registerSwitchControlWidget, { setProps: setSwitchControlProps }] = useSwitchControlWidget({ const [registerSwitchWidget, { setProps: setSwitchProps }] = useSwitchWidget({
show: true, show: true,
prev: () => { prev: () => {
const { option, value } = getTimeBarValue() const { option, value } = getTimeBarValue()
...@@ -240,7 +240,7 @@ ...@@ -240,7 +240,7 @@
console.log('[useBottomBarWidget] height', value) console.log('[useBottomBarWidget] height', value)
setToolBoxProps({ bottom: value }) setToolBoxProps({ bottom: value })
setSwitchControlProps({ bottom: value }) setSwitchProps({ bottom: value })
}, },
) )
</script> </script>
...@@ -259,7 +259,7 @@ ...@@ -259,7 +259,7 @@
<ToolBoxWidget @register="registerToolBoxWidget" /> <ToolBoxWidget @register="registerToolBoxWidget" />
<!-- 前后切换小部件 --> <!-- 前后切换小部件 -->
<SwitchControlWidget @register="registerSwitchControlWidget" /> <SwitchWidget @register="registerSwitchWidget" />
<!-- 图例小部件 --> <!-- 图例小部件 -->
<LegendWidget @register="registerLegendWidget" /> <LegendWidget @register="registerLegendWidget" />
...@@ -267,7 +267,7 @@ ...@@ -267,7 +267,7 @@
<!-- 底部 Bar 小部件 --> <!-- 底部 Bar 小部件 -->
<BottomBarWidget @register="registerBottomBarWidget"> <BottomBarWidget @register="registerBottomBarWidget">
<!-- 内容 Slot --> <!-- 内容 Slot -->
<view class="c-coolGray" @tap="testPackUp">底部交互控件/展示内容</view> <view class="flex-center c-gray" @tap="testPackUp">底部交互控件/展示内容</view>
</BottomBarWidget> </BottomBarWidget>
</view> </view>
</view> </view>
......
...@@ -14,6 +14,7 @@ declare module 'vue' { ...@@ -14,6 +14,7 @@ declare module 'vue' {
FDragItem: typeof import('./../src/components/FirstUI/fui-drag/f-drag-item.vue')['default'] FDragItem: typeof import('./../src/components/FirstUI/fui-drag/f-drag-item.vue')['default']
FIndexListItem: typeof import('./../src/components/FirstUI/fui-index-list/f-index-list-item.vue')['default'] FIndexListItem: typeof import('./../src/components/FirstUI/fui-index-list/f-index-list-item.vue')['default']
FloatFillterWidget: typeof import('./../src/components/Map/Widgets/FloatFillterWidget.vue')['default'] FloatFillterWidget: typeof import('./../src/components/Map/Widgets/FloatFillterWidget.vue')['default']
FloatFilterWidget: typeof import('./../src/components/Map/Widgets/FloatFilterWidget.vue')['default']
FuiActionsheet: typeof import('./../src/components/FirstUI/fui-actionsheet/fui-actionsheet.vue')['default'] FuiActionsheet: typeof import('./../src/components/FirstUI/fui-actionsheet/fui-actionsheet.vue')['default']
FuiAlert: typeof import('./../src/components/FirstUI/fui-alert/fui-alert.vue')['default'] FuiAlert: typeof import('./../src/components/FirstUI/fui-alert/fui-alert.vue')['default']
FuiAnimation: typeof import('./../src/components/FirstUI/fui-animation/fui-animation.vue')['default'] FuiAnimation: typeof import('./../src/components/FirstUI/fui-animation/fui-animation.vue')['default']
...@@ -143,6 +144,7 @@ declare module 'vue' { ...@@ -143,6 +144,7 @@ declare module 'vue' {
MenuHeader: typeof import('./../src/components/Layout/MenuHeader.vue')['default'] MenuHeader: typeof import('./../src/components/Layout/MenuHeader.vue')['default']
RouterLink: typeof import('vue-router')['RouterLink'] RouterLink: typeof import('vue-router')['RouterLink']
RouterView: typeof import('vue-router')['RouterView'] RouterView: typeof import('vue-router')['RouterView']
Switch: typeof import('./../src/components/Map/Widgets/Switch/src/Switch.vue')['default']
SwitchControl: typeof import('./../src/components/Map/Widgets/SwitchControl/src/SwitchControl.vue')['default'] SwitchControl: typeof import('./../src/components/Map/Widgets/SwitchControl/src/SwitchControl.vue')['default']
ThumbnailPreview: typeof import('./../src/components/ThumbnailPreview/index.vue')['default'] ThumbnailPreview: typeof import('./../src/components/ThumbnailPreview/index.vue')['default']
TimeBar: typeof import('./../src/components/Map/Widgets/TimeBar/src/TimeBar.vue')['default'] TimeBar: typeof import('./../src/components/Map/Widgets/TimeBar/src/TimeBar.vue')['default']
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论