1- import React , { useMemo , useRef , useEffect } from 'react' ;
1+ import React , { useRef , useEffect } from 'react' ;
22import {
33 css ,
44 Table ,
5- TableHeader ,
6- Row ,
7- Cell ,
8- cx ,
95 spacing ,
106 palette ,
11- IndexKeysBadge ,
127 KeylineCard ,
138 useDOMRect ,
149} from '@mongodb-js/compass-components' ;
1510
16- import TypeField from './type-field' ;
17- import SizeField from './size-field' ;
18- import UsageField from './usage-field' ;
19- import PropertyField from './property-field' ;
20- import type {
21- IndexDefinition ,
22- SortColumn ,
23- SortDirection ,
24- } from '../../modules/regular-indexes' ;
25- import IndexActions from './index-actions' ;
26-
2711// When row is hovered, we show the delete button
28- const rowStyles = css ( {
12+ export const rowStyles = css ( {
2913 ':hover' : {
3014 '.index-actions-cell' : {
3115 button : {
@@ -36,7 +20,7 @@ const rowStyles = css({
3620} ) ;
3721
3822// When row is not hovered, we hide the delete button
39- const indexActionsCellStyles = css ( {
23+ export const indexActionsCellStyles = css ( {
4024 button : {
4125 opacity : 0 ,
4226 '&:focus' : {
@@ -46,19 +30,19 @@ const indexActionsCellStyles = css({
4630 minWidth : spacing [ 5 ] ,
4731} ) ;
4832
49- const tableHeaderStyles = css ( {
33+ export const tableHeaderStyles = css ( {
5034 borderWidth : 0 ,
5135 borderBottomWidth : 3 ,
5236 '> div' : {
5337 justifyContent : 'space-between' ,
5438 } ,
5539} ) ;
5640
57- const cellStyles = css ( {
41+ export const cellStyles = css ( {
5842 verticalAlign : 'middle' ,
5943} ) ;
6044
61- const nestedRowCellStyles = css ( {
45+ export const nestedRowCellStyles = css ( {
6246 padding : 0 ,
6347} ) ;
6448
@@ -81,54 +65,23 @@ const spaceProviderStyles = css({
8165 overflow : 'hidden' ,
8266} ) ;
8367
84- type IndexesTableProps = {
85- indexes : IndexDefinition [ ] ;
86- canModifyIndex : boolean ;
87- serverVersion : string ;
88- onSortTable : ( column : SortColumn , direction : SortDirection ) => void ;
89- onDeleteIndex : ( index : IndexDefinition ) => void ;
90- onHideIndex : ( name : string ) => void ;
91- onUnhideIndex : ( name : string ) => void ;
68+ export type IndexesTableProps < Shape > = {
69+ [ 'data-testid' ] : string ;
70+ [ 'aria-label' ] : string ;
71+ columns : JSX . Element [ ] ;
72+ children : ( args : { datum : Shape ; index : number } ) => JSX . Element ;
73+ data : Shape [ ] ;
9274} ;
9375
94- export const IndexesTable : React . FunctionComponent < IndexesTableProps > = ( {
95- indexes,
96- canModifyIndex,
97- serverVersion,
98- onSortTable,
99- onDeleteIndex,
100- onHideIndex,
101- onUnhideIndex,
102- } ) => {
76+ export function IndexesTable < Shape > ( {
77+ [ 'data-testid' ] : dataTestId ,
78+ [ 'aria-label' ] : ariaLabel ,
79+ columns,
80+ children,
81+ data,
82+ } : IndexesTableProps < Shape > ) {
10383 const cardRef = useRef < HTMLDivElement > ( null ) ;
10484 const [ rectProps , { height : availableHeightInContainer } ] = useDOMRect ( ) ;
105- const columns = useMemo ( ( ) => {
106- const sortColumns : SortColumn [ ] = [
107- 'Name and Definition' ,
108- 'Type' ,
109- 'Size' ,
110- 'Usage' ,
111- 'Properties' ,
112- ] ;
113- const _columns = sortColumns . map ( ( name ) => {
114- return (
115- < TableHeader
116- data-testid = { `index-header-${ name } ` }
117- label = { name }
118- key = { name }
119- className = { tableHeaderStyles }
120- handleSort = { ( direction ) => {
121- onSortTable ( name , direction ) ;
122- } }
123- />
124- ) ;
125- } ) ;
126- // Actions column
127- if ( canModifyIndex ) {
128- _columns . push ( < TableHeader label = { '' } className = { tableHeaderStyles } /> ) ;
129- }
130- return _columns ;
131- } , [ canModifyIndex , onSortTable ] ) ;
13285
13386 useEffect ( ( ) => {
13487 /**
@@ -167,76 +120,21 @@ export const IndexesTable: React.FunctionComponent<IndexesTableProps> = ({
167120
168121 return (
169122 < div className = { spaceProviderStyles } { ...rectProps } >
170- < KeylineCard ref = { cardRef } data-testid = "indexes" className = { cardStyles } >
171- < Table
123+ < KeylineCard
124+ ref = { cardRef }
125+ data-testid = { dataTestId }
126+ className = { cardStyles }
127+ >
128+ < Table < Shape >
172129 className = { tableStyles }
173- data = { indexes }
130+ data = { data }
174131 columns = { columns }
175- data-testid = "indexes -list"
176- aria-label = "Indexes List Table"
132+ data-testid = { ` ${ dataTestId } -list` }
133+ aria-label = { ` ${ ariaLabel } List Table` }
177134 >
178- { ( { datum : index } ) => (
179- < Row
180- key = { index . name }
181- data-testid = { `index-row-${ index . name } ` }
182- className = { rowStyles }
183- >
184- < Cell data-testid = "index-name-field" className = { cellStyles } >
185- { index . name }
186- </ Cell >
187- < Cell data-testid = "index-type-field" className = { cellStyles } >
188- < TypeField type = { index . type } extra = { index . extra } />
189- </ Cell >
190- < Cell data-testid = "index-size-field" className = { cellStyles } >
191- < SizeField
192- size = { index . size }
193- relativeSize = { index . relativeSize }
194- />
195- </ Cell >
196- < Cell data-testid = "index-usage-field" className = { cellStyles } >
197- < UsageField usage = { index . usageCount } since = { index . usageSince } />
198- </ Cell >
199- < Cell data-testid = "index-property-field" className = { cellStyles } >
200- < PropertyField
201- cardinality = { index . cardinality }
202- extra = { index . extra }
203- properties = { index . properties }
204- />
205- </ Cell >
206- { /* Index actions column is conditional */ }
207- { canModifyIndex && (
208- < Cell data-testid = "index-actions-field" className = { cellStyles } >
209- { index . name !== '_id_' &&
210- index . extra . status !== 'inprogress' && (
211- < div
212- className = { cx (
213- indexActionsCellStyles ,
214- 'index-actions-cell'
215- ) }
216- >
217- < IndexActions
218- index = { index }
219- serverVersion = { serverVersion }
220- onDeleteIndex = { onDeleteIndex }
221- onHideIndex = { onHideIndex }
222- onUnhideIndex = { onUnhideIndex }
223- > </ IndexActions >
224- </ div >
225- ) }
226- </ Cell >
227- ) }
228- < Row >
229- < Cell
230- className = { cx ( nestedRowCellStyles , cellStyles ) }
231- colSpan = { canModifyIndex ? 6 : 5 }
232- >
233- < IndexKeysBadge keys = { index . fields } />
234- </ Cell >
235- </ Row >
236- </ Row >
237- ) }
135+ { children }
238136 </ Table >
239137 </ KeylineCard >
240138 </ div >
241139 ) ;
242- } ;
140+ }
0 commit comments