Skip to content

Commit a1c1416

Browse files
authored
[IndexTable] Use correct index in the sticky header (#11383)
<!-- ☝️How to write a good PR title: - Prefix it with [ComponentName] (if applicable), for example: [Button] - Start with a verb, for example: Add, Delete, Improve, Fix… - Give as much context as necessary and as little as possible - Open it as a draft if it’s a work in progress --> ### WHY are these changes introduced? In #11342 I changed the index that's used when calculating the width of the header cell. This didn't take into account selectable rows. This PR checks if the `selectable` prop is set, if so then add `1` to the index, otherwise, use the index directly. I've added a new [storybook story](http://localhost:6006/?path=/story/all-components-indextable--with-long-data-set-selectable) to test this. ### How to 🎩 In Storybook test the following components: - http://localhost:6006/?path=/story/all-components-indextable--with-long-data-set-selectable - http://localhost:6006/?path=/story/all-components-indextable--with-long-data-set-non-selectable 🖥 [Local development instructions](https://github.com/Shopify/polaris/blob/main/README.md#install-dependencies-and-build-workspaces) 🗒 [General tophatting guidelines](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md) 📄 [Changelog guidelines](https://github.com/Shopify/polaris/blob/main/.github/CONTRIBUTING.md#changelog) ### 🎩 checklist - [x] Tested a [snapshot](https://github.com/Shopify/polaris/blob/main/documentation/Releasing.md#-snapshot-releases) - [ ] Tested on [mobile](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md#cross-browser-testing) - [ ] Tested on [multiple browsers](https://help.shopify.com/en/manual/shopify-admin/supported-browsers) - [ ] Tested for [accessibility](https://github.com/Shopify/polaris/blob/main/documentation/Accessibility%20testing.md) - [ ] Updated the component's `README.md` with documentation changes - [ ] [Tophatted documentation](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting%20documentation.md) changes in the style guide
1 parent 384ffdd commit a1c1416

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

.changeset/rotten-dodos-decide.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris': patch
3+
---
4+
5+
[IndexTable] use the correct index for the sticky header when rows are selectable

polaris-react/src/components/IndexTable/IndexTable.stories.tsx

+76-3
Original file line numberDiff line numberDiff line change
@@ -6289,7 +6289,64 @@ export function WithNestedRowsWithThumbnailsOneCellNonSelectable() {
62896289
);
62906290
}
62916291

6292-
export function WithLongDataSet() {
6292+
export function WithLongDataSetNonSelectable() {
6293+
const orders = Array.from(Array(100).keys()).map((i) => ({
6294+
id: `${i}`,
6295+
order: i,
6296+
date: 'Jul 20 at 4:34pm',
6297+
customer: 'Jaydon Stanton',
6298+
total: `$969.44${i}`,
6299+
paymentStatus: <Badge progress="complete">Paid</Badge>,
6300+
fulfillmentStatus: <Badge progress="incomplete">Unfulfilled</Badge>,
6301+
}));
6302+
6303+
const resourceName = {
6304+
singular: 'order',
6305+
plural: 'orders',
6306+
};
6307+
6308+
const rowMarkup = orders.map(
6309+
(
6310+
{id, order, date, customer, total, paymentStatus, fulfillmentStatus},
6311+
index,
6312+
) => (
6313+
<IndexTable.Row id={id} key={id} position={index}>
6314+
<IndexTable.Cell>
6315+
<Text variant="bodyMd" fontWeight="bold" as="span">
6316+
{order}
6317+
</Text>
6318+
</IndexTable.Cell>
6319+
<IndexTable.Cell>{date}</IndexTable.Cell>
6320+
<IndexTable.Cell>{customer}</IndexTable.Cell>
6321+
<IndexTable.Cell>{total}</IndexTable.Cell>
6322+
<IndexTable.Cell>{paymentStatus}</IndexTable.Cell>
6323+
<IndexTable.Cell>{fulfillmentStatus}</IndexTable.Cell>
6324+
</IndexTable.Row>
6325+
),
6326+
);
6327+
6328+
return (
6329+
<LegacyCard>
6330+
<IndexTable
6331+
resourceName={resourceName}
6332+
itemCount={orders.length}
6333+
headings={[
6334+
{title: 'Order'},
6335+
{title: 'Date'},
6336+
{title: 'Customer'},
6337+
{title: 'Total', alignment: 'end'},
6338+
{title: 'Payment status'},
6339+
{title: 'Fulfillment status'},
6340+
]}
6341+
selectable={false}
6342+
>
6343+
{rowMarkup}
6344+
</IndexTable>
6345+
</LegacyCard>
6346+
);
6347+
}
6348+
6349+
export function WithLongDataSetSelectable() {
62936350
const orders = Array.from(Array(100).keys()).map((i) => ({
62946351
id: `${i}`,
62956352
order: i,
@@ -6333,6 +6390,21 @@ export function WithLongDataSet() {
63336390
),
63346391
);
63356392

6393+
const bulkActions = [
6394+
{
6395+
content: 'Add tags',
6396+
onAction: () => console.log('Todo: implement bulk add tags'),
6397+
},
6398+
{
6399+
content: 'Remove tags',
6400+
onAction: () => console.log('Todo: implement bulk remove tags'),
6401+
},
6402+
{
6403+
content: 'Delete customers',
6404+
onAction: () => console.log('Todo: implement bulk delete'),
6405+
},
6406+
];
6407+
63366408
return (
63376409
<LegacyCard>
63386410
<IndexTable
@@ -6343,14 +6415,15 @@ export function WithLongDataSet() {
63436415
}
63446416
onSelectionChange={handleSelectionChange}
63456417
headings={[
6346-
{title: 'Order'},
6418+
{title: 'Order', hidden: true},
63476419
{title: 'Date'},
63486420
{title: 'Customer'},
63496421
{title: 'Total', alignment: 'end'},
63506422
{title: 'Payment status'},
63516423
{title: 'Fulfillment status'},
63526424
]}
6353-
selectable={false}
6425+
bulkActions={bulkActions}
6426+
selectable
63546427
>
63556428
{rowMarkup}
63566429
</IndexTable>

polaris-react/src/components/IndexTable/IndexTable.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -1124,9 +1124,10 @@ function IndexTableBase({
11241124
}
11251125

11261126
function renderStickyHeading(heading: IndexTableHeading, index: number) {
1127+
const position = selectable ? index + 1 : index;
11271128
const headingStyle =
1128-
tableHeadingRects.current && tableHeadingRects.current.length > index
1129-
? {minWidth: tableHeadingRects.current[index].offsetWidth}
1129+
tableHeadingRects.current && tableHeadingRects.current.length > position
1130+
? {minWidth: tableHeadingRects.current[position].offsetWidth}
11301131
: undefined;
11311132
const headingAlignment = heading.alignment || 'start';
11321133

0 commit comments

Comments
 (0)