SFormLayout2.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. const n = `<template>
  2. <div class="form-layout" ref="layout">
  3. <a-spin :spinning="loading">
  4. <div class="tools" ref="toolsRef" v-if="title || subTitle || $slots.otherBtns">
  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" size="small">
  27. <a-step :title="step" v-for="step in steps" :key="step" :disabled="true" />
  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 class="form-layout-main-content">
  40. <div
  41. class="border-top"
  42. :style="{
  43. width: borderWidth
  44. }"
  45. ></div>
  46. <div class="form-layout-main-scroll" ref="mainScroll">
  47. <div
  48. class="form-layout-main"
  49. ref="formMain"
  50. :class="{ whiteBg: !!$slots.form || !!$slots.collapses }"
  51. :style="{
  52. width: formWidth
  53. }"
  54. id="form-container"
  55. >
  56. <div class="form-container-wrap" :style="formStyle" v-if="$slots.form">
  57. <slot name="form" />
  58. </div>
  59. <div class="collapses-content-wrap" v-if="$slots.collapses">
  60. <div class="collapese-container">
  61. <slot name="collapses" />
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. <div class="collapses-nav" v-if="showAnchor && $slots.collapses" :style="absStyle">
  67. <s-anchor el="form-layout-main-scroll"></s-anchor>
  68. </div>
  69. </div>
  70. <div class="modal">
  71. <slot name="other" />
  72. </div>
  73. <div class="form-fixed-footer">
  74. <a-space :size="spaceSize">
  75. <a-button v-if="goBack" @click="rollback" class="btn">
  76. <close-outlined />{{ $l('取消') }}
  77. </a-button>
  78. <a-button
  79. class="btn"
  80. v-if="steps.length"
  81. :disabled="stepActive === stepDefaultActive"
  82. @click="handPrev"
  83. >
  84. <arrow-left-outlined />{{ $l('上一步') }}
  85. </a-button>
  86. <a-button
  87. class="btn"
  88. type="primary"
  89. v-if="steps.length"
  90. :disabled="stepActive === steps.length - 1"
  91. @click="handNext"
  92. >
  93. <arrow-right-outlined />{{ $l('下一步') }}
  94. </a-button>
  95. <a-button v-if="isShowSave" type="primary" @click="save">
  96. <save-outlined />
  97. {{ $l(saveBtnText) }}
  98. </a-button>
  99. <slot name="footer" />
  100. </a-space>
  101. </div>
  102. <div class="calc-width" ref="caclWidth" :style="{ width: width }"></div>
  103. </a-spin>
  104. </div>
  105. </template>
  106. <script setup>
  107. import { computed, nextTick, onMounted, onUnmounted, ref, useSlots, watch } from 'vue'
  108. defineOptions({ name: 'SFormLayout' })
  109. const props = defineProps({
  110. // 手动指定内容高度,单位 px;0 表示自动撑满
  111. viewHeight: {
  112. type: Number,
  113. default: 0
  114. },
  115. // 是否显示右侧锚点导航
  116. showAnchor: {
  117. type: Boolean,
  118. default: true
  119. },
  120. // 表单 loading 遮罩
  121. loading: {
  122. type: Boolean,
  123. default: false
  124. },
  125. // 页面标题
  126. title: {
  127. type: String,
  128. required: true
  129. },
  130. // 标题右侧副标题
  131. subTitle: {
  132. type: String,
  133. default: ''
  134. },
  135. // 底部按钮是否显示图标
  136. showBtnIcon: {
  137. type: Boolean,
  138. default: true
  139. },
  140. // 步骤条配置,传了才显示步骤区
  141. steps: {
  142. type: Array,
  143. default: () => []
  144. },
  145. // 步骤条默认选中下标
  146. stepDefaultActive: {
  147. type: Number,
  148. default: 0
  149. },
  150. // 切换步骤前的校验函数,返回 false 停在当前步
  151. stepValid: {
  152. type: Function,
  153. default: undefined
  154. },
  155. // 页签配置,传了才显示页签区
  156. tabs: {
  157. type: Array,
  158. default: () => []
  159. },
  160. // 页签字段映射:{ label, value }
  161. tabKey: {
  162. type: Object,
  163. default: () => ({
  164. label: 'label',
  165. value: 'value'
  166. })
  167. },
  168. // 表单区自定义样式
  169. formStyle: {
  170. type: Object,
  171. default: () => ({})
  172. },
  173. // 是否显示返回按钮
  174. goBack: {
  175. type: Boolean,
  176. default: true
  177. },
  178. // 是否显示保存按钮
  179. isShowSave: {
  180. type: Boolean,
  181. default: true
  182. },
  183. // 保存按钮文案
  184. saveBtnText: {
  185. type: String,
  186. default: window.$l('保存')
  187. },
  188. // 表单内容区宽度,默认 1000px
  189. width: {
  190. type: String,
  191. default: '1000px'
  192. }
  193. })
  194. const emit = defineEmits(['footer-callback', 'tabsChange'])
  195. const slots = useSlots()
  196. // ---------- 模板 ref ----------
  197. const layout = ref(null)
  198. const mainScroll = ref(null)
  199. const toolsRef = ref(null)
  200. const stepRef = ref(null)
  201. const tabsRef = ref(null)
  202. const formMain = ref(null)
  203. const caclWidth = ref(null)
  204. const spaceSize = ref(8)
  205. // ---------- 状态 ----------
  206. const absStyle = ref({})
  207. const formWidth = ref('100%')
  208. const borderWidth = ref(0)
  209. // 高度类状态已删除:布局高度全部交给 CSS flex,不再做 JS 测量
  210. const stepActive = ref(0)
  211. // 页签取值字段:tabKey.value 未传时回退到 label,避免所有 pane 的 key 都是 undefined
  212. const tabKeyValue = computed(() => (props.tabKey && props.tabKey.value) || 'value')
  213. // 当前页签受控维护,保证首屏高亮项与 pane 的 key 是同一个值(原来取 label 而 key 取 value,两者不同则首屏不高亮)
  214. const activeTabKey = ref('')
  215. const syncActiveTab = () => {
  216. const list = props.tabs || []
  217. if (!list.length) {
  218. activeTabKey.value = ''
  219. return
  220. }
  221. const field = tabKeyValue.value
  222. if (!list.some((tab) => tab && tab[field] === activeTabKey.value)) {
  223. activeTabKey.value = list[0][field]
  224. }
  225. }
  226. watch(() => props.tabs, syncActiveTab, { immediate: true, deep: true })
  227. const initForm = computed(() => props.showAnchor && props.width)
  228. watch(initForm, () => handClacStyle(), { immediate: true, deep: true })
  229. // 高度全部由 CSS flex 决定(根 100% → 标题条/页签定高 → 表单区吃剩余 → 底栏定高),
  230. // 这里不需要再测量任何高度;宽度相关计算在 handClacStyle 里。
  231. function initStyle() {
  232. handClacStyle()
  233. }
  234. function rollback() {
  235. emit('footer-callback', 'cancel')
  236. }
  237. function handPrev() {
  238. stepActive.value--
  239. emit('footer-callback', 'prev', stepActive.value)
  240. }
  241. function handNext() {
  242. if (!window.$utils.isFunction(props.stepValid)) {
  243. stepActive.value++
  244. emit('footer-callback', 'next', stepActive.value)
  245. return
  246. }
  247. props.stepValid(stepActive.value).then((res) => {
  248. if (res) {
  249. stepActive.value++
  250. emit('footer-callback', 'next', stepActive.value)
  251. }
  252. })
  253. }
  254. function save() {
  255. emit('footer-callback', 'save')
  256. }
  257. function handTabChange(v) {
  258. // 同步受控页签,再按 key 找出整条配置回传(回传整条 item 是既有约定,不改)
  259. activeTabKey.value = v
  260. const item = props.tabs.find((i) => i[tabKeyValue.value] === v)
  261. emit('tabsChange', item)
  262. }
  263. // 锚点定位与表单区宽度自适应(宽度计算,与高度无关)
  264. function handClacStyle() {
  265. nextTick(() => {
  266. const style = {}
  267. const sEl = mainScroll.value?.offsetWidth || 0
  268. const mEl = caclWidth.value?.offsetWidth || 0
  269. if (mEl + 200 > sEl) {
  270. style.right = '0px'
  271. } else {
  272. style.left = props.width
  273. }
  274. absStyle.value = style
  275. if (props.showAnchor && slots.collapses) {
  276. formWidth.value = mEl + 200 > sEl ? \`\${sEl - 200}px\` : props.width
  277. } else {
  278. formWidth.value = mEl > sEl ? \`\${sEl}px\` : props.width
  279. }
  280. borderWidth.value = formMain.value.offsetWidth - 10 + 'px'
  281. })
  282. }
  283. // ---------- 生命周期 ----------
  284. let resizeObserver = null
  285. onMounted(() => {
  286. initStyle()
  287. stepActive.value = props.stepDefaultActive
  288. if (window.ResizeObserver && layout.value) {
  289. resizeObserver = new ResizeObserver(() => {
  290. initStyle()
  291. handClacStyle()
  292. })
  293. resizeObserver.observe(layout.value)
  294. }
  295. })
  296. onUnmounted(() => {
  297. resizeObserver?.disconnect()
  298. resizeObserver = null
  299. })
  300. defineExpose({
  301. save,
  302. rollback,
  303. handNext,
  304. handPrev,
  305. stepActive,
  306. formWidth
  307. })
  308. <\/script>
  309. <style lang="less" scoped>
  310. .form-layout {
  311. height: 100%;
  312. min-height: 0;
  313. overflow: hidden;
  314. display: flex;
  315. flex-direction: column;
  316. box-sizing: border-box;
  317. :deep(.ant-spin-nested-loading),
  318. :deep(.ant-spin-container) {
  319. flex: 1 1 auto;
  320. min-height: 0;
  321. display: flex;
  322. flex-direction: column;
  323. width: 100%;
  324. }
  325. }
  326. .tools {
  327. flex: 0 0 60px;
  328. height: 60px;
  329. display: flex;
  330. justify-content: space-between;
  331. align-items: center;
  332. .sub-title-wrap {
  333. margin: 10px 0 0;
  334. font-size: 16px;
  335. // text-indent: 32px;
  336. }
  337. .rollback {
  338. margin-right: 10px;
  339. color: var(--s-color);
  340. cursor: pointer;
  341. }
  342. .title {
  343. color: var(--s-text-color);
  344. font-size: 18px;
  345. font-weight: 500;
  346. }
  347. }
  348. .form-layout-main-content {
  349. position: relative;
  350. flex: 1 1 auto;
  351. min-height: 0;
  352. display: flex;
  353. flex-direction: column;
  354. .collapses-nav {
  355. width: 200px;
  356. position: absolute;
  357. top: 0;
  358. right: 0;
  359. z-index: 9;
  360. .anchor-link-item {
  361. width: 100%;
  362. height: 32px;
  363. }
  364. }
  365. }
  366. .form-layout-main-scroll {
  367. flex: 1 1 auto;
  368. min-height: 0;
  369. overflow: hidden auto;
  370. // 必须是 flex 容器,否则里面的 .form-layout-main 拿不到 flex:1,表单区退回内容高度
  371. display: flex;
  372. flex-direction: column;
  373. }
  374. .border-top {
  375. position: absolute;
  376. top: 0;
  377. left: 10px;
  378. z-index: 2;
  379. border-top: 10px solid var(--s-bg-color-container);
  380. border-radius: 8px 8px 0 0;
  381. }
  382. .form-layout-main {
  383. flex: 1 1 auto;
  384. min-height: 0;
  385. padding: 10px;
  386. box-sizing: border-box;
  387. position: relative;
  388. // 表单插槽内容要能吃满这个区域(业务在里面放 s-filter-table-toolbar 之类)
  389. display: flex;
  390. flex-direction: column;
  391. }
  392. .form-layout-main.whiteBg {
  393. background-color: var(--s-bg-color-container);
  394. border-radius: 6px 6px 0 0;
  395. }
  396. .form-container-wrap {
  397. width: 100%;
  398. flex: 1 1 auto;
  399. min-height: 0;
  400. }
  401. .collapses-content-wrap {
  402. width: 100%;
  403. display: flex;
  404. justify-content: flex-start;
  405. align-content: flex-start;
  406. .collapese-container {
  407. flex: 1;
  408. }
  409. }
  410. .step-wrap {
  411. width: 100%;
  412. flex: 0 0 auto;
  413. background-color: var(--s-bg-color-container);
  414. border-bottom: 1px solid var(--s-border-color-split);
  415. margin-bottom: 16px;
  416. }
  417. .tabs-wrap {
  418. flex: 0 0 auto;
  419. }
  420. .modal {
  421. flex: 0 0 auto;
  422. }
  423. .form-fixed-footer {
  424. width: 100%;
  425. height: 60px;
  426. // 由 position:fixed 改为 flex 定高贴底:不脱离文档流,表单区自动让位、不被遮挡
  427. flex: 0 0 60px;
  428. display: flex;
  429. justify-content: flex-start;
  430. align-items: center;
  431. // padding: 0 24px;
  432. border-top: 1px solid var(--s-border-color-split);
  433. box-sizing: border-box;
  434. z-index: 9;
  435. background-color: var(--s-bg-color-container);
  436. overflow: hidden;
  437. }
  438. </style>
  439. <style lang="less" scoped>
  440. :deep(.tabs-wrap) {
  441. overflow: hidden;
  442. }
  443. :deep(.tabs-wrap > .ant-tabs-card > .ant-tabs-content) {
  444. margin-top: -16px;
  445. margin-bottom: 8px;
  446. }
  447. :deep(.tabs-wrap > .ant-tabs-card > .ant-tabs-bar .ant-tabs-tab) {
  448. border-color: transparent;
  449. background: transparent;
  450. }
  451. </style>
  452. `;
  453. export {
  454. n as default
  455. };