@@ -11,47 +11,53 @@ import {
1111} from 'react' ;
1212import { Th , ThProps } from '@patternfly/react-table' ;
1313import { Button , getLanguageDirection } from '@patternfly/react-core' ;
14-
14+ export interface DataViewThResizableProps {
15+ /** Whether the column is resizable */
16+ isResizable ?: boolean ;
17+ /** Callback after the column is resized */
18+ onResize ?: (
19+ event : ReactMouseEvent | MouseEvent | ReactKeyboardEvent | KeyboardEvent | TouchEvent ,
20+ width : number
21+ ) => void ;
22+ /** Width of the column */
23+ width ?: number ;
24+ /** Minimum width of the column */
25+ minWidth ?: number ;
26+ /** Increment for keyboard navigation */
27+ increment ?: number ;
28+ /** Increment for keyboard navigation while shift is held */
29+ shiftIncrement ?: number ;
30+ /** Aria label for the resize button */
31+ resizeButtonAriaLabel ?: string ;
32+ }
1533export interface DataViewThProps {
1634 /** Cell content */
1735 content : ReactNode ;
1836 /** Resizable props */
19- resizableProps ?: {
20- /** Whether the column is resizable */
21- isResizable ?: boolean ;
22- /** Callback after the column is resized */
23- onResize ?: (
24- event : ReactMouseEvent | MouseEvent | ReactKeyboardEvent | KeyboardEvent | TouchEvent ,
25- width : number
26- ) => void ;
27- /** Width of the column */
28- width ?: number ;
29- /** Minimum width of the column */
30- minWidth ?: number ;
31- /** Increment for keyboard navigation */
32- increment ?: number ;
33- } ;
37+ resizableProps ?: DataViewThResizableProps ;
38+ /** @hide Indicates whether the table has resizable columns */
39+ hasResizableColumns ?: boolean ;
3440 /** Props passed to Th */
3541 props ?: ThProps ;
3642}
3743
3844export const DataViewTh : FC < DataViewThProps > = ( {
39- resizableProps = {
40- isResizable : false ,
41- width : 0 ,
42- increment : 10
43- } ,
4445 content,
46+ resizableProps = { } ,
47+ hasResizableColumns = false ,
4548 props : thProps ,
4649 ...restProps
4750} : DataViewThProps ) => {
4851 const thRef = useRef < HTMLTableCellElement > ( null ) ;
4952
50- const isResizable = resizableProps . isResizable ;
53+ const isResizable = resizableProps ?. isResizable || false ;
54+ const increment = resizableProps ?. increment || 5 ;
55+ const shiftIncrement = resizableProps ?. shiftIncrement || 25 ;
56+ const resizeButtonAriaLabel = resizableProps ?. resizeButtonAriaLabel || `Resize ${ content } ` ;
57+ const onResize = resizableProps ?. onResize || undefined ;
58+
5159 const resizeButtonRef = useRef < HTMLButtonElement > ( null ) ;
52- const [ width , setWidth ] = useState ( resizableProps . width ? resizableProps . width : 0 ) ;
53- const increment = resizableProps . increment || 5 ;
54- const onResize = resizableProps . onResize ;
60+ const [ width , setWidth ] = useState ( resizableProps ?. width ? resizableProps . width : 0 ) ;
5561 const setInitialVals = useRef ( true ) ;
5662 const dragOffset = useRef ( 0 ) ;
5763 const isResizing = useRef ( false ) ;
@@ -83,11 +89,11 @@ export const DataViewTh: FC<DataViewThProps> = ({
8389 } , [ ] ) ;
8490
8591 useEffect ( ( ) => {
86- if ( isResizable && setInitialVals . current && width === 0 ) {
92+ if ( ( isResizable || hasResizableColumns ) && setInitialVals . current && width === 0 ) {
8793 setWidth ( thRef . current ?. getBoundingClientRect ( ) . width || 0 ) ;
8894 setInitialVals . current = false ;
8995 }
90- } , [ isResizable , setInitialVals ] ) ;
96+ } , [ isResizable , hasResizableColumns , setInitialVals ] ) ;
9197
9298 const setDragOffset = ( e : ReactMouseEvent | ReactTouchEvent ) => {
9399 const isRTL = getLanguageDirection ( thRef . current as HTMLElement ) === 'rtl' ;
@@ -205,18 +211,18 @@ export const DataViewTh: FC<DataViewThProps> = ({
205211 const callbackTouchMove = useCallback ( handleTouchMove , [ ] ) ;
206212 const callbackMouseUp = useCallback ( handleMouseup , [ ] ) ;
207213
208- const handleKeys = ( e : React . KeyboardEvent ) => {
214+ const handleKeys = ( e : ReactKeyboardEvent ) => {
209215 const key = e . key ;
210216
211- if ( key !== 'Escape ' && key !== 'Enter ' && key !== 'ArrowLeft ' && key !== 'ArrowRight ' ) {
217+ if ( key !== 'Enter ' && key !== 'ArrowLeft ' && key !== 'ArrowRight ' && key !== 'Tab' && key !== 'Space ') {
212218 if ( isResizing ) {
213219 e . preventDefault ( ) ;
214220 }
215221 return ;
216222 }
217223 e . preventDefault ( ) ;
218224
219- if ( key === 'Escape ' || key === 'Enter ' ) {
225+ if ( key === 'Enter ' || key === 'Tab' || key === 'Space ') {
220226 onResize && onResize ( e , currWidth ) ;
221227 }
222228
@@ -225,17 +231,19 @@ export const DataViewTh: FC<DataViewThProps> = ({
225231
226232 let newSize = columnRect ?. width || 0 ;
227233 let delta = 0 ;
234+ const _increment = e . shiftKey ? shiftIncrement : increment ;
235+
228236 if ( key === 'ArrowRight' ) {
229237 if ( isRTL ) {
230- delta = - increment ;
238+ delta = - _increment ;
231239 } else {
232- delta = increment ;
240+ delta = _increment ;
233241 }
234242 } else if ( key === 'ArrowLeft' ) {
235243 if ( isRTL ) {
236- delta = increment ;
244+ delta = _increment ;
237245 } else {
238- delta = - increment ;
246+ delta = - _increment ;
239247 }
240248 }
241249 newSize = newSize + delta ;
@@ -255,6 +263,7 @@ export const DataViewTh: FC<DataViewThProps> = ({
255263 onKeyDown = { handleKeys }
256264 onTouchStart = { handleTouchStart }
257265 style = { { float : 'inline-end' } }
266+ aria-label = { resizeButtonAriaLabel }
258267 >
259268 test
260269 </ Button >
0 commit comments