前端自定义组件之多页面步骤条

本文将讲解如何通过自定义,实现多页面的步骤条组件。其中每个步骤的元素里都对应界面设计器的一个页面。以下是代码实现和原理分析。

代码实现

NextStepWidget 多页面步骤条 ts 组件

import {
  CallChaining,
  createRuntimeContextByView,
  customMutation,
  customQuery,
  RuntimeView,
  SPI,
  ViewCache,
  Widget,
  DefaultTabsWidget,
  BasePackWidget
} from '@oinone/kunlun-dependencies';
import { isEmpty } from 'lodash-es';
import { MyMetadataViewWidget } from './MyMetadataViewWidget';
import NextStep from './NextStep.vue';
import { IStepConfig, StepDirection } from './type';

@SPI.ClassFactory(BasePackWidget.Token({ widget: 'NextStep' }))
export class NextStepWidget extends DefaultTabsWidget {
  public initialize(props) {
    this.titles = props.template?.widgets?.map((item) => item.title) || [];
    props.template && (props.template.widgets = []);
    super.initialize(props);
    this.setComponent(NextStep);
    return this;
  }

  @Widget.Reactive()
  public get invisible() {
    return false;
  }

  // 配置的每一步名称
  @Widget.Reactive()
  public titles = [];

  // region 上一步下一步配置

  // 步骤配置,切换的顺序就是数组的顺序,模型没有限制
  @Widget.Reactive()
  public get stepJsonConfig() {
    let data = JSON.parse(
      this.getDsl().stepJsonConfig ||
        '[{"model":"resource.ResourceCountry","viewName":"国家form"},{"model":"resource.ResourceProvince","viewName":"省form"},{"model":"resource.ResourceCity","viewName":"市form"}]'
    );
    return data as IStepConfig[];
  }

  // 切换上一步下一步
  @Widget.Method()
  public async onStepChange(stepDirection: StepDirection) {
    // 没有激活的,说明是初始化,取第一步
    if (!this.activeStepKey) {
      const step = this.stepJsonConfig[0];
      if (step) {
        this.activeStepKey = `${step.model}_${step.viewName}`;
        await this.initStepView(step);
      }
      return;
    }

    // 获取当前步骤的索引
    if (this.currentActiveKeyIndex > -1) {
      await this.onSave();
      // 获取下一步索引
      const nextStepIndex =
        stepDirection === StepDirection.NEXT ? this.currentActiveKeyIndex + 1 : this.currentActiveKeyIndex - 1;
      // 在索引范围内,则渲染视图
      if (nextStepIndex >= 0 && nextStepIndex < this.stepJsonConfig.length) {
        const nextStep = this.stepJsonConfig[nextStepIndex];
        if (nextStep) {
          this.activeStepKey = `${nextStep.model}_${nextStep.viewName}`;
          await this.initStepView(nextStep);
        }
      }
    }
  }

  // region 创建上一步下一步视图

  // 每个步骤视图组件
  @Widget.Reactive()
  public stepViewWidget: Record<string, MyMetadataViewWidget> = {};

  // 当前激活的步骤,用 model 和 viewName 联合作为 key
  @Widget.Reactive()
  public activeStepKey: string = '';

  // 当前激活的步骤索引,即当前是第几步,从 0 开始
  @Widget.Reactive()
  public get currentActiveKeyIndex() {
    return this.stepJsonConfig.findIndex((step) => `${step.model}_${step.viewName}` === this.activeStepKey);
  }

  // 传入 step,动态创建一个步骤视图,并初始化数据
  public async initStepView(step: IStepConfig) {
    const widget = this.stepViewWidget[`${step.model}_${step.viewName}`];
    if (widget) {
      // 命中缓存
      this.initStepViewData(step);
      return;
    }

    // 根据 stepJsonConfig 里的 model 和 viewName 获取页面配置
    const resView = await ViewCache.get(step.model, step.viewName);
    if (resView) {
      // 创建一个元数据隔离的视图组件
      const widget = this.createDynamicWidget(resView, step);
      if (!widget) {
        throw new Error('Invalid widget.');
      }
      this.stepViewWidget[`${step.model}_${step.viewName}`] = widget;
      this.initStepViewData(step);
    }
  }

  // 根据取的中间协议视图 view,动态构建视图
  public createDynamicWidget(view: RuntimeView, step: IStepConfig) {
    if (view) {
      // 中间协议构建上下文
      const runtimeContext = createRuntimeContextByView(view, true, `Form_${Math.random()}`, this.currentHandle);
      runtimeContext.parentContext = this.rootRuntimeContext;
      // 取得上下文唯一标识

      const runtimeContextHandle = runtimeContext.handle;
      const widget = this.createWidget(
        new MyMetadataViewWidget(runtimeContextHandle),
        `Form_${step.model}_${step.viewName}`,
        {
          metadataHandle: runtimeContextHandle,
          rootHandle: runtimeContextHandle,
          mountedCallChaining: new CallChaining(),
          submitCallChaining: new CallChaining(),
          refreshCallChaining: new CallChaining(),
          dataSource: [{}],
          activeRecords: [{}],
          inline: false
        }
      );
      widget.initContext(runtimeContext);
      return widget;
    }
  }

  // 每个 step 的请求数据逻辑
  private async initStepViewData(step: IStepConfig) {
    // 当前步骤的 widget
    const widget = this.stepViewWidget[`${step.model}_${step.viewName}`];
    if (!widget) {
      return;
    }

    if (this.currentActiveKeyIndex > 0) {
      // 根据上一步的数据,构造数据回填
      const lastWidget =
        this.stepViewWidget[
          `${this.stepJsonConfig[this.currentActiveKeyIndex - 1].model}_${
            this.stepJsonConfig[this.currentActiveKeyIndex - 1].viewName
          }`
        ];
      const lastWidgetData = (await lastWidget.getData()) || {};
      const data = (await customQuery(step.model, 'construct', lastWidgetData)) as Record<string, unknown>;
      widget.setData(data);
      // await widget.refreshCallChaining?.syncCall();
    } else {
      if (isEmpty(await widget.getData())) {
        const data = (await customQuery(step.model, 'construct')) as Record<string, unknown>;
        widget.setData(data);
        // await widget.refreshCallChaining?.syncCall();
      }
    }
  }

  // region  初始化上一步下一步视图

  protected async $$beforeCreated() {
    await super.$$beforeCreated();
    let steps = this.stepJsonConfig;
    if (steps && steps.length) {
      await this.onStepChange(StepDirection.NEXT);
    }
    // 浏览器空闲就先把剩下的视图初始化掉
    window.requestIdleCallback(async () => {
      (steps || []).map(async (step) => {
        if (!this.stepViewWidget[`${step.model}_${step.viewName}`]) {
          this.initStepView(step);
        }
      });
    });
  }

  // region 提交数据

  // 保存
  @Widget.Method()
  public async onSave() {
    this.loading = true;
    const step = this.stepJsonConfig[this.currentActiveKeyIndex];
    const widget = this.stepViewWidget[`${step.model}_${step.viewName}`];
    if (!widget) {
      return;
    }

    const validatorRes = await widget.validator?.();
    if (validatorRes) {
      const submitData = (await widget.getData()) as any;
      await customMutation(step.model, 'create', submitData || {});
      this.loading = false;
    }
  }
}

NextStep.vue 多页面步骤条 vue 组件

<template>
  <div class="next-step">
    <a-steps :current="current">
      <a-step v-for="title in titles" :title="title" />
    </a-steps>
    <OioSpin :loading="loading" class="oio-spin">
      <div class="setp__main">
        <template v-for="(step, index) in stepJsonConfig">
          <div class="setp__item" v-show="currentActiveKeyIndex === index">
            <slot :name="`Form_${step.model}_${step.viewName}`" />
          </div>
        </template>
      </div>
      <div class="setp__footer" v-if="stepJsonConfig.length">
        <a-button v-if="current > 0" class="oio-button" type="primary" @click="previous"> 上一步 </a-button>
        <a-button v-if="current < stepJsonConfig.length - 1" class="oio-button" type="primary" @click="next">
          下一步
        </a-button>
        <a-button v-if="current === stepJsonConfig.length - 1" class="oio-button" type="primary" @click="finish">
          完成
        </a-button>
      </div>
    </OioSpin>
  </div>
</template>
<script setup lang="ts">
import { PropType, ref } from 'vue';
import { OioSpin } from '@oinone/kunlun-vue-ui-antd';
import { IStepConfig, StepDirection } from './type';

const props = defineProps({
  loading: {
    type: Boolean,
    default: false
  },
  titles: {
    type: Array as PropType<string[]>,
    default: []
  },
  stepJsonConfig: {
    type: Array as PropType<IStepConfig[]>,
    default: []
  },
  currentActiveKeyIndex: {
    type: Number,
    default: 0
  },
  onStepChange: {
    type: Function
  },
  onSave: {
    type: Function
  }
});

const current = ref<number>(0);

const next = () => {
  current.value++;
  props.onStepChange?.(StepDirection.NEXT);
};
const previous = () => {
  current.value--;
  props.onStepChange?.(StepDirection.PREVIOUS);
};

const finish = async () => {
  await props.onSave?.();
  window.history.back();
};
</script>
<style lang="scss" scoped>
.next-step {
  height: 100%;
  background-color: #fff;
  padding: 16px;

  .setp__main {
    display: flex;
    justify-content: flex-start;

    .setp__item {
      width: 100%;
    }
  }

  .setp__footer {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    gap: 16px;
    margin-top: 18px;
  }
}
</style>

MyMetadataViewWidget 数据隔离组件

import {
  ActiveRecord,
  ActiveRecords,
  CallChaining,
  FormWidget,
  MetadataViewWidget,
  queryDslWidget,
  Widget
} from '@oinone/kunlun-dependencies';

/**
 * 通过视图handle查找搜索组件
 * @param viewHandle
 */
const queryFormWidgetByViewHandle = (viewHandle: string): FormWidget | null => {
  const baseViewWidget = Widget.select(viewHandle);
  const formWidget = queryDslWidget(baseViewWidget?.getChildrenInstance(), FormWidget);
  if (formWidget) {
    return formWidget as unknown as FormWidget;
  }
  return null;
};

export class MyMetadataViewWidget extends MetadataViewWidget {
  @Widget.Provide()
  public mountedCallChaining: CallChaining | undefined;

  @Widget.Provide()
  public submitCallChaining: CallChaining | undefined;

  @Widget.Provide()
  public refreshCallChaining: CallChaining | undefined;

  @Widget.Provide()
  @Widget.Reactive()
  public dataSource: ActiveRecord[] = [];

  @Widget.Method()
  @Widget.Provide()
  public reloadDataSource(records: ActiveRecords | undefined) {
    if (Array.isArray(records)) {
      this.dataSource = records;
    } else {
      this.dataSource = [records || {}];
    }
  }

  @Widget.Provide()
  @Widget.Reactive()
  public activeRecords: ActiveRecord[] = [];

  @Widget.Method()
  @Widget.Provide()
  public reloadActiveRecords(records: ActiveRecords | undefined) {
    if (Array.isArray(records)) {
      this.activeRecords = records;
    } else {
      this.activeRecords = [records || {}];
    }
  }
  public initialize(props): this {
    this.mountedCallChaining = props.mountedCallChaining;
    this.submitCallChaining = props.submitCallChaining;
    this.refreshCallChaining = props.refreshCallChaining;
    this.dataSource = props.dataSource;
    this.activeRecords = props.activeRecords;
    super.initialize(props);
    return this;
  }

  protected mounted() {
    this.mountedCallChaining?.syncCall();
  }

  public async validator() {
    const formWidget = queryFormWidgetByViewHandle(this.currentHandle);
    const res = await formWidget?.validator();
    return res;
  }

  public async getData() {
    const formWidget = queryFormWidgetByViewHandle(this.currentHandle);
    const callResult = await formWidget?.submit();
    return callResult?.records as Record<string, unknown>;
  }

  protected getModelFields() {
    const formWidget = queryFormWidgetByViewHandle(this.currentHandle);
    return formWidget?.rootRuntimeContext.getRequestModelFields();
  }

  public setData(data: Record<string, unknown>) {
    if (data) {
      this.dataSource = [data];
      this.activeRecords = [data];
    }
  }

  public getRuntimeModel() {
    return this.runtimeContext?.model;
  }
}

原理分析

参考 https://doc.oinone.top/frontend/components/21426.html

Oinone社区 作者:银时原创文章,如若转载,请注明出处:https://doc.oinone.top/frontend/components/21432.html

访问Oinone官网:https://www.oinone.top获取数式Oinone低代码应用平台体验

(0)
银时的头像银时数式员工
上一篇 2025年7月21日 pm4:02
下一篇 2025年7月21日 pm8:49

相关推荐

  • 前端页面嵌套

    我们可能会遇到这些需求,如:页面中的一对多字段不是下拉框,而是另一个模型的表单组;页面中的步骤条表单,每一步的表单都需要界面设计器设计,同时这些表单可能属于不同模型。这时候我们就可以采取页面嵌套的方式,在当前页面中,动态创建一个界面设计器设计的子页面。以一对多字段,动态创建表单子页面举例,以下是代码实现和原理分析。 代码实现 AddSubformWidget 动态添加表单 ts 组件 import { ModelFieldType, ViewType, SPI, BaseFieldWidget, Widget, FormO2MFieldWidget, ActiveRecord, CallChaining, createRuntimeContextByView, queryViewDslByModelAndName, uniqueKeyGenerator } from '@oinone/kunlun-dependencies'; import { MyMetadataViewWidget } from './MyMetadataViewWidget'; import { watch } from 'vue'; import AddSubform from './AddSubform.vue'; @SPI.ClassFactory( BaseFieldWidget.Token({ viewType: ViewType.Form, ttype: ModelFieldType.OneToMany, widget: 'AddSubform' }) ) export class AddSubformWidget extends FormO2MFieldWidget { public initialize(props) { super.initialize(props); this.setComponent(AddSubform); return this; } @Widget.Reactive() public myMetadataViewWidget: MyMetadataViewWidget[] = []; @Widget.Reactive() public myMetadataViewWidgetKeys: string[] = []; @Widget.Reactive() public myMetadataViewWidgetLength = 0; // region 子视图配置 public get subviewModel() { return this.getDsl().subviewModel || 'clm.contractcenter.ContractSignatory'; } public get subviewName() { return this.getDsl().subviewName || '签署方_FORM_uiViewa9c114903e104800b15e8f3749656b64'; } // region 添加子视图块 // 按钮添加点击事件 @Widget.Method() public async onAddSubviewBlock() { const resView = await queryViewDslByModelAndName(this.subviewModel, this.subviewName); this.createDynamicSubviewWidget(resView); } // 创建子视图块 public async createDynamicSubviewWidget(view, activeRecord: ActiveRecord = {}) { if (view) { // 根据视图构建上下文 const runtimeContext = createRuntimeContextByView( { type: ViewType.Form, model: view.model, modelName: view.modelDefinition.name, module: view.modelDefinition.module, moduleName: view.modelDefinition.moduleName, name: view.name, dsl: view.template }, true, uniqueKeyGenerator(), this.currentHandle ); // 取得上下文唯一标识 const runtimeContextHandle = runtimeContext.handle; const slotKey = `Form_${uniqueKeyGenerator()}`; // 创建子视图组件 const widget = this.createWidget(new MyMetadataViewWidget(runtimeContextHandle), slotKey, // 插槽名 { metadataHandle: runtimeContextHandle,…

    2025年7月21日
    57500
  • oio-checkbox 对选框

    API 属性 Checkbox 参数 说明 类型 默认值 版本 autofocus 自动获取焦点 boolean false checked(v-model:checked) 指定当前是否选中 boolean false disabled 失效状态 boolean false indeterminate 设置 indeterminate 状态,只负责样式控制 boolean false value 与 CheckboxGroup 组合使用时的值 boolean | string | number – 事件 事件名称 说明 回调参数 版本 change 变化时回调函数 Function(e:Event) –

    2023年12月18日
    71500
  • oio-cascader 级联选择

    级联选择框。 何时使用 需要从一组相关联的数据集合进行选择,例如省市区,公司层级,事物分类等。 从一个较大的数据集合中进行选择时,用多级分类进行分隔,方便选择。 比起 Select 组件,可以在同一个浮层中完成选择,有较好的体验。 API <oio-cascader :options="options" v-model:value="value" /> 参数 说明 类型 默认值 Version allowClear 是否支持清除 boolean true autofocus 自动获取焦点 boolean false changeOnSelect (单选时生效)当此项为 true 时,点选每级菜单选项值都会发生变化,具体见上面的演示 boolean false disabled 禁用 boolean false displayRender 选择后展示的渲染函数,可使用 #displayRender="{labels, selectedOptions}" ({labels, selectedOptions}) => VNode labels => labels.join(' / ') dropdownClassName 自定义浮层类名 string – getTriggerContainer 菜单渲染父节点。默认渲染到 body 上,如果你遇到菜单滚动定位问题,试试修改为滚动的区域,并相对其定位。 Function(triggerNode) () => document.body loadData 用于动态加载选项,无法与 showSearch 一起使用 (selectedOptions) => void – maxTagCount 最多显示多少个 tag,响应式模式会对性能产生损耗 number | responsive – maxTagPlaceholder 隐藏 tag 时显示的内容 v-slot | function(omittedValues) – multiple 支持多选节点 boolean – options 可选项数据源 – placeholder 输入框占位文本 string ‘请选择’ searchValue 设置搜索的值,需要与 showSearch 配合使用 string – showSearch 在选择框中显示搜索框 boolean false tagRender 自定义 tag 内容,多选时生效 slot – value(v-model:value) 指定选中项 string[] | number[] – showSearch showSearch 为对象时,其中的字段: 参数 说明 类型 默认值 filterOption 接收 inputValue path 两个参数,当 path 符合筛选条件时,应返回 true,反之则返回 false。 function(inputValue, path): boolean 事件 事件名称 说明 回调参数 版本 change 选择完成后的回调 (value, selectedOptions) => void – search 监听搜索,返回输入的值 (value) => void – Option interface Option { value: string | number; label?: any; disabled?: boolean; children?: Option[]; // 标记是否为叶子节点,设置了 `loadData` 时有效 // 设为 `false` 时会强制标记为父节点,即使当前节点没有 children,也会显示展开图标 isLeaf?: boolean; }

    2023年12月18日
    73100
  • oio-drawer抽屉

    屏幕边缘滑出的浮层面板。 何时使用 抽屉从父窗体边缘滑入,覆盖住部分父窗体内容。用户在抽屉内操作时不必离开当前任务,操作完成后,可以平滑地回到原任务。 当需要一个附加的面板来控制父窗体内容,这个面板在需要时呼出。比如,控制界面展示样式,往界面中添加内容。 当需要在当前任务流中插入临时任务,创建或预览附加内容。比如展示协议条款,创建子对象。 API 参数 说明 类型 默认值 版本 class 对话框外层容器的类名 string – closable 是否显示左上角的关闭按钮 boolean true closeIcon 自定义关闭图标 VNode | slot destroyOnClose 关闭时销毁 Drawer 里的子元素 boolean false footer 抽屉的页脚 VNode | slot – getTriggerContainer 指定 Drawer 挂载的 HTML 节点 HTMLElement | () => HTMLElement | Selectors ‘body’ height 高度, 在 placement 为 top 或 bottom 时使用 string | number keyboard 是否支持键盘 esc 关闭 boolean true mask 是否展示遮罩 Boolean true maskClosable 点击蒙层是否允许关闭 boolean true placement 抽屉的方向 ‘top’ | ‘right’ | ‘bottom’ | ‘left’ ‘right’ style 可用于设置 Drawer 最外层容器的样式,和 drawerStyle 的区别是作用节点包括 mask CSSProperties – title 标题 string | slot – visible(v-model:visible) Drawer 是否可见 boolean – width 宽度 string | number 378 zIndex 设置 Drawer 的 z-index Number 1000 cancelCallback 点击遮罩层或右上角叉或取消按钮的回调, return true则关闭弹窗 function(e) enterCallback 点击确定回调 function(e)

    2023年12月18日
    74200
  • 前端表格复制

    我们可能会遇到表格复制的需求,也就是表格填写的时候,不是增加一行数据,而是增加一个表格。以下是代码实现和原理分析。 代码实现 在 boot 工程的 main.ts 中加入以下代码 import { registerDesignerFieldWidgetCreator, selectorDesignerFieldWidgetCreator } from '@oinone/kunlun-ui-designer-dependencies'; // 注册无代码组件,将表头分组的无代码组件,注册成字段表格组件 registerDesignerFieldWidgetCreator( { widget: 'DynamicCreateTable' }, selectorDesignerFieldWidgetCreator({ widget: TABLE_WIDGET })! ); DynamicCreateTableWidget 动态添加表格 ts 组件 import { FormO2MTableFieldWidget, Widget, DslDefinition, RuntimeView, SubmitValue, BaseFieldWidget, ModelFieldType, SPI, ViewType, ActiveRecord, uniqueKeyGenerator } from '@oinone/kunlun-dependencies'; import { MyMetadataViewWidget } from './MyMetadataViewWidget'; import DynamicCreateTable from './DynamicCreateTable.vue'; @SPI.ClassFactory( BaseFieldWidget.Token({ viewType: ViewType.Form, ttype: ModelFieldType.OneToMany, widget: 'DynamicCreateTable' }) ) export class DynamicCreateTableWidget extends FormO2MTableFieldWidget { public myMetadataViewWidget: MyMetadataViewWidget[] = []; @Widget.Reactive() public myMetadataViewWidgetLength = 0; @Widget.Reactive() public myMetadataViewWidgetKeys: string[] = []; protected props: Record<string, unknown> = {}; public initialize(props) { super.initialize(props); this.props = props; this.setComponent(DynamicCreateTable); return this; } // region 创建动态表格 @Widget.Method() public async createTableWidget(record: ActiveRecord) { const index = this.myMetadataViewWidget.length; const handle = uniqueKeyGenerator(); const slotKey = `MyMetadataViewWidget_${handle}`; const widget = this.createWidget( new MyMetadataViewWidget(handle), slotKey, // 插槽名称 { subIndex: index, metadataHandle: this.metadataHandle, rootHandle: this.rootHandle, automatic: true, internal: true, inline: true } ); this.initDynamicSubview(this.props, widget); widget.setData(record); this.myMetadataViewWidgetLength++; this.myMetadataViewWidgetKeys.push(slotKey); this.myMetadataViewWidget.push(widget); } protected initDynamicSubview(props: Record<string, unknown>, widget: MyMetadataViewWidget) { const { currentViewDsl } = this; let viewDsl = currentViewDsl; if (!viewDsl) { viewDsl = this.getViewDsl(props)…

    2025年7月21日
    43900

Leave a Reply

登录后才能评论