message-select.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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: '', belongNew: null },
  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. leftTitle: '都没有',
  27. rightTitle: '提交'
  28. },
  29. lifetimes: {
  30. attached() {
  31. console.log("attached",this.data.payload.belongNew)
  32. if(this.data.payload.belongNew){
  33. this.setData({
  34. leftTitle: '都没有',
  35. rightTitle: '提交'
  36. })
  37. }else{
  38. this.setData({
  39. leftTitle: '无变化',
  40. rightTitle: '完成'
  41. })
  42. }
  43. }
  44. },
  45. observers: {
  46. 'payload.options'(options) {
  47. this.setData({ options });
  48. },
  49. 'options'(options: AnyArray) {
  50. this.setData({ hasSelected: options.filter(option => !option.hide).some(option => option.checked) });
  51. }
  52. },
  53. methods: {
  54. handleTop(event: HandleEvent) {
  55. const { mark: { item } } = event;
  56. if (!item || !this.data.active) return;
  57. console.log("handleTop",item)
  58. const index = this.data.options.findIndex(option => option.id === item.id);
  59. const multiple = this.data.payload.multiple;
  60. const checked = !item.checked;
  61. const options = this._handle(this.data.options, item, index, multiple);
  62. this.setData({ options });
  63. if (checked && !multiple) { this.onSubmit(); }
  64. },
  65. handleSub(event: HandleEvent) {
  66. const { mark: { item } } = event;
  67. if (!item || !this.data.active) return;
  68. const index = this.data.subOptions.findIndex(option => option.id === item.id);
  69. const multiple = this.data.subMultiple;
  70. const checked = !item.checked;
  71. const subOptions = this._handle(this.data.subOptions, item, index, multiple);
  72. this.setData({ subOptions });
  73. if (checked && !multiple) { this.onConfirm(); }
  74. },
  75. _handle(options: Option[], item: Option, index: number, multiple?: boolean) {
  76. const checked = !item.checked;
  77. if (checked) {
  78. const fn = () => {
  79. if (multiple) {
  80. options[index].checked = checked;
  81. } else {
  82. options.forEach(option => { option.checked = option.id === item.id; })
  83. }
  84. return options;
  85. }
  86. if (item.options?.filter(option => !(<any>option)?.hide).length) {
  87. this.setData({
  88. subTitle: item.name,
  89. subOptions: item.options,
  90. subMultiple: (<any>item).css === 'checkbox'
  91. })
  92. this.onCancel = () => {
  93. this.setData({ subOptions: [] })
  94. }
  95. this.onConfirm = () => {
  96. if (!this.data.subOptions.some(option => option.checked)) {
  97. wx.showToast({ title: '请至少选择一项', icon: 'error' })
  98. } else {
  99. const options = fn();
  100. options[index].options = this.data.subOptions;
  101. this.setData({ subOptions: [], options })
  102. }
  103. }
  104. } else {
  105. return fn();
  106. }
  107. } else {
  108. options[index].checked = !item.checked;
  109. if (item.options) options[index].options = item.options.map(option => {
  110. if (!(<any>option)?.hide) option.checked = !item.checked;
  111. return option;
  112. })
  113. }
  114. return options;
  115. },
  116. onCancel() { },
  117. onConfirm() { },
  118. onSubmit() {
  119. if (this.data.result) return;
  120. if (!this.data.hasSelected) {
  121. wx.showToast({ title: '请至少选择一项', icon: 'error' })
  122. } else {
  123. console.log(this.data.options);
  124. const result = this.data.options
  125. .filter((item: any) => item?.checked && !item?.hide)
  126. .map((option: any) => {
  127. const sub = option.options
  128. ?.filter((item: any) => item?.checked && !item?.hide)
  129. .map((item: any) => item.name)
  130. .join(', ');
  131. return [option.name, sub].filter(Boolean).join(': ')
  132. })
  133. .join('; ');
  134. this.setData({ result });
  135. this.triggerEvent('next', { options: this.data.options });
  136. }
  137. },
  138. onSkip() {
  139. if (this.data.result || this.data.hasSelected) return;
  140. if (!this.data.payload.required) {
  141. this.setData({ result: '都没有' });
  142. this.triggerEvent('next', { options: this.data.options });
  143. }
  144. }
  145. }
  146. })