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

✨ - feat: allow passing allPagesSelectedManaged to control w… #122

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions src/components/data/datagrid/datagrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,25 @@ export type DataGridProps = {
allowSelectAll?: boolean;

/**
* Whether a select all checkbox should be shown (when `selectable=true`) allowing to select all pages.
* NOTE: No actual implementation of multi-page selection (other than calling `onSelectAllPages(selected)`) and
* receiving `allPagesSelected` is provided.
* Whether a select all checkbox should be shown (when `selectable=true`) allowing to select all pages. If
* `allPagesSelectedManaged=true` rows are considered selected when `allPagesSelected=true`. Clicking the
* "select all pages" checkbox calls `onSelectAllPages(selected)`.
*/
allowSelectAllPages?: boolean;

/** Whether all pages are selected. */
/**
* Whether all pages are selected. If `allPagesSelectedManaged=true`, setting this to `true` selects all rows. If
* `allPagesSelectedManaged=false`.
*/
allPagesSelected?: boolean;

/**
* Whether all rows are considered to be selected when `allowSelectAllPages=true`. If support for excluding rows from
* the multi-page selection is required: this should be set to false and the selected rows should be provided as part
* of the `selected` prop instead.
*/
allPagesSelectedManaged?: boolean;

/** The table layout algorithm. */
tableLayout?: "auto" | "fixed";

Expand Down Expand Up @@ -229,6 +239,7 @@ export const DataGrid: React.FC<DataGridProps> = ({
allowSelectAll = true,
allowSelectAllPages = false,
allPagesSelected = false,
allPagesSelectedManaged = true,
fieldsSelectable = false,
selected,
equalityChecker = (item1, item2) => item1 === item2,
Expand Down Expand Up @@ -282,6 +293,11 @@ export const DataGrid: React.FC<DataGridProps> = ({
selected && setSelectedState(selected);
}, [selected]);

// Update selectedState when selected prop changes.
useEffect(() => {
setAllPagesSelectedState(allPagesSelected);
}, [allPagesSelected]);

// Update sortState when sort prop changes.
useEffect(() => {
if (typeof sort === "string") {
Expand Down Expand Up @@ -324,7 +340,9 @@ export const DataGrid: React.FC<DataGridProps> = ({
const _pageSize = pageSize || _count;
const _pages = Math.ceil(_count / _pageSize);
const _selectedRows =
(allPagesSelectedState ? objectList : selectedState) || [];
(allPagesSelectedManaged && allPagesSelectedState
? objectList
: selectedState) || [];

const filteredObjectList = filterState
? filterAttributeDataArray(objectList, filterState)
Expand Down Expand Up @@ -437,6 +455,7 @@ export const DataGrid: React.FC<DataGridProps> = ({
allowSelectAll={allowSelectAll}
allowSelectAllPages={allowSelectAllPages}
allPagesSelected={allPagesSelectedState}
allPagesSelectedManaged={allPagesSelectedManaged}
selectedRows={_selectedRows}
selectionActions={selectionActions}
title={title}
Expand Down Expand Up @@ -494,6 +513,8 @@ export const DataGrid: React.FC<DataGridProps> = ({
setEditingState={setEditingState}
selectable={selectable}
selectedRows={_selectedRows}
allPagesSelected={allPagesSelectedState}
allPagesSelectedManaged={allPagesSelectedManaged}
equalityChecker={equalityChecker}
sortDirection={sortDirection}
sortField={sortField}
Expand Down Expand Up @@ -533,6 +554,7 @@ export type DataGridCaptionProps = {
allowSelectAll: boolean;
allowSelectAllPages: boolean;
allPagesSelected: boolean;
allPagesSelectedManaged: boolean;
selectedRows: AttributeData[] | null;
selectionActions?: ButtonProps[];
title: React.ReactNode;
Expand All @@ -556,6 +578,7 @@ export const DataGridCaption: React.FC<DataGridCaptionProps> = ({
allowSelectAll,
allowSelectAllPages,
allPagesSelected,
allPagesSelectedManaged,
selectionActions,
selectedRows,
title,
Expand Down Expand Up @@ -628,6 +651,7 @@ export const DataGridCaption: React.FC<DataGridCaptionProps> = ({
<DataGridSelectionCheckbox
checked={allSelected || false}
count={count}
disabled={allPagesSelectedManaged && allPagesSelected}
handleSelect={() => onSelectAll(!allSelected)}
labelSelect={labelSelectAll}
pages={pages}
Expand Down Expand Up @@ -777,7 +801,9 @@ export const DataGridHeading: React.FC<DataGridHeadingProps> = ({
return (
<thead className="mykn-datagrid__head" role="rowgroup">
<tr className="mykn-datagrid__row mykn-datagrid__row--header" role="row">
{selectable && <th className="mykn-datagrid__cell--checkbox"></th>}
{selectable && (
<th className="mykn-datagrid__cell mykn-datagrid__cell--checkbox"></th>
)}
{renderableFields.map((field) => (
<DataGridHeadingCell
key={`${dataGridId}-heading-${field2Caption(field.name)}`}
Expand Down Expand Up @@ -896,6 +922,8 @@ export type DataGridBodyProps = {
setEditingState: ([editingRow, fieldIndex]: [AttributeData, number]) => void;
selectable: boolean;
selectedRows: AttributeData[];
allPagesSelected: boolean;
allPagesSelectedManaged: boolean;
sortDirection: "ASC" | "DESC" | undefined;
sortField: string | undefined;
urlFields: string[];
Expand Down Expand Up @@ -928,6 +956,8 @@ export const DataGridBody: React.FC<DataGridBodyProps> = ({
renderableRows,
selectable,
selectedRows,
allPagesSelected,
allPagesSelectedManaged,
equalityChecker = (item1, item2) => item1 === item2,
setEditingState,
sortDirection,
Expand Down Expand Up @@ -958,6 +988,7 @@ export const DataGridBody: React.FC<DataGridBodyProps> = ({
equalityChecker(element, rowData),
) || false
}
disabled={allPagesSelectedManaged && allPagesSelected}
count={count}
handleSelect={handleSelect}
labelSelect={labelSelect}
Expand Down Expand Up @@ -1285,6 +1316,7 @@ export type DataGridSelectionCheckboxProps = {
amountSelected: number;
checked: boolean;
count: DataGridProps["count"];
disabled?: boolean;
handleSelect: (attributeData: AttributeData) => void;
sortedObjectList: AttributeData[];
labelSelect?: string;
Expand All @@ -1301,6 +1333,7 @@ export const DataGridSelectionCheckbox: React.FC<
> = ({
amountSelected,
checked,
disabled = false,
count,
handleSelect,
labelSelect,
Expand Down Expand Up @@ -1366,6 +1399,7 @@ export const DataGridSelectionCheckbox: React.FC<
return (
<Checkbox
checked={checked}
disabled={disabled}
onChange={() => handleSelect(rowData || {})}
aria-label={label}
>
Expand Down
7 changes: 7 additions & 0 deletions src/components/form/input/input.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
width: min(320px, 100%);
max-width: 100%;

&:disabled,
&--disabled,
&--muted {
color: var(--typography-color-muted);
opacity: 0.4;
}

&::placeholder {
color: var(--typography-color-muted);
}
Expand Down