Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
- `<Markdown />`
- Added `cutOff` property to set maximum number of raw Markdown characters to render
- `<Label />`
- `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:
Expand All @@ -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
- `<ApplicationViewability />`
- `show={"print"}` and `hide={"screen"}` content is still accessible by screen readers
- `<Label />`
- `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
Expand Down
9 changes: 9 additions & 0 deletions src/components/Form/Stories/FieldItem.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ Default.args = {
text: "Input label",
},
};
export const HiddenLabel = Template.bind({});
HiddenLabel.args = {
children: <TextField />,
labelProps: {
text: "Hidden label",
hidden: true,
tooltip: "if the tooltip gets focused the label is visible",
},
};
120 changes: 120 additions & 0 deletions src/components/Label/Label.test.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof Label>) => {
const { container } = render(<Label {...props} />);
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(<Label htmlFor="inputid" className="custom-class" />);
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: <em>text element</em>, info: <em>info element</em> });
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(<Label text="Label text" />);
expect(withoutTooltip.getElementsByClassName(`${eccgui}-label__tooltip`).length).toBe(0);

const { container: withTooltip } = render(<Label text="Label text" tooltip="Label tooltip" />);
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: <span>additional element</span> });
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 `<ApplicationViewability/>`, 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();
});
});
});
23 changes: 16 additions & 7 deletions src/components/Label/Label.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -41,6 +42,10 @@ export interface LabelProps extends React.LabelHTMLAttributes<HTMLLabelElement>
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 = ({
Expand All @@ -55,6 +60,7 @@ export const Label = ({
emphasis = "normal",
additionalElements,
inline,
hidden,
...otherLabelProps
}: LabelProps) => {
let htmlElementstring = isLayoutForElement;
Expand All @@ -67,7 +73,9 @@ export const Label = ({
{tooltip && (
<span className={`${eccgui}-label__tooltip`}>
<Tooltip content={tooltip} disabled={disabled} {...tooltipProps}>
<Icon name="item-info" small />
<span>
<Icon name="item-info" small />
</span>
</Tooltip>
</span>
)}
Expand All @@ -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:
Expand All @@ -93,10 +101,11 @@ export const Label = ({
...otherLabelProps,
},
labelContent,
)
) : (
<></>
);
);
return hidden ? <ApplicationViewability hide={"screen"}>{label}</ApplicationViewability> : label;
} else {
return <></>;
}
};

export default Label;
Loading