u-steps-item.vue 7.5 KB

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