1
0

ResizableHeaderCell.tsx 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import React from 'react';
  2. import { Resizable } from 'react-resizable';
  3. export interface ResizableHeaderCellProps {
  4. [x: string]: any;
  5. onResize?: ResizeFn;
  6. onResizeStart?: ResizeFn;
  7. onResizeStop?: ResizeFn;
  8. width?: number | string;
  9. }
  10. class ResizableHeaderCell extends React.PureComponent<ResizableHeaderCellProps> {
  11. render() {
  12. const { onResize, onResizeStart, onResizeStop, width, ...restProps } = this.props;
  13. if (typeof width !== 'number') {
  14. return <th {...restProps} />;
  15. }
  16. let { children } = restProps;
  17. // Fragment must be used here, otherwise there will be an error (seemingly a [email protected] problem)
  18. children = React.Children.map(children, (child, index) => <React.Fragment key={index}>{child}</React.Fragment>);
  19. return (
  20. <Resizable
  21. width={width}
  22. height={0}
  23. onResize={onResize}
  24. onResizeStart={onResizeStart}
  25. onResizeStop={onResizeStop}
  26. draggableOpts={{ enableUserSelectHack: false }}
  27. >
  28. <th {...restProps}>
  29. {children}
  30. </th>
  31. </Resizable>
  32. );
  33. }
  34. }
  35. export type ResizeFn = (e: React.SyntheticEvent) => any;
  36. export default ResizableHeaderCell;