u-steps-item.vue 8.0 KB

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