Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set default labels as index + 1 for canvas figures. #232

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/components/Viewer/Media/Media.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,53 @@
import { render, screen } from "@testing-library/react";

import { Canvas } from "@iiif/presentation-3";
import Media from "src/components/Viewer/Media/Media";
import React from "react";
import { render, screen } from "@testing-library/react";

const items = [
{
id: "https://example.org/manifest/1/canvas/1",
type: "Canvas",
height: 600,
width: 800,
label: { none: ["Canvas 1"] },
thumbnail: [
{
id: "https://example.org/item/1/default.jpg",
type: "Image",
height: 75,
width: 100,
format: "image/jpeg",
},
],
},
{
id: "https://example.org/manifest/1/canvas/2",
type: "Canvas",
height: 600,
width: 800,
label: { none: ["Canvas 2"] },
thumbnail: [
{
id: "https://example.org/item/2/default.jpg",
type: "Image",
height: 75,
width: 100,
format: "image/jpeg",
},
],
},
] as unknown as Canvas[];

describe("Media component", () => {
it("renders", () => {
render(<Media items={[]} activeItem={0} />);
render(<Media items={items} activeItem={0} />);
const media = screen.getByTestId("media");
expect(media);
expect(media.hasAttribute("aria-label")).toBe(true);
expect(media.getAttribute("data-active-canvas")).toBe(
"https://example.org/manifest/1/canvas/1",
);
expect(media.getAttribute("data-canvas-length")).toBe("2");
});
});
14 changes: 11 additions & 3 deletions src/components/Viewer/Media/Media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,33 @@ const Media: React.FC<MediaProps> = ({ items }) => {
handleFilter={handleFilter}
handleCanvasToggle={handleCanvasToggle}
activeIndex={activeIndex}
canvasLength={mediaItems.length}
canvasLength={items.length}
/>
<Group
aria-label={t("media.selectItem")}
data-testid="media"
data-active-canvas={items[activeIndex].id}
data-canvas-length={items.length}
data-filter={filter}
ref={scrollRef}
>
{mediaItems
.filter((item) => {
.filter((item, key) => {
if (!filter) return true;

if (item.canvas?.label) {
const label = getLabel(item.canvas.label);
if (Array.isArray(label))
return label[0].toLowerCase().includes(filter.toLowerCase());
} else {
const label = (key + 1).toString();
return label.includes(filter);
}
})
.map((item, index) => (
<Thumbnail
canvas={item.canvas as CanvasNormalized}
canvasIndex={index}
canvasIndex={mediaItems.findIndex((el) => el === item)}
handleChange={handleChange}
isActive={activeCanvas === item?.canvas?.id ? true : false}
key={item?.canvas?.id}
Expand Down
14 changes: 14 additions & 0 deletions src/components/Viewer/Media/Thumbnail.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ describe("Thumbnail component", () => {
});
});

describe("figure missing a label", () => {
const newProps = { ...props, canvas: { ...props.canvas, label: null } };

it("renders a fallback label for item index + 1", () => {
render(
<Group>
<Thumbnail {...newProps} />
</Group>,
);
expect(screen.getByTestId("fig-caption")).toHaveTextContent("2");
expect(screen.getByAltText("2"));
});
});

describe("audio and video thumbnail types", () => {
it("renders a duration value in the tag for audio type", () => {
const newProps = { ...props, type: "Sound" };
Expand Down
21 changes: 11 additions & 10 deletions src/components/Viewer/Media/Thumbnail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ const Thumbnail: React.FC<ThumbnailProps> = ({
type,
handleChange,
}) => {
const label = canvas?.label
? getLabel(canvas.label)
: (canvasIndex + 1).toString();

return (
<Item
aria-checked={isActive}
Expand All @@ -65,12 +69,7 @@ const Thumbnail: React.FC<ThumbnailProps> = ({
>
<figure>
<div>
{thumbnail?.id && (
<img
src={thumbnail.id}
alt={canvas?.label ? (getLabel(canvas.label) as string) : ""}
/>
)}
{thumbnail?.id && <img src={thumbnail.id} alt={label as string} />}

<Type>
<Tag isIcon data-testid="thumbnail-tag">
Expand All @@ -84,11 +83,13 @@ const Thumbnail: React.FC<ThumbnailProps> = ({
</Tag>
</Type>
</div>
{canvas?.label && (
<figcaption data-testid="fig-caption">
<figcaption data-testid="fig-caption">
{canvas.label ? (
<Label label={canvas.label} />
</figcaption>
)}
) : (
(canvasIndex + 1).toString()
)}
</figcaption>
</figure>
</Item>
);
Expand Down
Loading