perf: 优化交互结构,优化时间选择器组件

This commit is contained in:
奔跑的面条 2023-03-14 22:19:55 +08:00
parent 63db4f8c87
commit 40453c51bb
9 changed files with 125 additions and 118 deletions

View File

@ -17,17 +17,20 @@ export enum InteractEvents {
INTERACT_FN = 'interactFn' INTERACT_FN = 'interactFn'
} }
// 组件交互回调事件触发的类型 // 全局组件交互回调事件触发的类型(当然可以自定义名称)
export enum InteractEventOn { export enum InteractEventOn {
CLICK = 'click', CLICK = 'click',
CHANGE = 'change' CHANGE = 'change'
} }
// 确定交互组件触发类型 key名称
export const COMPONENT_INTERACT_EVENT_KET = 'componentInteractEventKey'
// 交互式组件的触发配置 // 交互式组件的触发配置
export type InteractActionType = { export type InteractActionsType = {
interactType: InteractEventOn interactType: InteractEventOn
interactName: string interactName: string
componentEmitEvents: { [T: string]: any[] } componentEmitEvents: { [T: string]: { value: any; label: string }[] }
} }
// vue3 生命周期事件 // vue3 生命周期事件

View File

@ -87,16 +87,14 @@ export const useChartDataFetch = (
} }
} }
// 立即调用 // 普通初始化与组件交互处理监听
fetchFn()
// 组件交互处理监听
watch( watch(
() => targetComponent.request, () => targetComponent.request,
() => { () => {
fetchFn() fetchFn()
}, },
{ {
immediate: true,
deep: true deep: true
} }
) )

View File

@ -9,14 +9,13 @@ type ChartEditStoreType = typeof useChartEditStore
export const useChartInteract = ( export const useChartInteract = (
chartConfig: CreateComponentType, chartConfig: CreateComponentType,
useChartEditStore: ChartEditStoreType, useChartEditStore: ChartEditStoreType,
param: { [name: string]: string }, param: { [T: string]: any },
onEvent: string interactEventOn: string
) => { ) => {
const chartEditStore = useChartEditStore() const chartEditStore = useChartEditStore()
const { interactEvents } = chartConfig.events const { interactEvents } = chartConfig.events
const fnOnEvent = interactEvents.filter(item => { const fnOnEvent = interactEvents.filter(item => {
return item.interactOn === onEvent return item.interactOn === interactEventOn
}) })
if (fnOnEvent.length === 0) return if (fnOnEvent.length === 0) return
@ -34,3 +33,8 @@ export const useChartInteract = (
}) })
}) })
} }
// 联动事件触发的 type 变更时,清除当前绑定内容
export const clearInteractEvent = (chartConfig: CreateComponentType) => {
}

View File

@ -1,52 +1,18 @@
import { NDatePicker } from 'naive-ui' import dayjs from 'dayjs'
import cloneDeep from 'lodash/cloneDeep'
import { PublicConfigClass } from '@/packages/public' import { PublicConfigClass } from '@/packages/public'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { InputsDateConfig } from './index'
import cloneDeep from 'lodash/cloneDeep'
import { chartInitConfig } from '@/settings/designSetting' import { chartInitConfig } from '@/settings/designSetting'
import { InteractEventOn, InteractActionType } from '@/enums/eventEnum' import { COMPONENT_INTERACT_EVENT_KET } from '@/enums/eventEnum'
import { interactActions, ComponentInteractEventEnum } from './interact'
// 时间组件类型 import { InputsDateConfig } from './index'
enum ComponentInteractEvent {
DATE = 'date',
DATERANGE = 'daterange'
}
export const option = { export const option = {
dataset: { // 时间组件展示类型,必须和 interactActions 中定义的数据一致
count: 0, [COMPONENT_INTERACT_EVENT_KET]: ComponentInteractEventEnum.DATE,
// 时间组件展示类型 daterange & date dataset: dayjs().valueOf()
type: ComponentInteractEvent.DATE,
range: undefined
}
} }
// 定义组件触发回调事件
const interactActions: InteractActionType[] = [
{
interactType: InteractEventOn.CHANGE,
interactName: '完成后的回调',
componentEmitEvents: {
[ComponentInteractEvent.DATE]: [
{
value: 'date',
label: '日期'
}
],
[ComponentInteractEvent.DATERANGE]: [
{
value: 'dateStart',
label: '开始时间'
},
{
value: 'dateEnd',
label: '结束时间'
}
]
}
}
]
export default class Config extends PublicConfigClass implements CreateComponentType { export default class Config extends PublicConfigClass implements CreateComponentType {
public key = InputsDateConfig.key public key = InputsDateConfig.key
public attr = { ...chartInitConfig, w: 260, h: 32, zIndex: -1 } public attr = { ...chartInitConfig, w: 260, h: 32, zIndex: -1 }

View File

@ -1,42 +1,23 @@
<template> <template>
<CollapseItem name="通用的Props" :expanded="true"> <collapse-item name="时间配置" :expanded="true">
<SettingItemBox name="基础"> <setting-item-box name="基础">
<setting-item name="类型"> <setting-item name="类型">
<n-select v-model:value="props.optionData.dataset.type" size="small" :options="datePickerTypeOptions" /> <n-select
v-model:value="props.optionData.componentInteractEventKey"
size="small"
:options="datePickerTypeOptions"
/>
</setting-item> </setting-item>
</SettingItemBox> </setting-item-box>
<SettingItemBox name="默认值"> <setting-item-box name="默认值" :alone="true">
<n-date-picker <n-date-picker
size="small" size="small"
:style="{ width: ['date'].includes(props.optionData.dataset.type) ? 'auto' : '250px' }" v-model:value="props.optionData.dataset"
v-model:value="props.optionData.dataset.range" :type="props.optionData.componentInteractEventKey"
:type="props.optionData.dataset.type"
clearable
/> />
</SettingItemBox> </setting-item-box>
<SettingItemBox> </collapse-item>
<template #name>
<n-text>动态</n-text>
<n-tooltip trigger="hover">
<template #trigger>
<n-icon size="21" :depth="3">
<help-outline-icon></help-outline-icon>
</n-icon>
</template>
<n-text>动态日期以默认值进行计算</n-text>
</n-tooltip>
</template>
<setting-item name="计算值">
<n-input-number v-model:value="props.optionData.dataset.count" size="small" placeholder="0">
<template #prefix>
<n-text depth="3"></n-text>
</template>
</n-input-number>
</setting-item>
</SettingItemBox>
</CollapseItem>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
@ -56,7 +37,7 @@ const props = defineProps({
const datePickerTypeOptions = [ const datePickerTypeOptions = [
{ {
label: '日期', label: '具体日期',
value: 'date' value: 'date'
}, },
{ {

View File

@ -1,71 +1,81 @@
<template> <template>
<div class="mill-date-box"> <div class="mill-date-box">
<div :style="`width:${w}px; height:${h}px;`"> <div :style="`width:${w}px; height:${h}px;`">
<n-date-picker v-model:value="rangeDate" :type="option.dataset.type" @update:value="onChange" /> <n-date-picker
v-model:value="option.dataset"
:type="chartConfig.option.componentInteractEventKey"
@update:value="onChange"
/>
</div> </div>
</div> </div>
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { PropType, toRefs, ref, shallowReactive, watch } from 'vue' import { PropType, toRefs, ref, shallowReactive, watch } from 'vue'
import dayjs from 'dayjs'
import { CreateComponentType } from '@/packages/index.d' import { CreateComponentType } from '@/packages/index.d'
import { useChartDataFetch } from '@/hooks' import { useChartDataFetch } from '@/hooks'
import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore' import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
import { option as configOption } from './config'
import { useChartInteract } from '@/hooks' import { useChartInteract } from '@/hooks'
import dayjs from 'dayjs' import { InteractEventOn } from '@/enums/eventEnum'
import { ComponentInteractParamsEnum } from './interact'
const props = defineProps({ const props = defineProps({
chartConfig: { chartConfig: {
type: Object as PropType<CreateComponentType & typeof option>, type: Object as PropType<CreateComponentType>,
required: true required: true
} }
}) })
const { w, h } = toRefs(props.chartConfig.attr) const { w, h } = toRefs(props.chartConfig.attr)
const rangeDate = ref() const rangeDate = ref<number | number[]>()
const option = shallowReactive({ const option = shallowReactive({
dataset: configOption.dataset dataset: props.chartConfig.option.dataset
}) })
//
const onChange = (v: number | number[]) => { const onChange = (v: number | number[]) => {
if (v instanceof Array) { if (v instanceof Array) {
const dateStart = dayjs(v[0]).format('YYYY-MM-DD') //
const dateEnd = dayjs(v[1]).format('YYYY-MM-DD') useChartInteract(
useChartInteract(props.chartConfig, useChartEditStore, { dateStart, dateEnd }, 'change') props.chartConfig,
useChartEditStore,
{
[ComponentInteractParamsEnum.DATE_START]: v[0] | dayjs().valueOf(),
[ComponentInteractParamsEnum.DATE_END]: v[1] | dayjs().valueOf(),
[ComponentInteractParamsEnum.DATE_RANGE]: `${v[0]}-${v[1]}`
},
InteractEventOn.CHANGE
)
} else { } else {
const date = dayjs(v).format('YYYY-MM-DD') //
useChartInteract(props.chartConfig, useChartEditStore, { date }, 'change') useChartInteract(
props.chartConfig,
useChartEditStore,
{ [ComponentInteractParamsEnum.DATE]: v },
InteractEventOn.CHANGE
)
} }
} }
// //
watch( watch(
() => props.chartConfig.option.dataset, () => props.chartConfig.option.dataset,
(newData: any) => { (newData: number | number[]) => {
option.dataset = newData option.dataset = newData
const { range, count } = newData //
if (!range) return onChange(newData)
if (newData.range instanceof Array) {
const countDate: number[] = [
dayjs(range[0]).add(count, 'day').valueOf(),
dayjs(range[1]).add(count, 'day').valueOf()
]
rangeDate.value = countDate
} else {
const countDate: number = dayjs(range).add(count, 'day').valueOf()
rangeDate.value = countDate
}
}, },
{ {
immediate: true, immediate: true
deep: true
} }
) )
// //
useChartDataFetch(props.chartConfig, useChartEditStore) useChartDataFetch(props.chartConfig, useChartEditStore, (newData: number | number[]) => {
option.dataset = newData
})
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>

View File

@ -0,0 +1,45 @@
import { InteractEventOn, InteractActionsType } from '@/enums/eventEnum'
// 时间组件类型
export enum ComponentInteractEventEnum {
DATE = 'date',
DATE_RANGE = 'daterange'
}
// 联动参数
export enum ComponentInteractParamsEnum {
DATE = 'date',
DATE_START = 'dateStart',
DATE_END = 'dateEnd',
DATE_RANGE = 'daterange'
}
// 定义组件触发回调事件
export const interactActions: InteractActionsType[] = [
{
interactType: InteractEventOn.CHANGE,
interactName: '完成后的回调',
componentEmitEvents: {
[ComponentInteractEventEnum.DATE]: [
{
value: ComponentInteractParamsEnum.DATE,
label: '日期'
}
],
[ComponentInteractEventEnum.DATE_RANGE]: [
{
value: ComponentInteractParamsEnum.DATE_START,
label: '开始时间'
},
{
value: ComponentInteractParamsEnum.DATE_END,
label: '结束时间'
},
{
value: ComponentInteractParamsEnum.DATE_RANGE,
label: '日期范围'
}
]
}
}
]

View File

@ -1,4 +1,4 @@
import { BaseEvent, EventLife, InteractEvents, InteractEventOn, InteractActionType } from '@/enums/eventEnum' import { BaseEvent, EventLife, InteractEvents, InteractEventOn, InteractActionsType } from '@/enums/eventEnum'
import type { GlobalThemeJsonType } from '@/settings/chartThemes/index' import type { GlobalThemeJsonType } from '@/settings/chartThemes/index'
import type { RequestConfigType } from '@/store/modules/chartEditStore/chartEditStore.d' import type { RequestConfigType } from '@/store/modules/chartEditStore/chartEditStore.d'
@ -120,7 +120,7 @@ export interface PublicConfigType {
} }
filter?: string filter?: string
status: StatusType status: StatusType
interactActions?: InteractActionType[], interactActions?: InteractActionsType[],
events: { events: {
baseEvent: { baseEvent: {
[K in BaseEvent]?: string [K in BaseEvent]?: string
@ -129,7 +129,7 @@ export interface PublicConfigType {
[K in EventLife]?: string [K in EventLife]?: string
} }
interactEvents: { interactEvents: {
[InteractEvents.INTERACT_ON]: InteractEventOn.CHANGE | InteractEventOn.CLICK | undefined [InteractEvents.INTERACT_ON]: InteractEventOn | undefined
[InteractEvents.INTERACT_COMPONENT_ID]: string | undefined [InteractEvents.INTERACT_COMPONENT_ID]: string | undefined
[InteractEvents.INTERACT_FN]: { [name: string]: string } [InteractEvents.INTERACT_FN]: { [name: string]: string }
}[] }[]

View File

@ -125,7 +125,7 @@ import { SelectOption, SelectGroupOption } from 'naive-ui'
import { SettingItemBox, SettingItem, CollapseItem } from '@/components/Pages/ChartItemSetting' import { SettingItemBox, SettingItem, CollapseItem } from '@/components/Pages/ChartItemSetting'
import { CreateComponentType, CreateComponentGroupType, ChartFrameEnum } from '@/packages/index.d' import { CreateComponentType, CreateComponentGroupType, ChartFrameEnum } from '@/packages/index.d'
import { RequestParamsTypeEnum } from '@/enums/httpEnum' import { RequestParamsTypeEnum } from '@/enums/httpEnum'
import { InteractEventOn } from '@/enums/eventEnum' import { InteractEventOn, COMPONENT_INTERACT_EVENT_KET } from '@/enums/eventEnum'
import { icon } from '@/plugins' import { icon } from '@/plugins'
import noData from '@/assets/images/canvas/noData.png' import noData from '@/assets/images/canvas/noData.png'
import { goDialog } from '@/utils' import { goDialog } from '@/utils'
@ -163,7 +163,7 @@ const fnDimensionsAndSource = (interactOn: InteractEventOn | undefined) => {
return item.interactType === interactOn return item.interactType === interactOn
}) })
return tableData?.componentEmitEvents[option.value.dataset.type] || [] return tableData?.componentEmitEvents[option.value[COMPONENT_INTERACT_EVENT_KET]] || []
} }
// //