Skip to content

Commit 66ed6d5

Browse files
spaceokasperbirch1
authored andcommitted
Cleaning up tag component
...and change datastructure of facet line components to make it easier to extend the functionality in the future
1 parent e14ca8b commit 66ed6d5

File tree

6 files changed

+74
-71
lines changed

6 files changed

+74
-71
lines changed

src/stories/Library/Modals/modal-facet-browser/FacetBrowser.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const FacetBrowser: React.FC<FacetBrowserProps> = ({
4040
>
4141
<div className="facet-browser__facet-group">
4242
{facet.tags.map((tag) => (
43-
<Tag facet key={tag} label={tag} />
43+
<Tag key={tag}>{tag}</Tag>
4444
))}
4545
</div>
4646
<button className="link-tag cursor-pointer facet-browser__more-btn">

src/stories/Library/search-result-page/FacetLine.tsx

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import * as React from "react";
22
import { FC } from "react";
33
import { Dropdown } from "../dropdown/Dropdown";
4+
import { Tag } from "../tag/Tag";
45

5-
export type FacetLineTermItem = {
6+
export type FacetLineItem<TType extends "facet" | "term"> = {
67
title: string;
7-
type: "term";
8-
score: number;
9-
terms: never;
10-
};
11-
export type FacetLineFacetItem = {
12-
title: string;
13-
type: "facet";
14-
terms: FacetLineTermItem[];
15-
score: never;
8+
type: TType;
9+
score: TType extends "term" ? number : never;
10+
facet: TType extends "term" ? string : never;
11+
terms: TType extends "facet" ? FacetLineItem<"term">[] : never;
1612
};
1713
export interface FacetLineProps {
18-
items: (FacetLineTermItem | FacetLineFacetItem)[];
14+
items: (FacetLineItem<"term"> | FacetLineItem<"facet">)[];
1915
}
2016

2117
const FacetLine: FC<FacetLineProps> = ({ items }) => {
@@ -24,17 +20,19 @@ const FacetLine: FC<FacetLineProps> = ({ items }) => {
2420
{items.map(({ type, title, terms, score }, index) => {
2521
if (type === "term") {
2622
return (
27-
<li key={index} className="tag tag--outlined facet-line__item">
28-
{title} ({score})
23+
<li key={index} className="facet-line__item">
24+
<Tag isClickable={false}>
25+
{title} ({score})
26+
</Tag>
2927
</li>
3028
);
3129
}
3230
if (type === "facet") {
3331
return (
3432
<li className="facet-line__item">
3533
<Dropdown
36-
ariaLabel="Choose periodical year"
37-
list={[{ title, disabled: true }, ...terms]}
34+
ariaLabel={title}
35+
list={[{ title }, ...terms]}
3836
arrowIcon="chevron"
3937
classNames="dropdown--grey-borders"
4038
/>
@@ -44,7 +42,9 @@ const FacetLine: FC<FacetLineProps> = ({ items }) => {
4442

4543
return null;
4644
})}
47-
<li className="tag tag--outlined facet-line__item">+ Flere filtre</li>
45+
<li className="facet-line__item">
46+
<Tag isClickable={false}>+ Flere filtre</Tag>
47+
</li>
4848
</ul>
4949
);
5050
};

src/stories/Library/search-result-page/FacetLineSelected.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import * as React from "react";
22
import { FC } from "react";
3+
import { Tag } from "../tag/Tag";
4+
import { FacetLineItem } from "./FacetLine";
35

46
export interface FacetLineSelectedProps {
5-
items: string[];
7+
items: FacetLineItem<"term">[];
68
}
79

810
const FacetLineSelected: FC<FacetLineSelectedProps> = ({ items }) => {
911
return (
1012
<ul className="facet-line-selected">
11-
{items.map((item) => (
13+
{items.map(({ title }) => (
1214
<li className="facet-line-selected__item">
13-
<span className="tag tag--small cursor-pointer">
14-
{item}
15-
<img
16-
className="tag-icon"
17-
src="icons/basic/icon-cross.svg"
18-
alt="close icon"
19-
/>
20-
</span>
15+
<Tag showCloseIcon hasBackground>
16+
{title}
17+
</Tag>
2118
</li>
2219
))}
2320
</ul>

src/stories/Library/search-result-page/SearchResultPage.tsx

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SearchResultInfo } from "./SearchResultInfo";
33
import ResultPager from "./ResultPager";
44
import { SearchResultTitle } from "./SearchResultTitle";
55
import { SearchResultZero } from "./SearchResultZero";
6-
import FacetLine, { FacetLineFacetItem, FacetLineTermItem } from "./FacetLine";
6+
import FacetLine, { FacetLineItem } from "./FacetLine";
77
import FacetLineSelected from "./FacetLineSelected";
88

99
export type SearchResultPageProps = {
@@ -91,28 +91,32 @@ const SearchResult = [
9191
];
9292

9393
const facetLineItems = [
94-
{ title: "Skønlitteratur", type: "term", score: 96 },
95-
{ title: "Faglitteratur", type: "term", score: 302 },
96-
{ title: "Film", type: "term", score: 28 },
97-
{ title: "Spil", type: "term", score: 21 },
94+
{ title: "fantasy", facet: "genreAndForm", type: "term", score: 96 },
9895
{
99-
title: "Emne",
96+
title: "Faglitteratur",
97+
facet: "fictionNonfiction",
98+
type: "term",
99+
score: 302,
100+
},
101+
{ title: "bog", facet: "materialType", type: "term", score: 28 },
102+
{ title: "Spil", facet: "workTypes", type: "term", score: 21 },
103+
{
104+
title: "emner",
100105
type: "facet",
101106
terms: [
102-
{ title: "Efterår", type: "term", score: 96 },
103-
{ title: "Børn og Unge", type: "term", score: 302 },
104-
{ title: "Sundhed", type: "term", score: 28 },
107+
{ title: "Børnebøger", facet: "emner", type: "term", score: 96 },
108+
{ title: "magi", facet: "emner", type: "term", score: 302 },
109+
{ title: "troldmænd", facet: "emner", type: "term", score: 28 },
105110
],
106111
},
107112
{ title: "Musik", type: "term", score: 12 },
108-
] as (FacetLineTermItem | FacetLineFacetItem)[];
113+
] as (FacetLineItem<"term"> | FacetLineItem<"facet">)[];
109114

110115
const selectedTerms = [
111-
"Eventyr",
112-
"J. K. Rowling",
113-
"Harry Potter",
114-
"Skønlitteratur",
115-
];
116+
{ title: "magi", facet: "subjects", type: "term", score: 302 },
117+
{ title: "bog", facet: "materialType", type: "term", score: 28 },
118+
{ title: "Spil", facet: "workTypes", type: "term", score: 21 },
119+
] as FacetLineItem<"term">[];
116120

117121
const SearchResultList = SearchResult.map((item) => {
118122
return <SearchResultItem {...item} />;

src/stories/Library/search-result-page/search-result-page.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
@extend %text-tags;
2323
height: 100%;
2424
padding: 8px 16px;
25-
min-width: 100px;
25+
min-width: 200px;
2626
}
2727
.dropdown__arrows {
2828
height: 100%;

src/stories/Library/tag/Tag.tsx

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,44 @@ import { useState } from "react";
22
import clsx from "clsx";
33

44
type TagProps = {
5-
label: string;
6-
removable?: boolean;
7-
facet?: boolean;
5+
children: React.ReactNode;
6+
size?: "small" | "large";
7+
usesCursor?: boolean;
8+
hasBackground?: boolean;
9+
showCloseIcon?: boolean;
10+
isClickable?: boolean;
811
};
912

10-
const Icon = () => (
11-
<img className="tag-icon" src="icons/basic/icon-cross.svg" alt="close icon" />
12-
);
13-
14-
export const Tag = ({ label, removable = false, facet }: TagProps) => {
13+
export const Tag = ({
14+
children,
15+
hasBackground = false,
16+
size = "small",
17+
usesCursor = true,
18+
showCloseIcon = false,
19+
isClickable = true,
20+
}: TagProps) => {
1521
const [selected, setSelected] = useState(false);
1622

17-
if (facet) {
18-
return (
19-
<button
20-
onClick={() => setSelected(!selected)}
21-
className={clsx(
22-
"tag tag--outlined",
23-
selected && "tag--outlined-selected"
24-
)}
25-
>
26-
{label}
27-
</button>
28-
);
29-
}
30-
3123
return (
32-
<span
24+
<button
25+
aria-pressed={selected}
26+
onClick={() => isClickable && setSelected(!selected)}
3327
className={clsx(
34-
"tag tag--small",
35-
removable ? "cursor-pointer" : "tag--large"
28+
"tag tag--outlined",
29+
(hasBackground || selected) && "tag--outlined-selected",
30+
usesCursor && "cursor-pointer",
31+
`tag--${size}`
3632
)}
3733
>
38-
{label}
39-
{removable && <Icon />}
40-
</span>
34+
{children}
35+
{showCloseIcon && (
36+
<img
37+
className="tag-icon"
38+
src="icons/basic/icon-cross.svg"
39+
alt="close icon"
40+
/>
41+
)}
42+
</button>
4143
);
4244
};
4345

0 commit comments

Comments
 (0)