switch.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  2. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  3. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  4. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  5. return c > 3 && r && Object.defineProperty(target, key, r), r;
  6. };
  7. import { wxComponent, SuperComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import props from './props';
  10. const { prefix } = config;
  11. const name = `${prefix}-switch`;
  12. let Switch = class Switch extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = ['t-class', 't-class-label', 't-class-body', 't-class-dot'];
  16. this.behaviors = ['wx://form-field'];
  17. this.properties = props;
  18. // 组件的内部数据
  19. this.data = {
  20. classPrefix: name,
  21. isActive: false,
  22. bodyStyle: '',
  23. };
  24. // lifetimes = {
  25. // attached() {
  26. // const { value, customValue } = this.data;
  27. // const [activeValue] = customValue;
  28. // this.setData({
  29. // isActive: value === activeValue,
  30. // });
  31. // },
  32. // };
  33. this.controlledProps = [
  34. {
  35. key: 'value',
  36. event: 'change',
  37. },
  38. ];
  39. this.observers = {
  40. value(val) {
  41. const [activeValue] = this.data.customValue;
  42. this.setData({
  43. isActive: val === activeValue,
  44. });
  45. this.handleColorChange();
  46. },
  47. };
  48. this.methods = {
  49. switchChange() {
  50. const { disabled, value, customValue } = this.data;
  51. const [activeValue, inactiveValue] = customValue;
  52. if (disabled)
  53. return;
  54. this._trigger('change', {
  55. value: value === activeValue ? inactiveValue : activeValue,
  56. });
  57. },
  58. handleColorChange() {
  59. const { disabled, colors = [] } = this.data;
  60. const [activedColor = '#0052d9', inactivedColor = 'rgba(0, 0, 0, .26)'] = colors;
  61. if (!disabled) {
  62. this.setData({
  63. bodyStyle: `background-color: ${this.data.isActive ? activedColor : inactivedColor}`,
  64. });
  65. }
  66. },
  67. onTapBackground() {
  68. this.switchChange();
  69. },
  70. onTapDot() {
  71. this.switchChange();
  72. },
  73. };
  74. }
  75. };
  76. Switch = __decorate([
  77. wxComponent()
  78. ], Switch);
  79. export default Switch;
  80. //# sourceMappingURL=switch.js.map