index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import Vue from "vue";
  2. import picker from "./picker";
  3. let BoxConstructor = Vue.extend(picker)
  4. let $picker = function (columns, config) {
  5. let defaultConfig = Object.assign({
  6. title: this.$t('common.select')
  7. }, config)
  8. return new Promise((res, err) => {
  9. let instance = new BoxConstructor({
  10. el: document.createElement('div'),
  11. data() {
  12. return {
  13. show: false,
  14. columns: [
  15. {
  16. values: columns.map(item => item.label),
  17. defaultIndex: columns.findIndex(item => item.value == defaultConfig.value) || 0
  18. }
  19. ],
  20. title: defaultConfig.title
  21. }
  22. },
  23. methods: {
  24. close() {
  25. this.show = false
  26. let $el = instance.$el
  27. setTimeout(() => {
  28. instance.$destroy()
  29. if ($el.parentNode) {
  30. $el.parentNode.removeChild($el)
  31. }
  32. }, 600)
  33. },
  34. input(boo) {
  35. if (boo) {
  36. this.show = boo
  37. } else {
  38. this.close()
  39. }
  40. },
  41. onConfirm(value, index) {
  42. this.close()
  43. res(columns[index].value, value)
  44. },
  45. onCancel() {
  46. this.close()
  47. err()
  48. },
  49. onChange() { }
  50. },
  51. mounted() {
  52. this.$nextTick(() => {
  53. this.show = true
  54. })
  55. },
  56. })
  57. document.body.appendChild(instance.$el);
  58. })
  59. }
  60. export {
  61. $picker
  62. }