Skip to content

Commit b8d18d7

Browse files
author
Kate Wood
committed
adds hasZebraStriping optional prop to IndexTable
adds ZebraStriping to IndexTable in storybook changeset
1 parent 7e778ee commit b8d18d7

File tree

4 files changed

+121
-0
lines changed

4 files changed

+121
-0
lines changed

.changeset/chatty-pets-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris': patch
3+
---
4+
5+
Added option zebra striping to `IndexTable` ([#8595](https://github.com/Shopify/polaris/pull/8595))

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ $loading-panel-height: 53px;
209209
color: var(--p-text-subdued);
210210
}
211211

212+
.ZebraStriping {
213+
tbody .TableRow:nth-child(2n + 1) .TableCell {
214+
background: none;
215+
}
216+
217+
tbody .TableRow:nth-child(2n) .TableCell {
218+
background: var(--p-surface-subdued);
219+
}
220+
}
221+
212222
.TableHeading {
213223
background: var(--p-surface-subdued);
214224
z-index: var(--p-z-index-1);

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,3 +2367,105 @@ export function SmallScreenWithAllOfItsElements() {
23672367
}
23682368
}
23692369
}
2370+
2371+
export function ZebraStriping() {
2372+
const customers = [
2373+
{
2374+
id: '3411',
2375+
url: '#',
2376+
name: 'Mae Jemison',
2377+
location: 'Decatur, USA',
2378+
orders: 20,
2379+
amountSpent: '$2,400',
2380+
},
2381+
{
2382+
id: '2561',
2383+
url: '#',
2384+
name: 'Ellen Ochoa',
2385+
location: 'Los Angeles, USA',
2386+
orders: 30,
2387+
amountSpent: '$140',
2388+
},
2389+
{
2390+
id: '3411',
2391+
url: '#',
2392+
name: 'Mae Jemison',
2393+
location: 'Decatur, USA',
2394+
orders: 20,
2395+
amountSpent: '$2,400',
2396+
},
2397+
{
2398+
id: '2561',
2399+
url: '#',
2400+
name: 'Ellen Ochoa',
2401+
location: 'Los Angeles, USA',
2402+
orders: 30,
2403+
amountSpent: '$140',
2404+
},
2405+
];
2406+
const resourceName = {
2407+
singular: 'customer',
2408+
plural: 'customers',
2409+
};
2410+
2411+
const {selectedResources, allResourcesSelected, handleSelectionChange} =
2412+
useIndexResourceState(customers);
2413+
2414+
const rowMarkup = customers.map(
2415+
({id, name, location, orders, amountSpent}, index) => (
2416+
<IndexTable.Row
2417+
id={id}
2418+
key={id}
2419+
selected={selectedResources.includes(id)}
2420+
position={index}
2421+
>
2422+
<IndexTable.Cell>
2423+
<Text variant="bodyMd" fontWeight="bold" as="span">
2424+
{name}
2425+
</Text>
2426+
</IndexTable.Cell>
2427+
<IndexTable.Cell>{location}</IndexTable.Cell>
2428+
<IndexTable.Cell>
2429+
<Text variant="bodyMd" as="span" alignment="end" numeric>
2430+
{orders}
2431+
</Text>
2432+
</IndexTable.Cell>
2433+
<IndexTable.Cell>
2434+
<Text variant="bodyMd" as="span" alignment="end" numeric>
2435+
{amountSpent}
2436+
</Text>
2437+
</IndexTable.Cell>
2438+
</IndexTable.Row>
2439+
),
2440+
);
2441+
2442+
return (
2443+
<LegacyCard>
2444+
<IndexTable
2445+
resourceName={resourceName}
2446+
itemCount={customers.length}
2447+
selectedItemsCount={
2448+
allResourcesSelected ? 'All' : selectedResources.length
2449+
}
2450+
onSelectionChange={handleSelectionChange}
2451+
headings={[
2452+
{title: 'Name'},
2453+
{title: 'Location'},
2454+
{
2455+
alignment: 'end',
2456+
id: 'order-count',
2457+
title: 'Order count',
2458+
},
2459+
{
2460+
alignment: 'end',
2461+
id: 'amount-spent',
2462+
title: 'Amount spent',
2463+
},
2464+
]}
2465+
hasZebraStriping
2466+
>
2467+
{rowMarkup}
2468+
</IndexTable>
2469+
</LegacyCard>
2470+
);
2471+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ export interface IndexTableBaseProps {
110110
/** Optional dictionary of sort toggle labels for each sortable column, with ascending and descending label,
111111
* with the key as the index of the column */
112112
sortToggleLabels?: IndexTableSortToggleLabels;
113+
/** Add zebra striping to table rows */
114+
hasZebraStriping?: boolean;
113115
}
114116

115117
export interface TableHeadingRect {
@@ -135,6 +137,7 @@ function IndexTableBase({
135137
sortColumnIndex,
136138
onSort,
137139
sortToggleLabels,
140+
hasZebraStriping,
138141
...restProps
139142
}: IndexTableBaseProps) {
140143
const {
@@ -702,6 +705,7 @@ function IndexTableBase({
702705
lastColumnSticky &&
703706
canScrollRight &&
704707
styles['Table-sticky-scrolling'],
708+
hasZebraStriping && styles.ZebraStriping,
705709
);
706710

707711
const emptyStateMarkup = emptyState ? (

0 commit comments

Comments
 (0)