uv-steps-item.vue 7.8 KB

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