uv-steps-item.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <template>
  2. <view class="uv-steps-item" ref="uv-steps-item" :class="[`uv-steps-item--${parentData.direction}`]" :style="[$uv.addStyle(customStyle)]">
  3. <view class="uv-steps-item__line" v-if="index + 1 < childLength" :class="[`uv-steps-item__line--${parentData.direction}`]" :style="[lineStyle]"></view>
  4. <view :class="['uv-steps-item__wrapper', `uv-steps-item__wrapper--${parentData.direction}`, parentData.dot && `uv-steps-item__wrapper--${parentData.direction}--dot`]">
  5. <slot name="icon">
  6. <view
  7. class="uv-steps-item__wrapper__dot"
  8. v-if="parentData.dot"
  9. :style="{
  10. backgroundColor: statusColor
  11. }"
  12. ></view>
  13. <view class="uv-steps-item__wrapper__icon" v-else-if="parentData.activeIcon || parentData.inactiveIcon">
  14. <uv-icon
  15. :name="index <= parentData.current ? parentData.activeIcon : parentData.inactiveIcon"
  16. :size="iconSize"
  17. :color="index <= parentData.current ? parentData.activeColor : parentData.inactiveColor"
  18. ></uv-icon>
  19. </view>
  20. <view
  21. v-else
  22. :style="{
  23. backgroundColor: statusClass === 'process' ? parentData.activeColor : 'transparent',
  24. borderColor: statusColor
  25. }"
  26. class="uv-steps-item__wrapper__circle"
  27. >
  28. <text
  29. v-if="statusClass === 'process' || statusClass === 'wait'"
  30. class="uv-steps-item__wrapper__circle__text"
  31. :style="{
  32. color: index == parentData.current ? '#ffffff' : parentData.inactiveColor
  33. }"
  34. >
  35. {{ index + 1 }}
  36. </text>
  37. <uv-icon v-else :color="statusClass === 'error' ? 'error' : parentData.activeColor" size="12" :name="statusClass === 'error' ? 'close' : 'checkmark'"></uv-icon>
  38. </view>
  39. </slot>
  40. </view>
  41. <view :class="['uv-steps-item__content', `uv-steps-item__content--${parentData.direction}`]" :style="[contentStyle]">
  42. <slot name="title">
  43. <uv-text :text="title" :type="parentData.current == index ? 'main' : 'content'" lineHeight="20px" :size="parentData.current == index ? 14 : 13"></uv-text>
  44. </slot>
  45. <slot name="desc">
  46. <uv-text :text="desc" type="tips" size="12"></uv-text>
  47. </slot>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. import mpMixin from '@/uni_modules/uv-ui-tools/libs/mixin/mpMixin.js'
  53. import mixin from '@/uni_modules/uv-ui-tools/libs/mixin/mixin.js'
  54. import props from './props.js'
  55. // #ifdef APP-NVUE
  56. const dom = uni.requireNativePlugin('dom')
  57. // #endif
  58. /**
  59. * StepsItem 步骤条的子组件
  60. * @description 本组件需要和uv-steps配合使用
  61. * @tutorial https://www.uvui.cn/components/steps.html
  62. * @property {String} title 标题文字
  63. * @property {String} current 描述文本
  64. * @property {String | Number} iconSize 图标大小 (默认 17 )
  65. * @property {Boolean} error 当前步骤是否处于失败状态 (默认 false )
  66. * @example <uv-steps current="0"><uv-steps-item title="已出库" desc="10:35" ></uv-steps-item></uv-steps>
  67. */
  68. export default {
  69. name: 'uv-steps-item',
  70. mixins: [mpMixin, mixin, props],
  71. data() {
  72. return {
  73. index: 0,
  74. childLength: 0,
  75. showLine: false,
  76. size: {
  77. height: 0,
  78. width: 0
  79. },
  80. parentData: {
  81. direction: 'row',
  82. current: 0,
  83. activeColor: '',
  84. inactiveColor: '',
  85. activeIcon: '',
  86. inactiveIcon: '',
  87. dot: false
  88. }
  89. }
  90. },
  91. watch: {
  92. parentData(newValue, oldValue) {}
  93. },
  94. created() {
  95. this.init()
  96. },
  97. computed: {
  98. lineStyle() {
  99. const style = {}
  100. if (this.parentData.direction === 'row') {
  101. style.width = this.size.width + 'px'
  102. style.left = this.size.width / 2 + 'px'
  103. } else {
  104. style.height = this.size.height + 'px'
  105. }
  106. style.backgroundColor = this.parent.children?.[this.index + 1]?.error
  107. ? '#f56c6c'
  108. : this.index < this.parentData.current
  109. ? this.parentData.activeColor
  110. : this.parentData.inactiveColor
  111. return style
  112. },
  113. statusClass() {
  114. const { index, error } = this
  115. const { current } = this.parentData
  116. if (current == index) {
  117. return error === true ? 'error' : 'process'
  118. } else if (error) {
  119. return 'error'
  120. } else if (current > index) {
  121. return 'finish'
  122. } else {
  123. return 'wait'
  124. }
  125. },
  126. statusColor() {
  127. let color = ''
  128. switch (this.statusClass) {
  129. case 'finish':
  130. color = this.parentData.activeColor
  131. break
  132. case 'error':
  133. color = '#f56c6c'
  134. break
  135. case 'process':
  136. color = this.parentData.dot ? this.parentData.activeColor : 'transparent'
  137. break
  138. default:
  139. color = this.parentData.inactiveColor
  140. break
  141. }
  142. return color
  143. },
  144. contentStyle() {
  145. const style = {}
  146. if (this.parentData.direction === 'column') {
  147. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  148. style.marginTop = this.parentData.dot ? '0px' : '6px'
  149. } else {
  150. style.marginTop = this.parentData.dot ? '2px' : '6px'
  151. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  152. }
  153. return style
  154. }
  155. },
  156. mounted() {
  157. this.parent && this.parent.updateFromChild()
  158. this.$uv.sleep().then(() => {
  159. this.getStepsItemRect()
  160. })
  161. },
  162. methods: {
  163. init() {
  164. // 初始化数据
  165. this.updateParentData()
  166. if (!this.parent) {
  167. return this.$uv.error('uv-steps-item必须要搭配uv-steps组件使用')
  168. }
  169. this.index = this.parent.children.indexOf(this)
  170. this.childLength = this.parent.children.length
  171. },
  172. updateParentData() {
  173. // 此方法在mixin中
  174. this.getParentData('uv-steps')
  175. },
  176. // 父组件数据发生变化
  177. updateFromParent() {
  178. this.init()
  179. },
  180. // 获取组件的尺寸,用于设置横线的位置
  181. getStepsItemRect() {
  182. // #ifndef APP-NVUE
  183. this.$uvGetRect('.uv-steps-item').then((size) => {
  184. this.size = size
  185. })
  186. // #endif
  187. // #ifdef APP-NVUE
  188. dom.getComponentRect(this.$refs['uv-steps-item'], (res) => {
  189. const { size } = res
  190. this.size = size
  191. })
  192. // #endif
  193. }
  194. }
  195. }
  196. </script>
  197. <style lang="scss" scoped>
  198. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  199. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  200. .uv-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: #f1f6fe;
  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: $uv-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: $uv-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: $uv-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: $uv-tips-color;
  283. &--row {
  284. top: 10px;
  285. height: 1px;
  286. }
  287. &--column {
  288. width: 1px;
  289. left: 10px;
  290. }
  291. }
  292. }
  293. </style>