Skip to content

Commit b4507af

Browse files
committed
fix callback width returning 0, add id to callback, add aria label to examples, update example resize callback with id
1 parent f89d922 commit b4507af

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

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

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { FunctionComponent, useState } from 'react';
2-
import {
3-
DataViewTable,
4-
DataViewTr,
5-
DataViewTh
6-
} from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
2+
import { DataViewTable, DataViewTr, DataViewTh } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
73
import { Button } from '@patternfly/react-core';
84
import { ActionsColumn } from '@patternfly/react-table';
95

@@ -105,50 +101,54 @@ const rows: DataViewTr[] = repositories.map(({ id, name, branches, prs, workspac
105101
const ouiaId = 'TableExample';
106102

107103
export const ResizableColumnsExample: FunctionComponent = () => {
108-
const [ screenReaderText, setScreenReaderText ] = useState('');
109-
110-
const onResize = (_e, width) => {
104+
const onResize = (
105+
_e: React.MouseEvent | MouseEvent | React.KeyboardEvent | KeyboardEvent | TouchEvent,
106+
id: string | number | undefined,
107+
width: number
108+
) => {
111109
// eslint-disable-next-line no-console
112-
console.log('resized width: ', width.toFixed(0));
113-
setScreenReaderText(`Column ${width.toFixed(0)} pixels`);
110+
console.log(`resized column id: ${id} width to: ${width.toFixed(0)}px`);
114111
};
115112

116113
const columns: DataViewTh[] = [
117114
null,
118115
'Repositories',
119116
{
120-
cell: 'col',
117+
cell: 'Branches',
121118
resizableProps: {
122119
isResizable: true,
123120
onResize,
124-
screenReaderText
125-
}
121+
resizeButtonAriaLabel: 'Resize repositories column'
122+
},
123+
props: { id: 'repositories' }
126124
},
127125
{
128126
cell: 'Pull requests',
129127
resizableProps: {
130128
isResizable: true,
131129
onResize,
132-
screenReaderText
130+
resizeButtonAriaLabel: 'Resize pull requests column'
133131
},
134-
props: { info: { tooltip: 'More information' } }
132+
props: { info: { tooltip: 'More information' }, id: 'pull-requests' }
135133
},
136134
{
137135
cell: 'This is a really long title',
138136
resizableProps: {
139137
isResizable: true,
140138
onResize,
141-
screenReaderText
139+
resizeButtonAriaLabel: 'Resize this is a really long title column'
142140
},
143-
props: { info: { tooltip: 'More information' } }
141+
props: { info: { tooltip: 'More information' }, id: 'this-is-a-really-long-title' }
144142
},
145-
{ cell: 'Last commit',
143+
{
144+
cell: 'Last commit',
146145
resizableProps: {
147146
isResizable: true,
148147
onResize,
149-
screenReaderText
148+
resizeButtonAriaLabel: 'Resize last commit column'
150149
},
151-
props: { sort: { sortBy: {}, columnIndex: 4 } } }
150+
props: { sort: { sortBy: {}, columnIndex: 4 }, id: 'last-commit' }
151+
}
152152
];
153153

154154
return <DataViewTable isResizable aria-label="Repositories table" ouiaId={ouiaId} columns={columns} rows={rows} />;

packages/module/src/DataViewTh/DataViewTh.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,10 @@ const useStyles = createUseStyles({
3737
export interface DataViewThResizableProps {
3838
/** Whether the column is resizable */
3939
isResizable?: boolean;
40-
/** Callback after the column is resized */
40+
/** Callback after the column is resized. Returns the triggering event, the column id passed in via cell props, and the new width of the column. */
4141
onResize?: (
4242
event: ReactMouseEvent | MouseEvent | ReactKeyboardEvent | KeyboardEvent | TouchEvent,
43+
id: string | number | undefined,
4344
width: number
4445
) => void;
4546
/** Width of the column */
@@ -76,6 +77,8 @@ export const DataViewTh: FC<DataViewThProps> = ({
7677
const thRef = useRef<HTMLTableCellElement>(null);
7778

7879
const [ width, setWidth ] = useState(resizableProps?.width ? resizableProps.width : 0);
80+
// Tracks the current column width for the onResize callback, because the width state is not updated until after the resize is complete
81+
const trackedWidth = useRef(0);
7982
const classes = useStyles();
8083

8184
const isResizable = resizableProps?.isResizable || false;
@@ -200,6 +203,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
200203
}
201204

202205
thRef.current?.style.setProperty('min-width', newSize + 'px');
206+
trackedWidth.current = newSize;
203207
setWidth(newSize);
204208
};
205209

@@ -212,7 +216,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
212216
dragOffset.current = 0;
213217

214218
// Call the onResize callback with the new width
215-
onResize && onResize(e, width);
219+
onResize && onResize(e, thProps?.id, trackedWidth.current);
216220

217221
// Handle scroll into view when column drag button is moved off screen
218222
if (resizeButtonRef.current && !isInView.current) {
@@ -233,7 +237,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
233237
dragOffset.current = 0;
234238

235239
// Call the onResize callback with the new width
236-
onResize && onResize(e, width);
240+
onResize && onResize(e, thProps?.id, trackedWidth.current);
237241

238242
document.removeEventListener('touchmove', callbackTouchMove);
239243
document.removeEventListener('touchend', callbackTouchEnd);
@@ -281,7 +285,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
281285

282286
thRef.current?.style.setProperty('min-width', newSize + 'px');
283287
setWidth(newSize);
284-
onResize && onResize(e, newSize);
288+
onResize && onResize(e, thProps?.id, newSize);
285289
};
286290

287291
return (

0 commit comments

Comments
 (0)