wd-step.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <view
  3. v-if="currentStatus"
  4. :class="`wd-step ${customClass} ${currentStatus ? 'is-' + currentStatus : ''} ${canAlignCenter ? 'is-center' : ''} ${
  5. vertical ? 'is-vertical' : ''
  6. }`"
  7. :style="rootStyle"
  8. >
  9. <view :class="`wd-step__header ${dot ? 'is-dot' : ''}`">
  10. <view :class="`wd-step__icon ${dot ? 'is-dot' : !!icon || $slots.icon ? 'is-icon' : 'is-text'}`">
  11. <view v-if="dot" class="wd-step__dot"></view>
  12. <slot v-else-if="$slots.icon" name="icon" />
  13. <wd-icon v-else-if="icon" custom-class="wd-step__icon-inner" :name="icon" />
  14. <view v-else class="wd-step__icon-outer">
  15. <wd-icon v-if="currentStatus === 'finished'" name="check-bold" />
  16. <wd-icon v-else-if="currentStatus === 'error'" name="close-bold" />
  17. <text v-else>{{ index + 1 }}</text>
  18. </view>
  19. </view>
  20. <view v-if="index < childrenLength - 1" class="wd-step__line"></view>
  21. </view>
  22. <view class="wd-step__content">
  23. <view :class="`wd-step__title ${$slots.description || description ? 'is-description' : ''}`">
  24. <slot v-if="$slots.title" name="title" />
  25. <text v-else>{{ currentTitle }}</text>
  26. </view>
  27. <view v-if="$slots.description || description" class="wd-step__description">
  28. <slot v-if="$slots.description" name="description" />
  29. <text v-else>{{ description }}</text>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script lang="ts">
  35. export default {
  36. name: 'wd-step',
  37. options: {
  38. addGlobalClass: true,
  39. virtualHost: true,
  40. styleIsolation: 'shared'
  41. }
  42. }
  43. </script>
  44. <script lang="ts" setup>
  45. import wdIcon from '../wd-icon/wd-icon.vue'
  46. import { computed } from 'vue'
  47. import { useParent } from '../composables/useParent'
  48. import { STEPS_KEY } from '../wd-steps/types'
  49. import { isDef, objToStyle } from '../common/util'
  50. import { useTranslate } from '../composables/useTranslate'
  51. import { stepProps } from './types'
  52. import type { CSSProperties } from 'vue'
  53. const props = defineProps(stepProps)
  54. const { parent: steps, index } = useParent(STEPS_KEY)
  55. const { translate } = useTranslate('steps')
  56. const currentStatus = computed(() => {
  57. return getCurrentStatus(index.value)
  58. })
  59. const currentTitle = computed(() => {
  60. return getCurrentTitle(currentStatus.value)
  61. })
  62. const rootStyle = computed(() => {
  63. const style: CSSProperties = {}
  64. if (steps) {
  65. const { vertical, space } = steps.props
  66. if (vertical) {
  67. if (isDef(space)) {
  68. style['height'] = space
  69. }
  70. } else {
  71. style['width'] = space || 100 / steps.children.length + '%'
  72. }
  73. }
  74. return `${objToStyle(style)}${props.customStyle}`
  75. })
  76. const canAlignCenter = computed(() => {
  77. if (isDef(steps)) {
  78. const { vertical, alignCenter } = steps.props
  79. return Boolean(!vertical && alignCenter)
  80. } else {
  81. return false
  82. }
  83. })
  84. const vertical = computed(() => {
  85. if (isDef(steps)) {
  86. return Boolean(steps.props.vertical)
  87. } else {
  88. return false
  89. }
  90. })
  91. const dot = computed(() => {
  92. if (isDef(steps)) {
  93. return Boolean(steps.props.dot)
  94. } else {
  95. return false
  96. }
  97. })
  98. const childrenLength = computed(() => {
  99. if (isDef(steps)) {
  100. return Number(steps.children.length)
  101. } else {
  102. return 0
  103. }
  104. })
  105. function getCurrentStatus(index: number) {
  106. if (props.status) {
  107. return props.status
  108. }
  109. if (steps) {
  110. const { active } = steps.props
  111. if (Number(active) > index) {
  112. return 'finished'
  113. } else if (Number(active) === index) {
  114. return 'process'
  115. } else {
  116. return 'wait'
  117. }
  118. } else {
  119. return 'wait'
  120. }
  121. }
  122. function getCurrentTitle(currentStatus: string) {
  123. if (props.title) return props.title
  124. switch (currentStatus) {
  125. case 'finished':
  126. return translate('finished')
  127. case 'error':
  128. return translate('failed')
  129. case 'process':
  130. return translate('process')
  131. case 'wait':
  132. default:
  133. return translate('wait')
  134. }
  135. }
  136. </script>
  137. <style lang="scss" scoped>
  138. @import './index.scss';
  139. </style>