| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- const n = `<template>
- <div class="s-form-item" :style="rootStyle">
- <a-form-item
- :label="label"
- :name="name"
- v-bind="{ ...colSpan, ...safeAttrs }"
- class="s-form-item"
- >
- <div class="label" v-if="$slots.label">
- <slot name="label" />
- </div>
- <div class="s-form-item-content">
- <slot />
- </div>
- </a-form-item>
- </div>
- </template>
- <script setup>
- import { useAttrs, computed } from 'vue'
- const attrs = useAttrs()
- const props = defineProps({
- // 标签文案
- label: {
- type: String,
- default: ''
- },
- // 标签宽度(不传沿用 SForm 的设置)
- labelWidth: {
- type: [Number, String],
- default: '100%'
- },
- // 支持嵌套字段:name 传字符串,或传数组路径(如 ['dsConfigRdb', 'dbCode']),与 a-form-item 一致
- name: {
- type: [String, Array],
- default: ''
- },
- // 多列布局(SForm column>1 或 SCollapsesItem column>1)时,本项独占一整行
- full: {
- type: Boolean,
- default: false
- }
- })
- // 父容器是 grid(SForm 的 a-form / SCollapsesItem 的内容区),跨满所有列即独占一行。
- // 单列时 1 / -1 等价于一整行,无副作用。
- const rootStyle = computed(() => (props.full ? { gridColumn: '1 / -1' } : {}))
- /**
- * attrs 归一化(防御层):与 SForm.vue 中的同名逻辑一致。
- * SFormItem 用 \`v-bind="safeAttrs"\` 把业务侧写在 \`<s-form-item>\` 上的属性透传给 a-form-item,
- * 而 a-form-item 会继续下沉到 slot 里的真实控件(a-input / a-select …),
- * 所以业务侧一个 \`:disabled="cond && obj.someId"\` 就能一路打到 antd 的 \`disabled: Boolean\` 上。
- * 详见 SForm.vue 中同名常量处的完整注释。
- */
- const BOOL_ATTRS = new Set([
- 'disabled',
- 'allowClear',
- 'clearable',
- 'showSearch',
- 'multiple',
- 'readonly',
- 'autofocus',
- 'bordered',
- 'checked',
- 'open',
- 'autoClearSearchValue',
- 'noDrag',
- 'isManual',
- 'expandAll',
- 'recursionChildren',
- 'treeCheckable',
- 'showOnlyOrganization',
- 'isDataScopeFilter'
- ])
- const toBool = (val) => {
- if (typeof val === 'boolean') return val
- if (val === '' || val === null || val === undefined) return val === '' ? true : val
- if (typeof val === 'string') return !(val === 'false' || val === '0')
- return Boolean(val)
- }
- const safeAttrs = computed(() => {
- const out = {}
- Object.keys(attrs || {}).forEach((key) => {
- const val = attrs[key]
- out[key] = BOOL_ATTRS.has(key) ? toBool(val) : val
- })
- return out
- })
- const colSpan = computed(() => {
- const width = props.labelWidth + ''
- return {
- labelCol: {
- style: {
- width: width.includes('px') ? width : width + 'px'
- }
- }
- }
- })
- <\/script>
- <style scope lang="less">
- .s-form-item {
- margin-bottom: 0;
- // SForm 的 a-form 是 CSS Grid,列宽写的是 1fr(= minmax(auto,1fr)),
- // 子项默认 min-width:auto 会被超长内容(如超长 URL 的下拉选中值)撑到 max-content,
- // 结果是整列宽于父容器、输入框溢出被弹窗裁掉。min-width:0 让轨道可以收缩到 1fr。
- min-width: 0;
- .label {
- padding-bottom: 8px;
- }
- .s-form-item-content {
- min-width: 0;
- .ant-input,
- .ant-select,
- .ant-picker,
- .ant-input-number {
- width: 100%;
- }
- }
- .ant-form-item {
- min-width: 0;
- }
- .ant-form-item-control {
- min-width: 0;
- }
- }
- </style>
- `;
- export {
- n as default
- };
|