index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import baseComponent from '../helpers/baseComponent'
  2. import { notFoundContent, getNotFoundContent, getSelectIndex, flattenOptions } from '../popup-select/utils'
  3. const defaults = {
  4. prefixCls: 'wux-select',
  5. value: '',
  6. options: [],
  7. multiple: false,
  8. max: -1,
  9. notFoundContent,
  10. virtualized: false,
  11. toolbar: {
  12. title: '请选择',
  13. cancelText: '取消',
  14. confirmText: '确定',
  15. },
  16. onChange() {},
  17. onConfirm() {},
  18. onCancel() {},
  19. }
  20. function runCallbacks(method, values, vm) {
  21. const { value } = values
  22. const { options, multiple } = vm.data
  23. const mergedOptions = flattenOptions(options)
  24. const index = getSelectIndex(mergedOptions, value, multiple)
  25. if (typeof vm.fns[method] === 'function') {
  26. vm.fns[method].call(vm, value, index, mergedOptions)
  27. }
  28. }
  29. baseComponent({
  30. useFunc: true,
  31. data: defaults,
  32. methods: {
  33. /**
  34. * 打开
  35. */
  36. open(opts = {}) {
  37. const options = this.$$mergeOptionsAndBindMethods(Object.assign({}, defaults, opts, {
  38. max: opts.max ? parseInt(opts.max) : -1,
  39. notFoundContent: getNotFoundContent(opts.notFoundContent),
  40. }))
  41. this.$$setData({ visible: true, ...options })
  42. },
  43. /**
  44. * 关闭
  45. */
  46. close(callback) {
  47. this.select = this.select || this.querySelector('#wux-popup-select')
  48. this.select && this.select.close(callback)
  49. },
  50. /**
  51. * 点击确定按钮时的回调函数
  52. */
  53. onConfirm(e) {
  54. return runCallbacks('onConfirm', e.detail, this)
  55. },
  56. /**
  57. * 点击取消按钮时的回调函数
  58. */
  59. onCancel(e) {
  60. return runCallbacks('onCancel', e.detail, this)
  61. },
  62. /**
  63. * 每列数据选择变化后的回调函数
  64. */
  65. onValueChange(e) {
  66. return runCallbacks('onChange', e.detail, this)
  67. },
  68. /**
  69. * 当显隐状态变化时回调函数
  70. */
  71. onVisibleChange(e) {
  72. this.$$setData({ visible: e.detail.visible })
  73. },
  74. },
  75. })