1- import type { OpenSettingFn } from '../../../utils/types ' ;
1+ import type { AppDialogServiceSupport } from '../../../utils/dialog-service ' ;
22import type { DeviceData } from './StandardTable' ;
33import type { DSelectItem } from '@react-devui/ui/components/select' ;
44
55import { isUndefined } from 'lodash' ;
6- import React , { useImperativeHandle , useState } from 'react' ;
6+ import { useState } from 'react' ;
77
8- import { useEventCallback } from '@react-devui/hooks' ;
8+ import { useMount } from '@react-devui/hooks' ;
99import { FormControl , FormGroup , useForm , Validators } from '@react-devui/ui' ;
1010import { DForm , DInput , DModal , DSelect } from '@react-devui/ui' ;
1111
@@ -14,38 +14,31 @@ import { useHttp } from '../../../core';
1414import { useAPI } from '../../../hooks' ;
1515
1616export interface AppDeviceModalProps {
17+ aDevice : DeviceData | undefined ;
1718 onSuccess : ( ) => void ;
1819}
1920
20- function DeviceModal ( props : AppDeviceModalProps , ref : React . ForwardedRef < OpenSettingFn < DeviceData > > ) : JSX . Element | null {
21- const { onSuccess } = props ;
21+ export function AppDeviceModal ( props : AppDeviceModalProps ) : JSX . Element | null {
22+ const { aDevice , onSuccess, aVisible , onClose , afterVisibleChange } = props as AppDeviceModalProps & AppDialogServiceSupport ;
2223
2324 const http = useHttp ( ) ;
24- const httpOfInit = useHttp ( ) ;
2525 const modelApi = useAPI ( http , '/device/model' ) ;
26- const modelApiOfInit = useAPI ( httpOfInit , '/device/model' ) ;
2726
28- const [ visible , setVisible ] = useState ( false ) ;
29- const [ device , setDevice ] = useState < DeviceData > ( ) ;
30- const [ form , updateForm ] = useForm (
31- ( ) =>
32- new FormGroup ( {
33- name : new FormControl ( '' , Validators . required ) ,
34- model : new FormControl < string | null > ( null , Validators . required ) ,
35- } )
36- ) ;
27+ const [ form , updateForm ] = useForm ( ( ) => {
28+ const form = new FormGroup ( {
29+ name : new FormControl ( '' , Validators . required ) ,
30+ model : new FormControl < string | null > ( null , Validators . required ) ,
31+ } ) ;
32+ if ( aDevice ) {
33+ form . reset ( { name : aDevice . name } ) ;
34+ }
35+ return form ;
36+ } ) ;
3737
3838 const [ modelList , setModelList ] = useState < DSelectItem < string > [ ] > ( ) ;
3939
40- const open = useEventCallback < OpenSettingFn < DeviceData > > ( ( device ) => {
41- setVisible ( true ) ;
42- setDevice ( device ) ;
43-
44- form . reset ( device ? { name : device . name } : undefined ) ;
45- updateForm ( ) ;
46-
47- setModelList ( undefined ) ;
48- modelApiOfInit . list ( ) . subscribe ( {
40+ useMount ( ( ) => {
41+ modelApi . list ( ) . subscribe ( {
4942 next : ( res ) => {
5043 setModelList (
5144 res . resources . map ( ( model ) => ( {
@@ -54,20 +47,18 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
5447 disabled : model . disabled ,
5548 } ) )
5649 ) ;
57- if ( device ) {
58- form . patchValue ( { model : device . model } ) ;
50+ if ( aDevice ) {
51+ form . patchValue ( { model : aDevice . model } ) ;
5952 updateForm ( ) ;
6053 }
6154 } ,
6255 } ) ;
6356 } ) ;
6457
65- useImperativeHandle ( ref , ( ) => open , [ open ] ) ;
66-
6758 return (
6859 < DModal
69- dVisible = { visible }
70- dHeader = { `${ device ? 'Edit' : 'Add' } Device` }
60+ dVisible = { aVisible }
61+ dHeader = { `${ aDevice ? 'Edit' : 'Add' } Device` }
7162 dFooter = {
7263 < DModal . Footer
7364 dOkProps = { { disabled : ! form . valid } }
@@ -83,11 +74,10 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
8374 }
8475 > </ DModal . Footer >
8576 }
77+ dSkipFirstTransition = { false }
8678 dMaskClosable = { false }
87- onClose = { ( ) => {
88- httpOfInit . abortAll ( ) ;
89- setVisible ( false ) ;
90- } }
79+ onClose = { onClose }
80+ afterVisibleChange = { afterVisibleChange }
9181 >
9282 < AppResponsiveForm >
9383 < DForm dUpdate = { updateForm } dLabelWidth = "6em" >
@@ -106,5 +96,3 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
10696 </ DModal >
10797 ) ;
10898}
109-
110- export const AppDeviceModal = React . forwardRef ( DeviceModal ) ;
0 commit comments