Skip to content

Commit

Permalink
[IndexTable] Add custom paginated select all text (#11785)
Browse files Browse the repository at this point in the history
### WHY are these changes introduced?

Addresses Shopify/web#122239

We need to update the IndexTable to be able to pass a custom
`paginatedSelectAllText` string, like we do
`paginatedSelectAllActionText` string. The prop added to the
`IndexTable` is `defaultPaginatedSelectAllText`.

<img width="1739" alt="Screenshot 2024-04-03 at 10 25 44"
src="https://github.com/Shopify/polaris/assets/2562596/d8a92876-6861-4134-af85-c7e065ad8be0">


### How to 🎩

🖥 [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)
- [x] Tested on
[mobile](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md#cross-browser-testing)
- [x] Tested on [multiple
browsers](https://help.shopify.com/en/manual/shopify-admin/supported-browsers)
- [x] Tested for
[accessibility](https://github.com/Shopify/polaris/blob/main/documentation/Accessibility%20testing.md)
- [x] Updated the component's `README.md` with documentation changes
- [x] [Tophatted
documentation](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting%20documentation.md)
changes in the style guide

---------

Co-authored-by: Chloe Rice <chloerice@users.noreply.github.com>
  • Loading branch information
mrcthms and chloerice committed Apr 4, 2024
1 parent 2adcbb5 commit 2a2f635
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shaggy-items-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/polaris': minor
---

Added the `defaultPaginatedSelectAllText` prop to `IndexTable` to support customizing the label of the checkbox in the header that selects all rows across pages when the table `hasMoreItems`
2 changes: 2 additions & 0 deletions polaris-react/src/components/IndexProvider/IndexProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function IndexProvider({
hasMoreItems,
condensed,
selectable: isSelectableIndex = true,
paginatedSelectAllText: defaultPaginatedSelectAllText,
}: IndexProviderProps) {
const {
paginatedSelectAllText,
Expand All @@ -32,6 +33,7 @@ export function IndexProvider({
itemCount,
hasMoreItems,
resourceName: passedResourceName,
defaultPaginatedSelectAllText,
});
const handleSelectionChange = useHandleBulkSelection({onSelectionChange});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,7 @@ export function WithBulkActionsAndSelectionAcrossPages() {
title: 'Amount spent',
},
]}
paginatedSelectAllText="Select everything in this store"
>
{rowMarkup}
</IndexTable>
Expand Down
4 changes: 4 additions & 0 deletions polaris-react/src/components/IndexTable/IndexTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export interface IndexTableBaseProps {
emptyState?: React.ReactNode;
sort?: React.ReactNode;
paginatedSelectAllActionText?: string;
paginatedSelectAllText?: string;
lastColumnSticky?: boolean;
selectable?: boolean;
/** List of booleans, which maps to whether sorting is enabled or not for each column. Defaults to false for all columns. */
Expand Down Expand Up @@ -177,6 +178,7 @@ function IndexTableBase({
selectedItemsCount,
condensed,
} = useIndexValue();

const handleSelectionChange = useIndexSelectionChange();
const i18n = useI18n();

Expand Down Expand Up @@ -1122,6 +1124,7 @@ export function IndexTable({
hasMoreItems,
condensed,
onSelectionChange,
paginatedSelectAllText,
...indexTableBaseProps
}: IndexTableProps) {
return (
Expand All @@ -1135,6 +1138,7 @@ export function IndexTable({
hasMoreItems={hasMoreItems}
condensed={condensed}
onSelectionChange={onSelectionChange}
paginatedSelectAllText={paginatedSelectAllText}
>
<IndexTableBase {...indexTableBaseProps}>{children}</IndexTableBase>
</IndexProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ describe('<IndexTable>', () => {
);
});

it('renders a custom select all string if present', () => {
it('renders a custom select all action string if present', () => {
const onSelectionChangeSpy = jest.fn();
const customString = 'Foo bar baz';
const index = mountWithApp(
Expand All @@ -426,6 +426,26 @@ describe('<IndexTable>', () => {
expect(index.find(BulkActions)).toContainReactText(customString);
});

it('renders a custom default select all string if present', () => {
const onSelectionChangeSpy = jest.fn();
const customString = 'Foo bar baz';
const index = mountWithApp(
<IndexTable
{...defaultProps}
selectable
hasMoreItems
selectedItemsCount="All"
itemCount={2}
promotedBulkActions={[{content: 'promoted action'}]}
onSelectionChange={onSelectionChangeSpy}
paginatedSelectAllText={customString}
>
{mockTableItems.map(mockRenderRow)}
</IndexTable>,
);
expect(index.find(BulkActions)).toContainReactText(customString);
});

it('does not need the bulkActions or promotedBulkActions props present to render the custom select all string', () => {
const onSelectionChangeSpy = jest.fn();
const customString = 'Foo bar baz';
Expand Down
4 changes: 4 additions & 0 deletions polaris-react/src/utilities/index-provider/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function useBulkSelectionData({
itemCount,
hasMoreItems,
resourceName: passedResourceName,
defaultPaginatedSelectAllText,
}: BulkSelectionDataOptions) {
const i18n = useI18n();

Expand Down Expand Up @@ -90,6 +91,9 @@ export function useBulkSelectionData({
}

if (selectedItemsCount === SELECT_ALL_ITEMS) {
if (defaultPaginatedSelectAllText) {
return defaultPaginatedSelectAllText;
}
return i18n.translate('Polaris.IndexProvider.allItemsSelected', {
itemsLength: itemCount,
resourceNamePlural: resourceName.plural.toLocaleLowerCase(),
Expand Down
2 changes: 2 additions & 0 deletions polaris-react/src/utilities/index-provider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface IndexProviderProps {
selection?: string | Range,
position?: number,
): void;
paginatedSelectAllText?: string;
}

export type HandleSelectionChange = (
Expand All @@ -47,6 +48,7 @@ export interface BulkSelectionDataOptions {
singular: string;
plural: string;
};
defaultPaginatedSelectAllText?: string;
}

export interface HandleBulkSelectionOptions {
Expand Down

0 comments on commit 2a2f635

Please sign in to comment.