index.vue 2.45 KB
<!--
 * @Date: 2022-08-30 13:46:51
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-11-24 16:52:05
 * @FilePath: /data-table/src/components/PickerField/index.vue
 * @Description: 单列选择器组件
-->
<template>
  <div v-if="HideShow" class="picker-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' : '']">{{ item.component_props.label }}</span>
      <span v-if="item.component_props.score" style="margin-left: 0.5rem; color: red;">({{ item.component_props.score }} 分)</span>
    </div>
    <van-field :name="item.name" :rules="item.rules" style="padding: 0;">
      <template #input>
        <my-component @active="onActive" />
      </template>
    </van-field>
  </div>
</template>

<script setup>
import { useRoute } from "vue-router";
import MyComponent from './MyComponent.vue';
import Cookies from 'js-cookie';

const $route = useRoute();

const props = defineProps({
  item: Object,
});
const emit = defineEmits(["active"]);

// 注入子组件属性
provide('props', props.item);

// 隐藏显示
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 onActive = (val) => {
  emit("active", val);
  // 适配cookie保存未完成表单
  const currentValue = val.value;
  const existingCookie = Cookies.get($route.query.code);

  if (existingCookie) {
    // 如果Cookie存在,更新它
    let obj = JSON.parse(existingCookie);
    obj[props.item.key] = currentValue; // 替换掉旧值
    Cookies.set($route.query.code, JSON.stringify(obj), { expires: 1 });
  } else {
    // 如果Cookie不存在,新增它
    Cookies.set($route.query.code, JSON.stringify({ [props.item.key]: currentValue }), { expires: 1 });
  }
};


</script>

<style lang="less" scoped>
.picker-field-page {
  margin: 1rem;
  .label {
    // padding: 1rem 1rem 0 0;
    font-size: 0.9rem;
    font-weight: bold;
  }
}

:deep(.van-cell--clickable) {
  border: var(--border-style);
  border-radius: 0.25rem;
  padding: 0.25rem 0.5rem;
  margin-top: 0.5rem;
}
</style>