switch.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. this.data = {
  19. classPrefix: name,
  20. isActive: false,
  21. bodyStyle: '',
  22. };
  23. this.controlledProps = [
  24. {
  25. key: 'value',
  26. event: 'change',
  27. },
  28. ];
  29. this.observers = {
  30. value(val) {
  31. const [activeValue] = this.data.customValue;
  32. this.setData({
  33. isActive: val === activeValue,
  34. });
  35. this.handleColorChange();
  36. },
  37. };
  38. this.methods = {
  39. switchChange() {
  40. const { disabled, value, customValue } = this.data;
  41. const [activeValue, inactiveValue] = customValue;
  42. if (disabled)
  43. return;
  44. this._trigger('change', {
  45. value: value === activeValue ? inactiveValue : activeValue,
  46. });
  47. },
  48. handleColorChange() {
  49. const { disabled, colors = [] } = this.data;
  50. const [activedColor = '#0052d9', inactivedColor = 'rgba(0, 0, 0, .26)'] = colors;
  51. if (!disabled) {
  52. this.setData({
  53. bodyStyle: `background-color: ${this.data.isActive ? activedColor : inactivedColor}`,
  54. });
  55. }
  56. },
  57. onTapBackground() {
  58. this.switchChange();
  59. },
  60. onTapDot() {
  61. this.switchChange();
  62. },
  63. };
  64. }
  65. };
  66. Switch = __decorate([
  67. wxComponent()
  68. ], Switch);
  69. export default Switch;