Skip to content

Commit

Permalink
fix: code improvement after the remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
moon-ds committed Oct 18, 2023
1 parent 7232cb3 commit c0196bf
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 45 deletions.
73 changes: 38 additions & 35 deletions next-docs/public/examples/table/ExpandableCheckboxes.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Checkbox, Chip, Tooltip, mergeClassnames } from "@heathmont/moon-core-tw";
import { OtherFrame } from "@heathmont/moon-icons-tw";
import { Table } from "@heathmont/moon-table-tw";
import React, { useMemo, useState } from "react";
import React, { useState } from "react";

interface HeaderProps {
rows: [];
Expand All @@ -11,17 +11,20 @@ interface HeaderProps {
}

const Example = () => {
const PREFIX = "expsel";
/**
* The PREFIX is necessary if you use several different tables with checkboxes on the same page.
* Each table should have its own unique PREFIX to avoid assigning identical indexes to elements.
* When using only one table, the PREFIX can be omitted.
*/
const PREFIX = "any__unique__string__for__each__table";

const [selected, setSelected] = useState<{ [key: string]: boolean }>({});

const columnShift = (depth: number) => {
const shiftMap: { [key: number]: string } = [
const shiftMap: { [key: number]: string } = [
'ps-0',
'ps-[25px]',
'ps-[50px]',
'ps-[75px]',
'ps-[100px]',
'ps-6',
'ps-12',
];

return shiftMap[depth];
Expand All @@ -30,15 +33,15 @@ const Example = () => {
const checkIfSelected = (id: string, canExpand: boolean, rowsById: { [key: string]: boolean }) => {
return canExpand
? Object.keys(rowsById)
.filter((rowId) => rowId.indexOf(id) === 0 && rowId !== id)
.every((rowId) => selected[rowId] === true)
.filter((rowId) => rowId.indexOf(id) === 0 && rowId !== id)
.every((rowId) => selected[rowId] === true)
: selected[id] === true;
}

const checkIfIndeterminate = (id: string, rowsById: { [key: string]: boolean }) => {
const matches = Object.keys(rowsById)
const matches = Object.keys(rowsById)
.filter((rowId) => rowId.indexOf(id) === 0 && rowId !== id);
return !matches.every((rowId) => selected[rowId] === true) && matches.some((rowId) => selected[rowId] === true);
return !matches.every((rowId) => selected[rowId] === true) && matches.some((rowId) => selected[rowId] === true);
}

const columnsInitial = [
Expand All @@ -57,10 +60,10 @@ const Example = () => {
<div className="flex items-center gap-x-1">
<div className="flex items-center h-full">
<Checkbox
id={PREFIX && PREFIX.length ? `${PREFIX}_root` : 'root'}
id={ PREFIX && PREFIX.length ? `${PREFIX}_root` : 'root'}
checked={(Object.keys(rowsById).length === Object.keys(selected).length)}
indeterminate={!!Object.keys(selected).length && Object.keys(selected).length < Object.keys(rowsById).length}
onClick={(e: any) => { e.stopPropagation() }}
onClick={(e) => { e.stopPropagation() }}
/>
</div>
{<span {...getToggleAllRowsExpandedProps()}>
Expand All @@ -69,28 +72,28 @@ const Example = () => {
</div>
),
Cell: ({ row, rowsById }: any) => (
<div className={mergeClassnames(
"flex items-center gap-x-1",
columnShift(row.depth),
)}
onClick={(e) => {
if ((e.target as unknown as HTMLElement).tagName === 'SPAN') e.stopPropagation();
}}
>
<div className="flex items-center h-full">
<Checkbox
id={PREFIX && PREFIX.length ? `${PREFIX}_${row.id}` : row.id}
checked={checkIfSelected(row.id, row.canExpand, rowsById)}
indeterminate={checkIfIndeterminate(row.id, rowsById)}
onClick={(e: any) => e.stopPropagation()}
/>
<div className={mergeClassnames(
"flex items-center gap-x-1",
columnShift(row.depth),
)}
onClick={(e) => {
if ((e.target as unknown as HTMLElement).tagName === 'SPAN') e.stopPropagation();
}}
>
<div className="flex items-center h-full">
<Checkbox
id={ PREFIX && PREFIX.length ? `${PREFIX}_${row.id}` : row.id}
checked={checkIfSelected(row.id, row.canExpand, rowsById)}
indeterminate={checkIfIndeterminate(row.id, rowsById)}
onClick={(e) => e.stopPropagation()}
/>
</div>
{ row.canExpand ? (
<span {...row.getToggleRowExpandedProps()}>
{row.isExpanded ? '👇' : '👉'}
</span>
) : null }
</div>
{row.canExpand ? (
<span {...row.getToggleRowExpandedProps()}>
{row.isExpanded ? '👇' : '👉'}
</span>
) : null}
</div>
)
},
],
Expand Down Expand Up @@ -302,7 +305,7 @@ const Example = () => {
expandedByDefault={true}
getOnRowSelect={() => (rows) => {
setSelected(rows.reduce((acc: {[key: string]: boolean}, item: any) => {
acc[`${item.id}`] = true;
acc[item.id] = true;
return acc;
}, {})
);
Expand Down
2 changes: 0 additions & 2 deletions next-docs/public/examples/table/LongData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ const Example = () => {
'Header': 'Currency',
'accessor': 'currency',
'Footer': '',
/*'maxWidth': Number.MAX_SAFE_INTEGER,
'width': 80 */
},
],
},
Expand Down
14 changes: 9 additions & 5 deletions next-docs/public/examples/table/SelectableCheckboxes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ interface HeaderProps {
}

const Example = () => {
const PREFIX = "selectable";
/**
* The PREFIX is necessary if you use several different tables with checkboxes on the same page.
* Each table should have its own unique PREFIX to avoid assigning identical indexes to elements.
* When using only one table, the PREFIX can be omitted.
*/
const PREFIX = "any_unique_string_for_each_table";

const [selected, setSelected] = useState<{ [key: string]: boolean }>({});

Expand All @@ -29,7 +34,7 @@ const Example = () => {
id={ PREFIX && PREFIX.length ? `${PREFIX}_root` : 'root' }
checked={(Object.keys(rowsById).length === Object.keys(selected).length)}
indeterminate={!!Object.keys(selected).length && Object.keys(selected).length < Object.keys(rowsById).length}
onClick={(e: any) => { e.stopPropagation() }}
onClick={(e) => { e.stopPropagation() }}
/>
</div>
),
Expand All @@ -38,7 +43,7 @@ const Example = () => {
<Checkbox
id={ PREFIX && PREFIX.length ? `${PREFIX}_${row.id}` : row.id }
checked={selected[row.id] === true}
onClick={(e: any) => e.stopPropagation()}
onClick={(e) => e.stopPropagation()}
/>
</div>
),
Expand Down Expand Up @@ -115,7 +120,6 @@ const Example = () => {
progress: <span>{Math.floor(index * 100)}</span>,
status: Math.floor(index * 100),
activity: Math.floor(index * 100),
//isSelected: index === 3,
};
});
};
Expand All @@ -142,7 +146,7 @@ const Example = () => {
withFooter={true}
selectable={true}
useCheckbox={true}
getOnRowSelect={() => (rows: any) => {
getOnRowSelect={() => (rows) => {
console.log(`IDs of selected rows - ${rows.map((row: any) => row.id)}`);
setSelected(rows.reduce((acc: {[key: string]: boolean}, item: any) => {
acc[item.id] = true;
Expand Down
2 changes: 1 addition & 1 deletion workspaces/tables/src/components/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ const Table = ({
);
};

const setForceUpdateRowSelectedState = (callback: () => React.Dispatch<React.SetStateAction<{[key: string]: boolean;}>>) => {
const setForceUpdateRowSelectedState = (callback: () => React.Dispatch<React.SetStateAction<{[key: string]: boolean}>>) => {
updateRowSelectState = callback;
}

Expand Down
3 changes: 1 addition & 2 deletions workspaces/tables/src/private/utils/renderRows.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { Fragment, useEffect, useState } from 'react';
import { Checkbox } from '@heathmont/moon-core-tw';
import type { Cell, Column, Row, UseExpandedRowProps } from 'react-table';
import type { Cell, Row, UseExpandedRowProps } from 'react-table';
import BodyTR from '../../components/BodyTR';
import TD from '../../components/TD';
import type RenderRowsProps from '../types/RenderRowsProps';
Expand Down

0 comments on commit c0196bf

Please sign in to comment.