index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <template>
  2. <view :class="['lb-picker', inline ? 'lb-picker-inline' : '']">
  3. <view class="lb-picker-mask"
  4. v-show="visible && !inline"
  5. :style="{ 'background-color': maskColor }"
  6. @touchmove.stop.prevent="moveHandle">
  7. </view>
  8. <view :class="['lb-picker-container', visible ? 'lb-picker-toggle' : '']"
  9. :style="{ borderRadius: `${radius} ${radius} 0 0` }">
  10. <view v-if="showHeader"
  11. class="lb-picker-header"
  12. :style="{
  13. height: pickerHeaderHeight,
  14. 'line-height': pickerHeaderHeight
  15. }">
  16. <view class="lb-picker-action lb-picker-left">
  17. <view class="lb-picker-action-cancel"
  18. @tap.stop="handleCancel">
  19. <slot v-if="$slots['cancel-text']"
  20. name="cancel-text"> </slot>
  21. <view v-else
  22. class="action-cancel-text"
  23. :style="{ color: cancelColor }">
  24. {{ cancelText }}
  25. </view>
  26. </view>
  27. </view>
  28. <view class="lb-picker-action lb-picker-center"
  29. v-if="$slots['action-center']">
  30. <slot name="action-center"></slot>
  31. </view>
  32. <view class="lb-picker-action lb-picker-right">
  33. <view class="lb-picker-action-confirm"
  34. @tap.stop="handleConfirm">
  35. <slot v-if="$slots['confirm-text']"
  36. name="confirm-text"> </slot>
  37. <view v-else
  38. class="action-confirm-text"
  39. :style="{ color: confirmColor }">
  40. {{ confirmText }}
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. <view class="lb-picker-content"
  46. :style="{ height: pickerContentHeight }">
  47. <!-- loading -->
  48. <view v-if="loading"
  49. class="lb-picker-loading">
  50. <slot name="loading">
  51. <view class="lb-picker-loading-img"></view>
  52. </slot>
  53. </view>
  54. <!-- 暂无数据 -->
  55. <view v-if="isEmpty && !loading"
  56. class="lb-picker-empty">
  57. <slot name="empty">
  58. <text class="lb-picker-empty-text"
  59. :style="{ color: emptyColor }">
  60. {{ emptyText }}
  61. </text>
  62. </slot>
  63. </view>
  64. <!-- 单选 -->
  65. <selector-picker v-if="mode === 'selector' && !loading && !isEmpty"
  66. :value="22"
  67. :list="list"
  68. :props="pickerProps"
  69. :height="pickerContentHeight"
  70. :inline="inline"
  71. @change="handleChange">
  72. </selector-picker>
  73. <!-- 多列联动 -->
  74. <multi-selector-picker v-if="mode === 'multiSelector' && !loading && !isEmpty"
  75. :value="value"
  76. :list="list"
  77. :level="level"
  78. :visible="visible"
  79. :props="pickerProps"
  80. :height="pickerContentHeight"
  81. :inline="inline"
  82. @change="handleChange">
  83. </multi-selector-picker>
  84. <!-- 非联动选择 -->
  85. <unlinked-selector-picker v-if="mode === 'unlinkedSelector' && !loading && !isEmpty"
  86. :value="value"
  87. :list="list"
  88. :visible="visible"
  89. :props="pickerProps"
  90. :height="pickerContentHeight"
  91. :inline="inline"
  92. @change="handleChange">
  93. </unlinked-selector-picker>
  94. </view>
  95. </view>
  96. </view>
  97. </template>
  98. <script>
  99. const defaultProps = {
  100. label: 'name',
  101. value: 'id',
  102. children: 'children'
  103. }
  104. import { getIndicatorHeight } from './utils'
  105. import SelectorPicker from './pickers/selector-picker'
  106. import MultiSelectorPicker from './pickers/multi-selector-picker'
  107. import UnlinkedSelectorPicker from './pickers/unlinked-selector-picker'
  108. const indicatorHeight = getIndicatorHeight()
  109. export default {
  110. components: {
  111. SelectorPicker,
  112. MultiSelectorPicker,
  113. UnlinkedSelectorPicker
  114. },
  115. props: {
  116. value: [String, Number, Array],
  117. list: Array,
  118. mode: {
  119. type: String,
  120. default: 'selector'
  121. },
  122. level: {
  123. type: Number,
  124. default: 1
  125. },
  126. props: {
  127. type: Object
  128. },
  129. cancelText: {
  130. type: String,
  131. default: '取消'
  132. },
  133. cancelColor: String,
  134. confirmText: {
  135. type: String,
  136. default: '确定'
  137. },
  138. confirmColor: String,
  139. canHide: {
  140. type: Boolean,
  141. default: true
  142. },
  143. emptyColor: String,
  144. emptyText: {
  145. type: String,
  146. default: '暂无数据'
  147. },
  148. radius: String,
  149. columnNum: {
  150. type: Number,
  151. default: 5
  152. },
  153. loading: Boolean,
  154. closeOnClickMask: {
  155. type: Boolean,
  156. default: true
  157. },
  158. maskColor: {
  159. type: String,
  160. default: 'rgba(0, 0, 0, 0.4)'
  161. },
  162. dataset: Object,
  163. inline: Boolean,
  164. showHeader: {
  165. type: Boolean,
  166. default: true
  167. }
  168. },
  169. data () {
  170. return {
  171. visible: false,
  172. myValue: this.value,
  173. picker: {},
  174. pickerProps: Object.assign({}, defaultProps, this.props),
  175. pickerHeaderHeight: indicatorHeight + 'px',
  176. pickerContentHeight: indicatorHeight * this.columnNum + 'px'
  177. }
  178. },
  179. computed: {
  180. isEmpty () {
  181. if (!this.list) return true
  182. if (this.list && !this.list.length) return true
  183. return false
  184. }
  185. },
  186. methods: {
  187. show () {
  188. if (this.inline) return
  189. this.visible = true
  190. },
  191. hide () {
  192. if (this.inline) return
  193. this.visible = false
  194. },
  195. handleCancel () {
  196. console.log(11)
  197. this.$emit('cancel', this.picker)
  198. if (this.canHide && !this.inline) {
  199. this.hide()
  200. }
  201. },
  202. handleConfirm () {
  203. if (this.isEmpty) {
  204. this.$emit('confirm', null)
  205. this.hide()
  206. } else {
  207. const picker = JSON.parse(JSON.stringify(this.picker))
  208. console.log(this.picker,'confirm')
  209. this.myValue = picker.value
  210. this.$emit('confirm', this.picker)
  211. if (this.canHide) this.hide()
  212. }
  213. },
  214. handleChange ({ value, item, index, change }) {
  215. this.picker.value = value
  216. this.picker.item = item
  217. this.picker.index = index
  218. this.picker.change = change
  219. this.picker.dataset = this.dataset || {}
  220. this.$emit('change', this.picker)
  221. },
  222. // handleMaskTap () {
  223. // if (this.closeOnClickMask) {
  224. // this.visible = false
  225. // }
  226. // },
  227. moveHandle () {}
  228. },
  229. watch: {
  230. value (newVal) {
  231. this.myValue = newVal
  232. },
  233. myValue (newVal) {
  234. this.$emit('input', newVal)
  235. },
  236. visible (newVisible) {
  237. if (newVisible) {
  238. this.$emit('show')
  239. } else {
  240. this.$emit('hide')
  241. }
  242. }
  243. }
  244. }
  245. </script>
  246. <style lang="scss" scoped>
  247. @import "./style/picker.scss";
  248. </style>