index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { VantComponent } from '../common/component';
  2. VantComponent({
  3. field: true,
  4. relation: {
  5. name: 'dropdown-menu',
  6. type: 'ancestor',
  7. linked(target) {
  8. this.parent = target;
  9. this.updateDataFromParent();
  10. },
  11. unlinked() {
  12. this.parent = null;
  13. }
  14. },
  15. props: {
  16. value: {
  17. type: null,
  18. observer: 'rerender'
  19. },
  20. title: {
  21. type: String,
  22. observer: 'rerender'
  23. },
  24. disabled: Boolean,
  25. titleClass: {
  26. type: String,
  27. observer: 'rerender'
  28. },
  29. options: {
  30. type: Array,
  31. value: [],
  32. observer: 'rerender'
  33. }
  34. },
  35. data: {
  36. transition: true,
  37. showPopup: false,
  38. showWrapper: false,
  39. displayTitle: ''
  40. },
  41. methods: {
  42. rerender() {
  43. wx.nextTick(() => {
  44. this.parent && this.parent.updateItemListData();
  45. });
  46. },
  47. updateDataFromParent() {
  48. if (this.parent) {
  49. const { overlay, duration, activeColor, closeOnClickOverlay, direction } = this.parent.data;
  50. this.setData({
  51. overlay,
  52. duration,
  53. activeColor,
  54. closeOnClickOverlay,
  55. direction
  56. });
  57. }
  58. },
  59. onClickOverlay() {
  60. this.toggle();
  61. this.$emit('close');
  62. },
  63. onOptionTap(event) {
  64. const { option } = event.currentTarget.dataset;
  65. const { value } = option;
  66. const shouldEmitChange = this.data.value !== value;
  67. this.setData({ showPopup: false, value });
  68. setTimeout(() => {
  69. this.setData({ showWrapper: false });
  70. }, this.data.duration || 0);
  71. this.rerender();
  72. if (shouldEmitChange) {
  73. this.$emit('change', value);
  74. }
  75. },
  76. toggle(show, options = {}) {
  77. const { showPopup, duration } = this.data;
  78. if (show == null) {
  79. show = !showPopup;
  80. }
  81. if (show === showPopup) {
  82. return;
  83. }
  84. if (!show) {
  85. const time = options.immediate ? 0 : duration;
  86. this.setData({ transition: !options.immediate, showPopup: show });
  87. setTimeout(() => {
  88. this.setData({ showWrapper: false });
  89. }, time);
  90. this.rerender();
  91. return;
  92. }
  93. this.parent.getChildWrapperStyle().then((wrapperStyle = '') => {
  94. this.setData({
  95. transition: !options.immediate,
  96. showPopup: show,
  97. wrapperStyle,
  98. showWrapper: true
  99. });
  100. this.rerender();
  101. });
  102. }
  103. }
  104. });