Skip to content

Commit e974b13

Browse files
committed
fix spread props & lint, shortcut useeffects for non-resize cols, mock observer for jest env
1 parent ad99460 commit e974b13

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

config/setupTests.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,15 @@ global.MutationObserver = class {
1111
observe(element, initObject) {}
1212
};
1313

14+
global.IntersectionObserver = class {
15+
constructor(callback, options) {}
16+
disconnect() {}
17+
observe(element) {}
18+
unobserve(element) {}
19+
};
20+
1421
jest.mock('react', () => ({
1522
...jest.requireActual('react'),
16-
useLayoutEffect: jest.requireActual('react').useEffect,
23+
useLayoutEffect: jest.requireActual('react').useEffect
1724
}));
18-
Element.prototype.scrollTo = () => {};
25+
Element.prototype.scrollTo = () => {};

packages/module/patternfly-docs/content/extensions/data-view/examples/Table/DataViewTableResizableColumnsExample.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const columns: DataViewTh[] = [
103103
'Repositories',
104104
{
105105
cell: 'col',
106+
// eslint-disable-next-line no-console
106107
resizableProps: { isResizable: true, onResize: (e, width) => console.log('resized width: ', width) }
107108
},
108109
'Pull requests',

packages/module/src/DataViewTh/DataViewTh.tsx

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ export const DataViewTh: FC<DataViewThProps> = ({
4242
increment: 10
4343
},
4444
content,
45-
...props
45+
props: thProps,
46+
...restProps
4647
}: DataViewThProps) => {
4748
const thRef = useRef<HTMLTableCellElement>(null);
4849

4950
const isResizable = resizableProps.isResizable;
5051
const resizeButtonRef = useRef<HTMLButtonElement>(null);
51-
const [ width, setWidth ] = useState(resizableProps.width ? resizableProps.width : 0);
52+
const [width, setWidth] = useState(resizableProps.width ? resizableProps.width : 0);
5253
const increment = resizableProps.increment || 5;
5354
const onResize = resizableProps.onResize;
5455
const setInitialVals = useRef(true);
@@ -58,9 +59,13 @@ export const DataViewTh: FC<DataViewThProps> = ({
5859
let currWidth = 0;
5960

6061
useEffect(() => {
62+
if (!isResizable) {
63+
return;
64+
}
65+
6166
const observed = resizeButtonRef.current;
6267
const observer = new IntersectionObserver(
63-
([ entry ]) => {
68+
([entry]) => {
6469
isInView.current = entry.isIntersecting;
6570
},
6671
{ threshold: 0.3 }
@@ -78,11 +83,11 @@ export const DataViewTh: FC<DataViewThProps> = ({
7883
}, []);
7984

8085
useEffect(() => {
81-
if (setInitialVals.current && width === 0) {
86+
if (isResizable && setInitialVals.current && width === 0) {
8287
setWidth(thRef.current?.getBoundingClientRect().width || 0);
8388
setInitialVals.current = false;
8489
}
85-
}, [ setInitialVals ]);
90+
}, [isResizable, setInitialVals]);
8691

8792
const setDragOffset = (e: ReactMouseEvent | ReactTouchEvent) => {
8893
const isRTL = getLanguageDirection(thRef.current as HTMLElement) === 'rtl';
@@ -190,7 +195,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
190195

191196
// Call the onResize callback with the new width
192197
onResize && onResize(e, currWidth);
193-
198+
194199
document.removeEventListener('touchmove', callbackTouchMove);
195200
document.removeEventListener('touchend', callbackTouchEnd);
196201
};
@@ -202,7 +207,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
202207

203208
const handleKeys = (e: React.KeyboardEvent) => {
204209
const key = e.key;
205-
210+
206211
if (key !== 'Escape' && key !== 'Enter' && key !== 'ArrowLeft' && key !== 'ArrowRight') {
207212
if (isResizing) {
208213
e.preventDefault();
@@ -217,7 +222,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
217222

218223
const isRTL = getLanguageDirection(thRef.current as HTMLElement) === 'rtl';
219224
const columnRect = thRef.current?.getBoundingClientRect();
220-
225+
221226
let newSize = columnRect?.width || 0;
222227
let delta = 0;
223228
if (key === 'ArrowRight') {
@@ -240,7 +245,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
240245
};
241246

242247
return (
243-
<Th {...props} style={{ minWidth: width }} ref={thRef}>
248+
<Th {...thProps} {...restProps} style={width > 0 ? { minWidth: width } : undefined} ref={thRef}>
244249
{content}
245250
{isResizable && (
246251
<Button

0 commit comments

Comments
 (0)