icon.js 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  8. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  9. return new (P || (P = Promise))(function (resolve, reject) {
  10. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  11. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  12. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  13. step((generator = generator.apply(thisArg, _arguments || [])).next());
  14. });
  15. };
  16. import { SuperComponent, wxComponent } from '../common/src/index';
  17. import config from '../common/config';
  18. import props from './props';
  19. import { styles, addUnit, getRect } from '../common/utils';
  20. const { prefix } = config;
  21. const name = `${prefix}-icon`;
  22. let Icon = class Icon extends SuperComponent {
  23. constructor() {
  24. super(...arguments);
  25. this.externalClasses = [`${prefix}-class`];
  26. this.properties = props;
  27. this.data = {
  28. componentPrefix: prefix,
  29. classPrefix: name,
  30. isImage: false,
  31. iconStyle: undefined,
  32. };
  33. this.observers = {
  34. 'name, color, size, style'() {
  35. this.setIconStyle();
  36. },
  37. };
  38. this.methods = {
  39. onTap(event) {
  40. this.triggerEvent('click', event.detail);
  41. },
  42. setIconStyle() {
  43. const { name, color, size, classPrefix } = this.data;
  44. const isImage = name.indexOf('/') !== -1;
  45. const sizeValue = addUnit(size);
  46. const colorStyle = color ? { color: color } : {};
  47. const fontStyle = size ? { 'font-size': sizeValue } : {};
  48. const iconStyle = Object.assign(Object.assign({}, colorStyle), fontStyle);
  49. this.setData({ isImage }, () => __awaiter(this, void 0, void 0, function* () {
  50. if (isImage) {
  51. let iconSize = sizeValue;
  52. if (!iconSize) {
  53. yield getRect(this, `.${classPrefix}`)
  54. .then((res) => {
  55. iconSize = addUnit(res === null || res === void 0 ? void 0 : res.height);
  56. })
  57. .catch(() => { });
  58. }
  59. iconStyle.width = iconSize;
  60. iconStyle.height = iconSize;
  61. }
  62. this.setData({ iconStyle: `${styles(iconStyle)}` });
  63. }));
  64. },
  65. };
  66. }
  67. };
  68. Icon = __decorate([
  69. wxComponent()
  70. ], Icon);
  71. export default Icon;