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

fix(TableSettings): group ordering state initializing #63

Merged
merged 1 commit into from
Oct 2, 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
45 changes: 31 additions & 14 deletions src/components/TableSettings/TableSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,9 @@ export const TableSettings = <TData extends unknown>({
const [visibilityState, setVisibilityState] = React.useState(
() => table.getState().columnVisibility,
);
const [orderState, setOrderState] = React.useState(() => getInitialOrderItems(filteredColumns));

const applyNewSettings = () => {
const columnOrder = orderStateToColumnOrder(orderState);

if (onSettingsApply) onSettingsApply({visibilityState, columnOrder});

if (filterable) table.setColumnVisibility(visibilityState);
if (sortable) table.setColumnOrder(columnOrder);

setOpen(false);
};
const [orderState, setOrderState] = React.useState(() =>
getInitialOrderItems(filteredColumns, table.getState().columnOrder),
);

const sensors = useSensors(useSensor(MouseSensor), useSensor(TouchSensor));

Expand Down Expand Up @@ -104,11 +95,37 @@ export const TableSettings = <TData extends unknown>({
});
};

const resetSettings = () => {
setVisibilityState(table.getState().columnVisibility);
setOrderState(getInitialOrderItems(filteredColumns, table.getState().columnOrder));
};

const cancelEditing = () => {
setOpen(false);
resetSettings();
};

const togglePopup = () => {
if (open) cancelEditing();
else setOpen(true);
};

const applyNewSettings = () => {
const columnOrder = orderStateToColumnOrder(orderState);

if (onSettingsApply) onSettingsApply({visibilityState, columnOrder});

if (filterable) table.setColumnVisibility(visibilityState);
if (sortable) table.setColumnOrder(columnOrder);

setOpen(false);
};

return (
<React.Fragment>
<Popup
open={open}
onClose={() => setOpen(false)}
onClose={cancelEditing}
anchorRef={anchorRef}
placement={POPUP_PLACEMENT}
contentClassName={b()}
Expand All @@ -135,7 +152,7 @@ export const TableSettings = <TData extends unknown>({
</Button>
</div>
</Popup>
<Button view="flat-secondary" size="m" ref={anchorRef} onClick={() => setOpen(!open)}>
<Button view="flat-secondary" size="m" ref={anchorRef} onClick={togglePopup}>
<Icon data={Gear} />
</Button>
</React.Fragment>
Expand Down
27 changes: 25 additions & 2 deletions src/components/TableSettings/TableSettings.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,39 @@ export const orderStateToColumnOrder = (state: Record<string, string[]>) => {
return result;
};

export const getInitialOrderItems = <TData extends unknown>(treeItems: Column<TData>[]) => {
export const getInitialOrderItems = <TData extends unknown>(
treeItems: Column<TData>[],
initialOrder: string[],
) => {
const orderMap = initialOrder.reduce<Record<string, number>>(
(acc, id, index) => ({...acc, [id]: index}),
{},
);

const stack = [...treeItems];
const result: Record<string, string[]> = {root: treeItems.map(({id}) => id)};
const parentNodes: string[] = [];

while (stack.length) {
const item = stack.shift();
if (!item) continue;
result[item.id] = item?.columns?.map(({id}) => id) ?? [];
if (item.columns) stack.push(...item.columns);
if (item.columns.length) {
stack.push(...item.columns);
parentNodes.push(item.id);
}
}

parentNodes.reverse().forEach((parent) => {
const childs = result[parent];
const childsOrders = childs.map((id) => orderMap[id] ?? -1);
const minIndex = Math.min(...childsOrders);
orderMap[parent] = minIndex;
});

Object.keys(result).forEach((key) => {
result[key] = result[key].sort((a, b) => orderMap[a] - orderMap[b]);
});

return result;
};
Loading