Skip to content

Commit cf571d0

Browse files
Add shortcut table sorting
1 parent 1acc7a5 commit cf571d0

File tree

6 files changed

+87
-23
lines changed

6 files changed

+87
-23
lines changed

.DS_Store

-6 KB
Binary file not shown.

studio/schemas/shortcut.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default {
77
title: 'Name',
88
name: 'name',
99
type: 'string',
10-
validation: Rule => Rule.unique()
10+
// validation: Rule => Rule.unique()
1111
},
1212
{
1313
title: 'Mac Command',

web/components/ShortcutTable.js

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,65 @@
1-
import React from 'react'
1+
import React, { useState } from 'react'
2+
import TableRows from './TableRows'
3+
import styled from 'styled-components'
4+
import sortAlpha from '../utils/sorting'
5+
// import sortDown from '../src/icons/sort-down.svg'
6+
// import sortUp from '../src/icons/sort-up.svg'
7+
// import refresh from '../src/icons/refresh.svg'
28

9+
const ShortcutNameTh = styled.th`
10+
&:hover {
11+
cursor: pointer;
12+
}
13+
`
14+
const SortTextSpan = styled.span`
15+
display: block;
16+
font-size: .6em;
17+
`
318
export default function ShortcutTable(props) {
4-
// console.log('ShortcutTable props: ', props)
19+
// console.log('ShortcutTable props: ', props)
20+
const [sortedRows, setSortedRows] = useState(null)
21+
const [sortOrder, setSortOrder] = useState('AZ')
22+
const sortTextEnum = ['Sorted A-Z', 'Sorted Z-A', 'Unsorted']
23+
const [sortText, setSortText] = useState(sortTextEnum[2])
24+
const unsortedRows = [...props.section.subsections]
25+
26+
function handleShortcutSort() {
27+
// console.log('handleShortcutSort')
28+
if (sortedRows === null) {
29+
setSortedRows(sortAlpha(unsortedRows, 'AZ'))
30+
setSortOrder('AZ')
31+
setSortText(sortTextEnum[0])
32+
} else if (sortOrder === 'AZ') {
33+
setSortedRows(sortAlpha(unsortedRows, 'ZA'))
34+
setSortOrder('ZA')
35+
setSortText(sortTextEnum[1])
36+
} else {
37+
setSortedRows(null)
38+
setSortText(sortTextEnum[2])
39+
}
40+
}
41+
42+
// function clearSort() { setSortedRows(null) }
43+
544
return (
645
<div className="table_container">
746
<table>
847
<thead>
948
<tr>
10-
<th>Shortcut</th>
49+
<ShortcutNameTh onClick={handleShortcutSort}>
50+
<span>Shortcut</span>
51+
<SortTextSpan>( {sortText} )</SortTextSpan>
52+
</ShortcutNameTh>
1153
<th>Mac</th>
1254
<th>Windows</th>
1355
</tr>
1456
</thead>
1557

1658
<tbody>
17-
{props.section.subsections.map(secData => {
18-
return (
19-
<tr key={secData.name}>
20-
<th className="row_header">{secData.name}</th>
21-
<td>
22-
{secData.mac_command &&
23-
<kbd>{secData.mac_command}</kbd>
24-
}
25-
26-
</td>
27-
<td>
28-
{secData.windows_command &&
29-
<kbd>{secData.windows_command}</kbd>
30-
}
31-
</td>
32-
</tr>
33-
)
34-
})}
59+
{sortedRows
60+
? <TableRows rows={sortedRows} />
61+
: <TableRows rows={unsortedRows} />
62+
}
3563
</tbody>
3664
</table>
3765
</div>

web/components/TableRows.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react'
2+
3+
export default function TableRows(props) {
4+
const rows = props.rows.map(row => {
5+
return (
6+
<tr key={row.name}>
7+
<th className="row_header">{row.name}</th>
8+
<td>
9+
{row.mac_command &&
10+
<kbd>{row.mac_command}</kbd>
11+
}
12+
</td>
13+
<td>
14+
{row.windows_command &&
15+
<kbd>{row.windows_command}</kbd>
16+
}
17+
</td>
18+
</tr>
19+
)
20+
})
21+
return rows
22+
}

web/components/TopicSection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import DescriptiveItem from './DescriptiveItem'
88
// `
99

1010
export default function TopicSection(props) {
11-
console.log('TS props: ', props);
11+
// console.log('TS props: ', props);
1212
return (
1313
<section
1414
id={props.section.anchor_id}

web/utils/sorting.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function sortAlpha(arr, order) {
2+
// console.log('sorter arr: ', arr)
3+
if (order === 'AZ') {
4+
return arr.sort((a, b) => {
5+
return (a.name > b.name) ? 1 : -1
6+
})
7+
} else {
8+
return arr.sort((a, b) => {
9+
return (a.name < b.name) ? 1 : -1
10+
})
11+
}
12+
}
13+
14+
export default sortAlpha

0 commit comments

Comments
 (0)