Skip to content
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

refactor(useDisableSelectRows): implement types #5539

Merged
Merged
13 changes: 12 additions & 1 deletion packages/ibm-products/src/components/Datagrid/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
TableCommonProps,
TableDispatch,
TableInstance,
TableState,
TableToggleAllRowsSelectedProps,
UseExpandedRowProps,
UseFiltersInstanceProps,
Expand Down Expand Up @@ -188,7 +189,17 @@ interface DataGridTableState
}

export interface DataGridTableInstance<T extends object = any>
extends TableInstance<T> {}
extends Omit<TableInstance<T>, 'state'>,
Partial<UsePaginationInstanceProps<any>> {
shouldDisableSelectRow?: (...args) => void;
makafsal marked this conversation as resolved.
Show resolved Hide resolved
state?: Partial<TableState & UseRowSelectState<any>>;
disableSelectAll?: boolean;
disableSelectRowsProps?: {
labels?: {
toggleAllRowsLabel?: boolean;
makafsal marked this conversation as resolved.
Show resolved Hide resolved
};
};
}

export interface RowAction {
id?: string;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Licensed Materials - Property of IBM
* 5724-Q36
* (c) Copyright IBM Corp. 2020
makafsal marked this conversation as resolved.
Show resolved Hide resolved
* US Government Users Restricted Rights - Use, duplication or disclosure
* restricted by GSA ADP Schedule Contract with IBM Corp.
*/

import { Hooks, Row, RowPropGetter, TableRowProps } from 'react-table';
import { DataGridTableInstance, DatagridRow } from './types';

interface PropGetterMeta {
makafsal marked this conversation as resolved.
Show resolved Hide resolved
instance?: DataGridTableInstance;
row?: Partial<Row<any> & DatagridRow<any>>;
}

const useDisableSelectRows = (hooks: Hooks) => {
updateSelectAll(hooks);
updatePageSelectAll(hooks);

const getRowProps: RowPropGetter<any> = (
props: Partial<TableRowProps>,
makafsal marked this conversation as resolved.
Show resolved Hide resolved
{ row, instance }: PropGetterMeta
) =>
[
props,
{
disabled: instance?.shouldDisableSelectRow?.(row),
},
] as Partial<TableRowProps>[];

hooks.getRowProps.push(getRowProps);
};

const updateSelectAll = (hooks: Hooks) => {
const getToggleAllRowsSelectedProps: RowPropGetter<any> = (
props: Partial<TableRowProps>,
{ instance }: PropGetterMeta
) => {
const selectableRows =
makafsal marked this conversation as resolved.
Show resolved Hide resolved
instance?.rows?.filter(
(row) =>
!(
instance.shouldDisableSelectRow &&
instance.shouldDisableSelectRow(row)
)
) || [];
const isAllRowsSelected =
makafsal marked this conversation as resolved.
Show resolved Hide resolved
selectableRows?.length > 0 &&
selectableRows.every(({ id }) => instance?.state?.selectedRowIds?.[id]);
return [
props,
{
onChange: (e) => {
selectableRows.forEach((row) =>
(row as DatagridRow)?.toggleRowSelected?.(e.target.checked)
);
},
style: { cursor: 'pointer' },
checked: isAllRowsSelected,
disabled: instance?.disableSelectAll,
title:
instance?.disableSelectRowsProps?.labels?.toggleAllRowsLabel ||
'Toggle All Rows Selected',
indeterminate: Boolean(
!isAllRowsSelected &&
Object.keys(instance?.state?.selectedRowIds || {}).length
),
},
];
};
hooks.getToggleAllRowsSelectedProps.push(getToggleAllRowsSelectedProps);
};

const updatePageSelectAll = (hooks: Hooks) => {
const getToggleAllPageRowsSelectedProps = (
szinta marked this conversation as resolved.
Show resolved Hide resolved
props: Partial<TableRowProps>,
{ instance }: PropGetterMeta
) => {
const selectableRows =
instance?.page?.filter(
(row) =>
!(
instance.shouldDisableSelectRow &&
instance.shouldDisableSelectRow(row)
)
) || [];
const isAllRowsSelected =
selectableRows?.length > 0 &&
selectableRows.every(({ id }) => instance?.state?.selectedRowIds?.[id]);
return [
props,
{
onChange: (e) => {
selectableRows.forEach((row) =>
(row as DatagridRow).toggleRowSelected(e.target.checked)
);
},
style: { cursor: 'pointer' },
checked: isAllRowsSelected,
disabled: instance?.disableSelectAll,
title:
instance?.disableSelectRowsProps?.labels?.toggleAllRowsLabel ||
'Toggle All Rows Selected',
indeterminate: Boolean(
!isAllRowsSelected &&
instance?.page?.some(
({ id }) => instance?.state?.selectedRowIds?.[id]
)
),
},
];
};
hooks.getToggleAllPageRowsSelectedProps.push(
getToggleAllPageRowsSelectedProps
);
};

export default useDisableSelectRows;
Loading