SFormItem2.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. const n = `<template>
  2. <div class="s-form-item" :style="rootStyle">
  3. <a-form-item
  4. :label="label"
  5. :name="name"
  6. v-bind="{ ...colSpan, ...safeAttrs }"
  7. class="s-form-item"
  8. >
  9. <div class="label" v-if="$slots.label">
  10. <slot name="label" />
  11. </div>
  12. <div class="s-form-item-content">
  13. <slot />
  14. </div>
  15. </a-form-item>
  16. </div>
  17. </template>
  18. <script setup>
  19. import { useAttrs, computed } from 'vue'
  20. const attrs = useAttrs()
  21. const props = defineProps({
  22. // 标签文案
  23. label: {
  24. type: String,
  25. default: ''
  26. },
  27. // 标签宽度(不传沿用 SForm 的设置)
  28. labelWidth: {
  29. type: [Number, String],
  30. default: '100%'
  31. },
  32. // 支持嵌套字段:name 传字符串,或传数组路径(如 ['dsConfigRdb', 'dbCode']),与 a-form-item 一致
  33. name: {
  34. type: [String, Array],
  35. default: ''
  36. },
  37. // 多列布局(SForm column>1 或 SCollapsesItem column>1)时,本项独占一整行
  38. full: {
  39. type: Boolean,
  40. default: false
  41. }
  42. })
  43. // 父容器是 grid(SForm 的 a-form / SCollapsesItem 的内容区),跨满所有列即独占一行。
  44. // 单列时 1 / -1 等价于一整行,无副作用。
  45. const rootStyle = computed(() => (props.full ? { gridColumn: '1 / -1' } : {}))
  46. /**
  47. * attrs 归一化(防御层):与 SForm.vue 中的同名逻辑一致。
  48. * SFormItem 用 \`v-bind="safeAttrs"\` 把业务侧写在 \`<s-form-item>\` 上的属性透传给 a-form-item,
  49. * 而 a-form-item 会继续下沉到 slot 里的真实控件(a-input / a-select …),
  50. * 所以业务侧一个 \`:disabled="cond && obj.someId"\` 就能一路打到 antd 的 \`disabled: Boolean\` 上。
  51. * 详见 SForm.vue 中同名常量处的完整注释。
  52. */
  53. const BOOL_ATTRS = new Set([
  54. 'disabled',
  55. 'allowClear',
  56. 'clearable',
  57. 'showSearch',
  58. 'multiple',
  59. 'readonly',
  60. 'autofocus',
  61. 'bordered',
  62. 'checked',
  63. 'open',
  64. 'autoClearSearchValue',
  65. 'noDrag',
  66. 'isManual',
  67. 'expandAll',
  68. 'recursionChildren',
  69. 'treeCheckable',
  70. 'showOnlyOrganization',
  71. 'isDataScopeFilter'
  72. ])
  73. const toBool = (val) => {
  74. if (typeof val === 'boolean') return val
  75. if (val === '' || val === null || val === undefined) return val === '' ? true : val
  76. if (typeof val === 'string') return !(val === 'false' || val === '0')
  77. return Boolean(val)
  78. }
  79. const safeAttrs = computed(() => {
  80. const out = {}
  81. Object.keys(attrs || {}).forEach((key) => {
  82. const val = attrs[key]
  83. out[key] = BOOL_ATTRS.has(key) ? toBool(val) : val
  84. })
  85. return out
  86. })
  87. const colSpan = computed(() => {
  88. const width = props.labelWidth + ''
  89. return {
  90. labelCol: {
  91. style: {
  92. width: width.includes('px') ? width : width + 'px'
  93. }
  94. }
  95. }
  96. })
  97. <\/script>
  98. <style scope lang="less">
  99. .s-form-item {
  100. margin-bottom: 0;
  101. // SForm 的 a-form 是 CSS Grid,列宽写的是 1fr(= minmax(auto,1fr)),
  102. // 子项默认 min-width:auto 会被超长内容(如超长 URL 的下拉选中值)撑到 max-content,
  103. // 结果是整列宽于父容器、输入框溢出被弹窗裁掉。min-width:0 让轨道可以收缩到 1fr。
  104. min-width: 0;
  105. .label {
  106. padding-bottom: 8px;
  107. }
  108. .s-form-item-content {
  109. min-width: 0;
  110. .ant-input,
  111. .ant-select,
  112. .ant-picker,
  113. .ant-input-number {
  114. width: 100%;
  115. }
  116. }
  117. .ant-form-item {
  118. min-width: 0;
  119. }
  120. .ant-form-item-control {
  121. min-width: 0;
  122. }
  123. }
  124. </style>
  125. `;
  126. export {
  127. n as default
  128. };