index.js 2.4 KB

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