u-steps-item.vue 8.0 KB

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