index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. console.log("list",list)
  28. this.setData({
  29. list
  30. });
  31. this.getResult();
  32. resolve(list.length);
  33. });
  34. });
  35. },
  36. onOption(e) {
  37. const {
  38. index,
  39. value
  40. } = e.currentTarget.dataset;
  41. if (this.data.list[index].value == value) return;
  42. this.setData({
  43. [`list[${index}].value`]: value
  44. });
  45. this.getResult();
  46. },
  47. onFocus(e) {
  48. this.data.focus = true;
  49. },
  50. getResult(isSubmit = false) {
  51. if (isSubmit && this.data.focus) {
  52. this.data.isSubmit = isSubmit;
  53. return "wait"
  54. } else {
  55. let obj = {};
  56. let breakOff = false;
  57. this.data.list.filter(v => {
  58. if (breakOff) return;
  59. if (v.type == '自定义') {
  60. if (!v.value) v.value = 0;
  61. if (v.value < v.min) v.value = v.min;
  62. }
  63. obj[v.key] = v.value || '';
  64. if (isSubmit && !obj[v.key]) {
  65. wx.showToast({
  66. title: `请完成定制"${v.label}"选项`,
  67. icon: "none"
  68. })
  69. breakOff = true;
  70. }
  71. })
  72. this.triggerEvent("onChange", breakOff ? false : obj)
  73. this.data.isSubmit = false;
  74. return breakOff ? false : obj
  75. }
  76. },
  77. /* 定制步进器 */
  78. changeStepper(e) {
  79. const {
  80. index,
  81. item
  82. } = e.currentTarget.dataset;
  83. if (!item.value) item.value = 0;
  84. if (e.type == 'plus') {
  85. item.value += 1
  86. } else if (e.type == 'minus') {
  87. item.value -= 1
  88. } else {
  89. item.value = (e.detail.value - 0).toFixed(item.decimalplaces);
  90. this.data.focus = false;
  91. }
  92. item.value = item.value - 0;
  93. if (item.value > item.max) item.value = item.max;
  94. if (item.value < item.min) item.value = item.min;
  95. this.setData({
  96. [`list[${index}]`]: item
  97. })
  98. this.getResult(this.data.isSubmit);
  99. },
  100. }
  101. })