index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <div class="scope-component-group">
  3. <div
  4. class="scope-item"
  5. :class="{ active: item.checked, disabled: item.disabled }"
  6. v-for="(item, index) in getOptions"
  7. :key="index"
  8. @click="onCellClick(item)"
  9. >
  10. <div class="scope-item-label">
  11. <span class="item-label">{{ item.label }}</span>
  12. <span v-if="item.vip && item.disabled" class="v-vip-icon"></span>
  13. <slot :item="item"></slot>
  14. </div>
  15. <div class="scope-item-action">
  16. <span class="jy-tag" v-if="item.tag && item.disabled">{{
  17. item.tag
  18. }}</span>
  19. <van-checkbox :value="item.checked" v-else>
  20. <template #icon>
  21. <slot name="icon" :item="item">
  22. <AppIcon
  23. :name="item.checked ? checkedIcon : defaultIcon"
  24. size="22"
  25. :color="item.checked ? checkedColor : defaultColor"
  26. />
  27. </slot>
  28. </template>
  29. </van-checkbox>
  30. </div>
  31. </div>
  32. </div>
  33. </template>
  34. <script>
  35. import { Checkbox } from 'vant'
  36. import { AppIcon } from '@/ui'
  37. export default {
  38. name: 'scopeSelectorContent',
  39. components: {
  40. [Checkbox.name]: Checkbox,
  41. AppIcon
  42. },
  43. props: {
  44. /**
  45. * 数据源
  46. */
  47. options: {
  48. type: Array,
  49. default() {
  50. return []
  51. }
  52. },
  53. /**
  54. * 是否多选
  55. */
  56. multiple: {
  57. type: Boolean,
  58. default: true
  59. },
  60. /**
  61. * 最小选中数量
  62. */
  63. minSelect: {
  64. type: Number,
  65. default: 1
  66. },
  67. /**
  68. * 默认选中的数据(不同场景默认数据可能不一致)
  69. */
  70. defaultVal: {
  71. type: Array,
  72. default() {
  73. return []
  74. }
  75. },
  76. /**
  77. * 未选中展示的icon 默认为unchoose1(灰色圆) 传入AppIcon的name 传空即不展示
  78. */
  79. defaultIcon: {
  80. type: String,
  81. default: 'unchoose1'
  82. },
  83. /**
  84. * 选中的icon图标 默认为choose1(圆形checkbox)传入AppIcon的name
  85. */
  86. checkedIcon: {
  87. type: String,
  88. default: 'choose1'
  89. },
  90. /**
  91. * 选中图标的颜色
  92. */
  93. checkedColor: {
  94. type: String,
  95. default: '#2ABED1'
  96. },
  97. /**
  98. * 默认图标的颜色
  99. */
  100. defaultColor: {
  101. type: String,
  102. default: '#C0C4CC'
  103. }
  104. },
  105. computed: {
  106. getCheckedValue() {
  107. return this.getOptions.filter((v) => v.checked).map((s) => s.key)
  108. }
  109. },
  110. data() {
  111. return {
  112. value: this.defaultVal,
  113. getOptions: this.options
  114. }
  115. },
  116. watch: {
  117. options() {
  118. this.initData(this.options)
  119. }
  120. },
  121. created() {
  122. this.initData(this.getOptions)
  123. },
  124. methods: {
  125. initData(data = []) {
  126. const list = JSON.parse(JSON.stringify(data))
  127. list.forEach((v) => {
  128. v.checked = false
  129. })
  130. this.getOptions = list
  131. if (this.value.length) {
  132. this.setState(this.value)
  133. }
  134. },
  135. setState(data) {
  136. if (!data) return
  137. const list = this.getOptions
  138. list.forEach((v) => {
  139. if (data.indexOf(v.key) > -1) {
  140. v.checked = true
  141. } else {
  142. v.checked = false
  143. }
  144. })
  145. this.value = data
  146. },
  147. getState() {
  148. return this.getCheckedValue
  149. },
  150. onCellClick(item) {
  151. if (item.disabled) {
  152. this.$emit('open-tag', item)
  153. } else {
  154. if (this.multiple) {
  155. if (item.checked) {
  156. const canCancelSelect =
  157. this.getOptions.filter((v) => v.checked).length > this.minSelect
  158. if (canCancelSelect) {
  159. item.checked = !item.checked
  160. }
  161. } else {
  162. item.checked = !item.checked
  163. if (item.label === '全部') {
  164. this.getOptions.forEach((v) => {
  165. v.checked = false
  166. })
  167. item.checked = !item.checked
  168. } else {
  169. this.getOptions.forEach((v) => {
  170. if (v.label === '全部') {
  171. v.checked = false
  172. }
  173. })
  174. }
  175. }
  176. } else {
  177. this.getOptions.forEach((v) => {
  178. v.checked = false
  179. })
  180. item.checked = true
  181. }
  182. }
  183. /**
  184. * 点击事件 (item, values)
  185. * @event change
  186. */
  187. this.$emit('change', {
  188. item,
  189. value: this.getCheckedValue
  190. })
  191. }
  192. }
  193. }
  194. </script>
  195. <style lang="scss" scoped>
  196. .scope-component-group {
  197. .scope-item {
  198. position: relative;
  199. display: flex;
  200. align-items: center;
  201. justify-content: space-between;
  202. padding: 12px 16px;
  203. font-size: 14px;
  204. font-weight: 500;
  205. color: $gray_7;
  206. line-height: 20px;
  207. box-sizing: border-box;
  208. &.active {
  209. color: $main;
  210. }
  211. &:not(:last-child)::after {
  212. position: absolute;
  213. content: '';
  214. bottom: 0;
  215. left: 16px;
  216. width: 100%;
  217. height: 1px;
  218. background: $border_color_3;
  219. }
  220. .v-vip-icon{
  221. display: inline-block;
  222. width: 14px;
  223. height: 14px;
  224. margin-left: 4px;
  225. background: url(@/assets/image/icon/vip/v_icon.png) no-repeat center;
  226. background-size: contain;
  227. margin-bottom: 2px;
  228. }
  229. }
  230. ::v-deep {
  231. .van-checkbox__icon {
  232. height: 1.1em;
  233. line-height: 1.1em;
  234. }
  235. }
  236. }
  237. </style>