This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Table): New clickable column headers
- Loading branch information
1 parent
b6eef4d
commit 2628d6b
Showing
6 changed files
with
285 additions
and
33 deletions.
There are no files selected for viewing
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,137 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import styled, {css} from 'styled-components'; | ||
|
||
import {alpha} from '../utils/colors'; | ||
import ArrowIcon from '../icons/Arrow'; | ||
import ButtonCore from '../ButtonCore'; | ||
|
||
import VisuallyHidden from '../VisuallyHidden'; | ||
|
||
const HeaderButton = styled(ButtonCore)` | ||
vertical-align: baseline; | ||
padding-right: ${p => p.theme.globals.spacing.xxs}; | ||
font-weight: bold; | ||
`; | ||
|
||
const ClickabilityIndicator = styled.span` | ||
position: relative; | ||
margin-left: ${p => p.theme.globals.spacing.xxs}; | ||
&::after { | ||
content: ''; | ||
position: absolute; | ||
width: 4px; | ||
height: 4px; | ||
top: calc(50% - 2px); | ||
left: calc(50% - 2px); | ||
background-color: currentcolor; | ||
border-radius: 50%; | ||
opacity: ${p => (p.isActive ? 0 : 0.5)}; | ||
transition: opacity 0.15s ease-in-out 0.15s; | ||
} | ||
${HeaderButton}:hover &::after, | ||
${HeaderButton}.focus-visible &::after { | ||
opacity: 0; | ||
transition-delay: 0s; | ||
} | ||
`; | ||
|
||
const StyledSortArrow = styled(ArrowIcon)` | ||
border-radius: 50%; | ||
transition-property: all; | ||
${p => | ||
!p.isActive && | ||
css` | ||
opacity: 0; | ||
`} | ||
${HeaderButton}:hover &, | ||
${HeaderButton}.focus-visible & { | ||
fill: ${p => p.theme.links}; | ||
opacity: 1; | ||
transition-delay: 0.15s; | ||
${p => | ||
p.isActive && | ||
` | ||
background-color: ${alpha(p.theme.shade, p.theme.shadeStrength)}; | ||
box-shadow: 0 0 0 3px | ||
${alpha(p.theme.shade, p.isActive ? p.theme.shadeStrength : 0)}; | ||
`} | ||
} | ||
`; | ||
|
||
function SortArrow({isActive, orderDir}) { | ||
return ( | ||
<ClickabilityIndicator isActive={isActive}> | ||
<StyledSortArrow | ||
vAlign | ||
dimmed | ||
isActive={isActive} | ||
scale={0.8} | ||
rotate={orderDir === 'desc' ? -90 : 90} | ||
/> | ||
</ClickabilityIndicator> | ||
); | ||
} | ||
|
||
const reverseOrder = { | ||
asc: 'desc', | ||
desc: 'asc', | ||
}; | ||
|
||
function ClickableHeader({ | ||
isActive, | ||
column, | ||
order, | ||
orderLabels, | ||
onRequestSort, | ||
children, | ||
}) { | ||
if (!column.sortable) { | ||
return children; | ||
} | ||
|
||
function getSortHandler({name, defaultOrder = 'asc'}) { | ||
if (typeof onRequestSort !== 'function') return null; | ||
|
||
return () => { | ||
// Only flip the order when the column to | ||
// be ordered by changes. Otherwise use the | ||
// column's `defaultOrder` prop | ||
onRequestSort({ | ||
property: name, | ||
order: isActive ? reverseOrder[order] : defaultOrder, | ||
}); | ||
}; | ||
} | ||
|
||
return ( | ||
<HeaderButton onClick={getSortHandler(column)}> | ||
{children} | ||
{column.sortable && ( | ||
<SortArrow | ||
isActive={isActive} | ||
orderDir={isActive ? order : column.defaultOrder} | ||
/> | ||
)} | ||
{isActive && <VisuallyHidden>{orderLabels[order]}</VisuallyHidden>} | ||
</HeaderButton> | ||
); | ||
} | ||
|
||
ClickableHeader.propTypes = { | ||
isActive: PropTypes.bool, | ||
children: PropTypes.node, | ||
column: PropTypes.object.isRequired, | ||
order: PropTypes.oneOf(['asc', 'desc']), | ||
orderLabels: PropTypes.shape({ | ||
asc: PropTypes.string.isRequired, | ||
desc: PropTypes.string.isRequired, | ||
}), | ||
onClick: PropTypes.func, | ||
}; | ||
|
||
export default ClickableHeader; |
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
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,38 @@ | ||
import {useState} from 'react'; | ||
|
||
function sortBy(property, order = 'asc') { | ||
const orderAsNumber = { | ||
asc: 1, | ||
desc: -1, | ||
}; | ||
|
||
const prop = property.toLowerCase(); | ||
|
||
return function(a, b) { | ||
const result = a[prop] < b[prop] ? -1 : a[prop] > b[prop] ? 1 : 0; | ||
return result * orderAsNumber[order]; | ||
}; | ||
} | ||
|
||
function DemoTableState({data, children}) { | ||
const [order, setOrder] = useState('asc'); | ||
const [orderProp, setOrderProp] = useState('Name'); | ||
|
||
function handleSort(newOrder) { | ||
setOrder(newOrder.order); | ||
setOrderProp(newOrder.property); | ||
} | ||
|
||
const orderedData = data.sort(sortBy(orderProp, order)); | ||
|
||
return children({ | ||
data: orderedData, | ||
sort: { | ||
order, | ||
property: orderProp, | ||
}, | ||
handleSort, | ||
}); | ||
} | ||
|
||
export default DemoTableState; |
File renamed without changes.
Oops, something went wrong.