index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. },
  21. methods: {
  22. init(data, prefix = '') {
  23. return new Promise((resolve) => {
  24. getCustomItems(data, prefix).then(list => {
  25. console.log("list", list)
  26. this.setData({
  27. list
  28. });
  29. this.getResult();
  30. resolve(list.length);
  31. });
  32. });
  33. },
  34. onOption(e) {
  35. const {
  36. index,
  37. value
  38. } = e.currentTarget.dataset;
  39. if (this.data.list[index].value == value) return;
  40. this.setData({
  41. [`list[${index}].value`]: value
  42. });
  43. this.getResult();
  44. },
  45. getResult(isSubmit = false) {
  46. let obj = {};
  47. let breakOff = false;
  48. this.data.list.filter(v => {
  49. if (breakOff) return;
  50. if (v.type == '自定义') {
  51. if (!v.value) v.value = 0;
  52. }
  53. obj[v.key] = v.value || '';
  54. if (isSubmit && !obj[v.key]) {
  55. wx.showToast({
  56. title: `请完成定制"${v.label}"选项`,
  57. icon: "none"
  58. })
  59. breakOff = true;
  60. } else if (v.tips) {
  61. wx.showToast({
  62. title: v.tips,
  63. icon: "none"
  64. })
  65. breakOff = true;
  66. }
  67. })
  68. this.triggerEvent("onChange", breakOff ? false : obj)
  69. return breakOff ? false : obj
  70. },
  71. valueChange(e) {
  72. const {
  73. index,
  74. item
  75. } = e.currentTarget.dataset;
  76. wx.hideToast()
  77. item.tips = '';
  78. item.value = (e.detail.value - 0).toFixed(item.decimalplaces);
  79. if (item.value > item.max) item.tips = `自定义${item.label}超出可选范围!`;
  80. if (item.value < item.min) item.tips = `自定义${item.label}少于可选范围!`;
  81. if (item.tips) wx.showToast({
  82. title: item.tips,
  83. icon: "none"
  84. })
  85. this.setData({
  86. [`list[${index}]`]: item
  87. })
  88. }
  89. }
  90. })