Skip to content

Commit ebb15c2

Browse files
committed
update keyboard interaction
1 parent 6455bb8 commit ebb15c2

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { FunctionComponent } from 'react';
2-
import { DataViewTable, DataViewTr, DataViewTh } from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
1+
import { FunctionComponent, useState } from 'react';
2+
import {
3+
DataViewTable,
4+
DataViewTr,
5+
DataViewTh,
6+
isDataViewThObject
7+
} from '@patternfly/react-data-view/dist/dynamic/DataViewTable';
38
import { Button } from '@patternfly/react-core';
49
import { ActionsColumn } from '@patternfly/react-table';
510

@@ -103,8 +108,7 @@ const columns: DataViewTh[] = [
103108
'Repositories',
104109
{
105110
cell: 'col',
106-
// eslint-disable-next-line no-console
107-
resizableProps: { isResizable: true, onResize: (e, width) => console.log('resized width: ', width) }
111+
resizableProps: { isResizable: true }
108112
},
109113
'Pull requests',
110114
{ cell: 'Workspaces', props: { info: { tooltip: 'More information' } } },
@@ -113,6 +117,17 @@ const columns: DataViewTh[] = [
113117

114118
const ouiaId = 'TableExample';
115119

116-
export const ResizableColumnsExample: FunctionComponent = () => (
117-
<DataViewTable isResizable aria-label="Repositories table" ouiaId={ouiaId} columns={columns} rows={rows} />
118-
);
120+
export const ResizableColumnsExample: FunctionComponent = () => {
121+
const [ screenReaderText, setScreenReaderText ] = useState('');
122+
123+
if (columns[2] && isDataViewThObject(columns[2]) && columns[2].resizableProps) {
124+
columns[2].resizableProps.onResize = (_e, width) => {
125+
// eslint-disable-next-line no-console
126+
console.log('resized width: ', width);
127+
setScreenReaderText(`Column ${width} pixels`);
128+
};
129+
columns[2].resizableProps.screenreaderText = screenReaderText;
130+
}
131+
132+
return <DataViewTable isResizable aria-label="Repositories table" ouiaId={ouiaId} columns={columns} rows={rows} />;
133+
};

packages/module/src/DataViewTh/DataViewTh.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface DataViewThResizableProps {
2929
shiftIncrement?: number;
3030
/** Aria label for the resize button */
3131
resizeButtonAriaLabel?: string;
32+
/** Screenreader text for the column */
33+
screenreaderText?: string;
3234
}
3335
export interface DataViewThProps {
3436
/** Cell content */
@@ -50,14 +52,16 @@ export const DataViewTh: FC<DataViewThProps> = ({
5052
}: DataViewThProps) => {
5153
const thRef = useRef<HTMLTableCellElement>(null);
5254

55+
const [ width, setWidth ] = useState(resizableProps?.width ? resizableProps.width : 0);
56+
5357
const isResizable = resizableProps?.isResizable || false;
5458
const increment = resizableProps?.increment || 5;
5559
const shiftIncrement = resizableProps?.shiftIncrement || 25;
5660
const resizeButtonAriaLabel = resizableProps?.resizeButtonAriaLabel || `Resize ${content}`;
5761
const onResize = resizableProps?.onResize || undefined;
62+
const screenreaderText = resizableProps?.screenreaderText || `Column ${width} pixels`;
5863

5964
const resizeButtonRef = useRef<HTMLButtonElement>(null);
60-
const [ width, setWidth ] = useState(resizableProps?.width ? resizableProps.width : 0);
6165
const setInitialVals = useRef(true);
6266
const dragOffset = useRef(0);
6367
const isResizing = useRef(false);
@@ -214,18 +218,19 @@ export const DataViewTh: FC<DataViewThProps> = ({
214218
const handleKeys = (e: ReactKeyboardEvent) => {
215219
const key = e.key;
216220

217-
if (key !== 'Enter' && key !== 'ArrowLeft' && key !== 'ArrowRight' && key !== 'Tab' && key !== 'Space') {
221+
if (key === 'Tab') {
222+
isResizing.current = false;
223+
}
224+
225+
if (key !== 'ArrowLeft' && key !== 'ArrowRight') {
218226
if (isResizing) {
219227
e.preventDefault();
220228
}
221229
return;
222230
}
223231
e.preventDefault();
224232

225-
if (key === 'Enter' || key === 'Tab' || key === 'Space') {
226-
onResize && onResize(e, currWidth);
227-
}
228-
233+
isResizing.current = true;
229234
const isRTL = getLanguageDirection(thRef.current as HTMLElement) === 'rtl';
230235
const columnRect = thRef.current?.getBoundingClientRect();
231236

@@ -247,6 +252,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
247252
}
248253
}
249254
newSize = newSize + delta;
255+
onResize && onResize(e, currWidth);
250256

251257
thRef.current?.style.setProperty('min-width', newSize + 'px');
252258
currWidth = newSize;
@@ -268,6 +274,11 @@ export const DataViewTh: FC<DataViewThProps> = ({
268274
test
269275
</Button>
270276
)}
277+
{isResizable && (
278+
<div aria-live="polite" className="pf-v6-screen-reader">
279+
{screenreaderText}
280+
</div>
281+
)}
271282
</Th>
272283
);
273284
};

0 commit comments

Comments
 (0)