1- import React , { useState , useEffect } from 'react' ;
2- import { Drawer , Button , Space , Divider , AppContainer , ProTable } from 'knowdesign' ;
1+ import React , { useState , useEffect , forwardRef , useImperativeHandle } from 'react' ;
2+ import { Drawer , Button , Space , Divider , AppContainer , ProTable , Utils } from 'knowdesign' ;
33import { IconFont } from '@knowdesign/icons' ;
4- import { IindicatorSelectModule } from './index' ;
4+ import { MetricSelect } from './index' ;
55import './style/indicator-drawer.less' ;
66import { useLocation } from 'react-router-dom' ;
77
88interface PropsType extends React . HTMLAttributes < HTMLDivElement > {
9- onClose : ( ) => void ;
10- visible : boolean ;
11- isGroup ?: boolean ; // 是否分组
12- indicatorSelectModule : IindicatorSelectModule ;
9+ metricSelect : MetricSelect ;
1310}
1411
1512interface MetricInfo {
@@ -27,25 +24,25 @@ type CategoryData = {
2724 metrics : MetricInfo [ ] ;
2825} ;
2926
30- const ExpandedRow = ( { metrics, category, selectedMetrics, selectedMetricChange } : any ) => {
31- const innerColumns = [
32- {
33- title : '指标名称' ,
34- dataIndex : 'name' ,
35- key : 'name' ,
36- } ,
37- {
38- title : '单位' ,
39- dataIndex : 'unit' ,
40- key : 'unit' ,
41- } ,
42- {
43- title : '描述' ,
44- dataIndex : 'desc' ,
45- key : 'desc' ,
46- } ,
47- ] ;
27+ const expandedRowColumns = [
28+ {
29+ title : '指标名称' ,
30+ dataIndex : 'name' ,
31+ key : 'name' ,
32+ } ,
33+ {
34+ title : '单位' ,
35+ dataIndex : 'unit' ,
36+ key : 'unit' ,
37+ } ,
38+ {
39+ title : '描述' ,
40+ dataIndex : 'desc' ,
41+ key : 'desc' ,
42+ } ,
43+ ] ;
4844
45+ const ExpandedRow = ( { metrics, category, selectedMetrics, selectedMetricChange } : any ) => {
4946 return (
5047 < div
5148 style = { {
@@ -62,7 +59,7 @@ const ExpandedRow = ({ metrics, category, selectedMetrics, selectedMetricChange
6259 showHeader : false ,
6360 noPagination : true ,
6461 rowKey : 'name' ,
65- columns : innerColumns ,
62+ columns : expandedRowColumns ,
6663 dataSource : metrics ,
6764 attrs : {
6865 rowSelection : {
@@ -79,13 +76,14 @@ const ExpandedRow = ({ metrics, category, selectedMetrics, selectedMetricChange
7976 ) ;
8077} ;
8178
82- const IndicatorDrawer = ( { onClose , visible , indicatorSelectModule } : PropsType ) => {
79+ const MetricSelect = forwardRef ( ( { metricSelect } : PropsType , ref ) => {
8380 const [ global ] = AppContainer . useGlobalValue ( ) ;
8481 const { pathname } = useLocation ( ) ;
8582 const [ confirmLoading , setConfirmLoading ] = useState < boolean > ( false ) ;
8683 const [ categoryData , setCategoryData ] = useState < CategoryData [ ] > ( [ ] ) ;
8784 const [ selectedCategories , setSelectedCategories ] = useState < string [ ] > ( [ ] ) ;
8885 const [ childrenSelectedRowKeys , setChildrenSelectedRowKeys ] = useState < SelectedMetrics > ( { } ) ;
86+ const [ visible , setVisible ] = useState < boolean > ( false ) ;
8987
9088 const columns = [
9189 {
@@ -96,13 +94,13 @@ const IndicatorDrawer = ({ onClose, visible, indicatorSelectModule }: PropsType)
9694 ] ;
9795
9896 const formateTableData = ( ) => {
99- const tableData = indicatorSelectModule . tableData ;
97+ const tableData = metricSelect . tableData ;
10098 const categoryData : {
10199 [ category : string ] : MetricInfo [ ] ;
102100 } = { } ;
103101
104102 tableData . forEach ( ( { name, desc } ) => {
105- const metricDefine = global . getMetricDefine ( indicatorSelectModule ?. metricType , name ) ;
103+ const metricDefine = global . getMetricDefine ( metricSelect ?. metricType , name ) ;
106104 const returnData = {
107105 name,
108106 desc,
@@ -125,12 +123,12 @@ const IndicatorDrawer = ({ onClose, visible, indicatorSelectModule }: PropsType)
125123 } ;
126124
127125 const formateSelectedKeys = ( ) => {
128- const newKeys = indicatorSelectModule . selectedRows ;
126+ const newKeys = metricSelect . selectedRows ;
129127 const result : SelectedMetrics = { } ;
130128 const selectedCategories : string [ ] = [ ] ;
131129
132130 newKeys . forEach ( ( name : string ) => {
133- const metricDefine = global . getMetricDefine ( indicatorSelectModule ?. metricType , name ) ;
131+ const metricDefine = global . getMetricDefine ( metricSelect ?. metricType , name ) ;
134132 if ( metricDefine ) {
135133 if ( ! result [ metricDefine . category ] ) {
136134 result [ metricDefine . category ] = [ name ] ;
@@ -217,10 +215,10 @@ const IndicatorDrawer = ({ onClose, visible, indicatorSelectModule }: PropsType)
217215 const allRowKeys : string [ ] = [ ] ;
218216 Object . entries ( childrenSelectedRowKeys ) . forEach ( ( [ , arr ] ) => allRowKeys . push ( ...arr ) ) ;
219217
220- indicatorSelectModule . submitCallback ( allRowKeys ) . then (
218+ metricSelect . submitCallback ( allRowKeys ) . then (
221219 ( ) => {
222220 setConfirmLoading ( false ) ;
223- onClose ( ) ;
221+ setVisible ( false ) ;
224222 } ,
225223 ( ) => {
226224 setConfirmLoading ( false ) ;
@@ -231,7 +229,7 @@ const IndicatorDrawer = ({ onClose, visible, indicatorSelectModule }: PropsType)
231229 const rowSelection = {
232230 selectedRowKeys : selectedCategories ,
233231 onChange : rowChange ,
234- // getCheckboxProps: (record: any) => indicatorSelectModule .checkboxProps && indicatorSelectModule .checkboxProps(record),
232+ // getCheckboxProps: (record: any) => metricSelect .checkboxProps && metricSelect .checkboxProps(record),
235233 getCheckboxProps : ( record : CategoryData ) => {
236234 const isAllSelected = record . metrics . length === childrenSelectedRowKeys [ record . category ] ?. length ;
237235 const isNotCheck = ! childrenSelectedRowKeys [ record . category ] || childrenSelectedRowKeys [ record . category ] ?. length === 0 ;
@@ -241,25 +239,33 @@ const IndicatorDrawer = ({ onClose, visible, indicatorSelectModule }: PropsType)
241239 } ,
242240 } ;
243241
244- useEffect ( formateTableData , [ indicatorSelectModule . tableData ] ) ;
242+ useEffect ( formateTableData , [ metricSelect . tableData ] ) ;
245243
246244 useEffect ( ( ) => {
247245 visible && formateSelectedKeys ( ) ;
248- } , [ visible , indicatorSelectModule . selectedRows ] ) ;
246+ } , [ visible , metricSelect . selectedRows ] ) ;
247+
248+ useImperativeHandle (
249+ ref ,
250+ ( ) => ( {
251+ open : ( ) => setVisible ( true ) ,
252+ } ) ,
253+ [ ]
254+ ) ;
249255
250256 return (
251257 < >
252258 < Drawer
253259 className = "indicator-drawer"
254- title = { indicatorSelectModule . drawerTitle || '指标筛选' }
260+ title = { metricSelect . drawerTitle || '指标筛选' }
255261 width = "868px"
256262 forceRender = { true }
257- onClose = { onClose }
263+ onClose = { ( ) => setVisible ( false ) }
258264 visible = { visible }
259265 maskClosable = { false }
260266 extra = {
261267 < Space >
262- < Button size = "small" onClick = { onClose } >
268+ < Button size = "small" onClick = { ( ) => setVisible ( false ) } >
263269 取消
264270 </ Button >
265271 < Button
@@ -281,6 +287,7 @@ const IndicatorDrawer = ({ onClose, visible, indicatorSelectModule }: PropsType)
281287 rowKey : 'category' ,
282288 columns : columns ,
283289 dataSource : categoryData ,
290+ noPagination : true ,
284291 attrs : {
285292 rowSelection : rowSelection ,
286293 expandable : {
@@ -319,6 +326,6 @@ const IndicatorDrawer = ({ onClose, visible, indicatorSelectModule }: PropsType)
319326 </ Drawer >
320327 </ >
321328 ) ;
322- } ;
329+ } ) ;
323330
324- export default IndicatorDrawer ;
331+ export default MetricSelect ;
0 commit comments