/* * @Author: yehuozhili * @Date: 2021-07-07 14:35:38 * @LastEditors: yehuozhili * @LastEditTime: 2022-04-24 00:29:46 * @FilePath: \dooringx\packages\dooringx-example\src\plugin\registComponents\button.tsx */ import { Button } from 'antd'; import React, { useEffect, useMemo, useState } from 'react'; import { changeUserValue, ComponentItemFactory, createPannelOptions, useDynamicAddEventCenter, } from 'dooringx-lib'; import { FormMap } from '../formTypes'; import { ComponentRenderConfigProps } from 'dooringx-lib/dist/core/components/componentItem'; let s = 0; function ButtonTemp(pr: ComponentRenderConfigProps) { const props = pr.data.props; const eventCenter = useMemo(() => { return pr.config.getEventCenter(); }, [pr.config]); useDynamicAddEventCenter(pr, `${pr.data.id}-init`, '初始渲染时机'); //注册名必须带id 约定! useDynamicAddEventCenter(pr, `${pr.data.id}-click`, '点击执行时机'); useEffect(() => { // 模拟抛出事件 if (pr.context === 'preview') { eventCenter.runEventQueue(`${pr.data.id}-init`, pr.config); } }, [eventCenter, pr.config, pr.context, pr.data.id]); const [text, setText] = useState(''); const op1 = props.op1; useEffect(() => { let unRegist = () => {}; if (op1) { console.log('kkk'); const functionCenter = eventCenter.getFunctionCenter(); unRegist = functionCenter.register({ id: `${pr.data.id}+changeText`, fn: async (ctx, next, config, args: any, _eventList, iname) => { const userSelect = iname.data; const ctxVal = changeUserValue( userSelect['改变文本数据源'], args, '_changeval', config, ctx ); s = s++; const text = ctxVal[0] + s; setText(text); next(); }, config: [ { name: '改变文本数据源', data: ['ctx', 'input', 'dataSource'], options: { receive: '_changeval', multi: false, }, }, ], name: `${pr.data.id}+改变文本函数`, componentId: pr.data.id, }); } return () => { if (pr.context === 'preview') { unRegist(); // 必须在预览时注销,否则影响二次点击效果,不在预览注销影响编辑时跨弹窗 } }; }, [op1]); return ( ); } const MButton = new ComponentItemFactory( 'button', '按钮', { style: [ createPannelOptions('input', { receive: 'text', label: '文字', }), ], fn: [ createPannelOptions('switch', { receive: 'op1', label: '改变文本函数', }), ], animate: [createPannelOptions('animateControl', {})], actions: [createPannelOptions('actionButton', {})], }, { props: { text: 'yehuozhili', sizeData: [100, 30], backgroundColor: 'rgba(0,132,255,1)', lineHeight: 1, borderRadius: 0, op1: false, borderData: { borderWidth: 0, borderColor: 'rgba(0,0,0,1)', borderStyle: 'solid', }, fontData: { fontSize: 14, textDecoration: 'none', fontStyle: 'normal', color: 'rgba(255,255,255,1)', fontWeight: 'normal', }, }, width: 100, // 绝对定位元素初始必须有宽高,否则适配会有问题。 height: 30, // 绝对定位元素初始必须有宽高,否则适配会有问题。 rotate: { canRotate: true, value: 0, }, canDrag: true, // false就不能拖 }, (data, context, store, config) => { return ; }, true ); export default MButton;