button.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // components/button/button.ts
  2. Component({
  3. behaviors: ['wx://form-field-button'],
  4. lifetimes: {
  5. attached() {
  6. console.log('[Button] attached, initializing...');
  7. const mode = this.data.block ? 'block' : 'line';
  8. const index = this.data.index;
  9. this.setData({
  10. src: `../../assets/bg/button-${mode}-${index}.bg.png`,
  11. className: this.data.block ? 'block' : `line-${index}`,
  12. isDisabled: false
  13. });
  14. console.log('[Button] initialized with isDisabled:', this.data.isDisabled);
  15. }
  16. },
  17. properties: {
  18. block: { type: Boolean, value: false },
  19. index: { type: Number, value: 1 },
  20. loading: { type: Boolean, value: false },
  21. },
  22. data: {
  23. list: [
  24. {
  25. name: '保存',
  26. value: 'save',
  27. },
  28. {
  29. name: '提交',
  30. value: 'cancel',
  31. },
  32. {
  33. name: '确定',
  34. value: 'confirm',
  35. },
  36. ],
  37. src: '',
  38. isDisabled: false,
  39. _lastResetTime: 0
  40. },
  41. methods: {
  42. handleSubmit() {
  43. console.log('[Button] handleSubmit called, isDisabled:', this.data.isDisabled);
  44. if (this.data.isDisabled) {
  45. console.log('[Button] button is disabled, returning');
  46. return;
  47. }
  48. // 如果是刚刚重置的按钮(100ms内),不要立即禁用
  49. const now = Date.now();
  50. if (now - this.data._lastResetTime < 100) {
  51. console.log('[Button] button was just reset, not disabling');
  52. this.triggerEvent('submit');
  53. return;
  54. }
  55. console.log('[Button] setting isDisabled to true');
  56. this.setData({ isDisabled: true });
  57. this.triggerEvent("submit");
  58. },
  59. resetState() {
  60. console.log("[Button] resetState called");
  61. console.log("[Button] current isDisabled:", this.data.isDisabled);
  62. this.setData({
  63. isDisabled: false,
  64. _lastResetTime: Date.now()
  65. });
  66. console.log('[Button] isDisabled set to:', this.data.isDisabled);
  67. },
  68. valFailure() {
  69. console.log("[Button] valFailure called");
  70. // 处理失败逻辑
  71. if (this) this.setData({ isDisabled: true }); // 重置按钮状态
  72. },
  73. },
  74. });