uni-steps.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <template>
  2. <view class="uni-steps">
  3. <view :class="[direction==='column'?'uni-steps__column':'uni-steps__row']">
  4. <view :class="[direction==='column'?'uni-steps__column-text-container':'uni-steps__row-text-container']">
  5. <view v-for="(item,index) in options" :key="index"
  6. :class="[direction==='column'?'uni-steps__column-text':'uni-steps__row-text']">
  7. <text :style="{color:index === active?activeColor:deactiveColor}"
  8. :class="[direction==='column'?'uni-steps__column-title':'uni-steps__row-title']">{{item.title}}</text>
  9. <text :style="{color: deactiveColor}"
  10. :class="[direction==='column'?'uni-steps__column-desc':'uni-steps__row-desc']">{{item.desc}}</text>
  11. </view>
  12. </view>
  13. <view :class="[direction==='column'?'uni-steps__column-container':'uni-steps__row-container']">
  14. <view :class="[direction==='column'?'uni-steps__column-line-item':'uni-steps__row-line-item']"
  15. v-for="(item,index) in options" :key="index" :style="{height: direction === 'column'?heightArr[index]+'px':'14px'}">
  16. <view
  17. :class="[direction==='column'?'uni-steps__column-line':'uni-steps__row-line',direction==='column'?'uni-steps__column-line--before':'uni-steps__row-line--before']"
  18. :style="{backgroundColor:index<=active&&index!==0?activeColor:index===0?'transparent':deactiveColor}">
  19. </view>
  20. <view :class="[direction==='column'?'uni-steps__column-check':'uni-steps__row-check']"
  21. v-if="index === active">
  22. <uni-icons :color="activeColor" :type="activeIcon" size="14" />
  23. </view>
  24. <view v-else :class="[direction==='column'?'uni-steps__column-circle':'uni-steps__row-circle']"
  25. :style="{backgroundColor:index<active?activeColor:deactiveColor}" />
  26. <view
  27. :class="[direction==='column'?'uni-steps__column-line':'uni-steps__row-line',direction==='column'?'uni-steps__column-line--after':'uni-steps__row-line--after']"
  28. :style="{backgroundColor:index<active&&index!==options.length-1?activeColor:index===options.length-1?'transparent':deactiveColor}" />
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. /**
  36. * Steps 步骤条
  37. * @description 评分组件
  38. * @tutorial https://ext.dcloud.net.cn/plugin?id=34
  39. * @property {Number} active 当前步骤
  40. * @property {String} direction = [row|column] 当前步骤
  41. * @value row 横向
  42. * @value column 纵向
  43. * @property {String} activeColor 选中状态的颜色
  44. * @property {Array} options 数据源,格式为:[{title:'xxx',desc:'xxx'},{title:'xxx',desc:'xxx'}]
  45. */
  46. export default {
  47. name: 'UniSteps',
  48. props: {
  49. direction: {
  50. // 排列方向 row column
  51. type: String,
  52. default: 'row'
  53. },
  54. activeColor: {
  55. // 激活状态颜色
  56. type: String,
  57. default: '#2979FF'
  58. },
  59. deactiveColor: {
  60. // 未激活状态颜色
  61. type: String,
  62. default: '#B7BDC6'
  63. },
  64. active: {
  65. // 当前步骤
  66. type: Number,
  67. default: 0
  68. },
  69. activeIcon: {
  70. // 当前步骤
  71. type: String,
  72. default: 'checkbox-filled'
  73. },
  74. options: {
  75. type: Array,
  76. default () {
  77. return []
  78. }
  79. } // 数据
  80. },
  81. data() {
  82. return {
  83. heightArr: [],
  84. }
  85. },
  86. mounted() {
  87. //根据内容设置步骤条的长度
  88. if (this.direction === 'column') {
  89. let that = this;
  90. //只能用类选择器,用id选择器所获取的元素信息不准确
  91. uni.createSelectorQuery().in(this).selectAll('.uni-steps__column-text').boundingClientRect(data => {
  92. that.heightArr = data.map(item => item.height + 1);
  93. }).exec()
  94. }
  95. },
  96. }
  97. </script>
  98. <style lang="scss">
  99. $uni-primary: #2979ff !default;
  100. $uni-border-color: #EDEDED;
  101. .uni-steps {
  102. /* #ifndef APP-NVUE */
  103. display: flex;
  104. width: 100%;
  105. /* #endif */
  106. /* #ifdef APP-NVUE */
  107. flex: 1;
  108. /* #endif */
  109. flex-direction: column;
  110. }
  111. .uni-steps__row {
  112. /* #ifndef APP-NVUE */
  113. display: flex;
  114. /* #endif */
  115. flex-direction: column;
  116. }
  117. .uni-steps__column {
  118. /* #ifndef APP-NVUE */
  119. display: flex;
  120. /* #endif */
  121. flex-direction: row-reverse;
  122. }
  123. .uni-steps__row-text-container {
  124. /* #ifndef APP-NVUE */
  125. display: flex;
  126. /* #endif */
  127. flex-direction: row;
  128. align-items: flex-end;
  129. margin-bottom: 8px;
  130. }
  131. .uni-steps__column-text-container {
  132. /* #ifndef APP-NVUE */
  133. display: flex;
  134. /* #endif */
  135. flex-direction: column;
  136. flex: 1;
  137. }
  138. .uni-steps__row-text {
  139. /* #ifndef APP-NVUE */
  140. display: inline-flex;
  141. /* #endif */
  142. flex: 1;
  143. flex-direction: column;
  144. }
  145. .uni-steps__column-text {
  146. padding: 6px 0px;
  147. border-bottom-style: solid;
  148. border-bottom-width: 1px;
  149. border-bottom-color: $uni-border-color;
  150. /* #ifndef APP-NVUE */
  151. display: flex;
  152. /* #endif */
  153. flex-direction: column;
  154. }
  155. .uni-steps__row-title {
  156. font-size: 14px;
  157. line-height: 16px;
  158. text-align: center;
  159. }
  160. .uni-steps__column-title {
  161. font-size: 14px;
  162. text-align: left;
  163. line-height: 18px;
  164. }
  165. .uni-steps__row-desc {
  166. font-size: 12px;
  167. line-height: 14px;
  168. text-align: center;
  169. }
  170. .uni-steps__column-desc {
  171. font-size: 12px;
  172. text-align: left;
  173. line-height: 18px;
  174. }
  175. .uni-steps__row-container {
  176. /* #ifndef APP-NVUE */
  177. display: flex;
  178. /* #endif */
  179. flex-direction: row;
  180. }
  181. .uni-steps__column-container {
  182. /* #ifndef APP-NVUE */
  183. display: inline-flex;
  184. /* #endif */
  185. width: 30px;
  186. flex-direction: column;
  187. }
  188. .uni-steps__row-line-item {
  189. /* #ifndef APP-NVUE */
  190. display: inline-flex;
  191. /* #endif */
  192. flex-direction: row;
  193. flex: 1;
  194. height: 14px;
  195. line-height: 14px;
  196. align-items: center;
  197. justify-content: center;
  198. }
  199. .uni-steps__column-line-item {
  200. /* #ifndef APP-NVUE */
  201. display: flex;
  202. /* #endif */
  203. flex-direction: column;
  204. align-items: center;
  205. justify-content: center;
  206. }
  207. .uni-steps__row-line {
  208. flex: 1;
  209. height: 1px;
  210. background-color: #B7BDC6;
  211. }
  212. .uni-steps__column-line {
  213. width: 1px;
  214. background-color: #B7BDC6;
  215. }
  216. .uni-steps__row-line--after {
  217. transform: translateX(1px);
  218. }
  219. .uni-steps__column-line--after {
  220. flex: 1;
  221. transform: translate(0px, 1px);
  222. }
  223. .uni-steps__row-line--before {
  224. transform: translateX(-1px);
  225. }
  226. .uni-steps__column-line--before {
  227. height: 6px;
  228. transform: translate(0px, -13px);
  229. }
  230. .uni-steps__row-circle {
  231. width: 5px;
  232. height: 5px;
  233. border-radius: 50%;
  234. background-color: #B7BDC6;
  235. margin: 0px 3px;
  236. }
  237. .uni-steps__column-circle {
  238. width: 5px;
  239. height: 5px;
  240. border-radius: 50%;
  241. background-color: #B7BDC6;
  242. margin: 4px 0px 5px 0px;
  243. }
  244. .uni-steps__row-check {
  245. margin: 0px 6px;
  246. }
  247. .uni-steps__column-check {
  248. height: 14px;
  249. line-height: 14px;
  250. margin: 2px 0px;
  251. }
  252. </style>