Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[material-ui][Table] Fixed Hover Background - Value Provided is Out of Range and update tests #45069

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .gitpod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This configuration file was automatically generated by Gitpod.
# Please adjust to your needs (see https://www.gitpod.io/docs/introduction/learn-gitpod/gitpod-yaml)
# and commit this file to your remote git repository to share the goodness with others.

# Learn more from ready-to-use templates: https://www.gitpod.io/docs/introduction/getting-started/quickstart

tasks:
- init: pnpm install && pnpm run build
command: pnpm run start


36 changes: 27 additions & 9 deletions packages/mui-material/src/TableRow/TableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ const useUtilityClasses = (ownerState) => {
return composeClasses(slots, getTableRowUtilityClass, classes);
};

const clamp = (value, min, max) => Math.min(Math.max(value, min), max);

const TableRowRoot = styled('tr', {
name: 'MuiTableRow',
slot: 'Root',
Expand All @@ -28,31 +30,47 @@ const TableRowRoot = styled('tr', {

return [styles.root, ownerState.head && styles.head, ownerState.footer && styles.footer];
},
})(
memoTheme(({ theme }) => ({
})
(memoTheme(({ theme }) => {
return {
color: 'inherit',
display: 'table-row',
verticalAlign: 'middle',
// We disable the focus ring for mouse, touch and keyboard users.
outline: 0,
[`&.${tableRowClasses.hover}:hover`]: {
backgroundColor: (theme.vars || theme).palette.action.hover,
},
[`&.${tableRowClasses.selected}`]: {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette.primary.mainChannel} / ${theme.vars.palette.action.selectedOpacity})`
: alpha(theme.palette.primary.main, theme.palette.action.selectedOpacity),
? `rgba(${theme.vars.palette.primary.mainChannel} / ${clamp(
theme.vars.palette.action.selectedOpacity,
0,
1
)})`
: alpha(
theme.palette.primary.main,
clamp(theme.palette.action.selectedOpacity, 0, 1)
),
'&:hover': {
backgroundColor: theme.vars
? `rgba(${theme.vars.palette.primary.mainChannel} / calc(${theme.vars.palette.action.selectedOpacity} + ${theme.vars.palette.action.hoverOpacity}))`
? `rgba(${theme.vars.palette.primary.mainChannel} / ${clamp(
theme.vars.palette.action.selectedOpacity + theme.vars.palette.action.hoverOpacity,
0,
1
)})`
: alpha(
theme.palette.primary.main,
theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity,
clamp(
theme.palette.action.selectedOpacity + theme.palette.action.hoverOpacity,
0,
1
)
),
},
},
})),
);
};
}));


const defaultComponent = 'tr';
/**
Expand Down
65 changes: 65 additions & 0 deletions packages/mui-material/test/integration/TableRowHoverIssue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// test/integration/TableRowHoverBackground.test.js
import * as React from 'react';
import { expect } from 'chai';
import { createRenderer } from '@mui/internal-test-utils';
import Table from '@mui/material/Table';
import TableBody from '@mui/material/TableBody';
import TableCell from '@mui/material/TableCell';
import TableContainer from '@mui/material/TableContainer';
import TableRow from '@mui/material/TableRow';
import Paper from '@mui/material/Paper';
import { createTheme, ThemeProvider } from '@mui/material/styles';

describe('<TableRow> hover background integration', () => {
const { render } = createRenderer();

describe('with theme palette action values', () => {
function TestTable({ hoverOpacity, selectedOpacity }) {
const theme = createTheme({
palette: {
action: {
hoverOpacity,
selectedOpacity,
},
},
});

return (
<ThemeProvider theme={theme}>
<TableContainer component={Paper}>
<Table>
<TableBody>
<TableRow hover>
<TableCell>Row 1</TableCell>
</TableRow>
<TableRow hover>
<TableCell>Row 2</TableCell>
</TableRow>
</TableBody>
</Table>
</TableContainer>
</ThemeProvider>
);
}

it('should render correctly with valid hoverOpacity', () => {
const { container } = render(<TestTable hoverOpacity={0.1} selectedOpacity={0.1} />);
const rows = container.querySelectorAll('tr');
expect(rows).to.have.length(2);
});

it('should handle invalid hoverOpacity gracefully (clamping behavior)', () => {
const { container } = render(<TestTable hoverOpacity={1.5} selectedOpacity={-0.5} />);
const rows = container.querySelectorAll('tr');
expect(rows).to.have.length(2); // Ensure rows render correctly
});

it('should apply clamped hoverOpacity and selectedOpacity in rendering', () => {
const { container } = render(<TestTable hoverOpacity={2} selectedOpacity={1} />);
const rows = container.querySelectorAll('tr');

// Validate rendering - assume clamping happens internally
expect(rows).to.have.length(2);
});
});
});
Loading