-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathAutoResizer.tsx
More file actions
60 lines (55 loc) · 1.69 KB
/
AutoResizer.tsx
File metadata and controls
60 lines (55 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import React from 'react';
import AutoSizer from 'react-virtualized-auto-sizer';
/**
* Decorator component that automatically adjusts the width and height of a single child
*/
const AutoResizer: React.FunctionComponent<AutoResizerProps> = ({ className, width, height, children, onResize }) => {
const disableWidth = typeof width === 'number';
const disableHeight = typeof height === 'number';
if (disableWidth && disableHeight) {
return (
<div className={className} style={{ width, height, position: 'relative' }}>
{children({ width, height })}
</div>
);
}
return (
<AutoSizer className={className} disableWidth={disableWidth} disableHeight={disableHeight} onResize={onResize}>
{(size) =>
children({
width: disableWidth ? width : size.width,
height: disableHeight ? height : size.height,
})
}
</AutoSizer>
);
};
interface ChildrenArgs {
width: number;
height: number;
}
export interface AutoResizerProps {
/**
* Class name for the component
*/
className?: string;
/**
* the width of the component, will be the container's width if not set
*/
width?: number;
/**
* the height of the component, will be the container's width if not set
*/
height?: number;
/**
* A callback function to render the children component
* The handler is of the shape of `({ width, height }) => node`
*/
children: (args: ChildrenArgs) => React.ReactNode;
/**
* A callback function when the size of the table container changed if the width and height are not set
* The handler is of the shape of `({ width, height }) => *`
*/
onResize?: (args: ChildrenArgs) => void;
}
export default AutoResizer;