Skip to content

Commit

Permalink
Merge pull request #27 from bryanmylee/fix/addResizedColumns
Browse files Browse the repository at this point in the history
Fix `addResizedColumns` interaction with `addGridLayout`
  • Loading branch information
bryanmylee authored Jul 2, 2022
2 parents b1e7a4f + dfa03f8 commit 284dad2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svelte-headless-table",
"version": "0.10.3",
"version": "0.10.4",
"scripts": {
"dev": "svelte-kit dev",
"build": "svelte-kit build",
Expand Down
39 changes: 34 additions & 5 deletions src/lib/plugins/addResizedColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ export type ResizedColumnsAttributeSet = NewTableAttributeSet<{
'box-sizing': 'border-box';
};
};
'tbody.tr.td': {
style?: {
width: string;
'min-width': string;
'max-width': string;
'box-sizing': 'border-box';
};
};
}>;

const getDragXPos = (event: Event): number => {
Expand Down Expand Up @@ -180,7 +188,7 @@ export const addResizedColumns =
window.removeEventListener('touchend', dragEnd);
}
};
const action = (node: Element) => {
const $props = (node: Element) => {
nodeForId[cell.id] = node;
if (cell instanceof FlatHeaderCell) {
columnWidths.update(($columnWidths) => ({
Expand All @@ -194,7 +202,7 @@ export const addResizedColumns =
},
};
};
action.drag = (node: Element) => {
$props.drag = (node: Element) => {
node.addEventListener('mousedown', dragStart);
node.addEventListener('touchstart', dragStart);
return {
Expand All @@ -204,12 +212,15 @@ export const addResizedColumns =
},
};
};
action.disabled = isCellDisabled(cell, disabledResizeIds);
$props.disabled = isCellDisabled(cell, disabledResizeIds);
const props = derived([], () => {
return action;
return $props;
});
const attrs = derived(columnWidths, ($columnWidths) => {
const width = $columnWidths[cell.id];
const width =
cell instanceof GroupHeaderCell
? sum(cell.ids.map((id) => $columnWidths[id]))
: $columnWidths[cell.id];
if (width === undefined) {
return {};
}
Expand All @@ -225,6 +236,24 @@ export const addResizedColumns =
});
return { props, attrs };
},
'tbody.tr.td': (cell) => {
const attrs = derived(columnWidths, ($columnWidths) => {
const width = $columnWidths[cell.id];
if (width === undefined) {
return {};
}
const widthPx = `${width}px`;
return {
style: {
width: widthPx,
'min-width': widthPx,
'max-width': widthPx,
'box-sizing': 'border-box' as const,
},
};
});
return { attrs };
},
},
};
};

1 comment on commit 284dad2

@vercel
Copy link

@vercel vercel bot commented on 284dad2 Jul 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.