u-steps-item.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 = this.size.height + 'px'
  110. // style.top = this.size.height / 2 + 'px'
  111. }
  112. style.backgroundColor = this.parent.children?.[this.index + 1]?.error ? color.error : this.index <
  113. this
  114. .parentData
  115. .current ? this.parentData.activeColor : this.parentData.inactiveColor
  116. return style
  117. },
  118. itemStyleInner() {
  119. return {
  120. ...this.itemStyle
  121. }
  122. },
  123. statusClass() {
  124. const {
  125. index,
  126. error
  127. } = this
  128. const {
  129. current
  130. } = this.parentData
  131. if (current == index) {
  132. return error === true ? 'error' : 'process'
  133. } else if (error) {
  134. return 'error'
  135. } else if (current > index) {
  136. return 'finish'
  137. } else {
  138. return 'wait'
  139. }
  140. },
  141. statusColor() {
  142. let colorTmp = ''
  143. switch (this.statusClass) {
  144. case 'finish':
  145. colorTmp = this.parentData.activeColor
  146. break
  147. case 'error':
  148. colorTmp = color.error
  149. break
  150. case 'process':
  151. colorTmp = this.parentData.dot ? this.parentData.activeColor : 'transparent'
  152. break
  153. default:
  154. colorTmp = this.parentData.inactiveColor
  155. break
  156. }
  157. return colorTmp
  158. },
  159. contentStyle() {
  160. const style = {}
  161. if (this.parentData.direction === 'column') {
  162. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  163. style.marginTop = this.parentData.dot ? '0px' : '6px'
  164. } else {
  165. style.marginTop = this.parentData.dot ? '2px' : '6px'
  166. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  167. }
  168. return style
  169. }
  170. },
  171. mounted() {
  172. this.parent && this.parent.updateFromChild()
  173. sleep().then(() => {
  174. this.getStepsItemRect()
  175. })
  176. },
  177. methods: {
  178. init() {
  179. // 初始化数据
  180. this.updateParentData()
  181. if (!this.parent) {
  182. return error('u-steps-item必须要搭配u-steps组件使用')
  183. }
  184. this.index = this.parent.children.indexOf(this)
  185. this.childLength = this.parent.children.length
  186. },
  187. updateParentData() {
  188. // 此方法在mixin中
  189. this.getParentData('u-steps')
  190. },
  191. // 父组件数据发生变化
  192. updateFromParent() {
  193. this.init()
  194. },
  195. // 获取组件的尺寸,用于设置横线的位置
  196. getStepsItemRect() {
  197. // #ifndef APP-NVUE
  198. this.$uGetRect('.u-steps-item').then(size => {
  199. this.size = size
  200. })
  201. // #endif
  202. // #ifdef APP-NVUE
  203. dom.getComponentRect(this.$refs['u-steps-item'], res => {
  204. const {
  205. size
  206. } = res
  207. this.size = size
  208. })
  209. // #endif
  210. }
  211. }
  212. }
  213. </script>
  214. <style lang="scss" scoped>
  215. @import "../../libs/css/components.scss";
  216. .u-steps-item {
  217. flex: 1;
  218. @include flex;
  219. &--row {
  220. flex-direction: column;
  221. align-items: center;
  222. position: relative;
  223. }
  224. &--column {
  225. position: relative;
  226. flex-direction: row;
  227. justify-content: flex-start;
  228. padding-bottom: 5px;
  229. }
  230. &__wrapper {
  231. @include flex;
  232. justify-content: center;
  233. align-items: center;
  234. position: relative;
  235. background-color: #fff;
  236. border-radius: 50px;
  237. &--column {
  238. width: 20px;
  239. height: 20px;
  240. &--dot {
  241. height: 20px;
  242. width: 20px;
  243. }
  244. }
  245. &--row {
  246. width: 20px;
  247. height: 20px;
  248. &--dot {
  249. width: 20px;
  250. height: 20px;
  251. }
  252. }
  253. &__circle {
  254. width: 20px;
  255. height: 20px;
  256. /* #ifndef APP-NVUE */
  257. box-sizing: border-box;
  258. flex-shrink: 0;
  259. /* #endif */
  260. border-radius: 100px;
  261. border-width: 1px;
  262. border-color: $u-tips-color;
  263. border-style: solid;
  264. @include flex(row);
  265. align-items: center;
  266. justify-content: center;
  267. transition: background-color 0.3s;
  268. &__text {
  269. color: $u-tips-color;
  270. font-size: 11px;
  271. @include flex(row);
  272. align-items: center;
  273. justify-content: center;
  274. text-align: center;
  275. line-height: 11px;
  276. }
  277. }
  278. &__dot {
  279. width: 10px;
  280. height: 10px;
  281. border-radius: 100px;
  282. background-color: $u-content-color;
  283. }
  284. }
  285. &__content {
  286. @include flex;
  287. flex: 1;
  288. &--row {
  289. flex-direction: column;
  290. align-items: center;
  291. }
  292. &--column {
  293. flex-direction: column;
  294. margin-left: 6px;
  295. }
  296. }
  297. &__line {
  298. position: absolute;
  299. background: $u-tips-color;
  300. &--row {
  301. top: 10px;
  302. height: 1px;
  303. }
  304. &--column {
  305. width: 1px;
  306. left: 10px;
  307. }
  308. }
  309. }
  310. </style>