-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement lexicographic navigation (#68)
* feat: lexicographic navigation * fix: delete seperate style * style: remove underline & hide under small screen --------- Co-authored-by: NCJ <[email protected]>
- Loading branch information
Showing
4 changed files
with
223 additions
and
136 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,50 @@ | ||
import React from 'react'; | ||
import { Link, Grid, Hidden } from '@mui/material'; | ||
import { MirrorDto } from '../types/mirror'; | ||
|
||
export default ({ data }: { data: MirrorDto[] }) => { | ||
const caps: { [key: string]: boolean } = {}; | ||
|
||
data.forEach(mirror => { | ||
if (mirror.id.length < 0) { | ||
throw new Error('mirror id empty'); | ||
} | ||
const cap = mirror.id[0].toLocaleUpperCase(); | ||
|
||
caps[cap] = true; | ||
}); | ||
|
||
const buttons: React.ReactNode[] = []; | ||
|
||
for (let i = 'A'.charCodeAt(0); i <= 'Z'.charCodeAt(0); i += 1) { | ||
const char = String.fromCharCode(i); | ||
if (caps[char]) { | ||
buttons.push( | ||
<Link href={`#${char}`} style={{ textDecoration: 'none' }}> | ||
{char} | ||
</Link> | ||
); | ||
}; | ||
} | ||
|
||
return ( | ||
<Hidden smDown> | ||
<Grid | ||
container | ||
flexDirection="column" | ||
flexWrap="nowrap" | ||
spacing={1} | ||
sx={{ | ||
position: 'fixed', | ||
bottom: '1rem', | ||
right: '1rem', | ||
width: 'fit-content', | ||
}} | ||
> | ||
{buttons.map(fab => ( | ||
<Grid item>{fab}</Grid> | ||
))} | ||
</Grid> | ||
</Hidden> | ||
); | ||
}; |
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 |
---|---|---|
@@ -1,26 +1,54 @@ | ||
import React from 'react'; | ||
import { Box, Grid } from '@mui/material'; | ||
import { Grid, Typography } from '@mui/material'; | ||
import SearchItemCard from './search-item-card'; | ||
import { Mirror } from '../types/mirror'; | ||
|
||
export default (props: { queryResults: Mirror[] }) => ( | ||
<Box sx={{ display: 'flex', justifyContent: 'center' }}> | ||
export default (props: { queryResults: Mirror[] }) => { | ||
const lexicoMap: { [key: string]: Mirror[] } = {}; | ||
const caps: string[] = []; | ||
props.queryResults.forEach(mirror => { | ||
if (mirror.id.length < 0) { | ||
throw new Error('mirror id empty'); | ||
} | ||
const cap = mirror.id[0].toLocaleUpperCase(); | ||
|
||
if (lexicoMap[cap]) { | ||
lexicoMap[cap].push(mirror); | ||
} else { | ||
caps.push(cap); | ||
lexicoMap[cap] = [mirror]; | ||
} | ||
}); | ||
caps.sort(); | ||
|
||
return ( | ||
<Grid | ||
container | ||
spacing={{ xs: 2 }} | ||
columns={{ xs: 1, sm: 2, md: 3, lg: 4 }} | ||
alignItems="stretch" | ||
spacing={2} | ||
columns={1} | ||
flexDirection="column" | ||
wrap="nowrap" | ||
> | ||
{ | ||
// sort by queryResults.id | ||
props.queryResults | ||
?.sort((a, b) => a.id.toLowerCase().localeCompare(b.id.toLowerCase())) | ||
.map((item, i) => ( | ||
<Grid item xs={1} key={i}> | ||
<SearchItemCard queryItem={item} /> | ||
</Grid> | ||
)) | ||
} | ||
{caps.map(cap => ( | ||
<Grid item> | ||
<Typography gutterBottom variant="h6" component="div" id={cap}> | ||
{cap} | ||
</Typography> | ||
<Grid | ||
container | ||
spacing={{ xs: 2 }} | ||
columns={{ xs: 1, sm: 2, md: 3, lg: 4 }} | ||
alignItems="stretch" | ||
key={cap} | ||
> | ||
{lexicoMap[cap].map(mirror => ( | ||
<Grid item xs={1} key={mirror.id}> | ||
<SearchItemCard queryItem={mirror} /> | ||
</Grid> | ||
))} | ||
</Grid> | ||
</Grid> | ||
))} | ||
</Grid> | ||
</Box> | ||
); | ||
); | ||
}; |
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.