index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. getCustomItems
  3. } from "../../../../../utils/customItemType";
  4. Component({
  5. properties: {
  6. topHei: {
  7. type: String,
  8. value: "20rpx"
  9. },
  10. onChange: {
  11. type: Function
  12. }
  13. },
  14. options: {
  15. multipleSlots: true
  16. },
  17. externalClasses: ["external-custom-box", "external-custom-label", "external-custom-stepper-box", "external-explain", "external-active", "external-custom-option"],
  18. data: {
  19. list: [],
  20. focus: false,
  21. isSubmit: false
  22. },
  23. methods: {
  24. init(data, prefix = '') {
  25. return new Promise((resolve) => {
  26. getCustomItems(data, prefix).then(list => {
  27. this.setData({
  28. list
  29. });
  30. this.getResult();
  31. resolve(list.length);
  32. });
  33. });
  34. },
  35. onOption(e) {
  36. const {
  37. index,
  38. value
  39. } = e.currentTarget.dataset;
  40. if (this.data.list[index].value == value) return;
  41. this.setData({
  42. [`list[${index}].value`]: value
  43. });
  44. this.getResult();
  45. },
  46. onFocus(e) {
  47. this.data.focus = true;
  48. },
  49. getResult(isSubmit = false) {
  50. if (isSubmit && this.data.focus) {
  51. this.data.isSubmit = isSubmit;
  52. return "wait"
  53. } else {
  54. let obj = {};
  55. let breakOff = false;
  56. this.data.list.filter(v => {
  57. if (breakOff) return;
  58. if (v.type == '自定义') {
  59. if (!v.value) v.value = 0;
  60. if (v.value < v.min) v.value = v.min;
  61. }
  62. obj[v.key] = v.value || '';
  63. if (isSubmit && !obj[v.key]) {
  64. wx.showToast({
  65. title: `请完成定制"${v.label}"选项`,
  66. icon: "none"
  67. })
  68. breakOff = true;
  69. }
  70. })
  71. this.triggerEvent("onChange", breakOff ? false : obj)
  72. this.data.isSubmit = false;
  73. return breakOff ? false : obj
  74. }
  75. },
  76. /* 定制步进器 */
  77. changeStepper(e) {
  78. const {
  79. index,
  80. item
  81. } = e.currentTarget.dataset;
  82. if (!item.value) item.value = 0;
  83. if (e.type == 'plus') {
  84. item.value += 1
  85. } else if (e.type == 'minus') {
  86. item.value -= 1
  87. } else {
  88. item.value = (e.detail.value - 0).toFixed(item.decimalplaces);
  89. this.data.focus = false;
  90. }
  91. item.value = item.value - 0;
  92. if (item.value > item.max) item.value = item.max;
  93. if (item.value < item.min) item.value = item.min;
  94. this.setData({
  95. [`list[${index}]`]: item
  96. })
  97. this.getResult(this.data.isSubmit);
  98. },
  99. }
  100. })