-
Notifications
You must be signed in to change notification settings - Fork 951
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
Adding sortable tables #6338
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
8b82106
Adding sortable tables
matthewshaver 55ad30a
Removing content
matthewshaver a82c25d
Removing fields
matthewshaver a34886c
Update website/snippets/_enterprise-permissions-table.md
matthewshaver 3445c64
Importing component
matthewshaver 1e4f076
Importing table widely
matthewshaver f9ade1b
Update website/src/theme/MDXComponents/index.js
matthewshaver 1d3f476
Removing need to define alignment
matthewshaver 0922f6e
Merge branch 'sortable-table' of https://github.com/dbt-labs/docs.get…
matthewshaver 55128c4
Merge branch 'current' into sortable-table
matthewshaver 04902ba
FIxing syntax
matthewshaver fdca973
Merge branch 'current' into sortable-table
mirnawong1 b387279
Fixing table markdown formatting
matthewshaver 4ec3975
Removing notes
matthewshaver 64b9d7b
Merge branch 'current' into sortable-table
matthewshaver 0f4353c
Update _enterprise-permissions-table.md
mirnawong1 d6dc431
Update _enterprise-permissions-table.md
mirnawong1 c41f504
Update _enterprise-permissions-table.md
mirnawong1 3d3bb44
Merge branch 'current' into sortable-table
matthewshaver 4948a16
Wrapping parsemarkdowntable in useMemo
matthewshaver d6dbaf7
Merge branch 'sortable-table' of https://github.com/dbt-labs/docs.get…
matthewshaver b6943d0
Merge branch 'current' into sortable-table
matthewshaver 39571fb
Update _enterprise-permissions-table.md
matthewshaver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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. 🚢