Skip to content

Commit

Permalink
chore: fix lint warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
beliarh committed Jul 19, 2024
1 parent 431d5de commit d727e9d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/components/BaseRow/BaseRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const BaseRow = React.forwardRef(
>
{checkIsGroupRow?.(row) ? (
<React.Fragment>
{row.getCanSelect() && renderCell(row.getVisibleCells()[0]!)}
{row.getCanSelect() && renderCell(row.getVisibleCells()[0])}
<td
colSpan={row.getCanSelect() ? columnsCount - 1 : columnsCount}
className={b('cell', b('group-header-cell', cellClassName))}
Expand Down
6 changes: 3 additions & 3 deletions src/components/DraggableRow/DraggableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export const DraggableRow = React.forwardRef(
const getDraggableRowDataAttributes = React.useCallback<
NonNullable<BaseRowProps<TData>['getRowAttributes']>
>(
(row) => ({
...getRowAttributes?.(row),
(draggableRow) => ({
...getRowAttributes?.(draggableRow),
...toDataAttributes({
key: row.id,
key: draggableRow.id,
depth,
draggable: true,
dragging: isDragging,
Expand Down
3 changes: 2 additions & 1 deletion src/components/SortableList/SortableList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export const SortableList = ({
isParentMode,
targetItemIndex,
}),
[isParentMode, isChildMode, isNextChildMode, activeItemKey, targetItemIndex],
// eslint-disable-next-line react-hooks/exhaustive-deps
[activeItemKey, isChildMode, isNextChildMode, isParentMode, targetItemIndex],
);

return (
Expand Down
43 changes: 26 additions & 17 deletions src/components/__stories__/hooks/useTreeDataReordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,35 @@ export const useTreeDataReordering = ({data, setData}: UseTreeDataReorderingProp
order.push(item.id);

if (item.children?.length) {
children[item.id] = [];
const childIds: string[] = [];

for (const child of item.children) {
parents[child.id] = item.id;
children[item.id]!.push(child.id);
childIds.push(child.id);
}

children[item.id] = childIds;

collectTreeItems(item.children);
}
}
};

collectTreeItems(dataClone);

const draggedItem = itemsMap[draggedItemKey]!;
const draggedItem = itemsMap[draggedItemKey] as TreeItem;
const baseItemParent = baseItemKey && parents[baseItemKey];
const baseNextItemParent = baseNextItemKey && parents[baseNextItemKey];
const isMovedAfterRoot = baseItemParent !== baseNextItemParent;

const updateRank = ({parentId, movedItemId, after, before}: UpdateRankPayload) => {
const movedItem = itemsMap[movedItemId]!;
const movedItem = itemsMap[movedItemId] as TreeItem;

const previousParentId = parents[movedItemId];
if (previousParentId) {
const previousParent = itemsMap[previousParentId]!;
previousParent.children = previousParent.children!.filter(
const previousParent = itemsMap[previousParentId] as TreeItem;

previousParent.children = (previousParent.children as TreeItem[]).filter(
(child) => child.id !== movedItemId,
);
} else {
Expand All @@ -87,10 +90,10 @@ export const useTreeDataReordering = ({data, setData}: UseTreeDataReorderingProp

if (after) {
const index = newParentChildren.indexOf(after);
newParent.children!.splice(index, 0, movedItem);
(newParent.children as TreeItem[]).splice(index, 0, movedItem);
} else if (before) {
const index = newParentChildren.indexOf(before);
newParent.children!.splice(index - 1, 0, movedItem);
(newParent.children as TreeItem[]).splice(index - 1, 0, movedItem);
}
} else if (after) {
const index = dataClone.findIndex((item) => item.id === after);
Expand Down Expand Up @@ -118,25 +121,28 @@ export const useTreeDataReordering = ({data, setData}: UseTreeDataReorderingProp

const childrenIndexById: Record<string, number> = {};

for (const id of childrenIds) {
childrenIndexById[id] = order.indexOf(id);
for (const childrenId of childrenIds) {
childrenIndexById[childrenId] = order.indexOf(childrenId);
}

const childrenIdsOrdered = [...childrenIds];

childrenIdsOrdered.sort(
(first, second) => childrenIndexById[first]! - childrenIndexById[second]!,
(first, second) => childrenIndexById[first] - childrenIndexById[second],
);

return childrenIdsOrdered[0] === id;
};

pullFromParent = pullFromParent && (isMovedAfterRoot || Boolean(parents[baseItemKey!]));
// eslint-disable-next-line no-param-reassign
pullFromParent =
pullFromParent && (isMovedAfterRoot || Boolean(parents[baseItemKey as string]));

// Put the moved item if it fells on another one or the entity in front of the pointer is an expanded entity
const shouldInsertToTarget =
!pullFromParent &&
(targetItemKey || children[baseItemKey!]?.includes(baseNextItemKey!));
(targetItemKey ||
children[baseItemKey as string]?.includes(baseNextItemKey as string));

if (shouldInsertToTarget) {
const parentId = targetItemKey || baseItemKey;
Expand All @@ -145,14 +151,17 @@ export const useTreeDataReordering = ({data, setData}: UseTreeDataReorderingProp

// First child in the parent node
if (!targetItemKey && baseItemKey) {
if (parents[draggedItemKey] === baseItemKey || isFirstChild(baseNextItemKey!)) {
if (
parents[draggedItemKey] === baseItemKey ||
isFirstChild(baseNextItemKey as string)
) {
updateRank({
parentId,
movedItemId,
before: itemsMap[baseNextItemKey!]!.id,
before: itemsMap[baseNextItemKey as string].id,
});
} else {
movedItemId = baseNextItemKey!;
movedItemId = baseNextItemKey as string;

updateRank({
parentId: parents[movedItemId],
Expand Down Expand Up @@ -191,7 +200,7 @@ export const useTreeDataReordering = ({data, setData}: UseTreeDataReorderingProp
parentId,
movedItemId: draggedItemKey,
after: base ? itemsMap[base]?.id : undefined,
before: base ? undefined : itemsMap[baseNextItemKey!]?.id,
before: base ? undefined : itemsMap[baseNextItemKey as string]?.id,
});
}
},
Expand Down

0 comments on commit d727e9d

Please sign in to comment.