Skip to content

Commit

Permalink
Fix page level selection selecting all items
Browse files Browse the repository at this point in the history
  • Loading branch information
chloerice committed Oct 11, 2024
1 parent 8c7ef4e commit 4631d71
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 68 deletions.
6 changes: 3 additions & 3 deletions polaris-react/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@
"selectAllLabel": "Select all {resourceNamePlural}",
"selected": "{selectedItemsCount} selected",
"undo": "Undo",
"selectAllItems": "Select all {itemsLength}+ {resourceNamePlural}",
"selectAllItems": "Select all {itemCount} in store",
"selectItem": "Select {resourceName}",
"selectButtonText": "Select",
"sortAccessibilityLabel": "sort {direction} by"
Expand Down Expand Up @@ -294,8 +294,8 @@
"selectAllMenu": {
"activator": "{selectedItemsCount} selected",
"items": {
"selectAllOnPage": "Select all 50 on page",
"selectAllInStore": "Select all {itemCount} in store",
"selectAllOnPage": "Select all {pageCount} on page",
"unselectAllOnPage": "Unselect all {pageCount} on page",
"unselectAll": "Unselect all"
}
},
Expand Down
20 changes: 4 additions & 16 deletions polaris-react/playground/OrdersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function Table({orders}: {orders: Order[]}) {
? pagedOrdersStartIndex + PAGE_LIMIT
: totalOrderCount;
const pagedOrders = orders.slice(pagedOrdersStartIndex, pagedOrdersEndIndex);

const pageRange = [pagedOrdersStartIndex, pagedOrdersEndIndex - 1];
const handlePagination = (direction: 'prev' | 'next') => () => {
setCurrentPage((current) =>
direction === 'prev' ? current - 1 : current + 1,
Expand Down Expand Up @@ -342,8 +342,6 @@ function Table({orders}: {orders: Order[]}) {
onPrevious: handlePagination('prev'),
};

console.log(pageCount);

const promotedBulkActions = [
{
content: 'Create shipping labels',
Expand All @@ -367,12 +365,6 @@ function Table({orders}: {orders: Order[]}) {
content: 'Remove tags',
onAction: () => console.log('Todo: implement bulk remove tags'),
},
{
icon: DeleteIcon,
destructive: true,
content: 'Delete customers',
onAction: () => console.log('Todo: implement bulk delete'),
},
];

const rowMarkup = pagedOrders.map(
Expand Down Expand Up @@ -445,14 +437,16 @@ function Table({orders}: {orders: Order[]}) {
<IndexTable
resourceName={resourceName}
itemCount={orders.length}
pageCount={pagedOrders.length}
pageSelectionRange={pageRange}
selectedItemsCount={
allResourcesSelected ? 'All' : selectedResources.length
}
onSelectionChange={handleSelectionChange}
pagination={pagination}
bulkActions={bulkActions}
promotedBulkActions={promotedBulkActions}
hasMoreItems={false}
hasMoreItems
headings={[
{title: 'Order'},
{title: 'Date'},
Expand Down Expand Up @@ -1013,12 +1007,6 @@ function OrdersIndexTableWithFilters(
});
});

console.log(
appliedFilterKeys,
appliedFilters,
savedViewFilters[selectedView],
);

const appliedFilterMatchesSavedFilter = (
appliedFilter: AppliedFilterInterface,
) => {
Expand Down
52 changes: 32 additions & 20 deletions polaris-react/src/components/BulkActions/BulkActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import type {
MenuGroupDescriptor,
Action,
} from '../../types';
import {Button} from '../Button';
import {ActionList} from '../ActionList';
import {Popover} from '../Popover';
import {InlineStack} from '../InlineStack';
Expand Down Expand Up @@ -46,8 +45,6 @@ type AriaLive = 'off' | 'polite' | undefined;
export interface BulkActionsProps {
/** Visually hidden text for screen readers */
accessibilityLabel?: string;
/** State of the bulk actions checkbox */
selected?: boolean | 'indeterminate';
/** Text to select all across pages */
paginatedSelectAllText?: string;
/** Action for selecting all across pages */
Expand All @@ -70,10 +67,12 @@ export interface BulkActionsProps {
selectMode?: boolean;
/** The number of rows selected in the table */
selectedItemsCount?: number | 'All';
/** The total number of rows in the table */
/** The total number of items in the store */
itemCount: number;
/** The total number of items on the current page */
pageCount: number;
/** Callback when the select all checkbox is clicked */
onSelect?(selectionType: 'page' | 'all' | 'none'): void;
onSelect?(selectionType: 'page' | 'all' | 'none', isSelecting: boolean): void;
/** @deprecated Used for forwarding the ref. Use `ref` prop instead */
innerRef?: React.Ref<any>;
/** @deprecated Callback when selectable state of list is changed. Unused callback */
Expand All @@ -100,13 +99,13 @@ export const BulkActions = ({
buttonSize = 'medium',
paginatedSelectAllAction,
paginatedSelectAllText,
selected,
onSelect,
onMoreActionPopoverToggle,
width,
selectMode,
selectedItemsCount = 0,
itemCount,
pageCount,
}: BulkActionsProps) => {
const i18n = useI18n();
const [popoverActive, setPopoverActive] = useState(false);
Expand All @@ -117,8 +116,8 @@ export const BulkActions = ({
};

const handleBulkSelection =
(selectionType: 'page' | 'all' | 'none') => () => {
onSelect?.(selectionType);
(selectionType: 'page' | 'all' | 'none', isSelecting: boolean) => () => {
onSelect?.(selectionType, isSelecting);
};

const [state, setState] = useReducer(
Expand Down Expand Up @@ -180,24 +179,37 @@ export const BulkActions = ({

const ariaLive: AriaLive = hasTextAndAction ? 'polite' : undefined;

// No idea why this translation borks...
// ? i18n.translate(
// 'Polaris.ResourceList.BulkActions.selectAllMenu.items.unselectAllOnPage',
// {pageCount},
// )
const selectAllOnPageContent =
selectedItemsCount === pageCount ||
selectedItemsCount === 'All' ||
selectedItemsCount === itemCount
? `Unselect all ${pageCount} on page`
: i18n.translate(
'Polaris.ResourceList.BulkActions.selectAllMenu.items.selectAllOnPage',
{pageCount},
);

const selectAllOnPageItem = {
content: i18n.translate(
'Polaris.ResourceList.BulkActions.selectAllMenu.items.selectAllOnPage',
content: selectAllOnPageContent,
onAction: handleBulkSelection(
'page',
!(
selectedItemsCount === pageCount ||
selectedItemsCount === 'All' ||
selectedItemsCount === itemCount
),
),
disabled: selected === true,
onAction: handleBulkSelection('page'),
};

const selectAllInStoreItem = paginatedSelectAllAction
? {
...paginatedSelectAllAction,
disabled: selectedItemsCount === 'All',
content: paginatedSelectAllText
? paginatedSelectAllText
: i18n.translate(
'Polaris.ResourceList.BulkActions.selectAllMenu.items.selectAllInStore',
{itemCount},
),
}
: null;

Expand All @@ -206,7 +218,7 @@ export const BulkActions = ({
'Polaris.ResourceList.BulkActions.selectAllMenu.items.unselectAll',
),
disabled: selectedItemsCount === 0,
onAction: handleBulkSelection('none'),
onAction: handleBulkSelection('all', false),
};

const selectAllActionsMarkup = selectMode ? (
Expand Down Expand Up @@ -375,7 +387,7 @@ export const BulkActions = ({
<Popover
active={popoverActive}
activator={activator}
preferredAlignment="right"
preferredAlignment="left"
onClose={togglePopover}
>
<ActionList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ interface Props {
selected: boolean;
selectMode: boolean;
itemCount: number;
pageCount: number;
selectedItemsCount: number | 'All';
}

Expand All @@ -61,6 +62,7 @@ const bulkActionProps: Props = {
disabled: false,
selectMode: true,
itemCount: 2,
pageCount: 2,
selectedItemsCount: 0,
};

Expand Down Expand Up @@ -345,6 +347,7 @@ describe('<BulkActions />', () => {
],
disabled: false,
itemCount: 2,
pageCount: 2,
selectedItemsCount: 0,
};
const bulkActions = mountWithApp(<BulkActions {...bulkActionProps} />);
Expand Down Expand Up @@ -384,6 +387,7 @@ describe('<BulkActions />', () => {
],
disabled: false,
itemCount: 2,
pageCount: 2,
selectedItemsCount: 0,
};
const bulkActions = mountWithApp(<BulkActions {...bulkActionProps} />);
Expand Down Expand Up @@ -430,6 +434,7 @@ describe('<BulkActions />', () => {
],
disabled: false,
itemCount: 2,
pageCount: 2,
selectedItemsCount: 0,
};
const bulkActions = mountWithApp(<BulkActions {...bulkActionProps} />);
Expand Down Expand Up @@ -468,6 +473,7 @@ describe('<BulkActions />', () => {
],
disabled: false,
itemCount: 2,
pageCount: 2,
selectedItemsCount: 0,
};
const bulkActions = mountWithApp(<BulkActions {...bulkActionProps} />);
Expand Down Expand Up @@ -517,6 +523,7 @@ describe('<BulkActions />', () => {
],
disabled: false,
itemCount: 2,
pageCount: 2,
selectedItemsCount: 0,
};
const bulkActions = mountWithApp(<BulkActions {...bulkActionProps} />);
Expand Down
6 changes: 6 additions & 0 deletions polaris-react/src/components/IndexProvider/IndexProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export function IndexProvider({
onSelectionChange,
selectedItemsCount = 0,
itemCount,
pageCount,
pageSelectionRange,
hasMoreItems,
condensed,
selectable: isSelectableIndex = true,
Expand All @@ -40,6 +42,8 @@ export function IndexProvider({
const contextValue = useMemo(
() => ({
itemCount,
pageCount,
pageSelectionRange,
selectMode: selectMode && isSelectableIndex,
selectable: isSelectableIndex,
resourceName,
Expand All @@ -54,6 +58,8 @@ export function IndexProvider({
}),
[
itemCount,
pageCount,
pageSelectionRange,
selectMode,
isSelectableIndex,
resourceName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {IndexRowContext} from '../../../utilities/index-provider';
describe('<IndexProvider />', () => {
const defaultProps = {
itemCount: 0,
pageCount: 0,
selectedItemsCount: 0,
onSelectionChange: () => {},
};
Expand Down
Loading

0 comments on commit 4631d71

Please sign in to comment.