tn-steps.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <view
  3. class="tn-steps-class tn-steps"
  4. :style="{
  5. flexDirection: direction
  6. }"
  7. >
  8. <view
  9. v-for="(item, index) in list"
  10. :key="index"
  11. class="tn-steps__item"
  12. :class="[`tn-steps__item--${direction}`]"
  13. @tap="clickStepItem(index)"
  14. >
  15. <!-- 数值模式 -->
  16. <view
  17. v-if="mode === 'number'"
  18. class="tn-steps__item__number"
  19. :style="{
  20. backgroundColor: currentIndex <= index ? 'transparent' : activeColor,
  21. borderColor: currentIndex <= index ? inActiveColor : activeColor
  22. }"
  23. >
  24. <text
  25. class="tn-steps__item__number__text"
  26. :class="[{'tn-steps__item__number__text--visible': currentIndex <= index}]"
  27. :style="{
  28. color: currentIndex <= index ? inActiveColor : activeColor
  29. }"
  30. >
  31. {{ index + 1}}
  32. </text>
  33. <view class="tn-steps__item__number__icon" :class="[`tn-icon-${item.icon || icon}`,{'tn-steps__item__number__icon--visible': currentIndex > index}]"></view>
  34. </view>
  35. <!-- 点模式 -->
  36. <view
  37. v-if="mode === 'dot'"
  38. class="tn-steps__item__dot"
  39. :style="{
  40. backgroundColor: currentIndex <= index ? inActiveColor : activeColor
  41. }"
  42. ></view>
  43. <!-- 图标模式 -->
  44. <view
  45. v-if="mode === 'icon'"
  46. class="tn-steps__item__icon"
  47. :class="[iconModeClass(index)]"
  48. :style="{
  49. color: currentIndex <= index ? inActiveColor : activeColor
  50. }"
  51. ></view>
  52. <!-- 点图标模式 -->
  53. <view v-if="mode === 'dotIcon'" class="tn-steps__item__dot-icon">
  54. <view v-if="currentIndex <= index" class="tn-steps__item__dot-icon--dot" :style="{backgroundColor: inActiveColor}"></view>
  55. <view v-else class="tn-steps__item__dot-icon--icon" :class="[iconModeClass(index)]" :style="{color: activeColor}"></view>
  56. </view>
  57. <!-- 步骤下的文字 -->
  58. <text
  59. v-if="showTitle"
  60. class="tn-steps__item__text tn-text-ellipsis"
  61. :class="[`tn-steps__item__text--${direction}`]"
  62. :style="{
  63. color: currentIndex <= index ? inActiveColor : activeColor
  64. }"
  65. >
  66. {{ item.name }}
  67. </text>
  68. <!-- 连接的横线 -->
  69. <view
  70. v-if="index < list.length - 1"
  71. class="tn-steps__item__line"
  72. :class="[`tn-steps__item__line--${mode}`]"
  73. :style="{
  74. borderColor: currentIndex <= index + 1 ? inActiveColor : activeColor
  75. }"
  76. ></view>
  77. </view>
  78. </view>
  79. </template>
  80. <script>
  81. export default {
  82. name: 'tn-steps',
  83. props: {
  84. // 模式类型
  85. // dot -> 点 number -> 数字 icon -> 图标 dotIcon -> 点图标
  86. mode: {
  87. type: String,
  88. default: 'dot'
  89. },
  90. // 步骤条的方向
  91. // row -> 横向 column -> 竖向
  92. direction: {
  93. type: String,
  94. default: 'row'
  95. },
  96. // 步骤条数据
  97. list: {
  98. type: Array,
  99. default() {
  100. return []
  101. }
  102. },
  103. // 当前激活的步数
  104. current: {
  105. type: Number,
  106. default: 0
  107. },
  108. // 激活步骤的颜色
  109. activeColor: {
  110. type: String,
  111. default: '#01BEFF'
  112. },
  113. // 未激活步骤的颜色
  114. inActiveColor: {
  115. type: String,
  116. default: '#AAAAAA'
  117. },
  118. // 激活后显示的图标,在数字模式下有效
  119. icon: {
  120. type: String,
  121. default: 'success'
  122. },
  123. // 是否显示标题
  124. showTitle: {
  125. type: Boolean,
  126. default: true
  127. }
  128. },
  129. computed: {
  130. // icon模式下图标的值
  131. iconModeClass() {
  132. return (index) => {
  133. const item = this.list[index]
  134. // 状态被选中并且对应数据下存在selectIcon属性
  135. if (this.currentIndex > index && item.hasOwnProperty('selectIcon')) {
  136. return `tn-icon-${item.selectIcon}`
  137. } else {
  138. // 未选中
  139. return `tn-icon-${item.icon || this.icon}`
  140. }
  141. }
  142. }
  143. },
  144. data() {
  145. return {
  146. currentIndex: 0
  147. }
  148. },
  149. watch: {
  150. current: {
  151. handler(val) {
  152. this.currentIndex = val
  153. },
  154. immediate: true
  155. }
  156. },
  157. methods: {
  158. // 点击了某一个选项
  159. clickStepItem(index) {
  160. const chooseIndex = index + 1
  161. this.currentIndex = chooseIndex
  162. this.$emit('click', { index: chooseIndex })
  163. }
  164. }
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. $tn-steps-item-number-width: 44rpx;
  169. $tn-steps-item-dot-width: 20rpx;
  170. .tn-steps {
  171. display: flex;
  172. flex-direction: row;
  173. &__item {
  174. flex: 1;
  175. position: relative;
  176. display: flex;
  177. align-items: center;
  178. justify-content: center;
  179. flex-direction: column;
  180. min-width: 100rpx;
  181. font-size: 28rpx;
  182. text-align: center;
  183. &__number {
  184. // display: flex;
  185. // flex-wrap: wrap;
  186. // align-items: center;
  187. // justify-content: center;
  188. position: relative;
  189. width: $tn-steps-item-number-width;
  190. height: $tn-steps-item-number-width;
  191. line-height: calc(#{$tn-steps-item-number-width} - 2rpx);
  192. border: 1px solid #AAAAAA;
  193. border-radius: 50%;
  194. overflow: hidden;
  195. transition: all 0.3s linear;
  196. &__text {
  197. position: absolute;
  198. top: 0;
  199. right: 0;
  200. bottom: 0;
  201. left: 0;
  202. margin: auto;
  203. transition: inherit;
  204. transform: translateY(-#{$tn-steps-item-number-width});
  205. &--visible {
  206. transform: translateY(0);
  207. }
  208. }
  209. &__icon {
  210. position: absolute;
  211. top: 0;
  212. right: 0;
  213. bottom: 0;
  214. left: 0;
  215. margin: auto;
  216. color: #FFFFFF;
  217. transition: all 0.3s linear;
  218. transform: translateY(#{$tn-steps-item-number-width});
  219. &--visible {
  220. transform: translateY(0);
  221. }
  222. }
  223. }
  224. &__dot {
  225. width: $tn-steps-item-dot-width;
  226. height: $tn-steps-item-dot-width;
  227. display: flex;
  228. flex-direction: row;
  229. border-radius: 50%;
  230. transition: all 0.3s linear;
  231. &--icon {
  232. width: $tn-steps-item-number-width;
  233. height: $tn-steps-item-number-width;
  234. }
  235. }
  236. &__icon {
  237. width: $tn-steps-item-number-width;
  238. height: $tn-steps-item-number-width;
  239. font-size: $tn-steps-item-number-width;
  240. transition: all 0.3s linear;
  241. }
  242. &__dot-icon {
  243. width: $tn-steps-item-number-width;
  244. height: $tn-steps-item-number-width;
  245. display: flex;
  246. flex-direction: row;
  247. align-items: center;
  248. justify-content: center;
  249. transition: all 0.3s linear;
  250. &--dot {
  251. width: $tn-steps-item-dot-width;
  252. height: $tn-steps-item-dot-width;
  253. border-radius: 50%;
  254. transition: inherit;
  255. }
  256. &--icon {
  257. width: $tn-steps-item-number-width;
  258. height: $tn-steps-item-number-width;
  259. font-size: $tn-steps-item-number-width;
  260. transition: inherit;
  261. }
  262. }
  263. &__text {
  264. transition: all 0.3s linear;
  265. &--row {
  266. margin-top: 14rpx;
  267. }
  268. &--column {
  269. margin-left: 14rpx;
  270. }
  271. }
  272. &__line {
  273. position: absolute;
  274. z-index: 0;
  275. vertical-align: middle;
  276. transition: all 0.3s linear;
  277. }
  278. &--row {
  279. display: flex;
  280. flex-direction: column;
  281. .tn-steps__item__line {
  282. border-bottom-width: 1px;
  283. border-bottom-style: solid;
  284. width: 50%;
  285. left: 75%;
  286. &--dot {
  287. top: calc(#{$tn-steps-item-dot-width} / 2);
  288. }
  289. &--number, &--icon, &--dotIcon {
  290. top: calc(#{$tn-steps-item-number-width} / 2);
  291. }
  292. }
  293. }
  294. &--column {
  295. display: flex;
  296. flex-direction: row;
  297. justify-content: flex-start;
  298. min-height: 120rpx;
  299. .tn-steps__item__line {
  300. border-left-width: 1px;
  301. border-left-style: solid;
  302. height: 50%;
  303. top: 75%;
  304. &--dot {
  305. left: calc(#{$tn-steps-item-dot-width} / 2);
  306. }
  307. &--number, &--icon, &--dotIcon {
  308. left: calc(#{$tn-steps-item-number-width} / 2);
  309. }
  310. }
  311. }
  312. }
  313. }
  314. </style>