1- import type { DeviceDoc , StandardQueryParams } from '../../../utils/types' ;
1+ import type { DeviceDoc , OpenModalFn , StandardQueryParams } from '../../../utils/types' ;
22import type { DSelectItem } from '@react-devui/ui/components/select' ;
33
44import { isUndefined } from 'lodash' ;
5- import { useEffect , useState } from 'react' ;
5+ import { useEffect , useRef , useState } from 'react' ;
66import { useTranslation } from 'react-i18next' ;
77import { Link } from 'react-router-dom' ;
88
99import { useAsync , useImmer , useMount } from '@react-devui/hooks' ;
1010import { DownOutlined , PlusOutlined } from '@react-devui/icons' ;
11- import {
12- DButton ,
13- DCard ,
14- DCheckbox ,
15- DDropdown ,
16- DModal ,
17- DPagination ,
18- DSelect ,
19- DSeparator ,
20- DSpinner ,
21- DTable ,
22- FormControl ,
23- FormGroup ,
24- useForm ,
25- Validators ,
26- } from '@react-devui/ui' ;
11+ import { DButton , DCard , DCheckbox , DDropdown , DModal , DPagination , DSelect , DSeparator , DSpinner , DTable } from '@react-devui/ui' ;
2712
2813import { AppRouteHeader , AppStatusDot , AppTableFilter } from '../../../components' ;
2914import { useAPI , useQueryParams } from '../../../hooks' ;
3015import { AppDeviceModal } from './DeviceModal' ;
3116import styles from './StandardTable.module.scss' ;
3217
33- type Device = DeviceDoc ;
18+ export type Device = DeviceDoc ;
3419
3520interface DeviceQueryParams {
3621 keyword : string ;
@@ -42,6 +27,8 @@ interface DeviceQueryParams {
4227}
4328
4429export default function StandardTable ( ) : JSX . Element | null {
30+ const deviceModalRef = useRef < OpenModalFn < Device > > ( null ) ;
31+
4532 const { t } = useTranslation ( ) ;
4633 const async = useAsync ( ) ;
4734 const modelApi = useAPI ( '/device/model' ) ;
@@ -84,23 +71,6 @@ export default function StandardTable(): JSX.Element | null {
8471 device : Device ;
8572 } > ( ) ;
8673
87- const [ paramsOfDeviceModal , setParamsOfDeviceModal ] = useImmer < {
88- visible : boolean ;
89- device : Device | undefined ;
90- } > ( ) ;
91- const [ deviceForm , updateDeviceForm ] = useForm (
92- ( ) =>
93- new FormGroup ( {
94- name : new FormControl ( '' , Validators . required ) ,
95- model : new FormControl < string | null > ( null , Validators . required ) ,
96- } )
97- ) ;
98- const openDeviceModal = ( device ?: Device ) => {
99- setParamsOfDeviceModal ( { visible : true , device } ) ;
100- deviceForm . reset ( device ? { name : device . name , model : device . model } : undefined ) ;
101- updateDeviceForm ( ) ;
102- } ;
103-
10474 useMount ( ( ) => {
10575 modelApi . list ( ) . subscribe ( {
10676 next : ( res ) => {
@@ -182,29 +152,7 @@ export default function StandardTable(): JSX.Element | null {
182152 } }
183153 />
184154 ) }
185- { paramsOfDeviceModal && (
186- < AppDeviceModal
187- aVisible = { paramsOfDeviceModal . visible }
188- aHeader = { `${ paramsOfDeviceModal . device ? 'Edit' : 'Add' } Device` }
189- aForm = { deviceForm }
190- aUpdateForm = { updateDeviceForm }
191- aModelList = { modelList }
192- onClose = { ( ) => {
193- setParamsOfDeviceModal ( ( draft ) => {
194- if ( draft ) {
195- draft . visible = false ;
196- }
197- } ) ;
198- } }
199- onSubmit = { ( ) =>
200- new Promise ( ( r ) => {
201- async . setTimeout ( ( ) => {
202- r ( true ) ;
203- } , 500 ) ;
204- } )
205- }
206- />
207- ) }
155+ < AppDeviceModal ref = { deviceModalRef } aModelList = { modelList } />
208156 < AppRouteHeader >
209157 < AppRouteHeader . Breadcrumb
210158 aList = { [
@@ -216,7 +164,7 @@ export default function StandardTable(): JSX.Element | null {
216164 aActions = { [
217165 < DButton
218166 onClick = { ( ) => {
219- openDeviceModal ( ) ;
167+ deviceModalRef . current ?. ( ) ;
220168 } }
221169 dIcon = { < PlusOutlined /> }
222170 >
@@ -377,7 +325,7 @@ export default function StandardTable(): JSX.Element | null {
377325 onItemClick = { ( action ) => {
378326 switch ( action ) {
379327 case 'edit' :
380- openDeviceModal ( data ) ;
328+ deviceModalRef . current ?. ( data ) ;
381329 break ;
382330
383331 case 'delete' :
0 commit comments