index.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. Component({
  2. options: {
  3. addGlobalClass: true,
  4. },
  5. properties: {
  6. show: Boolean,
  7. title: String,
  8. options: {
  9. type: Object,
  10. observer() {
  11. this.init();
  12. },
  13. },
  14. multiple: {
  15. type: Boolean,
  16. observer() {
  17. this.init();
  18. },
  19. },
  20. showConfirmButton: Boolean,
  21. showCloseButton: Boolean,
  22. confirmButtonText: {
  23. type: String,
  24. value: '确定',
  25. },
  26. cancelButtonText: {
  27. type: String,
  28. value: '取消',
  29. },
  30. emptyTip: {
  31. type: String,
  32. value: '请选择',
  33. },
  34. },
  35. data: {
  36. _options: [],
  37. checkedIndexes: [],
  38. },
  39. methods: {
  40. attached() {
  41. this.toast = this.selectComponent('#t-toast');
  42. },
  43. init() {
  44. const checkedIndexes = [];
  45. const _options = this.properties.options.map((opt, i) => {
  46. const checked = !!opt.checked;
  47. if (checked) {
  48. if (this.properties.multiple) checkedIndexes[0] = i;
  49. else checkedIndexes.push(i);
  50. }
  51. return {
  52. title: opt.title,
  53. checked,
  54. };
  55. });
  56. this.setData({ checkedIndexes, _options });
  57. },
  58. onOptionTap(e) {
  59. const { index } = e.currentTarget.dataset;
  60. const { checkedIndexes } = this.data;
  61. let data = {};
  62. if (this.properties.multiple) {
  63. if (checkedIndexes.includes(index)) {
  64. checkedIndexes.splice(index, 1);
  65. data = { checkedIndexes, [`_options[${index}].checked`]: false };
  66. } else {
  67. checkedIndexes.push(index);
  68. data = { checkedIndexes, [`_options[${index}].checked`]: true };
  69. }
  70. } else {
  71. if (checkedIndexes[0] === index) {
  72. // 单选不可取消选择
  73. return;
  74. }
  75. data = {
  76. [`_options[${index}].checked`]: true,
  77. checkedIndexes: [index],
  78. };
  79. if (checkedIndexes[0] !== undefined) {
  80. data[`_options[${checkedIndexes[0]}].checked`] = false;
  81. }
  82. }
  83. this.setData(data);
  84. this.triggerEvent('select', { index });
  85. this._onOptionTap && this._onOptionTap(index);
  86. if (!this.properties.showConfirmButton && !this.properties.multiple) {
  87. // 没有确认按钮且是单选的情况下,选择选项则自动确定
  88. this._onConfirm && this._onConfirm([index]);
  89. this.setData({ show: false });
  90. }
  91. },
  92. onCancel() {
  93. this.triggerEvent('cancel');
  94. this._onCancel && this._onCancel();
  95. this.setData({ show: false });
  96. },
  97. onConfirm() {
  98. if (this.data.checkedIndexes.length === 0) {
  99. this.toast.show({
  100. icon: '',
  101. text: this.properties.emptyTip,
  102. });
  103. return;
  104. }
  105. const indexed = this.data.checkedIndexes;
  106. this.triggerEvent('confirm', { indexed });
  107. this._onConfirm && this._onConfirm(indexed);
  108. this.setData({ show: false });
  109. },
  110. },
  111. });