checkbox.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 classPrefix = `${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 } = parent.data;
  28. const valueSet = new Set(value);
  29. const data = {
  30. disabled: disabled || this.data.disabled,
  31. };
  32. data.checked = valueSet.has(this.data.value);
  33. if (this.data.checkAll) {
  34. data.checked = valueSet.size > 0;
  35. }
  36. this.setData(data);
  37. },
  38. },
  39. };
  40. this.options = {
  41. multipleSlots: true,
  42. };
  43. this.properties = Object.assign(Object.assign({}, Props), { theme: {
  44. type: String,
  45. value: 'default',
  46. }, borderless: {
  47. type: Boolean,
  48. value: false,
  49. } });
  50. this.data = {
  51. prefix,
  52. classPrefix,
  53. };
  54. this.controlledProps = [
  55. {
  56. key: 'checked',
  57. event: 'change',
  58. },
  59. ];
  60. this.methods = {
  61. onChange(e) {
  62. const { disabled, readonly } = this.data;
  63. if (disabled || readonly)
  64. return;
  65. const { target } = e.currentTarget.dataset;
  66. const { contentDisabled } = this.data;
  67. if (target === 'text' && contentDisabled) {
  68. return;
  69. }
  70. const checked = !this.data.checked;
  71. const [parent] = this.getRelationNodes('../checkbox-group/checkbox-group');
  72. if (parent) {
  73. parent.updateValue(Object.assign(Object.assign({}, this.data), { checked }));
  74. }
  75. else {
  76. this._trigger('change', { checked });
  77. }
  78. },
  79. };
  80. }
  81. };
  82. CheckBox = __decorate([
  83. wxComponent()
  84. ], CheckBox);
  85. export default CheckBox;