uv-steps-item.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. }">{{ index + 1}}</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.left = this.size.width / 2 + 'px'
  136. } else {
  137. style.height = this.size.height + 'px'
  138. }
  139. style.backgroundColor = this.parent.children?.[this.index + 1]?.error ? '#f56c6c' : this.index < this.parentData.current ? this.parentData.activeColor : this.parentData.inactiveColor
  140. return style
  141. },
  142. statusClass() {
  143. const {
  144. index,
  145. error
  146. } = this
  147. const {
  148. current
  149. } = this.parentData
  150. if (current == index) {
  151. return error === true ? 'error' : 'process'
  152. } else if (error) {
  153. return 'error'
  154. } else if (current > index) {
  155. return 'finish'
  156. } else {
  157. return 'wait'
  158. }
  159. },
  160. statusColor() {
  161. let color = ''
  162. switch (this.statusClass) {
  163. case 'finish':
  164. color = this.parentData.activeColor
  165. break
  166. case 'error':
  167. color = '#f56c6c'
  168. break
  169. case 'process':
  170. color = this.parentData.dot ? this.parentData.activeColor : 'transparent'
  171. break
  172. default:
  173. color = this.parentData.inactiveColor
  174. break
  175. }
  176. return color
  177. },
  178. contentStyle() {
  179. const style = {}
  180. if (this.parentData.direction === 'column') {
  181. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  182. style.marginTop = this.parentData.dot ? '0px' : '6px'
  183. } else {
  184. style.marginTop = this.parentData.dot ? '2px' : '6px'
  185. style.marginLeft = this.parentData.dot ? '2px' : '6px'
  186. }
  187. return style
  188. }
  189. },
  190. mounted() {
  191. this.parent && this.parent.updateFromChild()
  192. this.$uv.sleep().then(() => {
  193. this.getStepsItemRect()
  194. })
  195. },
  196. methods: {
  197. init() {
  198. // 初始化数据
  199. this.updateParentData()
  200. if (!this.parent) {
  201. return this.$uv.error('uv-steps-item必须要搭配uv-steps组件使用')
  202. }
  203. this.index = this.parent.children.indexOf(this)
  204. this.childLength = this.parent.children.length
  205. },
  206. updateParentData() {
  207. // 此方法在mixin中
  208. this.getParentData('uv-steps')
  209. },
  210. // 父组件数据发生变化
  211. updateFromParent() {
  212. this.init()
  213. },
  214. // 获取组件的尺寸,用于设置横线的位置
  215. getStepsItemRect() {
  216. // #ifndef APP-NVUE
  217. this.$uvGetRect('.uv-steps-item').then(size => {
  218. this.size = size
  219. })
  220. // #endif
  221. // #ifdef APP-NVUE
  222. dom.getComponentRect(this.$refs['uv-steps-item'], res => {
  223. const {
  224. size
  225. } = res
  226. this.size = size
  227. })
  228. // #endif
  229. }
  230. }
  231. }
  232. </script>
  233. <style lang="scss" scoped>
  234. @import '@/uni_modules/uv-ui-tools/libs/css/components.scss';
  235. @import '@/uni_modules/uv-ui-tools/libs/css/color.scss';
  236. .uv-steps-item {
  237. flex: 1;
  238. @include flex;
  239. &--row {
  240. flex-direction: column;
  241. align-items: center;
  242. position: relative;
  243. }
  244. &--column {
  245. position: relative;
  246. flex-direction: row;
  247. justify-content: flex-start;
  248. padding-bottom: 5px;
  249. }
  250. &__wrapper {
  251. @include flex;
  252. justify-content: center;
  253. align-items: center;
  254. position: relative;
  255. background-color: #fff;
  256. &--column {
  257. width: 20px;
  258. height: 32px;
  259. &--dot {
  260. height: 20px;
  261. width: 20px;
  262. }
  263. }
  264. &--row {
  265. width: 32px;
  266. height: 20px;
  267. &--dot {
  268. width: 20px;
  269. height: 20px;
  270. }
  271. }
  272. &__circle {
  273. width: 20px;
  274. height: 20px;
  275. /* #ifndef APP-NVUE */
  276. box-sizing: border-box;
  277. flex-shrink: 0;
  278. /* #endif */
  279. border-radius: 100px;
  280. border-width: 1px;
  281. border-color: $uv-tips-color;
  282. border-style: solid;
  283. @include flex(row);
  284. align-items: center;
  285. justify-content: center;
  286. transition: background-color 0.3s;
  287. &__text {
  288. color: $uv-tips-color;
  289. font-size: 11px;
  290. @include flex(row);
  291. align-items: center;
  292. justify-content: center;
  293. text-align: center;
  294. line-height: 11px;
  295. }
  296. }
  297. &__dot {
  298. width: 10px;
  299. height: 10px;
  300. border-radius: 100px;
  301. background-color: $uv-content-color;
  302. }
  303. }
  304. &__content {
  305. @include flex;
  306. flex: 1;
  307. &--row {
  308. flex-direction: column;
  309. align-items: center;
  310. }
  311. &--column {
  312. flex-direction: column;
  313. margin-left: 6px;
  314. }
  315. }
  316. &__line {
  317. position: absolute;
  318. background: $uv-tips-color;
  319. &--row {
  320. top: 10px;
  321. height: 1px;
  322. }
  323. &--column {
  324. width: 1px;
  325. left: 10px;
  326. }
  327. }
  328. }
  329. </style>