dropmenu-item.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // components/dropmenu/dropmenu.ts
  2. Component<
  3. {
  4. label: string
  5. value: string
  6. show: boolean
  7. },
  8. {
  9. options: ComponentPropertyArrayType
  10. defaultValue: ComponentPropertyStringType
  11. },
  12. {
  13. onShowClick(e: WechatMiniprogram.BaseEvent): void
  14. onOptionClick(e: WechatMiniprogram.BaseEvent): void
  15. setShow(e: boolean): void
  16. },
  17. {
  18. parent: CustomComponent
  19. currentActive: number
  20. }
  21. >({
  22. relations: {
  23. './dropmenu': {
  24. type: 'parent',
  25. linked(parent: CustomComponent) {
  26. this.parent = parent
  27. },
  28. },
  29. },
  30. options: {
  31. addGlobalClass: true,
  32. multipleSlots: true,
  33. },
  34. /**
  35. * 组件的属性列表
  36. */
  37. properties: {
  38. options: {
  39. type: Array,
  40. value: [],
  41. },
  42. defaultValue: {
  43. type: String,
  44. value: '',
  45. },
  46. },
  47. /**
  48. * 组件的初始数据
  49. */
  50. data: {
  51. label: '',
  52. value: '',
  53. show: false,
  54. },
  55. observers: {
  56. 'options, defaultValue': function (options: WxOptionStringDto[], defaultValue) {
  57. this.setData({
  58. label: options.find((o) => o.value === defaultValue)?.label,
  59. value: defaultValue,
  60. })
  61. },
  62. },
  63. /**
  64. * 组件的方法列表
  65. */
  66. methods: {
  67. setShow(state: boolean) {
  68. console.log('hit show')
  69. if (state !== this.data.show) {
  70. this.setData({
  71. show: state,
  72. })
  73. }
  74. },
  75. onShowClick() {
  76. if (this.parent) {
  77. const idx = this.parent.children.indexOf(this)
  78. this.parent.updateChildren(this.data.show ? -1 : idx)
  79. if(idx > -1) {
  80. console.log(this)
  81. }
  82. }
  83. },
  84. onOptionClick(e: WechatMiniprogram.BaseEvent<{}, { value: string; label: string }>) {
  85. const { value, label } = e.currentTarget.dataset
  86. this.setData({
  87. show: false,
  88. value,
  89. label,
  90. })
  91. this.triggerEvent('change', { label, value })
  92. },
  93. },
  94. })