Skip to content
Open
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
9 changes: 9 additions & 0 deletions src/components/OutputField/OutputField.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions src/components/OutputField/OutputField.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const meta: Meta<typeof OutputField> = {
type: "boolean",
},
},
truncate: {
control: {
type: "boolean",
},
},
},

args: {
Expand Down Expand Up @@ -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,
},
};
14 changes: 14 additions & 0 deletions src/components/OutputField/OutputField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<OutputField
label="Label"
id="output-field"
value="Output value"
truncate
/>,
);
const output = screen.getByRole("status", { name: "Label" });
expect(output).toHaveClass("output-field-truncate");
expect(output).toHaveAttribute("title", "Output value");
});
14 changes: 13 additions & 1 deletion src/components/OutputField/OutputField.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { FC, ReactNode } from "react";
import Field from "../Field";
import "./OutputField.scss";
import classNames from "classnames";

export type Props = {
/**
Expand All @@ -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;
};

/**
Expand All @@ -35,6 +40,7 @@ export const OutputField: FC<Props> = ({
value,
help,
required,
truncate,
}) => {
return (
<Field
Expand All @@ -45,7 +51,13 @@ export const OutputField: FC<Props> = ({
className="output-field"
required={required}
>
<output id={id} className="mono-font u-sv2">
<output
id={id}
title={truncate ? value : undefined}
className={classNames("mono-font", "u-sv2", {
"output-field-truncate": truncate,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"output-field-truncate": truncate,
"u-truncate": truncate,

Can we use the existing helper from vanilla?

})}
>
<b>{value}</b>
</output>
</Field>
Expand Down
Loading