index.vue 4.88 KB
<!--
 * @Date: 2024-05-27 14:28:57
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-06-04 18:32:57
 * @FilePath: /data-table/src/components/GroupField/index.vue
 * @Description: 组集合输入控件
-->
<template>
  <div v-if="HideShow" class="group-field-page">
    <div class="label">
      <span v-if="item.component_props.disabled_show"><van-icon name="https://cdn.ipadbiz.cn/custom_form/icon/closed-eye1.png" /></span>
      <span v-if="item.component_props.required" style="color: red">*&nbsp;</span>
      <span :class="[ReadonlyShow ? 'readonly-show' : '', InfoPageLabelShow ? 'info-page-label' : '']">{{ item.component_props.label }}</span>
    </div>
    <div v-if="item.component_props.note" class="note" v-html="item.component_props.note" />
    <div>
      <div v-for="(data, idx) in listData" :key="idx" style="border: 1px solid #eee; border-top: 0; margin: 1rem; position: relative;">
        <van-icon @click="onRemove(data, idx)" v-if="listData.length > 1" size="1.5rem" color="#ee0a24" name="clear" style="position: absolute; right: -0.5rem; top: -0.5rem;" />
        <van-cell-group :border="false">
          <component v-for="(item, index) in data" :id="item.key" :key="index"
            :is="item.component" :item="item" @active="onActive" />
        </van-cell-group>
      </div>
      <div style="margin: 1rem;">
        <van-button icon="plus" type="primary" block @click="addData">追加一条记录</van-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { v4 as uuidv4 } from "uuid";
import { styleColor } from "@/constant.js";
import { useRoute } from "vue-router";
import { createComponentType } from "@/hooks/useComponentType";
import _ from 'lodash';

const props = defineProps({
  item: Object,
});

// TAG: 自定义主题颜色
const themeVars = {
  radioColor: styleColor.baseColor,
};
// 隐藏显示
const HideShow = computed(() => {
  return !props.item.component_props.disabled
});
// 只读显示-流程模式
const ReadonlyShow = computed(() => {
  return ($route.query.page_type === 'flow' || $route.query.page_type === 'edit') && !props.item.component_props.readonly;
});

// 详情页标签置灰
const InfoPageLabelShow = computed(() => {
  return $route.query.page_type === 'info';
});

// let obj = {
//   "field_10_group[0]_7653" : "1",
//   "field_10_group[1]_4154" : "11",
//   "field_11_group[0]_3440" : "2",
//   "field_11_group[1]_4282" : "22"
// }

// TODO:模拟集合组数据源
const groupData = _.cloneDeep(props.item.component_props.field_groups);
groupData.forEach(item => {
  let randomNum = Math.floor(1000 + Math.random() * 9000);
  item.field_name = item.field_name + "_group" + '[0]_' + randomNum; // 组合唯一标识
})

const listData = ref([]);
const formData = ref([]);

// 格式化表单数据结构
const formatData = (data) => {
  const arr = [];
  data.forEach((field) => {
    const { interaction_type, data_type, field_id, field_name, ...component_props } = field;
    component_props.is_field_group = true; // 集合标识
    // 生成组件属性
    const temp = {
      key: field_name,
      value: component_props.default ? component_props.default : "",
      component_props,
    };
    arr.push(temp);
  });
  return arr;
};


// const gender_value = ref(props.item.component_props.default);
const $route = useRoute();

onMounted(async () => {
  const form_data = groupData;
  let page_form = [];
  form_data.forEach((element) => {
    page_form.push(element);
  });
  formData.value = formatData(page_form);
  // 生成自定义组件
  createComponentType(formData.value);

  listData.value.push(formData.value);
});

const emit = defineEmits(["active", "remove"]);

const onChange = (item) => {
}

const addData = async () => { // 添加一行数据
  const groupData = _.cloneDeep(props.item.component_props.field_groups);
  groupData.forEach(item => {
    let randomNum = Math.floor(1000 + Math.random() * 9000);
    item.field_name = `${item.field_name}_group[${listData.value.length}]_${randomNum}`; // 组合唯一标识
  })

  const form_data = groupData;
  let page_form = [];
  form_data.forEach((element) => {
    page_form.push(element);
  });

  formData.value = formatData(page_form);

  // 生成自定义组件
  createComponentType(formData.value);

  listData.value.push(formData.value)
}

const onRemove = (data, idx) => { // 移除列表item
  let item = listData.value.splice(idx, 1);
  emit("remove", item);
}

// 操作绑定自定义字段回调
const onActive = (item) => {
  emit("active", item);
};
</script>

<style lang="less" scoped>
.group-field-page {
  .label {
    padding: 1rem 1rem 0 1rem;
    font-size: 0.9rem;
    font-weight: bold;
  }
  .note {
    font-size: 0.9rem;
    margin-left: 1rem;
    color: gray;
    padding-bottom: 0.5rem;
    white-space: pre-wrap;
  }
  .radio-wrapper {
    border: var(--border-style);
    border-radius: 0.25rem;
    padding: 0.25rem 0.5rem;
    margin-bottom: 0.25rem;
  }
}
</style>