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

Adding sortable tables #6338

Merged
merged 23 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8b82106
Adding sortable tables
matthewshaver Oct 22, 2024
55ad30a
Removing content
matthewshaver Oct 22, 2024
a82c25d
Removing fields
matthewshaver Oct 22, 2024
a34886c
Update website/snippets/_enterprise-permissions-table.md
matthewshaver Oct 22, 2024
3445c64
Importing component
matthewshaver Oct 22, 2024
1e4f076
Importing table widely
matthewshaver Oct 22, 2024
f9ade1b
Update website/src/theme/MDXComponents/index.js
matthewshaver Oct 22, 2024
1d3f476
Removing need to define alignment
matthewshaver Oct 22, 2024
0922f6e
Merge branch 'sortable-table' of https://github.com/dbt-labs/docs.get…
matthewshaver Oct 22, 2024
55128c4
Merge branch 'current' into sortable-table
matthewshaver Oct 22, 2024
04902ba
FIxing syntax
matthewshaver Nov 1, 2024
fdca973
Merge branch 'current' into sortable-table
mirnawong1 Nov 4, 2024
b387279
Fixing table markdown formatting
matthewshaver Nov 8, 2024
4ec3975
Removing notes
matthewshaver Nov 8, 2024
64b9d7b
Merge branch 'current' into sortable-table
matthewshaver Nov 8, 2024
0f4353c
Update _enterprise-permissions-table.md
mirnawong1 Nov 8, 2024
d6dc431
Update _enterprise-permissions-table.md
mirnawong1 Nov 8, 2024
c41f504
Update _enterprise-permissions-table.md
mirnawong1 Nov 8, 2024
3d3bb44
Merge branch 'current' into sortable-table
matthewshaver Nov 12, 2024
4948a16
Wrapping parsemarkdowntable in useMemo
matthewshaver Nov 13, 2024
d6dbaf7
Merge branch 'sortable-table' of https://github.com/dbt-labs/docs.get…
matthewshaver Nov 13, 2024
b6943d0
Merge branch 'current' into sortable-table
matthewshaver Nov 13, 2024
39571fb
Update _enterprise-permissions-table.md
matthewshaver Nov 13, 2024
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
12 changes: 12 additions & 0 deletions website/package-lock.json

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

1 change: 1 addition & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"gray-matter": "^4.0.3",
"hast-util-is-element": "^1.1.0",
"js-yaml": "^4.1.0",
"markdown-to-jsx": "^7.5.0",
"mobx": "^6.3.9",
"node-polyfill-webpack-plugin": "^1.1.4",
"papaparse": "^5.3.2",
Expand Down
114 changes: 114 additions & 0 deletions website/src/components/sortableTable/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import React, { useState, useMemo } from 'react';
import Markdown from 'markdown-to-jsx';

const stripMarkdown = (text) => {
let strippedText = text.replace(/\[([^\]]+)\]\([^)]+\)/g, '$1');
strippedText = strippedText.replace(/[_*`~]/g, '');
return strippedText;
};

const parseMarkdownTable = (markdown) => {
Copy link
Member

Choose a reason for hiding this comment

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

Hey Matt! This is a totally awesome implementation. Really nice work here. It was clever to include cases for different alignment codes for table rows in markdown - I always forget those syntax options exist!

Re: what I was saying in our meeting yesterday: if you really wanted to, you could wrap parseMarkdownTable in a useMemo hook to avoid it running on every re-render for performance reasons! But I did check out the component on the enterprise perms table, adjusted some things to re-run the state, and the performance hit is negligible.

I wouldn't say it's a blocking change IMO, but feel free to update it if you want to! All in all, this LGTM. 🚢

const rows = markdown.trim().split('\n');
const headers = rows[0].split('|').map((header) => header.trim()).filter(Boolean);

const alignmentsRow = rows[1].split('|').map((align) => align.trim()).filter(Boolean);
const columnAlignments = alignmentsRow.map((alignment) => {
if (alignment.startsWith(':') && alignment.endsWith(':')) {
return 'center';
} else if (alignment.startsWith(':')) {
return 'left';
} else if (alignment.endsWith(':')) {
return 'right';
} else {
return 'left';
}
});

const data = rows.slice(2).map(row => row.split('|').map(cell => cell.trim()).filter(Boolean));

return { headers, data, columnAlignments };
};

const SortableTable = ({ children }) => {
const { headers, data: initialData, columnAlignments } = useMemo(
() => parseMarkdownTable(children),
[children]
);

const [data, setData] = useState(initialData);
const [sortConfig, setSortConfig] = useState({ key: '', direction: 'asc' });

const sortTable = (keyIndex) => {
const newDirection = (sortConfig.key === keyIndex && sortConfig.direction === 'asc') ? 'desc' : 'asc';
setSortConfig({ key: keyIndex, direction: newDirection });

const sortedData = [...data].sort((a, b) => {
const aVal = stripMarkdown(a[keyIndex]);
const bVal = stripMarkdown(b[keyIndex]);
if (aVal < bVal) return newDirection === 'asc' ? -1 : 1;
if (aVal > bVal) return newDirection === 'asc' ? 1 : -1;
return 0;
});

setData(sortedData);
};

return (
<table>
<thead>
<tr>
{headers.map((header, index) => (
<th
key={index}
onClick={() => sortTable(index)}
style={{
cursor: 'pointer',
position: 'relative',
textAlign: columnAlignments[index],
padding: '10px'
}}
>
<div style={{
display: 'flex',
alignItems: 'center',
justifyContent: columnAlignments[index] === 'center' ? 'center' : columnAlignments[index]
}}>
<span style={{ marginRight: '5px' }}>{header}</span>
<span style={{
opacity: sortConfig.key === index && sortConfig.direction === 'asc' ? 1 : (sortConfig.key === index ? 0.5 : 0.5)
}}>
</span>
<span style={{
marginLeft: '5px',
opacity: sortConfig.key === index && sortConfig.direction === 'desc' ? 1 : (sortConfig.key === index ? 0.5 : 0.5)
}}>
</span>
</div>
</th>
))}
</tr>
</thead>
<tbody>
{data.map((row, rowIndex) => (
<tr key={rowIndex}>
{row.map((cell, cellIndex) => (
<td
key={cellIndex}
style={{
textAlign: columnAlignments[cellIndex],
padding: '8px'
}}
>
<Markdown>{cell || '\u00A0'}</Markdown>
</td>
))}
</tr>
))}
</tbody>
</table>
);
};

export default SortableTable;
2 changes: 2 additions & 0 deletions website/src/theme/MDXComponents/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Mermaid from '@theme/Mermaid';
/* dbt Customizations:
* Imports the following components below for export
*/
import SortableTable from '@site/src/components/sortableTable';
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem'
import Changelog from '@site/src/components/changelog';
Expand Down Expand Up @@ -95,5 +96,6 @@ const MDXComponents = {
DetailsToggle: DetailsToggle,
Expandable: Expandable,
ConfettiTrigger: ConfettiTrigger,
SortableTable: SortableTable,
};
export default MDXComponents;
Loading