u-steps-item.vue 7.7 KB

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