Files
zkxw/mobile-oa-h5/src/market/components/MarketFormSection.vue
T
zlwuandCursor fefef41524 fix(mobile): 协议单表单将项目归属移入协议信息分区
按专员/经理协议单诉求,把项目归属嵌入协议信息卡片并置于协议状态上方,避免独立成卡打断填写顺序。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 17:57:57 +08:00

119 lines
4.9 KiB
Vue

<template>
<section class="market-form-section">
<h2>{{ section.title }}</h2>
<template v-if="section.repeat">
<article v-for="(row, index) in rows" :key="index" class="market-form-repeat">
<div class="market-form-repeat__head">
<strong>{{ section.title }} {{ index + 1 }}</strong>
<button type="button" :disabled="disabled" @click="removeRow(index)">移除</button>
</div>
<MarketFormField
v-for="field in visibleFields"
:key="field.key"
:field="field"
:model-value="row[field.key]"
:options="fieldOptions(field, index)"
:uploading="uploading"
:disabled="disabled"
:search-customers="searchCustomers"
@update:model-value="setRepeatValue(index, field, $event)"
@upload="$emit('upload', { section, field, index, file: $event })"
@customer-select="$emit('customer-select', { section, ...$event, index })"
/>
</article>
<van-button plain type="primary" size="small" :disabled="disabled" @click="addRow">添加{{ section.title }}</van-button>
</template>
<div
v-else
class="market-form-fields"
:class="{ 'market-form-fields--grid-2': section.fieldsLayout === 'grid-2' }"
>
<template v-for="item in fieldsWithInsert" :key="item.field.key">
<slot v-if="item.insertBefore" name="insert" />
<MarketFormField
:field="item.field"
:model-value="value?.[item.field.key]"
:options="fieldOptions(item.field)"
:uploading="uploading"
:disabled="fieldDisabled(item.field)"
:search-customers="searchCustomers"
@update:model-value="setValue(item.field, $event)"
@upload="$emit('upload', { section, field: item.field, file: $event })"
@customer-select="$emit('customer-select', { section, ...$event })"
/>
</template>
<slot v-if="appendInsert" name="insert" />
</div>
</section>
</template>
<script setup>
import { computed, useSlots } from 'vue'
import MarketFormField from './MarketFormField.vue'
import { sampleCodeOptionsForRow } from '../reference-options.js'
const props = defineProps({
section: { type: Object, required: true },
modelValue: { type: [Object, Array], default: () => ({}) },
options: { type: Object, default: () => ({}) },
source: { type: String, default: '' },
mode: { type: String, default: 'create' },
uploading: { type: Boolean, default: false },
disabled: { type: Boolean, default: false },
searchCustomers: { type: Function, default: null },
/** 在首个匹配可见字段前插入 #insert 插槽(如协议状态下的项目归属) */
insertBeforeKeys: { type: Array, default: () => [] }
})
const emit = defineEmits(['update:modelValue', 'upload', 'field-change', 'customer-select'])
const slots = useSlots()
const value = computed(() => props.modelValue || {})
const rows = computed(() => Array.isArray(props.modelValue) ? props.modelValue : [])
const visibleFields = computed(() => (props.section.fields || []).filter(
(field) =>
!field.hidden &&
(!field.source || field.source === props.source) &&
!(Array.isArray(field.hiddenSources) && field.hiddenSources.includes(props.source)) &&
!(typeof field.showWhen === 'function' && field.showWhen(value.value) === false) &&
!(field.createOnly && props.mode !== 'create')
))
const fieldsWithInsert = computed(() => {
const keys = props.insertBeforeKeys || []
let inserted = false
return visibleFields.value.map((field) => {
const insertBefore = Boolean(slots.insert) && !inserted && keys.includes(field.key)
if (insertBefore) inserted = true
return { field, insertBefore }
})
})
const appendInsert = computed(() => {
if (!slots.insert || !(props.insertBeforeKeys || []).length) return false
return !fieldsWithInsert.value.some((item) => item.insertBefore)
})
function fieldOptions(field, rowIndex = -1) {
const base = field.options || props.options[field.optionKey] || (field.optionKey === 'users' ? props.options.staff : []) || []
if (field.key === 'sampleCodeId' && props.section.repeat && rowIndex >= 0) {
return sampleCodeOptionsForRow(base, rows.value, rowIndex)
}
return base
}
function fieldDisabled(field) {
return props.disabled || (field.key === 'hospitalId' && !value.value?.regionId)
}
function setValue(field, next) {
emit('update:modelValue', { ...value.value, [field.key]: next })
emit('field-change', { section: props.section, field, value: next })
}
function setRepeatValue(index, field, next) {
const result = rows.value.map((row, rowIndex) => rowIndex === index ? { ...row, [field.key]: next } : row)
emit('update:modelValue', result)
emit('field-change', { section: props.section, field, value: next, index })
}
function addRow() {
emit('update:modelValue', [...rows.value, {}])
}
function removeRow(index) {
emit('update:modelValue', rows.value.filter((_, rowIndex) => rowIndex !== index))
}
</script>