index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 || H5
  52. toastTitle = this.getLabel(item)
  53. // #endif
  54. // #ifndef APP || 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. }
  75. return item[this.props.label] || item
  76. }
  77. },
  78. watch: {
  79. value() {
  80. if (!this.isConfirmChange) {
  81. this.init('value')
  82. }
  83. },
  84. list() {
  85. this.init('list')
  86. }
  87. }
  88. }