SFormBasicLayout2.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. const n = `<template>
  2. <div class="s-form-basic-layout" ref="layoutRef">
  3. <a-spin :spinning="loading">
  4. <div class="tools" ref="toolsRef">
  5. <div class="title">
  6. <span style="margin-right: 10px">{{ title }}</span>
  7. <a-tooltip
  8. placement="topLeft"
  9. v-if="subTitle"
  10. :mouseEnterDelay="0.5"
  11. :mouseLeaveDelay="0.5"
  12. >
  13. <template #title>
  14. {{ subTitle }}
  15. </template>
  16. <InfoCircleOutlined />
  17. </a-tooltip>
  18. </div>
  19. <div class="btns">
  20. <a-space :size="spaceSize">
  21. <slot name="otherBtns" />
  22. </a-space>
  23. </div>
  24. </div>
  25. <div class="step-wrap" ref="stepRef" v-if="steps && steps.length">
  26. <a-steps :current="stepActive" type="navigation">
  27. <a-step :title="step" v-for="step in steps" :key="step" disabled />
  28. </a-steps>
  29. </div>
  30. <div class="tabs-wrap" ref="tabsRef" v-if="tabs && tabs.length">
  31. <a-tabs :active-key="activeTabKey" @change="handTabChange">
  32. <a-tab-pane
  33. :tab="tab[tabKey.label]"
  34. v-for="tab in tabs"
  35. :key="tab[tabKeyValue]"
  36. ></a-tab-pane>
  37. </a-tabs>
  38. </div>
  39. <div
  40. class="form-layout-main"
  41. :class="{ whiteBg: !!$slots.form }"
  42. :id="commonId"
  43. ref="formMain"
  44. >
  45. <div class="form-container-wrap" :style="formStyle" v-if="$slots.form">
  46. <slot name="form" />
  47. </div>
  48. <div class="collapses-content-wrap" v-if="$slots.collapses">
  49. <slot name="collapses" />
  50. </div>
  51. </div>
  52. <div class="modal">
  53. <slot name="other" />
  54. </div>
  55. <div class="s-form-basic-fixed-footer" ref="footerRef">
  56. <a-space :size="spaceSize">
  57. <a-button v-if="goBack" class="btn" @click="rollback">
  58. <close-outlined />{{ $l('取消') }}
  59. </a-button>
  60. <a-button
  61. class="btn"
  62. v-if="steps.length"
  63. :disabled="stepActive === stepDefaultActive"
  64. @click="handPrev"
  65. >
  66. <arrow-left-outlined />{{ $l('上一步') }}
  67. </a-button>
  68. <a-button
  69. class="btn"
  70. type="primary"
  71. v-if="steps.length"
  72. :disabled="stepActive === steps.length - 1"
  73. @click="handNext"
  74. >
  75. <arrow-right-outlined />{{ $l('下一步') }}
  76. </a-button>
  77. <a-button v-if="isShowSave" type="primary" @click="save">
  78. <save-outlined />
  79. {{ $l(saveBtnText) }}
  80. </a-button>
  81. <slot name="footer" />
  82. </a-space>
  83. </div>
  84. </a-spin>
  85. </div>
  86. </template>
  87. <script setup>
  88. import { computed, onMounted, ref, watch } from 'vue'
  89. defineOptions({ name: 'SFormBasicLayout' })
  90. const props = defineProps({
  91. // 表单 loading 遮罩
  92. loading: {
  93. type: Boolean,
  94. default: false
  95. },
  96. // 表单主体容器的 id(锚点/表单定位用)
  97. commonId: {
  98. type: String,
  99. default: 'form-container'
  100. },
  101. // 页面标题
  102. title: {
  103. type: String,
  104. required: true
  105. },
  106. // 标题右侧副标题
  107. subTitle: {
  108. type: String,
  109. default: ''
  110. },
  111. // 底部按钮是否显示图标
  112. showBtnIcon: {
  113. type: Boolean,
  114. default: true
  115. },
  116. // 步骤条配置,传了才显示步骤区
  117. steps: {
  118. type: Array,
  119. default: () => []
  120. },
  121. // 步骤条默认选中下标
  122. stepDefaultActive: {
  123. type: Number,
  124. default: 0
  125. },
  126. // 上一步校验:返回 Promise<boolean>,resolve(false) 时停在当前步
  127. stepValid: {
  128. type: Function,
  129. default: undefined
  130. },
  131. // 页签配置,传了才显示页签区
  132. tabs: {
  133. type: Array,
  134. default: () => []
  135. },
  136. // 页签字段映射:{ label, value }
  137. tabKey: {
  138. type: Object,
  139. default: () => ({
  140. label: 'label',
  141. value: 'value'
  142. })
  143. },
  144. // 表单区自定义样式
  145. formStyle: {
  146. type: Object,
  147. default: () => ({})
  148. },
  149. // 是否显示返回按钮
  150. goBack: {
  151. type: Boolean,
  152. default: true
  153. },
  154. // 是否显示保存按钮
  155. isShowSave: {
  156. type: Boolean,
  157. default: true
  158. },
  159. // 保存按钮文案
  160. saveBtnText: {
  161. type: String,
  162. default: '保存'
  163. }
  164. })
  165. const emit = defineEmits(['footer-callback', 'tabsChange'])
  166. // ---------- 模板 ref ----------
  167. const layoutRef = ref(null)
  168. const toolsRef = ref(null)
  169. const stepRef = ref(null)
  170. const tabsRef = ref(null)
  171. const formMain = ref(null)
  172. const footerRef = ref(null)
  173. const spaceSize = ref(8)
  174. // ---------- 状态 ----------
  175. // 高度类状态已删除:布局高度全部交给 CSS flex,不再做 JS 测量
  176. const stepActive = ref(0)
  177. // 页签取值字段:tabKey.value 未传时回退到 label,避免 pane 的 key 全是 undefined
  178. const tabKeyValue = computed(() => (props.tabKey && props.tabKey.value) || 'value')
  179. // 受控页签,保证首屏高亮项与 pane 的 key 同源
  180. const activeTabKey = ref('')
  181. const syncActiveTab = () => {
  182. const list = props.tabs || []
  183. if (!list.length) {
  184. activeTabKey.value = ''
  185. return
  186. }
  187. const field = tabKeyValue.value
  188. if (!list.some((tab) => tab && tab[field] === activeTabKey.value)) {
  189. activeTabKey.value = list[0][field]
  190. }
  191. }
  192. watch(() => props.tabs, syncActiveTab, { immediate: true, deep: true })
  193. // ---------- 方法 ----------
  194. // 高度全部由 CSS flex 决定(根 100% → 标题条/步骤条/页签定高 → 表单区吃剩余 → 底栏定高贴底),
  195. // 不再测量任何高度;保留空实现兼容既有调用点。
  196. function initStyle() {}
  197. function rollback() {
  198. emit('footer-callback', 'cancel')
  199. }
  200. function handPrev() {
  201. stepActive.value--
  202. emit('footer-callback', 'prev', stepActive.value)
  203. }
  204. function handNext() {
  205. if (typeof props.stepValid !== 'function') {
  206. stepActive.value++
  207. emit('footer-callback', 'next', stepActive.value)
  208. return
  209. }
  210. props.stepValid(stepActive.value).then((res) => {
  211. if (res) {
  212. stepActive.value++
  213. emit('footer-callback', 'next', stepActive.value)
  214. }
  215. })
  216. }
  217. function save() {
  218. emit('footer-callback', 'save')
  219. }
  220. function handTabChange(v) {
  221. activeTabKey.value = v
  222. const item = props.tabs.find((i) => i[tabKeyValue.value] === v)
  223. emit('tabsChange', item)
  224. }
  225. // ---------- 生命周期 ----------
  226. onMounted(() => {
  227. initStyle()
  228. stepActive.value = props.stepDefaultActive
  229. })
  230. defineExpose({
  231. save,
  232. rollback,
  233. handNext,
  234. handPrev,
  235. stepActive
  236. })
  237. <\/script>
  238. <style lang="less" scoped>
  239. // 高度实现:纯 CSS flex,零 JS 测量。
  240. // 前提:父容器有确定高度(基座 BasicLayout 的 router-view 已带 height)。
  241. .s-form-basic-layout {
  242. height: 100%;
  243. min-height: 0;
  244. display: flex;
  245. flex-direction: column;
  246. overflow: hidden;
  247. box-sizing: border-box;
  248. }
  249. // a-spin 包在中间,把剩余高度透传下去
  250. .s-form-basic-layout > :deep(.ant-spin-nested-loading),
  251. .s-form-basic-layout > :deep(.ant-spin-container) {
  252. flex: 1 1 auto;
  253. min-height: 0;
  254. display: flex;
  255. flex-direction: column;
  256. }
  257. .tools {
  258. flex: 0 0 60px;
  259. height: 60px;
  260. display: flex;
  261. justify-content: space-between;
  262. align-items: center;
  263. // border-bottom: 1px solid var(--s-bg-color);
  264. // padding: 0 16px;
  265. box-sizing: border-box;
  266. .title {
  267. font-size: 18px;
  268. font-weight: 500;
  269. }
  270. }
  271. .form-layout-main {
  272. flex: 1 1 auto;
  273. min-height: 0;
  274. display: flex;
  275. flex-direction: column;
  276. align-items: flex-start;
  277. overflow-x: hidden;
  278. overflow-y: auto;
  279. &::-webkit-scrollbar {
  280. opacity: 0 !important;
  281. position: relative;
  282. z-index: -1;
  283. display: none;
  284. }
  285. }
  286. .form-layout-main.whiteBg {
  287. background-color: var(--s-bg-color-container);
  288. border-radius: 6px 6px 0 0;
  289. overflow: hidden;
  290. }
  291. .form-container-wrap {
  292. width: 100%;
  293. flex: 1 1 auto;
  294. min-height: 0;
  295. padding: 32px;
  296. box-sizing: border-box;
  297. overflow: auto;
  298. }
  299. .collapses-content-wrap {
  300. width: 100%;
  301. }
  302. .step-wrap {
  303. width: 100%;
  304. }
  305. .s-form-basic-fixed-footer {
  306. width: 100%;
  307. height: 60px;
  308. // 由 position:fixed 改为 flex 定高贴底:不脱离文档流,表单区自动让位
  309. flex: 0 0 60px;
  310. display: flex;
  311. justify-content: flex-start;
  312. align-items: center;
  313. border-top: 1px solid var(--s-border-color-split);
  314. box-sizing: border-box;
  315. z-index: 9;
  316. background-color: var(--s-bg-color-container);
  317. overflow: hidden;
  318. }
  319. :deep(.tabs-wrap) {
  320. background: var(--s-bg-color);
  321. overflow: hidden;
  322. }
  323. :deep(.tabs-wrap > .ant-tabs-card > .ant-tabs-content) {
  324. margin-top: -16px;
  325. margin-bottom: 8px;
  326. }
  327. :deep(.tabs-wrap > .ant-tabs-card > .ant-tabs-bar) {
  328. border-color: var(--s-bg-color);
  329. }
  330. :deep(.tabs-wrap > .ant-tabs-card > .ant-tabs-bar .ant-tabs-tab) {
  331. border-color: transparent;
  332. background: transparent;
  333. }
  334. </style>
  335. `;
  336. export {
  337. n as default
  338. };