index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { getColumns, isObject, isFunction } from '../utils'
  2. export const commonMixin = {
  3. data () {
  4. return {
  5. isConfirmChange: false,
  6. indicatorStyle: `height: 34px`,
  7. pressTimeout: null
  8. }
  9. },
  10. created () {
  11. this.init('init')
  12. },
  13. methods: {
  14. init (changeType) {
  15. if (this.list && this.list.length) {
  16. const column = getColumns({
  17. value: this.value,
  18. list: this.list,
  19. mode: this.mode,
  20. props: this.props,
  21. level: this.level
  22. })
  23. const { columns, value, item, index } = column
  24. this.selectValue = value
  25. this.selectItem = item
  26. this.pickerColumns = columns
  27. this.pickerValue = index
  28. this.$emit('change', {
  29. value: this.selectValue,
  30. item: this.selectItem,
  31. index: this.pickerValue,
  32. change: changeType
  33. })
  34. }
  35. },
  36. touchstart (e) {
  37. if (!this.pressEnable) return
  38. clearTimeout(this.pressTimeout)
  39. this.pressTimeout = setTimeout(() => {
  40. let item = {}
  41. let toastTitle = ''
  42. // #ifdef APP-NVUE
  43. item = e.target.dataset.item
  44. // #endif
  45. // #ifdef H5
  46. item = JSON.parse(e.currentTarget.dataset.item)
  47. // #endif
  48. // #ifndef APP-NVUE || H5
  49. item = e.currentTarget.dataset.item
  50. // #endif
  51. // #ifdef APP-PLUS || H5
  52. toastTitle = this.getLabel(item)
  53. // #endif
  54. // #ifndef APP-PLUS || H5
  55. toastTitle = item[this.props.label] || item
  56. // #endif
  57. uni.showToast({
  58. title: toastTitle,
  59. icon: 'none'
  60. })
  61. }, this.pressTime)
  62. },
  63. touchmove () {
  64. if (!this.pressEnable) return
  65. clearTimeout(this.pressTimeout)
  66. },
  67. touchend () {
  68. if (!this.pressEnable) return
  69. clearTimeout(this.pressTimeout)
  70. },
  71. getLabel (item, rowIndex, columnIndex) {
  72. if (this.formatter && isFunction(this.formatter)) {
  73. return this.formatter({ item, rowIndex, columnIndex })
  74. } else {
  75. return item[this.props.label] || item
  76. }
  77. }
  78. },
  79. watch: {
  80. value () {
  81. if (!this.isConfirmChange) {
  82. this.init('value')
  83. }
  84. },
  85. list () {
  86. this.init('list')
  87. }
  88. }
  89. }