message-select.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // module/chats/components/message-select/message-select.ts
  2. interface Option {
  3. id: string;
  4. name: string;
  5. checked?: boolean;
  6. options: Option[] | null;
  7. }
  8. interface HandleEvent {
  9. mark: {
  10. type: 'sub' | 'options';
  11. index: number;
  12. item: Option;
  13. }
  14. }
  15. Component({
  16. properties: {
  17. payload: { type: Object, value: { title: '', multiple: false, options: [] }, result: '' },
  18. active: { type: Boolean, value: false },
  19. },
  20. data: {
  21. options: [] as Option[],
  22. subOptions: [] as Option[],
  23. subMultiple: false,
  24. itemHeight: 48,
  25. result: '', hasSelected: false,
  26. },
  27. observers: {
  28. 'payload.options'(options) {
  29. this.setData({ options });
  30. },
  31. 'options'(options: AnyArray) {
  32. this.setData({ hasSelected: options.some(option => option.checked) });
  33. }
  34. },
  35. methods: {
  36. handleTop(event: HandleEvent) {
  37. const { mark: { item } } = event;
  38. if (!item || !this.data.active) return;
  39. const index = this.data.options.findIndex(option=>option.id === item.id);
  40. const multiple = this.data.payload.multiple;
  41. const checked = !item.checked;
  42. const options = this._handle(this.data.options, item, index, multiple);
  43. this.setData({ options });
  44. if (checked && !multiple) { this.onSubmit(); }
  45. },
  46. handleSub(event: HandleEvent) {
  47. const { mark: { item } } = event;
  48. if (!item || !this.data.active) return;
  49. const index = this.data.subOptions.findIndex(option=>option.id === item.id);
  50. const multiple = this.data.subMultiple;
  51. const checked = !item.checked;
  52. const subOptions = this._handle(this.data.subOptions, item, index, multiple);
  53. this.setData({ subOptions });
  54. if (checked && !multiple) { this.onConfirm(); }
  55. },
  56. _handle(options: Option[], item: Option, index: number, multiple?: boolean) {
  57. const checked = !item.checked;
  58. if (checked) {
  59. const fn = () => {
  60. if (multiple) {
  61. options[index].checked = checked;
  62. } else {
  63. options.forEach(option => { option.checked = option.id === item.id; })
  64. }
  65. return options;
  66. }
  67. if (item.options?.filter(option => !(<any>option)?.hide).length) {
  68. this.setData({
  69. subTitle: item.name,
  70. subOptions: item.options,
  71. subMultiple: (<any>item).css === 'checkbox'
  72. })
  73. this.onCancel = () => {
  74. this.setData({ subOptions: [] })
  75. }
  76. this.onConfirm = () => {
  77. if (!this.data.subOptions.some(option => option.checked)) {
  78. wx.showToast({ title: '请至少选择一项', icon: 'error' })
  79. } else {
  80. const options = fn();
  81. options[index].options = this.data.subOptions;
  82. this.setData({ subOptions: [], options })
  83. }
  84. }
  85. } else {
  86. return fn();
  87. }
  88. } else {
  89. options[index].checked = !item.checked;
  90. if (item.options) options[index].options = item.options.map(option => {
  91. if (!(<any>option)?.hide) option.checked = !item.checked;
  92. return option;
  93. })
  94. }
  95. return options;
  96. },
  97. onCancel() { },
  98. onConfirm() { },
  99. onSubmit() {
  100. if (this.data.result) return;
  101. if (!this.data.hasSelected) {
  102. wx.showToast({ title: '请至少选择一项', icon: 'error' })
  103. } else {
  104. console.log(this.data.options);
  105. const result = this.data.options
  106. .filter((item: any) => item?.checked && !item?.hide)
  107. .map((option: any) => {
  108. const sub = option.options
  109. ?.filter((item: any) => item?.checked && !item?.hide)
  110. .map((item: any) => item.name)
  111. .join(', ');
  112. return [option.name, sub].filter(Boolean).join(': ')
  113. })
  114. .join('; ');
  115. this.setData({ result })
  116. this.triggerEvent('next', { options: this.data.options });
  117. }
  118. },
  119. onSkip() {
  120. if (this.data.result || this.data.hasSelected) return;
  121. if (!this.data.payload.required) {
  122. this.setData({ result: '都没有' });
  123. this.triggerEvent('next', { options: this.data.options });
  124. }
  125. }
  126. }
  127. })