Skip to content

feat(AnalyticalTable): add subComponentsBehavior prop #4986

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

Merged
merged 8 commits into from
Aug 30, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { cssVarToRgb, cypressPassThroughTestsFactory } from '@/cypress/support/utils';
import { ThemingParameters } from '@ui5/webcomponents-react-base';
import { useCallback, useEffect, useRef, useState } from 'react';
import type { AnalyticalTablePropTypes } from '../..';
Expand All @@ -6,13 +7,13 @@ import {
AnalyticalTableHooks,
AnalyticalTableScaleWidthMode,
AnalyticalTableSelectionBehavior,
AnalyticalTableSubComponentsBehavior,
Button,
Input
} from '../..';
import { AnalyticalTableSelectionMode, AnalyticalTableVisibleRowCountMode, ValueState } from '../../enums/index.js';
import { useManualRowSelect } from './pluginHooks/useManualRowSelect';
import { useRowDisableSelection } from './pluginHooks/useRowDisableSelection';
import { cssVarToRgb, cypressPassThroughTestsFactory } from '@/cypress/support/utils';

const generateMoreData = (count) => {
return new Array(count).fill('').map((item, index) => ({
Expand Down Expand Up @@ -1476,6 +1477,13 @@ describe('AnalyticalTable', () => {
});

it('render subcomponents', () => {
const renderRowSubComponentLarge = (row) => {
return (
<div title="subcomponent" style={{ height: '200px', width: '100%', display: 'flex', alignItems: 'end' }}>
{`SubComponent ${row.index}`}
</div>
);
};
const renderRowSubComponent = () => {
return <div title="subcomponent">SubComponent</div>;
};
Expand Down Expand Up @@ -1519,7 +1527,7 @@ describe('AnalyticalTable', () => {
data={data}
columns={columns}
renderRowSubComponent={renderRowSubComponent}
alwaysShowSubComponent
subComponentsBehavior={AnalyticalTableSubComponentsBehavior.Visible}
/>
);
cy.findAllByText('SubComponent').should('be.visible').should('have.length', 4);
Expand All @@ -1531,12 +1539,40 @@ describe('AnalyticalTable', () => {
data={data}
columns={columns}
renderRowSubComponent={onlyFirstRowWithSubcomponent}
alwaysShowSubComponent
subComponentsBehavior={AnalyticalTableSubComponentsBehavior.Visible}
/>
);
cy.findByText('SingleSubComponent').should('be.visible').should('have.length', 1);
cy.findByTitle('Expand Node').should('not.exist');
cy.findByTitle('Collapse Node').should('not.exist');

cy.mount(
<AnalyticalTable
data={data}
columns={columns}
renderRowSubComponent={renderRowSubComponentLarge}
visibleRows={3}
subComponentsBehavior={AnalyticalTableSubComponentsBehavior.Visible}
/>
);

cy.findByText('SubComponent 1').should('exist').and('not.be.visible');
cy.findByTitle('Expand Node').should('not.exist');
cy.findByTitle('Collapse Node').should('not.exist');

cy.mount(
<AnalyticalTable
data={data}
columns={columns}
renderRowSubComponent={renderRowSubComponentLarge}
visibleRows={3}
subComponentsBehavior={AnalyticalTableSubComponentsBehavior.IncludeHeight}
/>
);
cy.findByText('SubComponent 1').should('be.visible');
cy.findByText('SubComponent 2').should('be.visible');
cy.findByTitle('Expand Node').should('not.exist');
cy.findByTitle('Collapse Node').should('not.exist');
});

it('pop-in columns', () => {
Expand Down Expand Up @@ -2300,7 +2336,7 @@ describe('AnalyticalTable', () => {
<AnalyticalTable
data={generateMoreData(50)}
columns={columns.slice(0, 2)}
alwaysShowSubComponent
subComponentsBehavior={AnalyticalTableSubComponentsBehavior.Visible}
renderRowSubComponent={renderSubComp}
/>
);
Expand Down Expand Up @@ -2344,7 +2380,7 @@ describe('AnalyticalTable', () => {
<AnalyticalTable
data={generateMoreData(50)}
columns={columns.slice(0, 2)}
alwaysShowSubComponent
subComponentsBehavior={AnalyticalTableSubComponentsBehavior.Visible}
renderRowSubComponent={renderSubComp2}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,14 @@ const InfiniteScrollTable = (props) => {
## AnalyticalTable with subcomponents

Adding custom subcomponents below table rows can be achieved by setting the `renderRowSubComponent` prop.
The prop expects a function with an optional parameter containing the `row` instance, there you can control which row should display subcomponents. If you want to display the subcomponent at the bottom of the row without an expandable container, you can set the `alwaysShowSubComponent` prop to `true`.
The prop expects a function with an optional parameter containing the `row` instance, there you can control which row should display subcomponents. If you want to display the subcomponent at the bottom of the row without an expandable container, you can set `subComponentsBehavior` prop to `"Visible"` or to `"IncludeHeight"`. "Visible" simply adds the subcomponent to the row without including its height in the initial calculation of the table body, whereas "IncludeHeight" does.

### Notes

- When `renderRowSubComponent` is set, `grouping` is disabled.
- When rendering active elements inside the subcomponent, make sure to add the `data-subcomponent-active-element' attribute, otherwise focus behavior won't be consistent.

<ControlsWithNote of={ComponentStories.Subcomponents} include={['alwaysShowSubComponent', 'renderRowSubComponent']} />
<ControlsWithNote of={ComponentStories.Subcomponents} include={['renderRowSubComponent', 'subComponentsBehavior']} />

<Canvas sourceState="none" of={ComponentStories.Subcomponents} />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useVirtualizer } from '@tanstack/react-virtual';
import { clsx } from 'clsx';
import type { MutableRefObject, ReactNode } from 'react';
import React, { useCallback, useMemo, useRef } from 'react';
import type { AnalyticalTablePropTypes } from '../index.js';
import type { ScrollToRefType } from '../interfaces.js';
import { getSubRowsByString } from '../util/index.js';
import { EmptyRow } from './EmptyRow.js';
Expand All @@ -23,13 +24,14 @@ interface VirtualTableBodyProps {
renderRowSubComponent: (row?: Record<string, unknown>) => ReactNode;
popInRowHeight: number;
isRtl: boolean;
markNavigatedRow?: (row?: Record<string, unknown>) => boolean;
markNavigatedRow?: AnalyticalTablePropTypes['markNavigatedRow'];
alwaysShowSubComponent: boolean;
dispatch?: (e: { type: string; payload?: Record<string, unknown> }) => void;
subComponentsHeight?: Record<string, { rowId: string; subComponentHeight?: number }>;
columnVirtualizer: Record<string, any>;
manualGroupBy?: boolean;
subRowsKey: string;
scrollContainerRef?: MutableRefObject<HTMLDivElement>;
}

const measureElement = (el) => el.offsetHeight;
Expand Down Expand Up @@ -57,7 +59,8 @@ export const VirtualTableBody = (props: VirtualTableBodyProps) => {
subComponentsHeight,
columnVirtualizer,
manualGroupBy,
subRowsKey
subRowsKey,
scrollContainerRef
} = props;

const itemCount = Math.max(minRows, rows.length);
Expand Down Expand Up @@ -102,6 +105,7 @@ export const VirtualTableBody = (props: VirtualTableBodyProps) => {
);
return (
<div
ref={scrollContainerRef}
data-component-name="AnalyticalTableBodyScrollableContainer"
style={{
position: 'relative',
Expand Down
Loading