File tree Expand file tree Collapse file tree 5 files changed +114
-26
lines changed Expand file tree Collapse file tree 5 files changed +114
-26
lines changed Original file line number Diff line number Diff line change
1
+ import TagList from "./index" ;
2
+
3
+ const tags = [
4
+ { name : "Static Tag" } ,
5
+ { name : "Linked Tag" , destination : "https://rubinobservatory.org" } ,
6
+ ] ;
7
+
8
+ describe ( "<TagList>" , ( ) => {
9
+ it ( "should render an unordered list" , ( ) => {
10
+ cy . mount ( < TagList /> ) ;
11
+ cy . get ( "ul" ) . should ( "exist" ) ;
12
+ } ) ;
13
+
14
+ it ( "should render tag names with a pound symbol by default" , ( ) => {
15
+ cy . mount ( < TagList tags = { tags } /> ) ;
16
+ cy . get ( "ul > li" ) . each ( ( $el , i ) => {
17
+ cy . wrap ( $el ) . contains ( `#${ tags [ i ] . name } ` ) ;
18
+ } ) ;
19
+ } ) ;
20
+
21
+ it ( "should omit pound symbol if specified" , ( ) => {
22
+ cy . mount ( < TagList tags = { tags } showPound = { false } /> ) ;
23
+ cy . get ( "ul > li" ) . each ( ( $el , i ) => {
24
+ cy . wrap ( $el ) . contains ( tags [ i ] . name ) ;
25
+ } ) ;
26
+ } ) ;
27
+
28
+ it ( "should render links if provided" , ( ) => {
29
+ cy . mount ( < TagList tags = { tags } /> ) ;
30
+ cy . get ( "ul > li" ) . each ( ( $el , i ) => {
31
+ if ( tags [ i ] . destination ) {
32
+ cy . wrap ( $el ) . children ( "a" ) . should ( "exist" ) ;
33
+ } else {
34
+ cy . wrap ( $el ) . children ( "a" ) . should ( "not.exist" ) ;
35
+ }
36
+ } ) ;
37
+ } ) ;
38
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import { FunctionComponent } from "react" ;
2
+ import classNames from "classnames" ;
3
+ import Link from "next/link" ;
4
+ import styles from "./styles.module.css" ;
5
+
6
+ export type Tag = {
7
+ name : string ;
8
+ destination ?: string ;
9
+ } ;
10
+ interface TagListProps {
11
+ tags : Array < Tag > ;
12
+ showPound ?: boolean ;
13
+ withLinebreaks ?: boolean ;
14
+ className ?: string ;
15
+ }
16
+
17
+ const TagList : FunctionComponent < TagListProps > = ( {
18
+ tags = [ ] ,
19
+ showPound = true ,
20
+ withLinebreaks = false ,
21
+ className,
22
+ } ) => {
23
+ return (
24
+ < ul
25
+ data-no-break = { ! withLinebreaks }
26
+ className = { classNames ( styles . tagList , className ) }
27
+ >
28
+ { tags . map ( ( { name, destination } ) => {
29
+ const tagName = showPound ? `#${ name } ` : name ;
30
+
31
+ return (
32
+ < li className = { styles . tag } data-no-break = { ! withLinebreaks } key = { name } >
33
+ { destination ? (
34
+ < Link href = { destination } rel = "tag" >
35
+ { tagName }
36
+ </ Link >
37
+ ) : (
38
+ tagName
39
+ ) }
40
+ </ li >
41
+ ) ;
42
+ } ) }
43
+ </ ul >
44
+ ) ;
45
+ } ;
46
+
47
+ TagList . displayName = "Molecule.TagList" ;
48
+
49
+ export default TagList ;
Original file line number Diff line number Diff line change
1
+ .tagList {
2
+ & [data-no-break = "true" ] {
3
+ display : flex;
4
+ flex-wrap : wrap;
5
+ column-gap : 1ex ;
6
+ }
7
+ }
8
+
9
+ .tag {
10
+ & a {
11
+ color : var (--color-font-accent );
12
+ }
13
+
14
+ & [data-no-break = "true" ] {
15
+ display : inline-block;
16
+ white-space : nowrap;
17
+ }
18
+ }
Original file line number Diff line number Diff line change 1
1
import PropTypes from "prop-types" ;
2
- import Link from "next/link" ;
3
2
import { useTranslation } from "react-i18next" ;
4
3
import AsideSection from "../Section" ;
5
- import * as Styled from "./styles " ;
4
+ import TagList from "@/components/molecules/TagList " ;
6
5
7
6
export default function Tags ( { tags, rootHomeLink } ) {
8
7
const { t } = useTranslation ( ) ;
9
8
10
9
if ( ! tags ) return null ;
11
10
if ( tags . length <= 0 ) return null ;
12
11
12
+ const tagsWithLinks = tags . map ( ( { slug, title } ) => {
13
+ return {
14
+ name : title ,
15
+ destination : `/${ rootHomeLink ?. uri } ?search=${ slug } ` ,
16
+ } ;
17
+ } ) ;
18
+
13
19
return (
14
20
< AsideSection title = { t ( `tags` ) } >
15
- < Styled . TagList >
16
- { tags . map ( ( tag , i ) => {
17
- if ( rootHomeLink ?. uri && tag . slug ) {
18
- return (
19
- < Styled . Tag key = { i } >
20
- < Link
21
- prefetch = { false }
22
- href = { `/${ rootHomeLink ?. uri } ?search=${ tag . slug } ` }
23
- >
24
- { `#${ tag . title } ` }
25
- </ Link >
26
- </ Styled . Tag >
27
- ) ;
28
- }
29
- } ) }
30
- </ Styled . TagList >
21
+ < TagList tags = { tagsWithLinks } />
31
22
</ AsideSection >
32
23
) ;
33
24
}
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments