Skip to content

Commit

Permalink
feat(table): add option to pin header and footer
Browse files Browse the repository at this point in the history
  • Loading branch information
erzhan-temir-mamyrov committed Mar 22, 2023
1 parent 151c4a5 commit 7001b97
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
10 changes: 8 additions & 2 deletions packages/react/src/components/Table/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ const propTypes = {
preserveCellWhiteSpace: PropTypes.bool,
/** display icon button in filter row */
hasFilterRowIcon: PropTypes.bool,
/** Freezes table header and footer */
pinHeaderAndFooter: PropTypes.bool,
}),

/** Size prop from Carbon to shrink row height (and header height in some instances) */
Expand Down Expand Up @@ -409,6 +411,7 @@ export const defaultProps = (baseProps) => ({
wrapCellText: 'always',
preserveCellWhiteSpace: false,
hasFilterRowIcon: false,
pinHeaderAndFooter: false,
},
size: undefined,
view: {
Expand Down Expand Up @@ -840,7 +843,9 @@ const Table = (props) => {
<TableContainer
style={style}
data-testid={`${id || testId}-table-container`}
className={classnames(className, `${iotPrefix}--table-container`)}
className={classnames(className, `${iotPrefix}--table-container`, {
[`${iotPrefix}--table-container--pin-header-and-footer`]: options.pinHeaderAndFooter,
})}
>
{
/* If there is no items being rendered in the toolbar, don't render the toolbar */
Expand Down Expand Up @@ -1031,7 +1036,8 @@ const Table = (props) => {
'useAutoTableLayoutForResize',
'hasMultiSort',
'preserveColumnWidths',
'hasFilterRowIcon'
'hasFilterRowIcon',
'pinHeaderAndFooter'
),
hasRowExpansion: !!options.hasRowExpansion,
wrapCellText: options.wrapCellText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ class FilterHeaderRow extends Component {
filterRowIconDescription: PropTypes.string,
/** call back function for when icon button in filter row is clicked (evt) => {} */
onFilterRowIconClick: PropTypes.func.isRequired,
/** Freezes table header and footer */
pinHeaderAndFooter: PropTypes.bool,
};

static defaultProps = {
Expand All @@ -151,6 +153,7 @@ class FilterHeaderRow extends Component {
showColumnGroups: false,
filterRowIcon: null,
filterRowIconDescription: 'Edit filters',
pinHeaderAndFooter: false,
};

state = {
Expand Down Expand Up @@ -239,10 +242,20 @@ class FilterHeaderRow extends Component {
const tableTopOffset = this.filterCellRef.current
.closest(`.${iotPrefix}--table-container`)
.getBoundingClientRect().top;
this.setState((prevState) => ({
...prevState,
filterIconTopOffset: siblingTopOffset - tableTopOffset,
}));
this.setState((prevState) => {
if (this.props.pinHeaderAndFooter) {
// Subtract 48px of toolbar height
return {
...prevState,
filterIconTopOffset: siblingTopOffset - tableTopOffset - 48,
};
}

return {
...prevState,
filterIconTopOffset: siblingTopOffset - tableTopOffset,
};
});
}
};

Expand Down
4 changes: 4 additions & 0 deletions packages/react/src/components/Table/TableHead/TableHead.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ const propTypes = {
/** Preserves the widths of existing columns when one or more columns are added, removed, hidden, shown or resized. */
preserveColumnWidths: PropTypes.bool,
hasFilterRowIcon: PropTypes.bool,
/** Freezes table header and footer */
pinHeaderAndFooter: PropTypes.bool,
}),
/** List of columns */
columns: TableColumnsPropTypes.isRequired,
Expand Down Expand Up @@ -205,6 +207,7 @@ const TableHead = ({
useAutoTableLayoutForResize,
preserveColumnWidths,
useRadioButtonSingleSelect,
pinHeaderAndFooter,
},
columns,
columnGroups,
Expand Down Expand Up @@ -689,6 +692,7 @@ const TableHead = ({
filterRowIcon={filterRowIcon}
filterRowIconDescription={filterRowIconDescription}
onFilterRowIconClick={onFilterRowIconClick}
pinHeaderAndFooter={pinHeaderAndFooter}
/>
)}
{activeBar === 'column' && (
Expand Down
19 changes: 19 additions & 0 deletions packages/react/src/components/Table/_table.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@ td.#{$iot-prefix}--table__cell--sortable {
.addons-iot-table-container {
overflow-x: auto;
}

&--pin-header-and-footer {
height: 100%;

.addons-iot-table-container,
.#{$prefix}--data-table-content {
height: inherit;
}

thead {
position: sticky;
top: 0;
z-index: 4;
}

.#{$prefix}--table-toolbar {
z-index: 5;
}
}
}

.#{$iot-prefix}-table-container--dropdown-height-fix {
Expand Down

0 comments on commit 7001b97

Please sign in to comment.