u-steps-item.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <view class="u-steps-item" ref="u-steps-item" :class="[`u-steps-item--${parentData.direction}`]">
  3. <view class="u-steps-item__line" v-if="index + 1 < childLength"
  4. :class="[`u-steps-item__line--${parentData.direction}`]" :style="[lineStyle]"></view>
  5. <view class="u-steps-item__wrapper"
  6. :class="[`u-steps-item__wrapper--${parentData.direction}`, parentData.dot && `u-steps-item__wrapper--${parentData.direction}--dot`]"
  7. :style="[itemStyleInner]">
  8. <slot name="icon">
  9. <view class="u-steps-item__wrapper__dot" v-if="parentData.dot" :style="{
  10. backgroundColor: statusColor
  11. }">
  12. </view>
  13. <view class="u-steps-item__wrapper__icon" v-else-if="parentData.activeIcon || parentData.inactiveIcon">
  14. <u-icon :name="index <= parentData.current ? parentData.activeIcon : parentData.inactiveIcon"
  15. :size="iconSize"
  16. :color="index <= parentData.current ? parentData.activeColor : parentData.inactiveColor">
  17. </u-icon>
  18. </view>
  19. <view v-else :style="{
  20. backgroundColor: statusClass === 'process' ? parentData.activeColor : 'transparent',
  21. borderColor: statusColor
  22. }" class="u-steps-item__wrapper__circle">
  23. <text v-if="statusClass === 'process' || statusClass === 'wait'"
  24. class="u-steps-item__wrapper__circle__text" :style="{
  25. color: index == parentData.current ? '#ffffff' : parentData.inactiveColor
  26. }">{{ index + 1}}</text>
  27. <u-icon v-else :color="statusClass === 'error' ? 'error' : parentData.activeColor" size="12"
  28. :name="statusClass === 'error' ? 'close' : 'checkmark'"></u-icon>
  29. </view>
  30. </slot>
  31. </view>
  32. <view class="u-steps-item__content" :class="[`u-steps-item__content--${parentData.direction}`,
  33. parentData.current == index ? 'u-steps-item__content--current' : '']"
  34. :style="[contentStyle]">
  35. <slot name="content" :index="index">
  36. </slot>
  37. <template v-if="!$slots['content']">
  38. <view class="u-steps-item__content__title">
  39. <slot name="title">
  40. </slot>
  41. <up-text v-if="!$slots['title']" :text="title" :type="parentData.current == index ? 'main' : 'content'" lineHeight="20px"
  42. :size="parentData.current == index ? 14 : 13"></up-text>
  43. </view>
  44. <view class="u-steps-item__content__desc">
  45. <slot name="desc">
  46. </slot>
  47. <up-text v-if="!$slots['desc']" :text="desc" type="tips" size="12"></up-text>
  48. </view>
  49. </template>
  50. </view>
  51. <!-- <view
  52. class="u-steps-item__line"
  53. v-if="showLine && parentData.direction === 'column'"
  54. :class="[`u-steps-item__line--${parentData.direction}`]"
  55. :style="[lineStyle]"
  56. ></view> -->
  57. </view>
  58. </template>
  59. <script>
  60. import { props } from './props';
  61. import { mpMixin } from '../../libs/mixin/mpMixin';
  62. import { mixin } from '../../libs/mixin/mixin';
  63. import { sleep, error } from '../../libs/function/index';
  64. import color from '../../libs/config/color';
  65. // #ifdef APP-NVUE
  66. const dom = uni.requireNativePlugin('dom')
  67. // #endif
  68. /**
  69. * StepsItem 步骤条的子组件
  70. * @description 本组件需要和u-steps配合使用
  71. * @tutorial https://uview-plus.jiangruyi.com/components/steps.html
  72. * @property {String} title 标题文字
  73. * @property {String} current 描述文本
  74. * @property {String | Number} iconSize 图标大小 (默认 17 )
  75. * @property {Boolean} error 当前步骤是否处于失败状态 (默认 false )
  76. * @example <u-steps current="0"><u-steps-item title="已出库" desc="10:35" ></u-steps-item></u-steps>
  77. */
  78. export default {
  79. name: 'u-steps-item',
  80. mixins: [mpMixin, mixin, props],
  81. data() {
  82. return {
  83. index: 0,
  84. childLength: 0,
  85. showLine: false,
  86. size: {
  87. height: 0,
  88. width: 0
  89. },
  90. parentData: {
  91. direction: 'row',
  92. current: 0,
  93. activeColor: '',
  94. inactiveColor: '',
  95. activeIcon: '',
  96. inactiveIcon: '',
  97. dot: false
  98. }
  99. }
  100. },
  101. watch: {
  102. 'parentData'(newValue, oldValue) {
  103. }
  104. },
  105. created() {
  106. this.init()
  107. },
  108. // #ifdef MP-TOUTIAO
  109. options: {
  110. virtualHost: false
  111. },
  112. // #endif
  113. computed: {
  114. lineStyle() {
  115. const style = {}
  116. if (this.parentData.direction === 'row') {
  117. style.width = this.size.width + 'px'
  118. style.left = this.size.width / 2 + 'px'
  119. } else {
  120. style.height = this.size.height + 'px'
  121. // style.top = this.size.height / 2 + 'px'
  122. }
  123. style.backgroundColor = this.parent.children?.[this.index + 1]?.error ? color.error : this.index <
  124. this
  125. .parentData
  126. .current ? this.parentData.activeColor : this.parentData.inactiveColor
  127. return style
  128. },
  129. itemStyleInner() {
  130. return {
  131. ...this.itemStyle
  132. }
  133. },
  134. statusClass() {
  135. const {
  136. index,
  137. error
  138. } = this
  139. const {
  140. current
  141. } = this.parentData
  142. if (current == index) {
  143. return error === true ? 'error' : 'process'
  144. } else if (error) {
  145. return 'error'
  146. } else if (current > index) {
  147. return 'finish'
  148. } else {
  149. return 'wait'
  150. }
  151. },
  152. statusColor() {
  153. let colorTmp = ''
  154. switch (this.statusClass) {
  155. case 'finish':
  156. colorTmp = this.parentData.activeColor
  157. break
  158. case 'error':
  159. colorTmp = color.error
  160. break
  161. case 'process':
  162. colorTmp = this.parentData.dot ? this.parentData.activeColor : 'transparent'
  163. break
  164. default:
  165. colorTmp = this.parentData.inactiveColor
  166. break
  167. }
  168. return colorTmp
  169. },
  170. contentStyle() {
  171. const style = {}
  172. if (this.parentData.direction === 'column') {
  173. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  174. style.marginTop = this.parentData.dot ? '0px' : '6px'
  175. } else {
  176. style.marginTop = this.parentData.dot ? '2px' : '6px'
  177. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  178. }
  179. return style
  180. }
  181. },
  182. mounted() {
  183. this.parent && this.parent.updateFromChild()
  184. sleep().then(() => {
  185. this.getStepsItemRect()
  186. })
  187. },
  188. methods: {
  189. init() {
  190. // 初始化数据
  191. this.updateParentData()
  192. if (!this.parent) {
  193. return error('u-steps-item必须要搭配u-steps组件使用')
  194. }
  195. this.index = this.parent.children.indexOf(this)
  196. this.childLength = this.parent.children.length
  197. },
  198. updateParentData() {
  199. // 此方法在mixin中
  200. this.getParentData('u-steps')
  201. },
  202. // 父组件数据发生变化
  203. updateFromParent() {
  204. this.init()
  205. },
  206. // 获取组件的尺寸,用于设置横线的位置
  207. getStepsItemRect() {
  208. // #ifndef APP-NVUE
  209. this.$uGetRect('.u-steps-item').then(size => {
  210. this.size = size
  211. })
  212. // #endif
  213. // #ifdef APP-NVUE
  214. dom.getComponentRect(this.$refs['u-steps-item'], res => {
  215. const {
  216. size
  217. } = res
  218. this.size = size
  219. })
  220. // #endif
  221. }
  222. }
  223. }
  224. </script>
  225. <style lang="scss" scoped>
  226. @import "../../libs/css/components.scss";
  227. .u-steps-item {
  228. flex: 1;
  229. @include flex;
  230. &--row {
  231. flex-direction: column;
  232. align-items: center;
  233. position: relative;
  234. }
  235. &--column {
  236. position: relative;
  237. flex-direction: row;
  238. justify-content: flex-start;
  239. padding-bottom: 5px;
  240. }
  241. &__wrapper {
  242. @include flex;
  243. justify-content: center;
  244. align-items: center;
  245. position: relative;
  246. background-color: #fff;
  247. border-radius: 50px;
  248. &--column {
  249. width: 20px;
  250. height: 20px;
  251. &--dot {
  252. height: 20px;
  253. width: 20px;
  254. }
  255. }
  256. &--row {
  257. width: 20px;
  258. height: 20px;
  259. &--dot {
  260. width: 20px;
  261. height: 20px;
  262. }
  263. }
  264. &__circle {
  265. width: 20px;
  266. height: 20px;
  267. /* #ifndef APP-NVUE */
  268. box-sizing: border-box;
  269. flex-shrink: 0;
  270. /* #endif */
  271. border-radius: 100px;
  272. border-width: 1px;
  273. border-color: $u-tips-color;
  274. border-style: solid;
  275. @include flex(row);
  276. align-items: center;
  277. justify-content: center;
  278. transition: background-color 0.3s;
  279. &__text {
  280. color: $u-tips-color;
  281. font-size: 11px;
  282. @include flex(row);
  283. align-items: center;
  284. justify-content: center;
  285. text-align: center;
  286. line-height: 11px;
  287. }
  288. }
  289. &__dot {
  290. width: 10px;
  291. height: 10px;
  292. border-radius: 100px;
  293. background-color: $u-content-color;
  294. }
  295. }
  296. &__content {
  297. @include flex;
  298. flex: 1;
  299. &--row {
  300. flex-direction: column;
  301. align-items: center;
  302. }
  303. &--column {
  304. flex-direction: column;
  305. margin-left: 6px;
  306. }
  307. }
  308. &__line {
  309. position: absolute;
  310. background: $u-tips-color;
  311. &--row {
  312. top: 10px;
  313. height: 1px;
  314. }
  315. &--column {
  316. width: 1px;
  317. left: 10px;
  318. }
  319. }
  320. }
  321. </style>