Skip to content

Commit ce0c5cb

Browse files
committed
feat: TagList molecule
1 parent b21c1cf commit ce0c5cb

File tree

5 files changed

+114
-26
lines changed

5 files changed

+114
-26
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

components/page/Aside/Tags/index.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
import PropTypes from "prop-types";
2-
import Link from "next/link";
32
import { useTranslation } from "react-i18next";
43
import AsideSection from "../Section";
5-
import * as Styled from "./styles";
4+
import TagList from "@/components/molecules/TagList";
65

76
export default function Tags({ tags, rootHomeLink }) {
87
const { t } = useTranslation();
98

109
if (!tags) return null;
1110
if (tags.length <= 0) return null;
1211

12+
const tagsWithLinks = tags.map(({ slug, title }) => {
13+
return {
14+
name: title,
15+
destination: `/${rootHomeLink?.uri}?search=${slug}`,
16+
};
17+
});
18+
1319
return (
1420
<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} />
3122
</AsideSection>
3223
);
3324
}

components/page/Aside/Tags/styles.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)