diff --git a/CHANGELOG.md b/CHANGELOG.md
index c10851fc3..be85ba82e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- `noDrag` parameter: Add the `nodrag` class to the Switch element. Default: `true`
- ``
- Added `cutOff` property to set maximum number of raw Markdown characters to render
+- ``
+ - `hidden` property: label is not displayed but stays accessible for screen readers and keyboard navigation
- new `utils` methods:
- `truncateMarkdownDisplay`: helper function to iterate over `Markdown` renderings to improve the experienced `cutOff` value
- new icons:
@@ -36,6 +38,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- fix description and story to point out that `PropertyValueList` need always to be used as wrapper
- ``
- `show={"print"}` and `hide={"screen"}` content is still accessible by screen readers
+- ``
+ - `tooltip` content is accessible via keyboard navigation
- BOM issue on compressed stylesheet
- first rule `selector` becomes `BOM:selector` that is valid but will never apply
- we fixed this problem by adding a dummy rule as first rule
diff --git a/src/components/Form/Stories/FieldItem.stories.tsx b/src/components/Form/Stories/FieldItem.stories.tsx
index 5c7dfc1b3..39306eb96 100644
--- a/src/components/Form/Stories/FieldItem.stories.tsx
+++ b/src/components/Form/Stories/FieldItem.stories.tsx
@@ -28,3 +28,12 @@ Default.args = {
text: "Input label",
},
};
+export const HiddenLabel = Template.bind({});
+HiddenLabel.args = {
+ children: ,
+ labelProps: {
+ text: "Hidden label",
+ hidden: true,
+ tooltip: "if the tooltip gets focused the label is visible",
+ },
+};
diff --git a/src/components/Label/Label.test.tsx b/src/components/Label/Label.test.tsx
new file mode 100644
index 000000000..892049b1f
--- /dev/null
+++ b/src/components/Label/Label.test.tsx
@@ -0,0 +1,120 @@
+import React from "react";
+import { render, screen } from "@testing-library/react";
+
+import "@testing-library/jest-dom";
+
+import { CLASSPREFIX as eccgui } from "../../configuration/constants";
+
+import Label from "./Label";
+import { Default as LabelStory } from "./Label.stories";
+
+const renderLabel = (props: React.ComponentProps) => {
+ const { container } = render();
+ return container.firstElementChild as HTMLElement;
+};
+
+describe("Label", () => {
+ describe("markup of the content", () => {
+ it("should render nothing if no content is set at all", () => {
+ const { container } = render();
+ expect(container).toBeEmptyDOMElement();
+ });
+ it("should render the text", () => {
+ renderLabel({ text: "Label text" });
+ expect(screen.getByText("Label text")).toHaveClass(`${eccgui}-label__text`);
+ });
+ it("should render the info", () => {
+ renderLabel({ info: "Label info" });
+ expect(screen.getByText("Label info")).toHaveClass(`${eccgui}-label__info`);
+ });
+ it("should render the children", () => {
+ renderLabel({ children: "Label children" });
+ expect(screen.getByText("Label children")).toHaveClass(`${eccgui}-label__other`);
+ });
+ it("should render elements used as text and info", () => {
+ renderLabel({ text: text element, info: info element });
+ expect(screen.getByText("text element").parentElement).toHaveClass(`${eccgui}-label__text`);
+ expect(screen.getByText("info element").parentElement).toHaveClass(`${eccgui}-label__info`);
+ });
+ it("should render an info icon with a tooltip only if a tooltip is set", () => {
+ const { container: withoutTooltip } = render();
+ expect(withoutTooltip.getElementsByClassName(`${eccgui}-label__tooltip`).length).toBe(0);
+
+ const { container: withTooltip } = render();
+ const tooltip = withTooltip.getElementsByClassName(`${eccgui}-label__tooltip`);
+ expect(tooltip.length).toBe(1);
+ expect(tooltip[0].getElementsByClassName(`${eccgui}-icon`).length).toBe(1);
+ });
+ it("should render additional elements separated by a vertical spacing", () => {
+ const label = renderLabel({ text: "Label text", additionalElements: additional element });
+ expect(screen.getByText("additional element")).toBeInTheDocument();
+ expect(label.getElementsByClassName(`${eccgui}-separation__spacing-vertical`).length).toBe(1);
+ });
+ it("should render all content of the story example", () => {
+ const label = renderLabel(LabelStory.args);
+ [`__text`, `__info`, `__tooltip`].forEach((contentClass) => {
+ expect(label.getElementsByClassName(`${eccgui}-label${contentClass}`).length).toBe(1);
+ });
+ });
+ });
+
+ describe("used HTML element", () => {
+ it("should be a `label` element by default", () => {
+ expect(renderLabel({ text: "Label text" }).tagName).toBe("LABEL");
+ });
+ it("should be the element set by `isLayoutForElement`", () => {
+ expect(renderLabel({ text: "Label text", isLayoutForElement: "div" }).tagName).toBe("DIV");
+ });
+ it("should be a `span` element if it is disabled, because a `label` is always active", () => {
+ expect(renderLabel({ text: "Label text", disabled: true }).tagName).toBe("SPAN");
+ });
+ it("should keep the element set by `isLayoutForElement` if it is disabled", () => {
+ expect(renderLabel({ text: "Label text", isLayoutForElement: "div", disabled: true }).tagName).toBe("DIV");
+ });
+ it("should forward other label properties to the element", () => {
+ const label = renderLabel({ text: "Label text", htmlFor: "inputid", id: "labelid" });
+ expect(label).toHaveAttribute("for", "inputid");
+ expect(label).toHaveAttribute("id", "labelid");
+ });
+ });
+
+ describe("CSS classes of the element", () => {
+ it("should use the `normal` emphasis by default", () => {
+ expect(renderLabel({ text: "Label text" })).toHaveClass(`${eccgui}-label`, `${eccgui}-label--normal`);
+ });
+ it("should use the emphasis modifier", () => {
+ expect(renderLabel({ text: "Label text", emphasis: "strong" })).toHaveClass(`${eccgui}-label--strong`);
+ });
+ it("should set the inline modifier only if `inline` is used", () => {
+ expect(renderLabel({ text: "Label text" })).not.toHaveClass(`${eccgui}-label--inline`);
+ expect(renderLabel({ text: "Label text", inline: true })).toHaveClass(`${eccgui}-label--inline`);
+ });
+ it("should set the disabled modifier only if `disabled` is used", () => {
+ expect(renderLabel({ text: "Label text" })).not.toHaveClass(`${eccgui}-label--disabled`);
+ expect(renderLabel({ text: "Label text", disabled: true })).toHaveClass(`${eccgui}-label--disabled`);
+ });
+ it("should keep the default classes if a custom class is used", () => {
+ expect(renderLabel({ text: "Label text", className: "custom-class" })).toHaveClass(
+ `${eccgui}-label`,
+ `${eccgui}-label--normal`,
+ "custom-class",
+ );
+ });
+ });
+
+ /**
+ * The label is wrapped by ``, it is hidden by CSS rules only,
+ * so that it is still available for screen readers.
+ */
+ describe("hidden label", () => {
+ it("should not be hidden by default", () => {
+ expect(renderLabel({ text: "Label text" })).not.toHaveClass(`${eccgui}-application__hide--screen`);
+ });
+ it("should be hidden on screen but kept in the rendering", () => {
+ const label = renderLabel({ text: "Label text", hidden: true });
+ expect(label.tagName).toBe("LABEL");
+ expect(label).toHaveClass(`${eccgui}-label`, `${eccgui}-application__hide--screen`);
+ expect(screen.getByText("Label text")).toBeInTheDocument();
+ });
+ });
+});
diff --git a/src/components/Label/Label.tsx b/src/components/Label/Label.tsx
index c621570f3..3b99bc61e 100644
--- a/src/components/Label/Label.tsx
+++ b/src/components/Label/Label.tsx
@@ -1,6 +1,7 @@
import React from "react";
import { CLASSPREFIX as eccgui } from "../../configuration/constants";
+import ApplicationViewability from "../Application/ApplicationViewability";
import Icon from "../Icon/Icon";
import Spacing from "../Separation/Spacing";
import Tooltip, { TooltipProps } from "../Tooltip/Tooltip";
@@ -41,6 +42,10 @@ export interface LabelProps extends React.LabelHTMLAttributes
additionalElements?: React.ReactNode | React.ReactNode[];
/** Force label to get displayed as inline block element. */
inline?: boolean;
+ /**
+ * Hide label on screen, but keep it accessible for screen readers.
+ */
+ hidden?: boolean;
}
export const Label = ({
@@ -55,6 +60,7 @@ export const Label = ({
emphasis = "normal",
additionalElements,
inline,
+ hidden,
...otherLabelProps
}: LabelProps) => {
let htmlElementstring = isLayoutForElement;
@@ -67,7 +73,9 @@ export const Label = ({
{tooltip && (
-
+
+
+
)}
@@ -81,8 +89,8 @@ export const Label = ({
>
);
- return !!text || !!info || !!tooltip || !!children || !!additionalElements ? (
- React.createElement(
+ if (!!text || !!info || !!tooltip || !!children || !!additionalElements) {
+ const label = React.createElement(
htmlElementstring,
{
className:
@@ -93,10 +101,11 @@ export const Label = ({
...otherLabelProps,
},
labelContent,
- )
- ) : (
- <>>
- );
+ );
+ return hidden ? {label} : label;
+ } else {
+ return <>>;
+ }
};
export default Label;