| 1 |
import { Component } from 'react'; |
| 2 |
import { OUIAProps } from '@patternfly/react-core/dist/esm/helpers/OUIA/ouia'; |
| 3 |
import { debounce, canUseDOM } from '@patternfly/react-core/dist/esm/helpers/util'; |
| 4 |
import { Tr } from './Tr'; |
| 5 |
import { IRow } from './TableTypes'; |
| 6 |
|
| 7 |
// legacy export now, RowWrapperRow can simply be typed as IRow in the future |
| 8 |
export interface RowWrapperRow { |
| 9 |
isOpen?: boolean; |
| 10 |
isExpanded?: boolean; |
| 11 |
isEditable?: boolean; |
| 12 |
} |
| 13 |
|
| 14 |
export interface RowWrapperProps extends OUIAProps { |
| 15 |
children?: React.ReactNode; |
| 16 |
trRef?: React.Ref<any> | Function; |
| 17 |
className?: string; |
| 18 |
onScroll?: React.UIEventHandler; |
| 19 |
onResize?: React.UIEventHandler; |
| 20 |
row?: IRow; |
| 21 |
rowProps?: { |
| 22 |
rowIndex: number; |
| 23 |
rowKey: string; |
| 24 |
}; |
| 25 |
/** Value to set the data-ouia-component-id.*/ |
| 26 |
ouiaId?: number | string; |
| 27 |
} |
| 28 |
|
| 29 |
class RowWrapper extends Component<RowWrapperProps> { |
| 30 |
static displayName = 'RowWrapper'; |
| 31 |
static defaultProps = { |
| 32 |
className: '' as string, |
| 33 |
row: { |
| 34 |
isOpen: undefined as boolean, |
| 35 |
isExpanded: undefined as boolean, |
| 36 |
isHeightAuto: undefined as boolean, |
| 37 |
isEditable: undefined as boolean |
| 38 |
} as IRow, |
| 39 |
rowProps: null as any |
| 40 |
}; |
| 41 |
_unmounted: boolean; |
| 42 |
|
| 43 |
constructor(props: RowWrapperProps) { |
| 44 |
super(props); |
| 45 |
|
| 46 |
if (props.onScroll) { |
| 47 |
this.handleScroll = debounce(this.handleScroll, 100); |
| 48 |
} |
| 49 |
if (props.onResize) { |
| 50 |
this.handleResize = debounce(this.handleResize, 100); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
componentDidMount() { |
| 55 |
this._unmounted = false; |
| 56 |
|
| 57 |
if (canUseDOM) { |
| 58 |
if (this.props.onScroll) { |
| 59 |
window.addEventListener('scroll', this.handleScroll); |
| 60 |
} |
| 61 |
if (this.props.onResize) { |
| 62 |
window.addEventListener('resize', this.handleResize); |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
componentWillUnmount() { |
| 68 |
this._unmounted = true; |
| 69 |
|
| 70 |
if (canUseDOM) { |
| 71 |
if (this.props.onScroll) { |
| 72 |
window.removeEventListener('scroll', this.handleScroll); |
| 73 |
} |
| 74 |
if (this.props.onResize) { |
| 75 |
window.removeEventListener('resize', this.handleResize); |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
handleScroll = (event: Event): void => { |
| 81 |
if (!this._unmounted) { |
| 82 |
this.props.onScroll(event as unknown as React.UIEvent<Element>); |
| 83 |
} |
| 84 |
}; |
| 85 |
|
| 86 |
handleResize = (event: Event) => { |
| 87 |
if (!this._unmounted) { |
| 88 |
this.props.onResize(event as unknown as React.UIEvent<Element>); |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
render() { |
| 93 |
const { |
| 94 |
/* eslint-disable @typescript-eslint/no-unused-vars */ |
| 95 |
onScroll, |
| 96 |
onResize, |
| 97 |
row: { isExpanded, isEditable, isClickable, isRowSelected }, |
| 98 |
rowProps, |
| 99 |
/* eslint-enable @typescript-eslint/no-unused-vars */ |
| 100 |
trRef, |
| 101 |
className, |
| 102 |
ouiaId, |
| 103 |
...props |
| 104 |
} = this.props; |
| 105 |
|
| 106 |
return ( |
| 107 |
<Tr |
| 108 |
{...props} |
| 109 |
ref={trRef as React.Ref<any>} |
| 110 |
isExpanded={isExpanded} |
| 111 |
isEditable={isEditable} |
| 112 |
className={className} |
| 113 |
ouiaId={ouiaId} |
| 114 |
isClickable={isClickable} |
| 115 |
isRowSelected={isRowSelected} |
| 116 |
/> |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
export { RowWrapper }; |