wk-select-list.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <template>
  2. <view class="wk-select-list list">
  3. <view
  4. v-for="item in optionsList"
  5. :key="item.index"
  6. :class="{active: item.checked}"
  7. class="list-item"
  8. @click="handleSelected(item.index)">
  9. <view class="list-item_icon">
  10. <image :src="$static('images/icon/check.png')" class="checked-icon" />
  11. </view>
  12. <view v-if="hasSlot" class="list-item_label">
  13. <slot :scope-data="item.data" />
  14. </view>
  15. <text v-else class="list-item_label">
  16. {{ item.data[labelField] }}
  17. </text>
  18. </view>
  19. </view>
  20. </template>
  21. <script>
  22. /**
  23. * 选项选择
  24. * @property {Array<Object>} list 选项列表
  25. * @property {Array} defaultVal 默认勾选的项
  26. * @property {String} labelField 选项中展示的字段
  27. * @property {String} valueField 选项值字段
  28. * @property {Number} max 最大能选择几项,默认正无穷
  29. * @property {Number} min 至少选择几项,默认0
  30. * @event {Function} change 勾选项发生变化
  31. * @slots 自定义选项样式
  32. */
  33. import { isObject } from '@/utils/types.js'
  34. export default {
  35. name: 'WkSelectList',
  36. props: {
  37. value: {
  38. type: Array,
  39. default: () => []
  40. },
  41. list: {
  42. type: Array,
  43. required: true
  44. },
  45. labelField: {
  46. type: String,
  47. default: 'label'
  48. },
  49. valueField: {
  50. type: String,
  51. default: 'value'
  52. },
  53. max: {
  54. type: Number,
  55. default: 1 / 0
  56. },
  57. min: {
  58. type: Number,
  59. default: 0
  60. }
  61. },
  62. data() {
  63. return {
  64. maxlength: 0,
  65. optionsList: [], // 选项数组
  66. lastIndex: null // 记录最后一次选中的是第几项
  67. }
  68. },
  69. computed: {
  70. hasSlot() {
  71. return this.$scopedSlots.default
  72. }
  73. },
  74. watch: {
  75. list: {
  76. handler(val) {
  77. this.optionsList = this.list.map((item, index) => {
  78. return {
  79. data: item,
  80. checked: false,
  81. index: index
  82. }
  83. })
  84. // console.log('stht,', this)
  85. this.getCheckedList()
  86. },
  87. immediate: true,
  88. deep: true
  89. },
  90. value: {
  91. handler() {
  92. this.getCheckedList()
  93. },
  94. deep: true
  95. },
  96. max: {
  97. handler(val) {
  98. if (this.$isEmpty(val) || val === 0) {
  99. this.maxlength = 1 / 0
  100. } else {
  101. this.maxlength = val
  102. }
  103. },
  104. immediate: true,
  105. deep: true
  106. }
  107. },
  108. methods: {
  109. /**
  110. * 勾选默认选中的项
  111. */
  112. getCheckedList() {
  113. const that = this
  114. this.optionsList.forEach((item, index) => {
  115. const findIndex = this.value.findIndex(val => {
  116. if (isObject(val)) {
  117. return item.data[that.valueField] === val[that.valueField]
  118. } else {
  119. return item.data[that.valueField] === val
  120. }
  121. })
  122. if (findIndex !== -1) {
  123. if (that.lastIndex === null) {
  124. that.lastIndex = index
  125. }
  126. item.checked = true
  127. } else {
  128. item.checked = false
  129. }
  130. that.$set(item, 'checked', item.checked)
  131. })
  132. },
  133. /**
  134. * 选中
  135. * @param {Number} index
  136. */
  137. handleSelected(index) {
  138. const item = this.optionsList[index]
  139. item.checked = !item.checked
  140. // 判断已经选择的项是否超出最大选择数量,如果超出则把最后一次选中项的置为未选中
  141. const checkedLen = this.optionsList.filter(o => o.checked).length
  142. if (
  143. index !== this.lastIndex &&
  144. this.lastIndex !== null &&
  145. (checkedLen > this.maxlength)
  146. ) {
  147. const lastItem = this.optionsList[this.lastIndex]
  148. lastItem.checked = false
  149. this.$set(this.optionsList, this.lastIndex, lastItem)
  150. }
  151. if (item.checked) {
  152. this.lastIndex = index
  153. }
  154. this.$set(this.optionsList, index, item)
  155. this.emitSelected()
  156. },
  157. emitSelected() {
  158. const filterRes = this.optionsList.filter(o => o.checked)
  159. console.log('filterRes: ', filterRes)
  160. const res = filterRes.map(o => o.data)
  161. this.$emit('input', res)
  162. this.$emit('change', res)
  163. }
  164. }
  165. }
  166. </script>
  167. <style scoped lang="scss">
  168. .wk-select-list {
  169. .list-item {
  170. padding: 0 30rpx;
  171. background-color: white;
  172. @include left;
  173. .list-item_icon {
  174. width: 38rpx;
  175. height: 38rpx;
  176. border: 1rpx solid #ccc;
  177. border-radius: 50%;
  178. margin-right: 22rpx;
  179. @include center;
  180. .checked-icon {
  181. width: 23rpx;
  182. height: 23rpx;
  183. }
  184. }
  185. .list-item_label {
  186. flex: 1;
  187. font-size: 30rpx;
  188. color: $dark;
  189. border-bottom: 1rpx solid $border-color;
  190. padding: 24rpx 0;
  191. }
  192. &.active {
  193. .list-item_icon {
  194. background-color: $theme-color;
  195. border-color: $theme-color;
  196. }
  197. }
  198. }
  199. }
  200. </style>