Skip to content

Commit

Permalink
feat(Table): rework render props (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
beliarh authored Jul 23, 2024
1 parent a87a5d9 commit 3360374
Show file tree
Hide file tree
Showing 40 changed files with 494 additions and 446 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"prepare": "husky",
"prepublishOnly": "npm run build",
"start": "storybook dev",
"test": "jest",
"test": "jest --passWithNoTests",
"test-storybook": "test-storybook",
"test:coverage": "jest --coverage",
"test:watch": "jest --watchAll",
Expand Down
113 changes: 0 additions & 113 deletions src/components/BaseRow/BaseRow.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/components/BaseRow/index.ts

This file was deleted.

26 changes: 17 additions & 9 deletions src/components/Cell/Cell.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import React from 'react';

import type {Cell as CellProperties} from '@tanstack/react-table';
import type {Cell as CellType} from '@tanstack/react-table';
import {flexRender} from '@tanstack/react-table';

import {getCellClassModes} from '../../utils/getCellClassModes';
import {getCellStyles} from '../../utils/getCellStyles';
import {getCellClassModes, getCellStyles} from '../../utils';
import {b} from '../Table/Table.classname';

export interface CellProps<TData> {
cell: CellProperties<TData, unknown>;
className?: string;
export interface CellProps<TData> extends React.TdHTMLAttributes<HTMLTableCellElement> {
cell?: CellType<TData, unknown>;
}

export const Cell = <TData,>({cell, className}: CellProps<TData>) => {
export const Cell = <TData,>({
cell,
children,
className,
style,
...restProps
}: CellProps<TData>) => {
return (
<td className={b('cell', getCellClassModes(cell), className)} style={getCellStyles(cell)}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
<td
className={b('cell', getCellClassModes(cell), className)}
style={getCellStyles(cell, style)}
{...restProps}
>
{cell ? flexRender(cell.column.columnDef.cell, cell.getContext()) : children}
</td>
);
};
25 changes: 11 additions & 14 deletions src/components/DraggableRow/DraggableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import {useSortable} from '@dnd-kit/sortable';
import {useForkRef} from '@gravity-ui/uikit';

import {useDraggableRowDepth, useDraggableRowStyle} from '../../hooks';
import {toDataAttributes} from '../../utils';
import type {BaseRowProps} from '../BaseRow';
import {BaseRow} from '../BaseRow';
import type {RowProps} from '../Row';
import {Row} from '../Row';
import {SortableListContext} from '../SortableListContext';
import {TableContext} from '../TableContext';

export interface DraggableRowProps<TData, TScrollElement extends Element | Window = HTMLDivElement>
extends BaseRowProps<TData, TScrollElement> {}
extends RowProps<TData, TScrollElement> {}

export const DraggableRow = React.forwardRef(
<TData, TScrollElement extends Element | Window = HTMLDivElement>(
Expand Down Expand Up @@ -50,24 +49,22 @@ export const DraggableRow = React.forwardRef(
});

const getDraggableRowDataAttributes = React.useCallback<
NonNullable<BaseRowProps<TData>['getRowAttributes']>
NonNullable<RowProps<TData>['getRowAttributes']>
>(
(draggableRow) => ({
...getRowAttributes?.(draggableRow),
...toDataAttributes({
key: draggableRow.id,
depth,
draggable: true,
dragging: isDragging,
dragActive: isDragActive,
expanded: isDragActive && isParent,
}),
'data-key': draggableRow.id,
'data-depth': depth,
'data-draggable': true,
'data-dragging': isDragging,
'data-drag-active': isDragActive,
'data-expanded': isDragActive && isParent,
}),
[getRowAttributes, depth, isDragging, isDragActive, isParent],
);

return (
<BaseRow
<Row
ref={handleRowRef}
getRowAttributes={getDraggableRowDataAttributes}
row={row}
Expand Down
3 changes: 3 additions & 0 deletions src/components/GroupHeader/GroupHeader.classname.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {block} from '../../utils';

export const b = block('group-header');
40 changes: 40 additions & 0 deletions src/components/GroupHeader/GroupHeader.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@use '../variables';

$block: '.#{variables.$ns}group-header';

#{$block} {
position: sticky;
inset-inline-start: 0;
margin: 0;

&__button {
appearance: none;

display: flex;
gap: 8px;
padding: 0;
width: 100%;

border: none;
outline: none;
background: inherit;
cursor: pointer;
}

&__icon {
display: inline-block;
vertical-align: middle;
transform: rotate(-90deg);
transition: transform 0.1s ease-out;

&_expanded {
transform: rotate(0);
}
}

&__content {
display: inline-flex;
gap: 4px;
font-weight: 500;
}
}
37 changes: 37 additions & 0 deletions src/components/GroupHeader/GroupHeader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';

import type {Row} from '@tanstack/react-table';

import {b} from './GroupHeader.classname';

import './GroupHeader.scss';

export interface GroupHeaderProps<TData> {
className?: string;
getGroupTitle?: (row: Row<TData>) => React.ReactNode;
row: Row<TData>;
}

export const GroupHeader = <TData,>({className, getGroupTitle, row}: GroupHeaderProps<TData>) => {
return (
<h2 className={b(null, className)}>
<button className={b('button')} onClick={row.getToggleExpandedHandler()}>
<svg
className={b('icon', {expanded: row.getIsExpanded()})}
viewBox="0 0 16 16"
width="16"
height="16"
>
<path
d="M2.97 5.47a.75.75 0 0 1 1.06 0L8 9.44l3.97-3.97a.75.75 0 1 1 1.06 1.06l-4.5 4.5a.75.75 0 0 1-1.06 0l-4.5-4.5a.75.75 0 0 1 0-1.06Z"
fill="currentColor"
/>
</svg>
<span className={b('content')}>
<span className={b('title')}>{getGroupTitle?.(row) ?? row.id}</span>
<span className={b('total')}>{row.subRows.length}</span>
</span>
</button>
</h2>
);
};
1 change: 1 addition & 0 deletions src/components/GroupHeader/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './GroupHeader';
Loading

0 comments on commit 3360374

Please sign in to comment.