index.js 2.8 KB

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