以下为完整代码,可在控件商城导入
/* eslint-disable no-undef *//* eslint-disable react/react-in-jsx-scope */const types = {type: 'SAMPLE_BLINK_BUTTON_WIDGET',icon: 'https://static.codemao.cn/appcraft/extension-widgets/production/blink-button.svg',title: '闪烁按钮',platforms: ['web', 'android', 'ios'],isInvisibleWidget: false,isGlobalWidget: false,properties: [{key: '__width',label: '宽度',valueType: 'number',defaultValue: 68,},{key: '__height',label: '高度',valueType: 'number',defaultValue: 40,},{key: 'content',label: '按钮文案',valueType: 'string',defaultValue: '按钮',},{key: 'backgroundColor',label: '按钮颜色',valueType: 'color',defaultValue: '#1495ef',},{key: 'textColor',label: '文本颜色',valueType: 'color',defaultValue: '#ffffff',},{key: 'fontSize',label: '字体大小',valueType: 'number',defaultValue: 16,},{key: 'borderRadius',label: '圆角',valueType: 'number',defaultValue: 5,},],methods: [{key: 'blink',label: '开始闪烁',params: [],},],events: [{key: 'onClick',label: '被点击',params: [{key: 'content',label: '按钮文字',valueType: 'string',},],},],};class BlinkButtonWidget extends VisibleWidget {constructor(props) {super(props);this.content = props.content;this.backgroundColor = props.backgroundColor;this.borderRadius = props.borderRadius;this.fontSize = props.fontSize;this.textColor = props.textColor;}// dom 绑定的事件方法必须使用箭头函数,或者添加 bindonClick = () => {this.emit('onClick', this.content);};blink = () => {const startColor = this.backgroundColor;setTimeout(() => {this.setProps({backgroundColor: this.getRandomColor(),});}, 100);setTimeout(() => {this.setProps({backgroundColor: this.getRandomColor(),});}, 200);setTimeout(() => {this.setProps({backgroundColor: this.getRandomColor(),});}, 300);setTimeout(() => {this.setProps({backgroundColor: this.getRandomColor(),});}, 400);setTimeout(() => {this.setProps({backgroundColor: this.getRandomColor(),});}, 500);setTimeout(() => {this.setProps({backgroundColor: this.getRandomColor(),});}, 600);setTimeout(() => {this.setProps({backgroundColor: startColor,});}, 700);};getRandomColor() {const r = Math.floor(Math.random() * 255);const g = Math.floor(Math.random() * 255);const b = Math.floor(Math.random() * 255);const color = 'rgb(' + r + ',' + g + ',' + b + ')';return color;}render() {return (<buttononClick={this.onClick}style={{background: this.backgroundColor,minWidth: 30,minHeight: 20,width: '100%',height: '100%',border: 'none',borderRadius: this.borderRadius,color: this.textColor,fontSize: this.fontSize,}}>{this.content}</button>);}}exports.types = types;exports.widget = BlinkButtonWidget;
