index.vue 6.6 KB

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