Skip to content

Commit

Permalink
feat(Table): add hover styles for interactive rows (#93)
Browse files Browse the repository at this point in the history
Co-authored-by: kseniyakuzina <kseniyakuzina@yandex-team.ru>
  • Loading branch information
kseniya57 and kseniyakuzina authored Jan 20, 2025
1 parent ad6ef23 commit 371d444
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
14 changes: 11 additions & 3 deletions src/components/Table/Table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,17 @@ $block: '.#{variables.$ns}styled-table';
border-block-end: 1px solid var(--g-color-line-generic);
}

&__row:last-of-type {
#{$block}__cell {
border-block-end: none;
&__row {
&_interactive {
&:hover {
background-color: var(--g-color-base-generic);
}
}

&:last-of-type {
#{$block}__cell {
border-block-end: none;
}
}
}
}
10 changes: 7 additions & 3 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const Table = React.forwardRef(
rowClassName: rowClassNameProp,
headerClassName,
size = 'm',
onRowClick,
...props
}: TableProps<TData, TScrollElement>,
ref: React.Ref<HTMLTableElement>,
Expand Down Expand Up @@ -53,12 +54,14 @@ export const Table = React.forwardRef(
}, [footerCellClassNameProp]);

const rowClassName: TableProps<TData>['rowClassName'] = React.useMemo(() => {
const modifiers = {interactive: Boolean(onRowClick)};

if (typeof rowClassNameProp === 'function') {
return (...args) => b('row', rowClassNameProp(...args));
return (...args) => b('row', modifiers, rowClassNameProp(...args));
}

return b('row', rowClassNameProp);
}, [rowClassNameProp]);
return b('row', modifiers, rowClassNameProp);
}, [onRowClick, rowClassNameProp]);

return (
<BaseTable
Expand All @@ -69,6 +72,7 @@ export const Table = React.forwardRef(
headerCellClassName={headerCellClassName}
headerClassName={b('header', headerClassName)}
rowClassName={rowClassName}
onRowClick={onRowClick}
{...props}
/>
);
Expand Down
10 changes: 10 additions & 0 deletions src/components/Table/__stories__/Table.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ export const Default: StoryObj<typeof DefaultStory> = {
render: DefaultStory,
};

export const WithInteractiveRows: StoryObj<typeof DefaultStory> = {
render: DefaultStory,
};

WithInteractiveRows.args = {
onRowClick: (row) => {
alert(`Row "${row.original.name}" clicked`);
},
};

export const SizeS: StoryObj<typeof SizeSStory> = {
render: SizeSStory,
};
Expand Down
5 changes: 3 additions & 2 deletions src/components/Table/__stories__/stories/DefaultStory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {useTable} from '../../../../hooks';
import {columns} from '../../../BaseTable/__stories__/constants/columns';
import {data} from '../../../BaseTable/__stories__/constants/data';
import {Table} from '../../index';
import type {TableProps} from '../../index';

export const DefaultStory = () => {
export const DefaultStory = (props: Omit<TableProps<(typeof data)[0]>, 'table'>) => {
const table = useTable({
columns,
data,
});

return <Table table={table} />;
return <Table table={table} {...props} />;
};

0 comments on commit 371d444

Please sign in to comment.