以下为完整代码,可在控件商城导入

    1. /* eslint-disable no-undef */
    2. /* eslint-disable react/react-in-jsx-scope */
    3. const types = {
    4. type: 'SAMPLE_BLINK_BUTTON_WIDGET',
    5. icon: 'https://static.codemao.cn/appcraft/extension-widgets/production/blink-button.svg',
    6. title: '闪烁按钮',
    7. platforms: ['web', 'android', 'ios'],
    8. isInvisibleWidget: false,
    9. isGlobalWidget: false,
    10. properties: [
    11. {
    12. key: '__width',
    13. label: '宽度',
    14. valueType: 'number',
    15. defaultValue: 68,
    16. },
    17. {
    18. key: '__height',
    19. label: '高度',
    20. valueType: 'number',
    21. defaultValue: 40,
    22. },
    23. {
    24. key: 'content',
    25. label: '按钮文案',
    26. valueType: 'string',
    27. defaultValue: '按钮',
    28. },
    29. {
    30. key: 'backgroundColor',
    31. label: '按钮颜色',
    32. valueType: 'color',
    33. defaultValue: '#1495ef',
    34. },
    35. {
    36. key: 'textColor',
    37. label: '文本颜色',
    38. valueType: 'color',
    39. defaultValue: '#ffffff',
    40. },
    41. {
    42. key: 'fontSize',
    43. label: '字体大小',
    44. valueType: 'number',
    45. defaultValue: 16,
    46. },
    47. {
    48. key: 'borderRadius',
    49. label: '圆角',
    50. valueType: 'number',
    51. defaultValue: 5,
    52. },
    53. ],
    54. methods: [
    55. {
    56. key: 'blink',
    57. label: '开始闪烁',
    58. params: [],
    59. },
    60. ],
    61. events: [
    62. {
    63. key: 'onClick',
    64. label: '被点击',
    65. params: [
    66. {
    67. key: 'content',
    68. label: '按钮文字',
    69. valueType: 'string',
    70. },
    71. ],
    72. },
    73. ],
    74. };
    75. class BlinkButtonWidget extends VisibleWidget {
    76. constructor(props) {
    77. super(props);
    78. this.content = props.content;
    79. this.backgroundColor = props.backgroundColor;
    80. this.borderRadius = props.borderRadius;
    81. this.fontSize = props.fontSize;
    82. this.textColor = props.textColor;
    83. }
    84. // dom 绑定的事件方法必须使用箭头函数,或者添加 bind
    85. onClick = () => {
    86. this.emit('onClick', this.content);
    87. };
    88. blink = () => {
    89. const startColor = this.backgroundColor;
    90. setTimeout(() => {
    91. this.setProps({
    92. backgroundColor: this.getRandomColor(),
    93. });
    94. }, 100);
    95. setTimeout(() => {
    96. this.setProps({
    97. backgroundColor: this.getRandomColor(),
    98. });
    99. }, 200);
    100. setTimeout(() => {
    101. this.setProps({
    102. backgroundColor: this.getRandomColor(),
    103. });
    104. }, 300);
    105. setTimeout(() => {
    106. this.setProps({
    107. backgroundColor: this.getRandomColor(),
    108. });
    109. }, 400);
    110. setTimeout(() => {
    111. this.setProps({
    112. backgroundColor: this.getRandomColor(),
    113. });
    114. }, 500);
    115. setTimeout(() => {
    116. this.setProps({
    117. backgroundColor: this.getRandomColor(),
    118. });
    119. }, 600);
    120. setTimeout(() => {
    121. this.setProps({
    122. backgroundColor: startColor,
    123. });
    124. }, 700);
    125. };
    126. getRandomColor() {
    127. const r = Math.floor(Math.random() * 255);
    128. const g = Math.floor(Math.random() * 255);
    129. const b = Math.floor(Math.random() * 255);
    130. const color = 'rgb(' + r + ',' + g + ',' + b + ')';
    131. return color;
    132. }
    133. render() {
    134. return (
    135. <button
    136. onClick={this.onClick}
    137. style={{
    138. background: this.backgroundColor,
    139. minWidth: 30,
    140. minHeight: 20,
    141. width: '100%',
    142. height: '100%',
    143. border: 'none',
    144. borderRadius: this.borderRadius,
    145. color: this.textColor,
    146. fontSize: this.fontSize,
    147. }}>
    148. {this.content}
    149. </button>
    150. );
    151. }
    152. }
    153. exports.types = types;
    154. exports.widget = BlinkButtonWidget;