Commit 04531a7856d1db7061c617356e5fec7a844c69ef

Authored by fengwotao
1 parent 32cb530c

perf(external/Composes): 覆盖原作者自带tab切换,加在扩展目录下

  1 +/**
  2 + * 覆盖原作者默认hooks
  3 + * hooks/useChartInteract.hook.ts
  4 + * 目的 修改tab切换显示不同内容
  5 + * 新增代码是写在注释之间,其他原作者代码逻辑未做修改
  6 + */
  7 +import { toRefs } from 'vue'
  8 +import { CreateComponentType } from '@/packages/index.d'
  9 +import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  10 +
  11 +// 获取类型
  12 +type ChartEditStoreType = typeof useChartEditStore
  13 +
  14 +// Params 参数修改触发 api 更新图表请求
  15 +export const useChartInteract = (
  16 + chartConfig: CreateComponentType,
  17 + useChartEditStore: ChartEditStoreType,
  18 + param: { [T: string]: any },
  19 + interactEventOn: string
  20 +) => {
  21 + /**新增代码 */
  22 + let combineData: any = null
  23 + const {
  24 + option: { dataset }
  25 + } = chartConfig
  26 + combineData = dataset
  27 + const { data } = param
  28 + /**新增代码 */
  29 + const chartEditStore = useChartEditStore()
  30 + const { interactEvents } = chartConfig.events
  31 + const fnOnEvent = interactEvents.filter(item => {
  32 + return item.interactOn === interactEventOn
  33 + })
  34 +
  35 + if (fnOnEvent.length === 0) return
  36 + fnOnEvent.forEach((item, itemIndex) => {
  37 + const index = chartEditStore.fetchTargetIndex(item.interactComponentId)
  38 + if (index === -1) return
  39 + const { Params, Header } = toRefs(chartEditStore.componentList[index].request.requestParams)
  40 + Object.keys(item.interactFn).forEach(key => {
  41 + if (Params.value[key]) {
  42 + Params.value[key] = param[item.interactFn[key]]
  43 + }
  44 + if (Header.value[key]) {
  45 + Header.value[key] = param[item.interactFn[key]]
  46 + }
  47 + })
  48 + /**新增代码 */
  49 + combineData.forEach((combinItem: any, combinIndex: number) => {
  50 + if (itemIndex === combinIndex) {
  51 + combinItem.targetItem = chartEditStore.componentList[index]
  52 + }
  53 + })
  54 + /**新增代码 */
  55 + })
  56 + /**新增代码 */
  57 + combineData.forEach((item: any) => {
  58 + try {
  59 + if (item.value === data) item.targetItem.status.hide = false
  60 + else {
  61 + item.targetItem.status.hide = true
  62 + }
  63 + } catch (e) {
  64 + console.log(`useChartInteract.hook.ts:${e}`)
  65 + }
  66 + })
  67 + /**新增代码 */
  68 +}
  69 +
  70 +// 联动事件触发的 type 变更时,清除当前绑定内容
  71 +
  72 +/**新增代码 */
  73 +// eslint-disable-next-line @typescript-eslint/no-empty-function
  74 +export const clearInteractEvent = (chartConfig: CreateComponentType) => {}
  75 +/**新增代码 */
... ...
  1 +import cloneDeep from 'lodash/cloneDeep'
  2 +import { PublicConfigClass } from '@/packages/public'
  3 +import { CreateComponentType } from '@/packages/index.d'
  4 +import { chartInitConfig } from '@/settings/designSetting'
  5 +import { COMPONENT_INTERACT_EVENT_KET } from '@/enums/eventEnum'
  6 +import { interactActions, ComponentInteractEventEnum } from './interact'
  7 +import { OverrideInputsTabConfig } from './index'
  8 +
  9 +export const option = {
  10 + // 时间组件展示类型,必须和 interactActions 中定义的数据一致
  11 + [COMPONENT_INTERACT_EVENT_KET]: ComponentInteractEventEnum.DATA,
  12 + // 默认值
  13 + tabLabel: '选项1',
  14 + // 样式
  15 + tabType: 'segment',
  16 + // 暴露配置内容给用户
  17 + dataset: [
  18 + {
  19 + label: '选项1',
  20 + value: '1'
  21 + },
  22 + {
  23 + label: '选项2',
  24 + value: '2'
  25 + },
  26 + {
  27 + label: '选项3',
  28 + value: '3'
  29 + }
  30 + ]
  31 +}
  32 +
  33 +export default class Config extends PublicConfigClass implements CreateComponentType {
  34 + public key = OverrideInputsTabConfig.key
  35 + public attr = { ...chartInitConfig, w: 460, h: 32, zIndex: -1 }
  36 + public chartConfig = cloneDeep(OverrideInputsTabConfig)
  37 + public interactActions = interactActions
  38 + public option = cloneDeep(option)
  39 +}
... ...
  1 +<template>
  2 + <collapse-item name="标签页配置" :expanded="true">
  3 + <setting-item-box name="默认值" :alone="true">
  4 + <n-select size="small" v-model:value="optionData.tabType" :options="tabTypeOptions" />
  5 + </setting-item-box>
  6 + </collapse-item>
  7 +</template>
  8 +
  9 +<script lang="ts" setup>
  10 +import { PropType } from 'vue'
  11 +import { CollapseItem, SettingItemBox } from '@/components/Pages/ChartItemSetting'
  12 +import { option } from './config'
  13 +
  14 +defineProps({
  15 + optionData: {
  16 + type: Object as PropType<typeof option>,
  17 + required: true
  18 + }
  19 +})
  20 +
  21 +const tabTypeOptions = [
  22 + {
  23 + label: '线条',
  24 + value: 'bar'
  25 + },
  26 + {
  27 + label: '分段',
  28 + value: 'segment'
  29 + }
  30 +]
  31 +</script>
... ...
  1 +import { ConfigType, PackagesCategoryEnum, ChartFrameEnum } from '@/packages/index.d'
  2 +import { ChatCategoryEnum, ChatCategoryEnumName } from '@/packages/components/Informations/index.d'
  3 +import { useWidgetKey } from '@/packages/external/useWidgetKey'
  4 +
  5 +const { key, conKey, chartKey } = useWidgetKey('OverrideInputsTab', true)
  6 +
  7 +export const OverrideInputsTabConfig: ConfigType = {
  8 + key,
  9 + chartKey,
  10 + conKey,
  11 + title: '自定义标签选择器',
  12 + category: ChatCategoryEnum.MORE,
  13 + categoryName: ChatCategoryEnumName.MORE,
  14 + package: PackagesCategoryEnum.INFORMATIONS,
  15 + chartFrame: ChartFrameEnum.STATIC,
  16 + image: 'inputs_tab.png'
  17 +}
... ...
  1 +<template>
  2 + <n-tabs :type="option.value.tabType" @update:value="onChange">
  3 + <n-tab v-for="(item, index) in option.value.dataset" :name="item.label" :key="index"> {{ item.label }} </n-tab>
  4 + </n-tabs>
  5 +</template>
  6 +
  7 +<script setup lang="ts">
  8 +import { PropType, toRefs, shallowReactive, watch, onMounted } from 'vue'
  9 +import cloneDeep from 'lodash/cloneDeep'
  10 +import { CreateComponentType } from '@/packages/index.d'
  11 +import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  12 +import { useChartInteract } from '@/hooks/external/useChartInteract.hook'
  13 +import { InteractEventOn } from '@/enums/eventEnum'
  14 +import { ComponentInteractParamsEnum } from './interact'
  15 +
  16 +const props = defineProps({
  17 + chartConfig: {
  18 + type: Object as PropType<CreateComponentType>,
  19 + required: true
  20 + }
  21 +})
  22 +
  23 +const { w, h } = toRefs(props.chartConfig.attr)
  24 +const option = shallowReactive({
  25 + value: cloneDeep(props.chartConfig.option)
  26 +})
  27 +
  28 +// 监听事件改变
  29 +const onChange = (v: string) => {
  30 + if (v === undefined) return
  31 + const selectItem = option.value.dataset.find((item: { label: string; value: any }) => item.label === v)
  32 + // 存储到联动数据
  33 + useChartInteract(
  34 + props.chartConfig,
  35 + useChartEditStore,
  36 + { [ComponentInteractParamsEnum.DATA]: selectItem.value },
  37 + InteractEventOn.CHANGE
  38 + )
  39 +}
  40 +
  41 +/**新增代码 */
  42 +onMounted(() => {
  43 + onChange(option.value.dataset[0].label)
  44 +})
  45 +/**新增代码 */
  46 +
  47 +// 手动更新
  48 +watch(
  49 + () => props.chartConfig.option,
  50 + (newData: any) => {
  51 + option.value = newData
  52 + onChange(newData.tabValue)
  53 + },
  54 + {
  55 + immediate: true,
  56 + deep: true
  57 + }
  58 +)
  59 +</script>
... ...
  1 +import { InteractEventOn, InteractActionsType } from '@/enums/eventEnum'
  2 +
  3 +// 时间组件类型
  4 +export enum ComponentInteractEventEnum {
  5 + DATA = 'data'
  6 +}
  7 +
  8 +// 联动参数
  9 +export enum ComponentInteractParamsEnum {
  10 + DATA = 'data'
  11 +}
  12 +
  13 +// 定义组件触发回调事件
  14 +export const interactActions: InteractActionsType[] = [
  15 + {
  16 + interactType: InteractEventOn.CHANGE,
  17 + interactName: '选择完成',
  18 + componentEmitEvents: {
  19 + [ComponentInteractEventEnum.DATA]: [
  20 + {
  21 + value: ComponentInteractParamsEnum.DATA,
  22 + label: '选择项'
  23 + }
  24 + ]
  25 + }
  26 + }
  27 +]
... ...
... ... @@ -7,6 +7,7 @@ import { OverrideImageConfig } from '@/packages/components/external/Informations
7 7 import { OverrideCarouselConfig } from '@/packages/components/external/Informations/Mores/OverrideCarousel'
8 8 import { OverrideSelectConfig } from '@/packages/components/external/Informations/Mores/OverrideSelect'
9 9 import { OverrideInputsDateConfig } from '@/packages/components/external/Informations/Mores/OverrideInputsDate'
  10 +import { OverrideInputsTabConfig } from '@/packages/components/external/Informations/Mores/OverrideInputsTab'
10 11
11 12 export function useInjectLib(packagesList: EPackagesType) {
12 13
... ... @@ -18,6 +19,7 @@ export function useInjectLib(packagesList: EPackagesType) {
18 19 addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideCarouselConfig)
19 20 addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideSelectConfig)
20 21 addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideInputsDateConfig)
  22 + addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideInputsTabConfig)
21 23 }
22 24
23 25 /**
... ...