-
Notifications
You must be signed in to change notification settings - Fork 65
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
feat(component): allow state override of table select all checkbox #224
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,20 @@ import { Pagination } from '../../Pagination'; | |
import { Text } from '../../Typography'; | ||
import { TableItem, TablePagination, TableSelectable } from '../types'; | ||
|
||
import { StyledActions } from './styled'; | ||
import { StyledActions, StyledPaginationContainer } from './styled'; | ||
|
||
export interface ActionsProps<T> { | ||
itemName?: string; | ||
items: T[]; | ||
pagination?: TablePagination; | ||
selectable?: TableSelectable<T>; | ||
tableId: string; | ||
} | ||
|
||
export const Actions = memo( | ||
<T extends TableItem>({ selectable, pagination, tableId, items = [], ...props }: ActionsProps<T>) => { | ||
<T extends TableItem>({ selectable, pagination, tableId, itemName, items = [], ...props }: ActionsProps<T>) => { | ||
const totalItems = pagination ? pagination.totalItems : items.length; | ||
|
||
const handleSelectAll = () => { | ||
if (!selectable) { | ||
return; | ||
|
@@ -31,20 +34,48 @@ export const Actions = memo( | |
} | ||
}; | ||
|
||
const renderSelectAllAction = ({ itemType, selectedItems }: TableSelectable<T>) => { | ||
const getSelectAllChecked = () => { | ||
if (!selectable) { | ||
return false; | ||
} | ||
|
||
const { selectAllState, selectedItems } = selectable; | ||
|
||
switch (selectAllState) { | ||
case 'ALL': | ||
return true; | ||
|
||
case 'PARTIAL': | ||
case 'NONE': | ||
return false; | ||
|
||
default: | ||
const totalSelectedItems = selectedItems.length; | ||
const totalItemsInPage = items.length; | ||
|
||
return totalSelectedItems === totalItemsInPage && totalItemsInPage > 0; | ||
} | ||
}; | ||
|
||
const renderSelectAllAction = () => { | ||
if (!selectable) { | ||
return null; | ||
} | ||
|
||
const { selectAllState, selectedItems } = selectable; | ||
const totalSelectedItems = selectedItems.length; | ||
const totalItemsInPage = items.length; | ||
const isChecked = totalSelectedItems === totalItemsInPage && totalItemsInPage > 0; | ||
const isIndeterminate = totalSelectedItems > 0 && totalSelectedItems !== totalItemsInPage; | ||
|
||
const isChecked = getSelectAllChecked(); | ||
const isIndeterminate = | ||
selectAllState === 'PARTIAL' || (totalSelectedItems > 0 && totalSelectedItems !== totalItemsInPage); | ||
|
||
return ( | ||
<Flex.Item flexGrow={2}> | ||
<Flex.Item marginRight="xxSmall"> | ||
<Flex flexDirection="row"> | ||
<Checkbox isIndeterminate={isIndeterminate} checked={isChecked} onChange={handleSelectAll} /> | ||
<Text marginLeft="small"> | ||
{totalSelectedItems === 0 | ||
? `${totalItemsInPage} ${itemType}` | ||
: `${totalSelectedItems}/${totalItemsInPage} ${itemType}`} | ||
{totalSelectedItems === 0 ? `${totalItems}` : `${totalSelectedItems}/${totalItems}`} | ||
</Text> | ||
</Flex> | ||
</Flex.Item> | ||
|
@@ -54,13 +85,27 @@ export const Actions = memo( | |
const renderPagination = useMemo( | ||
() => | ||
pagination && ( | ||
<Flex.Item style={{ marginLeft: 'auto' }}> | ||
<StyledPaginationContainer> | ||
<Pagination {...pagination} /> | ||
</Flex.Item> | ||
</StyledPaginationContainer> | ||
), | ||
[pagination], | ||
); | ||
|
||
const renderItemName = () => { | ||
if (typeof itemName !== 'string') { | ||
return null; | ||
} | ||
|
||
const text = Boolean(selectable) ? itemName : `${totalItems} ${itemName}`; | ||
|
||
return ( | ||
<Flex.Item> | ||
<Text margin="none">{text}</Text> | ||
</Flex.Item> | ||
); | ||
}; | ||
|
||
return ( | ||
<StyledActions | ||
alignItems="center" | ||
|
@@ -70,7 +115,8 @@ export const Actions = memo( | |
padding="small" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🍹Could you try and remove the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case, we have multiple actions, one of them being the However, I can move this to a custom-styled Flex. We should add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh yeah, well in that case maybe you could change the |
||
{...props} | ||
> | ||
{selectable && renderSelectAllAction(selectable)} | ||
{renderSelectAllAction()} | ||
{renderItemName()} | ||
{renderPagination} | ||
</StyledActions> | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🍹 consider putting these strings in a constants definition or similar