u-steps-item.vue 8.1 KB

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