diff --git a/change/@fluentui-react-table-a86be917-7fe4-41a7-a5aa-19d22c2aa1f8.json b/change/@fluentui-react-table-a86be917-7fe4-41a7-a5aa-19d22c2aa1f8.json new file mode 100644 index 0000000000000..863bb42a6e8e5 --- /dev/null +++ b/change/@fluentui-react-table-a86be917-7fe4-41a7-a5aa-19d22c2aa1f8.json @@ -0,0 +1,7 @@ +{ + "type": "patch", + "comment": "fix: TableHeaderCell should not render button when not sortable", + "packageName": "@fluentui/react-table", + "email": "lingfangao@hotmail.com", + "dependentChangeType": "patch" +} diff --git a/packages/react-components/react-table/package.json b/packages/react-components/react-table/package.json index 6c62417940232..effdfe42942c4 100644 --- a/packages/react-components/react-table/package.json +++ b/packages/react-components/react-table/package.json @@ -16,6 +16,8 @@ "bundle-size": "bundle-size measure", "clean": "just-scripts clean", "code-style": "just-scripts code-style", + "e2e": "cypress run --component", + "e2e:local": "cypress open --component", "just": "just-scripts", "lint": "just-scripts lint", "test": "jest --passWithNoTests", diff --git a/packages/react-components/react-table/src/components/DataGridHeaderCell/__snapshots__/DataGridHeaderCell.test.tsx.snap b/packages/react-components/react-table/src/components/DataGridHeaderCell/__snapshots__/DataGridHeaderCell.test.tsx.snap index ddebf4fc22d05..bc66c6cec1c3a 100644 --- a/packages/react-components/react-table/src/components/DataGridHeaderCell/__snapshots__/DataGridHeaderCell.test.tsx.snap +++ b/packages/react-components/react-table/src/components/DataGridHeaderCell/__snapshots__/DataGridHeaderCell.test.tsx.snap @@ -7,14 +7,12 @@ exports[`DataGridHeaderCell renders a default state 1`] = ` role="columnheader" tabindex="0" > - + `; diff --git a/packages/react-components/react-table/src/components/TableHeaderCell/TableHeaderCell.test.tsx b/packages/react-components/react-table/src/components/TableHeaderCell/TableHeaderCell.test.tsx index c6b91c9a198f7..8dd02d432f961 100644 --- a/packages/react-components/react-table/src/components/TableHeaderCell/TableHeaderCell.test.tsx +++ b/packages/react-components/react-table/src/components/TableHeaderCell/TableHeaderCell.test.tsx @@ -65,16 +65,26 @@ describe('TableHeaderCell', () => { expect(container.querySelector('svg')).toBe(null); }); + it('should have interactive button when sortable', () => { + const { getByRole } = render( + + Cell + , + ); + + const button = getByRole('button'); + expect(button?.getAttribute('tabindex')).toEqual('0'); + }); + it('should not have interactive button when not sortable', () => { - const { container } = render( + const { queryByRole } = render( Cell , ); - const button = container.querySelector('button'); - expect(button?.getAttribute('role')).toEqual('presentation'); - expect(button?.getAttribute('tabindex')).toEqual('-1'); + const button = queryByRole('button'); + expect(button).toBeNull(); }); it.each(['ascending', 'descending'])( diff --git a/packages/react-components/react-table/src/components/TableHeaderCell/__snapshots__/TableHeaderCell.test.tsx.snap b/packages/react-components/react-table/src/components/TableHeaderCell/__snapshots__/TableHeaderCell.test.tsx.snap index 6cc1897b4105b..3c62b58246e7d 100644 --- a/packages/react-components/react-table/src/components/TableHeaderCell/__snapshots__/TableHeaderCell.test.tsx.snap +++ b/packages/react-components/react-table/src/components/TableHeaderCell/__snapshots__/TableHeaderCell.test.tsx.snap @@ -5,14 +5,12 @@ exports[`TableHeaderCell renders a default state 1`] = ` - + `; diff --git a/packages/react-components/react-table/src/components/TableHeaderCell/useTableHeaderCell.tsx b/packages/react-components/react-table/src/components/TableHeaderCell/useTableHeaderCell.tsx index 1cf13bfe072a7..beeb43575c6d7 100644 --- a/packages/react-components/react-table/src/components/TableHeaderCell/useTableHeaderCell.tsx +++ b/packages/react-components/react-table/src/components/TableHeaderCell/useTableHeaderCell.tsx @@ -2,9 +2,9 @@ import * as React from 'react'; import { getNativeElementProps, resolveShorthand, useMergedRefs } from '@fluentui/react-utilities'; import { useFocusWithin } from '@fluentui/react-tabster'; import { ArrowUpRegular, ArrowDownRegular } from '@fluentui/react-icons'; +import { useARIAButtonShorthand } from '@fluentui/react-aria'; import type { TableHeaderCellProps, TableHeaderCellState } from './TableHeaderCell.types'; import { useTableContext } from '../../contexts/tableContext'; -import { useARIAButtonShorthand } from '@fluentui/react-aria'; const sortIcons = { ascending: , @@ -27,10 +27,11 @@ export const useTableHeaderCell_unstable = ( const { noNativeElements, sortable } = useTableContext(); const rootComponent = props.as ?? noNativeElements ? 'div' : 'th'; + return { components: { root: rootComponent, - button: 'button', + button: 'div', sortIcon: 'span', aside: 'span', }, @@ -48,11 +49,9 @@ export const useTableHeaderCell_unstable = ( button: useARIAButtonShorthand(props.button, { required: true, defaultProps: { - role: 'presentation', - tabIndex: -1, - type: 'button', - ...(sortable && { - role: undefined, + as: 'div', + ...(!sortable && { + role: 'presentation', tabIndex: undefined, }), }, diff --git a/packages/react-components/react-table/src/components/TableHeaderCell/useTableHeaderCellStyles.styles.ts b/packages/react-components/react-table/src/components/TableHeaderCell/useTableHeaderCellStyles.styles.ts index ac2deb4a7e86b..e680f7c39fa69 100644 --- a/packages/react-components/react-table/src/components/TableHeaderCell/useTableHeaderCellStyles.styles.ts +++ b/packages/react-components/react-table/src/components/TableHeaderCell/useTableHeaderCellStyles.styles.ts @@ -33,6 +33,7 @@ const useFlexLayoutStyles = makeStyles({ */ const useStyles = makeStyles({ root: { + fontWeight: tokens.fontWeightRegular, ...shorthands.padding('0px', tokens.spacingHorizontalS), ...createCustomFocusIndicatorStyle( { @@ -114,6 +115,7 @@ export const useTableHeaderCellStyles_unstable = (state: TableHeaderCellState): state.noNativeElements ? layoutStyles.flex.root : layoutStyles.table.root, state.root.className, ); + state.button.className = mergeClasses( tableHeaderCellClassNames.button, styles.resetButton,