1- import React , { useEffect , useRef } from 'react' ;
2- import { Button , Space , message } from 'antd' ;
1+ import React , {
2+ forwardRef ,
3+ useEffect ,
4+ useImperativeHandle ,
5+ useRef ,
6+ } from 'react' ;
7+ import { message } from 'antd' ;
38import { MessageType } from 'antd/lib/message' ;
49import { render } from 'amis' ;
510import './cxd.css' ;
@@ -14,7 +19,6 @@ import {
1419 RendererAction ,
1520 RendererEvent ,
1621} from 'amis-core' ;
17- import RunScript from '../RunScript' ;
1822import { useState } from '@/hooks/useImmer' ;
1923import { runScript } from '@/webview/service' ;
2024
@@ -25,14 +29,7 @@ const request: { count: number; hideLoading?: MessageType } = {
2529
2630interface IProps {
2731 schema : object ;
28- scripts ?: [
29- {
30- method : string ;
31- remark : string ;
32- } ,
33- ] ;
3432 path : string ;
35- onFormChange : ( values : object ) => void ;
3633}
3734
3835// 动作定义
@@ -74,8 +71,7 @@ export class RunScriptAction implements RendererAction {
7471// @ts -ignore
7572registerAction ( 'runScript' , new RunScriptAction ( ) ) ;
7673
77- export default ( props : IProps ) => {
78- const [ scriptModalVisible , setScriptModalVisible ] = useState ( false ) ;
74+ export default forwardRef ( ( props : IProps , ref ) => {
7975 const [ model , setModel ] = useState ( { } as object ) ;
8076 const amisScoped = useRef < any > ( ) ;
8177 const env = {
@@ -187,65 +183,41 @@ export default (props: IProps) => {
187183 } ,
188184 } ;
189185
186+ useImperativeHandle ( ref , ( ) => ( {
187+ getValues : ( ) => {
188+ const component = amisScoped . current . getComponentByName ( 'page.form' ) ;
189+ if ( component ) {
190+ setModel ( component . getValues ( ) ) ;
191+ return component . getValues ( ) ;
192+ }
193+ return { } ;
194+ } ,
195+ setValues : ( values : object ) => {
196+ const component = amisScoped . current . getComponentByName ( 'page.form' ) ;
197+ if ( component ) {
198+ component . setValues ( values ) ;
199+ }
200+ setModel ( values ) ;
201+ } ,
202+ } ) ) ;
203+
190204 useEffect ( ( ) => {
191205 componentData . model = model ;
192206 componentData . materialPath = props . path ;
193207 } , [ model , props . path ] ) ;
194208
195- const handleOpenRunScriptModal = ( ) => {
196- const values = amisScoped . current
197- . getComponentByName ( 'page.form' )
198- . getValues ( ) ;
199- setModel ( values ) ;
200- setScriptModalVisible ( true ) ;
201- } ;
202-
203- const handleRunScriptResult = ( result : object ) => {
204- amisScoped . current . getComponentByName ( 'page.form' ) . setValues ( result ) ;
205- props . onFormChange ( result ) ;
206- setScriptModalVisible ( false ) ;
207- } ;
208-
209209 return (
210210 < >
211211 { render (
212212 props . schema as any ,
213213 {
214- scopeRef : ( ref : any ) => {
215- amisScoped . current = ref ;
214+ scopeRef : ( scopeRef : any ) => {
215+ amisScoped . current = scopeRef ;
216216 } ,
217217 useMobileUI : false ,
218218 } ,
219219 env ,
220220 ) }
221- < br > </ br >
222- < Space >
223- < Button type = "primary" size = "small" onClick = { handleOpenRunScriptModal } >
224- 执行脚本
225- </ Button >
226- < Button
227- type = "primary"
228- size = "small"
229- onClick = { ( ) => {
230- const values = amisScoped . current
231- . getComponentByName ( 'page.form' )
232- . getValues ( ) ;
233- props . onFormChange ( values ) ;
234- } }
235- >
236- 生成模板数据
237- </ Button >
238- </ Space >
239- < RunScript
240- visible = { scriptModalVisible }
241- materialPath = { props . path }
242- model = { model }
243- scripts = { props . scripts }
244- onCancel = { ( ) => {
245- setScriptModalVisible ( false ) ;
246- } }
247- onOk = { handleRunScriptResult }
248- />
249221 </ >
250222 ) ;
251- } ;
223+ } ) ;
0 commit comments