checkbox.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 { SuperComponent, wxComponent } from '../common/src/index';
  8. import config from '../common/config';
  9. import Props from './props';
  10. const { prefix } = config;
  11. const name = `${prefix}-checkbox`;
  12. let CheckBox = class CheckBox extends SuperComponent {
  13. constructor() {
  14. super(...arguments);
  15. this.externalClasses = [
  16. `${prefix}-class`,
  17. `${prefix}-class-label`,
  18. `${prefix}-class-icon`,
  19. `${prefix}-class-content`,
  20. `${prefix}-class-border`,
  21. ];
  22. this.behaviors = ['wx://form-field'];
  23. this.relations = {
  24. '../checkbox-group/checkbox-group': {
  25. type: 'ancestor',
  26. linked(parent) {
  27. const { value, disabled, borderless } = parent.data;
  28. const valueSet = new Set(value);
  29. const checkedFromParent = valueSet.has(this.data.value);
  30. const data = {
  31. _disabled: this.data.disabled == null ? disabled : this.data.disabled,
  32. };
  33. if (borderless) {
  34. data.borderless = true;
  35. }
  36. data.checked = this.data.checked || checkedFromParent;
  37. if (this.data.checked) {
  38. parent.updateValue(this.data);
  39. }
  40. if (this.data.checkAll) {
  41. data.checked = valueSet.size > 0;
  42. }
  43. this.setData(data);
  44. },
  45. },
  46. };
  47. this.options = {
  48. multipleSlots: true,
  49. };
  50. this.properties = Object.assign(Object.assign({}, Props), { theme: {
  51. type: String,
  52. value: 'default',
  53. }, tId: {
  54. type: String,
  55. } });
  56. this.data = {
  57. prefix,
  58. classPrefix: name,
  59. _disabled: false,
  60. };
  61. this.observers = {
  62. disabled(v) {
  63. this.setData({ _disabled: v });
  64. },
  65. };
  66. this.controlledProps = [
  67. {
  68. key: 'checked',
  69. event: 'change',
  70. },
  71. ];
  72. this.methods = {
  73. handleTap(e) {
  74. const { _disabled, readonly, contentDisabled } = this.data;
  75. const { target } = e.currentTarget.dataset;
  76. if (_disabled || readonly || (target === 'text' && contentDisabled))
  77. return;
  78. const { value, label } = this.data;
  79. const checked = !this.data.checked;
  80. const parent = this.$parent;
  81. if (parent) {
  82. parent.updateValue(Object.assign(Object.assign({}, this.data), { checked, item: { label, value, checked } }));
  83. }
  84. else {
  85. this._trigger('change', { context: { value, label }, checked });
  86. }
  87. },
  88. setDisabled(disabled) {
  89. this.setData({
  90. _disabled: this.data.disabled || disabled,
  91. });
  92. },
  93. };
  94. }
  95. };
  96. CheckBox = __decorate([
  97. wxComponent()
  98. ], CheckBox);
  99. export default CheckBox;