1
- import type { OpenSettingFn } from '../../../utils/types ' ;
1
+ import type { AppDialogServiceSupport } from '../../../utils/dialog-service ' ;
2
2
import type { DeviceData } from './StandardTable' ;
3
3
import type { DSelectItem } from '@react-devui/ui/components/select' ;
4
4
5
5
import { isUndefined } from 'lodash' ;
6
- import React , { useImperativeHandle , useState } from 'react' ;
6
+ import { useState } from 'react' ;
7
7
8
- import { useEventCallback } from '@react-devui/hooks' ;
8
+ import { useMount } from '@react-devui/hooks' ;
9
9
import { FormControl , FormGroup , useForm , Validators } from '@react-devui/ui' ;
10
10
import { DForm , DInput , DModal , DSelect } from '@react-devui/ui' ;
11
11
@@ -14,38 +14,31 @@ import { useHttp } from '../../../core';
14
14
import { useAPI } from '../../../hooks' ;
15
15
16
16
export interface AppDeviceModalProps {
17
+ aDevice : DeviceData | undefined ;
17
18
onSuccess : ( ) => void ;
18
19
}
19
20
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 ;
22
23
23
24
const http = useHttp ( ) ;
24
- const httpOfInit = useHttp ( ) ;
25
25
const modelApi = useAPI ( http , '/device/model' ) ;
26
- const modelApiOfInit = useAPI ( httpOfInit , '/device/model' ) ;
27
26
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
+ } ) ;
37
37
38
38
const [ modelList , setModelList ] = useState < DSelectItem < string > [ ] > ( ) ;
39
39
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 ( {
49
42
next : ( res ) => {
50
43
setModelList (
51
44
res . resources . map ( ( model ) => ( {
@@ -54,20 +47,18 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
54
47
disabled : model . disabled ,
55
48
} ) )
56
49
) ;
57
- if ( device ) {
58
- form . patchValue ( { model : device . model } ) ;
50
+ if ( aDevice ) {
51
+ form . patchValue ( { model : aDevice . model } ) ;
59
52
updateForm ( ) ;
60
53
}
61
54
} ,
62
55
} ) ;
63
56
} ) ;
64
57
65
- useImperativeHandle ( ref , ( ) => open , [ open ] ) ;
66
-
67
58
return (
68
59
< DModal
69
- dVisible = { visible }
70
- dHeader = { `${ device ? 'Edit' : 'Add' } Device` }
60
+ dVisible = { aVisible }
61
+ dHeader = { `${ aDevice ? 'Edit' : 'Add' } Device` }
71
62
dFooter = {
72
63
< DModal . Footer
73
64
dOkProps = { { disabled : ! form . valid } }
@@ -83,11 +74,10 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
83
74
}
84
75
> </ DModal . Footer >
85
76
}
77
+ dSkipFirstTransition = { false }
86
78
dMaskClosable = { false }
87
- onClose = { ( ) => {
88
- httpOfInit . abortAll ( ) ;
89
- setVisible ( false ) ;
90
- } }
79
+ onClose = { onClose }
80
+ afterVisibleChange = { afterVisibleChange }
91
81
>
92
82
< AppResponsiveForm >
93
83
< DForm dUpdate = { updateForm } dLabelWidth = "6em" >
@@ -106,5 +96,3 @@ function DeviceModal(props: AppDeviceModalProps, ref: React.ForwardedRef<OpenSet
106
96
</ DModal >
107
97
) ;
108
98
}
109
-
110
- export const AppDeviceModal = React . forwardRef ( DeviceModal ) ;
0 commit comments