1
0

ResizableHeaderCell.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. /** For compatibility with previous versions, the default value is true. If you don't want to resize, set it to false */
  10. resize?: boolean
  11. }
  12. class ResizableHeaderCell extends React.PureComponent<ResizableHeaderCellProps> {
  13. render() {
  14. const { onResize, onResizeStart, onResizeStop, width, resize, ...restProps } = this.props;
  15. if (typeof width !== 'number' || resize === false) {
  16. return <th {...restProps} />;
  17. }
  18. let { children } = restProps;
  19. // Fragment must be used here, otherwise there will be an error (seemingly a [email protected] problem)
  20. children = React.Children.map(children, (child, index) => <React.Fragment key={index}>{child}</React.Fragment>);
  21. return (
  22. <Resizable
  23. width={width as number}
  24. height={0}
  25. onResize={onResize}
  26. onResizeStart={onResizeStart}
  27. onResizeStop={onResizeStop}
  28. draggableOpts={{ enableUserSelectHack: false }}
  29. axis='x'
  30. >
  31. <th {...restProps}>
  32. {children}
  33. </th>
  34. </Resizable>
  35. );
  36. }
  37. }
  38. export type ResizeFn = (e: React.SyntheticEvent) => any;
  39. export default ResizableHeaderCell;