u-steps-item.vue 8.0 KB

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