u-steps-item.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. <up-icon :name="index <= parentData.current ? parentData.activeIcon : parentData.inactiveIcon"
  15. :size="iconSize"
  16. :color="index <= parentData.current ? parentData.activeColor : parentData.inactiveColor">
  17. </up-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. <up-icon v-else :color="statusClass === 'error' ? 'error' : parentData.activeColor" size="12"
  28. :name="statusClass === 'error' ? 'close' : 'checkmark'"></up-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" lineHeight="20px"
  42. :type="parentData.current == index ? 'main' : 'content'"
  43. :size="parentData.current == index ? 14 : 13"></up-text>
  44. </view>
  45. <view class="u-steps-item__content__desc">
  46. <slot name="desc">
  47. </slot>
  48. <up-text v-if="!$slots['desc']" :text="desc" type="tips" size="12"></up-text>
  49. </view>
  50. </template>
  51. </view>
  52. <!-- <view
  53. class="u-steps-item__line"
  54. v-if="showLine && parentData.direction === 'column'"
  55. :class="[`u-steps-item__line--${parentData.direction}`]"
  56. :style="[lineStyle]"
  57. ></view> -->
  58. </view>
  59. </template>
  60. <script>
  61. import { props } from './props';
  62. import { mpMixin } from '../../libs/mixin/mpMixin';
  63. import { mixin } from '../../libs/mixin/mixin';
  64. import { sleep, error } from '../../libs/function/index';
  65. import color from '../../libs/config/color';
  66. // #ifdef APP-NVUE
  67. const dom = uni.requireNativePlugin('dom')
  68. // #endif
  69. /**
  70. * StepsItem 步骤条的子组件
  71. * @description 本组件需要和u-steps配合使用
  72. * @tutorial https://uview-plus.jiangruyi.com/components/steps.html
  73. * @property {String} title 标题文字
  74. * @property {String} current 描述文本
  75. * @property {String | Number} iconSize 图标大小 (默认 17 )
  76. * @property {Boolean} error 当前步骤是否处于失败状态 (默认 false )
  77. * @example <u-steps current="0"><u-steps-item title="已出库" desc="10:35" ></u-steps-item></u-steps>
  78. */
  79. export default {
  80. name: 'u-steps-item',
  81. mixins: [mpMixin, mixin, props],
  82. data() {
  83. return {
  84. index: 0,
  85. childLength: 0,
  86. showLine: false,
  87. size: {
  88. height: 0,
  89. width: 0
  90. },
  91. parentData: {
  92. direction: 'row',
  93. current: 0,
  94. activeColor: '',
  95. inactiveColor: '',
  96. activeIcon: '',
  97. inactiveIcon: '',
  98. dot: false
  99. }
  100. }
  101. },
  102. watch: {
  103. 'parentData'(newValue, oldValue) {
  104. }
  105. },
  106. created() {
  107. this.init()
  108. },
  109. // #ifdef MP-TOUTIAO
  110. options: {
  111. virtualHost: false
  112. },
  113. // #endif
  114. computed: {
  115. lineStyle() {
  116. const style = {}
  117. if (this.parentData.direction === 'row') {
  118. style.width = this.size.width + 'px'
  119. style.left = this.size.width / 2 + 'px'
  120. } else {
  121. style.height = this.size.height + 'px'
  122. // style.top = this.size.height / 2 + 'px'
  123. }
  124. style.backgroundColor = this.parent.children?.[this.index + 1]?.error ? color.error : this.index <
  125. this
  126. .parentData
  127. .current ? this.parentData.activeColor : this.parentData.inactiveColor
  128. return style
  129. },
  130. itemStyleInner() {
  131. return {
  132. ...this.itemStyle
  133. }
  134. },
  135. statusClass() {
  136. const {
  137. index,
  138. error
  139. } = this
  140. const {
  141. current
  142. } = this.parentData
  143. if (current == index) {
  144. return error === true ? 'error' : 'process'
  145. } else if (error) {
  146. return 'error'
  147. } else if (current > index) {
  148. return 'finish'
  149. } else {
  150. return 'wait'
  151. }
  152. },
  153. statusColor() {
  154. let colorTmp = ''
  155. switch (this.statusClass) {
  156. case 'finish':
  157. colorTmp = this.parentData.activeColor
  158. break
  159. case 'error':
  160. colorTmp = color.error
  161. break
  162. case 'process':
  163. colorTmp = this.parentData.dot ? this.parentData.activeColor : 'transparent'
  164. break
  165. default:
  166. colorTmp = this.parentData.inactiveColor
  167. break
  168. }
  169. return colorTmp
  170. },
  171. contentStyle() {
  172. const style = {}
  173. if (this.parentData.direction === 'column') {
  174. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  175. style.marginTop = this.parentData.dot ? '0px' : '6px'
  176. } else {
  177. style.marginTop = this.parentData.dot ? '2px' : '6px'
  178. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  179. }
  180. return style
  181. }
  182. },
  183. mounted() {
  184. this.parent && this.parent.updateFromChild()
  185. sleep().then(() => {
  186. this.getStepsItemRect()
  187. })
  188. },
  189. methods: {
  190. init() {
  191. // 初始化数据
  192. this.updateParentData()
  193. if (!this.parent) {
  194. return error('u-steps-item必须要搭配u-steps组件使用')
  195. }
  196. this.index = this.parent.children.indexOf(this)
  197. this.childLength = this.parent.children.length
  198. },
  199. updateParentData() {
  200. // 此方法在mixin中
  201. this.getParentData('u-steps')
  202. },
  203. // 父组件数据发生变化
  204. updateFromParent() {
  205. this.init()
  206. },
  207. // 获取组件的尺寸,用于设置横线的位置
  208. getStepsItemRect() {
  209. // #ifndef APP-NVUE
  210. this.$uGetRect('.u-steps-item').then(size => {
  211. this.size = size
  212. })
  213. // #endif
  214. // #ifdef APP-NVUE
  215. dom.getComponentRect(this.$refs['u-steps-item'], res => {
  216. const {
  217. size
  218. } = res
  219. this.size = size
  220. })
  221. // #endif
  222. }
  223. }
  224. }
  225. </script>
  226. <style lang="scss" scoped>
  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. &__title {
  300. // #ifdef H5
  301. cursor: pointer;
  302. // #endif
  303. }
  304. &--row {
  305. flex-direction: column;
  306. align-items: center;
  307. }
  308. &--column {
  309. flex-direction: column;
  310. margin-left: 6px;
  311. }
  312. }
  313. &__line {
  314. position: absolute;
  315. background: $u-tips-color;
  316. &--row {
  317. top: 10px;
  318. height: 1px;
  319. }
  320. &--column {
  321. width: 1px;
  322. left: 10px;
  323. }
  324. }
  325. }
  326. </style>