Skip to content

Commit

Permalink
fix(DataTable): backport tab fixes to v10 (#14675)
Browse files Browse the repository at this point in the history
* fix(DataTable): backport fix to v10

* test(snapshot): update snapshots
  • Loading branch information
tw15egan authored Sep 21, 2023
1 parent fe6f69a commit 23bdcfc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
overflow-x: auto;
}

.#{$prefix}--data-table-content:focus {
@include focus-outline('outline');
}

//----------------------------------------------------------------------------
// Table title text
//----------------------------------------------------------------------------
Expand Down
34 changes: 31 additions & 3 deletions packages/react/src/components/DataTable/Table.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*/

import * as FeatureFlags from '@carbon/feature-flags';
import React from 'react';
import React, { useCallback, useLayoutEffect, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';
import debounce from 'lodash.debounce';
import deprecate from '../../prop-types/deprecate.js';
import { useWindowEvent } from '../../internal/useEvent';
import { usePrefix } from '../../internal/usePrefix';

export const Table = ({
Expand All @@ -25,6 +27,8 @@ export const Table = ({
...other
}) => {
const prefix = usePrefix();
const [isScrollable, setIsScrollable] = useState(true);
const tableRef = useRef(null);
const componentClass = cx(`${prefix}--data-table`, className, {
[`${prefix}--data-table--${size}`]: size,
[`${prefix}--data-table--sort`]: isSortable,
Expand All @@ -34,9 +38,33 @@ export const Table = ({
[`${prefix}--data-table--sticky-header`]: stickyHeader,
[`${prefix}--data-table--visible-overflow-menu`]: !overflowMenuOnHover,
});

// Used to set a tabIndex when the Table is horizontally scrollable
const setTabIndex = useCallback(() => {
const tableContainer = tableRef?.current?.parentNode;
const tableHeader = tableRef?.current?.firstChild;

if (tableHeader?.scrollWidth > tableContainer?.clientWidth) {
setIsScrollable(true);
} else {
setIsScrollable(false);
}
}, []);

const debouncedSetTabIndex = debounce(setTabIndex, 100);

useWindowEvent('resize', debouncedSetTabIndex);

useLayoutEffect(() => {
setTabIndex();
}, [setTabIndex]);

const table = (
<div className={`${prefix}--data-table-content`}>
<table {...other} className={componentClass}>
<div
className={`${prefix}--data-table-content`}
// eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
tabIndex={isScrollable ? 0 : undefined}>
<table {...other} className={componentClass} ref={tableRef}>
{children}
</table>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exports[`DataTable.Table should support enable sticky header 1`] = `
>
<div
className="bx--data-table-content"
tabIndex={0}
>
<table
className="bx--data-table bx--data-table--no-border bx--data-table--sticky-header"
Expand Down
4 changes: 4 additions & 0 deletions packages/styles/scss/components/data-table/_data-table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
overflow-x: auto;
}

.#{$prefix}--data-table-content:focus {
@include focus-outline('outline');
}

//----------------------------------------------------------------------------
// Table title text
//----------------------------------------------------------------------------
Expand Down

0 comments on commit 23bdcfc

Please sign in to comment.