diff --git a/src/components/OutputField/OutputField.scss b/src/components/OutputField/OutputField.scss index f677cf197..3ef48f5cb 100644 --- a/src/components/OutputField/OutputField.scss +++ b/src/components/OutputField/OutputField.scss @@ -8,4 +8,13 @@ .p-form__control { margin-top: 0.25rem; } + + // "output" is inline by default, text-overflow needs a block box to clip against. + .output-field-truncate { + display: block; + max-width: 100%; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + } } diff --git a/src/components/OutputField/OutputField.stories.tsx b/src/components/OutputField/OutputField.stories.tsx index 7ae349fd3..d20ff02fb 100644 --- a/src/components/OutputField/OutputField.stories.tsx +++ b/src/components/OutputField/OutputField.stories.tsx @@ -31,6 +31,11 @@ const meta: Meta = { type: "boolean", }, }, + truncate: { + control: { + type: "boolean", + }, + }, }, args: { @@ -63,3 +68,12 @@ export const Required: Story = { required: true, }, }; + +export const Truncate: Story = { + name: "Truncate", + args: { + value: + "a-very-long-value-that-should-be-truncated-with-an-ellipsis-when-it-overflows", + truncate: true, + }, +}; diff --git a/src/components/OutputField/OutputField.test.tsx b/src/components/OutputField/OutputField.test.tsx index 988432834..3147f94b0 100644 --- a/src/components/OutputField/OutputField.test.tsx +++ b/src/components/OutputField/OutputField.test.tsx @@ -15,3 +15,17 @@ it("renders an OutputField", () => { ); expect(screen.getByRole("status", { name: "Label" })).toBeInTheDocument(); }); + +it("truncates the value and shows it in a title attribute", () => { + render( + , + ); + const output = screen.getByRole("status", { name: "Label" }); + expect(output).toHaveClass("output-field-truncate"); + expect(output).toHaveAttribute("title", "Output value"); +}); diff --git a/src/components/OutputField/OutputField.tsx b/src/components/OutputField/OutputField.tsx index 4e1d75b0a..56b3821ca 100644 --- a/src/components/OutputField/OutputField.tsx +++ b/src/components/OutputField/OutputField.tsx @@ -1,6 +1,7 @@ import React, { FC, ReactNode } from "react"; import Field from "../Field"; import "./OutputField.scss"; +import classNames from "classnames"; export type Props = { /** @@ -23,6 +24,10 @@ export type Props = { * Whether the output field is required. */ required?: boolean; + /** + * Whether the value should be truncated with an ellipsis when it overflows, showing the full value in a title tooltip. + */ + truncate?: boolean; }; /** @@ -35,6 +40,7 @@ export const OutputField: FC = ({ value, help, required, + truncate, }) => { return ( = ({ className="output-field" required={required} > - + {value}