checkbox.js 3.2 KB

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