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

Improves the DataTable RowStatus [FC-0036] #2838

Merged
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
16 changes: 11 additions & 5 deletions src/DataTable/RowStatus.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@ import { FormattedMessage } from 'react-intl';
import DataTableContext from './DataTableContext';

function RowStatus({ className, statusText }) {
const { page, rows, itemCount } = useContext(DataTableContext);
const pageSize = page?.length || rows?.length;
const {
page, rows, itemCount, state,
} = useContext(DataTableContext);
const rowCount = page?.length || rows?.length;
const pageSize = state?.pageSize || 0;
const pageIndex = state?.pageIndex || 0;
const firstRow = pageSize * pageIndex + 1;
const lastRow = firstRow + rowCount - 1;

if (!pageSize) {
if (!rowCount) {
return null;
}
return (
<div className={className} data-testid="row-status">
{statusText || (
<FormattedMessage
id="pgn.DataTable.RowStatus.statusText"
defaultMessage="Showing {pageSize} of {itemCount}."
defaultMessage="Showing {firstRow} - {lastRow} of {itemCount}."
description="A text describing how many rows is shown in the table"
values={{ itemCount, pageSize }}
values={{ itemCount, firstRow, lastRow }}
/>
)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/DataTable/tests/DataTable.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ describe('<DataTable />', () => {
it('displays a control bar', () => {
render(<DataTableWrapper {...props} />);
expect(screen.getByTestId('table-control-bar')).toBeInTheDocument();
expect(screen.getAllByText('Showing 7 of 7.')[0]).toBeInTheDocument();
expect(screen.getAllByText('Showing 1 - 7 of 7.')[0]).toBeInTheDocument();
});

it('displays a table', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/DataTable/tests/RowStatus.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ describe('<RowStatus />', () => {
it('displays the row status with pagination', () => {
const pageSize = 10;
const { getByText } = render(<RowStatusWrapper value={{ ...instance, page: Array(pageSize) }} />);
const statusText = getByText(`Showing ${pageSize} of ${instance.itemCount}.`);
const statusText = getByText(`Showing 1 - ${pageSize} of ${instance.itemCount}.`);
expect(statusText).toBeInTheDocument();
});
it('displays the row status without pagination', () => {
const pageSize = 10;
const { getByText } = render(<RowStatusWrapper value={{ ...instance, rows: Array(pageSize) }} />);
const statusText = getByText(`Showing ${pageSize} of ${instance.itemCount}.`);
const statusText = getByText(`Showing 1 - ${pageSize} of ${instance.itemCount}.`);
expect(statusText).toBeInTheDocument();
});
it('sets class names on the parent', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/DataTable/tests/SmartStatus.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ describe('<SmartStatus />', () => {
const { getByText } = render(
<SmartStatusWrapper value={instance} />,
);
expect(getByText(`Showing ${instance.page.length} of ${itemCount}.`)).toBeInTheDocument();
expect(getByText(`Showing 1 - ${instance.page.length} of ${itemCount}.`)).toBeInTheDocument();
});
it('Shows the number of items on the page if selection is off and there are no filters', () => {
const { getByText } = render(
<SmartStatusWrapper value={instance} />,
);
expect(getByText(`Showing ${instance.page.length} of ${itemCount}.`)).toBeInTheDocument();
expect(getByText(`Showing 1 - ${instance.page.length} of ${itemCount}.`)).toBeInTheDocument();
});
it('shows an alternate selection status', () => {
const altStatusText = 'horses R cool';
Expand Down