Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Use Markdown for Measure/Segment description fields (#80883)

Kevin Kelleheracaaa731068d
diff --git a/e2e/support/helpers/e2e-datamodel-helpers.ts b/e2e/support/helpers/e2e-datamodel-helpers.ts
index 46664525fc6..909b3e8d3fe 100644
--- a/e2e/support/helpers/e2e-datamodel-helpers.ts
+++ b/e2e/support/helpers/e2e-datamodel-helpers.ts
@@ -589,7 +589,8 @@ function getSegmentEditorNameInput() {
}
function getSegmentEditorDescriptionInput() {
- return getSegmentEditor().findByLabelText("Give it a description");
+ getSegmentEditor().findByLabelText("Give it a description").click();
+ return getSegmentEditor().findByPlaceholderText("Only if it really needs it");
}
function getSegmentEditorFilterPlaceholder() {
@@ -681,7 +682,8 @@ function getMeasureEditorNameInput() {
}
function getMeasureEditorDescriptionInput() {
- return getMeasureEditor().findByLabelText("Give it a description");
+ getMeasureEditor().findByLabelText("Give it a description").click();
+ return getMeasureEditor().findByPlaceholderText("Only if it really needs it");
}
function getMeasureEditorAggregationPlaceholder() {
diff --git a/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.module.css b/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.module.css
index 250a3874179..d419604938e 100644
--- a/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.module.css
+++ b/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.module.css
@@ -2,6 +2,15 @@
overflow: auto;
}
-.descriptionLabel {
- margin-bottom: 0.75rem;
+/* Double class so this beats EditableText's Emotion hover/focus border */
+.descriptionInput {
+ && {
+ border-color: var(--mb-color-border-neutral);
+ cursor: text;
+
+ &:hover,
+ &:focus-within {
+ border-color: var(--mb-color-core-brand);
+ }
+ }
}
diff --git a/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.tsx b/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.tsx
index 78ba1bc36ef..c1cab2ec9e4 100644
--- a/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.tsx
+++ b/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.tsx
@@ -1,7 +1,9 @@
import { t } from "ttag";
+import { EditableText } from "metabase/common/components/EditableText";
+import { Markdown } from "metabase/common/components/Markdown";
import { MeasureAggregationPicker } from "metabase/querying/measures";
-import { Card, Stack, Text, TextInput } from "metabase/ui";
+import { Card, Stack, Text } from "metabase/ui";
import type * as Lib from "metabase-lib";
import S from "./MeasureEditor.module.css";
@@ -32,21 +34,25 @@ export function MeasureEditor({
/>
)}
{!readOnly && (
- <TextInput
- label={t`Give it a description`}
- placeholder={t`Only if it really needs it`}
- value={description}
- onChange={(e) => onDescriptionChange(e.target.value)}
- maw={400}
- classNames={{
- label: S.descriptionLabel,
- }}
- />
+ <Stack gap="sm">
+ <Text fw="bold">{t`Give it a description`}</Text>
+ <EditableText
+ placeholder={t`Only if it really needs it`}
+ initialValue={description}
+ onContentChange={onDescriptionChange}
+ maw={400}
+ className={S.descriptionInput}
+ aria-label={t`Give it a description`}
+ isMultiline
+ isMarkdown
+ isOptional
+ />
+ </Stack>
)}
{readOnly && description && (
<Stack gap="sm">
<Text fw="bold">{t`Description`}</Text>
- <Text c="text-secondary">{description}</Text>
+ <Markdown c="text-secondary">{description}</Markdown>
</Stack>
)}
</Stack>
diff --git a/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.unit.spec.tsx b/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.unit.spec.tsx
index 3542806fd8b..faa8863698f 100644
--- a/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.unit.spec.tsx
+++ b/frontend/src/metabase/data-studio/measures/components/MeasureEditor/MeasureEditor.unit.spec.tsx
@@ -4,6 +4,16 @@ import { DEFAULT_TEST_QUERY, SAMPLE_PROVIDER } from "metabase-lib/test-helpers";
import { MeasureEditor } from "./MeasureEditor";
+const MARKDOWN_DESCRIPTION = "A **bold** [link](https://metabase.com)";
+
+function expectFormattedMarkdown() {
+ expect(screen.getByText("bold").tagName).toBe("STRONG");
+ expect(screen.getByRole("link", { name: "link" })).toHaveAttribute(
+ "href",
+ "https://metabase.com",
+ );
+}
+
describe("MeasureEditor", () => {
const query = Lib.createTestQuery(SAMPLE_PROVIDER, DEFAULT_TEST_QUERY);
const description = "A sample description";
@@ -28,7 +38,7 @@ describe("MeasureEditor", () => {
).toBeInTheDocument();
});
- it("shows description as plain text when read-only", () => {
+ it("shows description when read-only", () => {
renderWithProviders(
<MeasureEditor
query={query}
@@ -48,4 +58,31 @@ describe("MeasureEditor", () => {
screen.queryByText("Pick an aggregation function"),
).not.toBeInTheDocument();
});
+
+ it("renders markdown formatting when editable", () => {
+ renderWithProviders(
+ <MeasureEditor
+ query={query}
+ description={MARKDOWN_DESCRIPTION}
+ onQueryChange={onQueryChange}
+ onDescriptionChange={onDescriptionChange}
+ />,
+ );
+
+ expectFormattedMarkdown();
+ });
+
+ it("renders markdown formatting when read-only", () => {
+ renderWithProviders(
+ <MeasureEditor
+ query={query}
+ description={MARKDOWN_DESCRIPTION}
+ onQueryChange={onQueryChange}
+ onDescriptionChange={onDescriptionChange}
+ readOnly
+ />,
+ );
+
+ expectFormattedMarkdown();
+ });
});
diff --git a/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.module.css b/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.module.css
index 250a3874179..d419604938e 100644
--- a/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.module.css
+++ b/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.module.css
@@ -2,6 +2,15 @@
overflow: auto;
}
-.descriptionLabel {
- margin-bottom: 0.75rem;
+/* Double class so this beats EditableText's Emotion hover/focus border */
+.descriptionInput {
+ && {
+ border-color: var(--mb-color-border-neutral);
+ cursor: text;
+
+ &:hover,
+ &:focus-within {
+ border-color: var(--mb-color-core-brand);
+ }
+ }
}
diff --git a/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.tsx b/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.tsx
index 5f363437dbd..d7ca05aec7a 100644
--- a/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.tsx
+++ b/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.tsx
@@ -1,7 +1,9 @@
import { t } from "ttag";
+import { EditableText } from "metabase/common/components/EditableText";
+import { Markdown } from "metabase/common/components/Markdown";
import { SegmentFilterEditor } from "metabase/querying/segments";
-import { Card, Stack, Text, TextInput } from "metabase/ui";
+import { Card, Stack, Text } from "metabase/ui";
import type * as Lib from "metabase-lib";
import S from "./SegmentEditor.module.css";
@@ -32,21 +34,25 @@ export function SegmentEditor({
/>
)}
{!readOnly && (
- <TextInput
- label={t`Give it a description`}
- placeholder={t`Only if it really needs it`}
- value={description}
- onChange={(e) => onDescriptionChange(e.target.value)}
- maw={400}
- classNames={{
- label: S.descriptionLabel,
- }}
- />
+ <Stack gap="sm">
+ <Text fw="bold">{t`Give it a description`}</Text>
+ <EditableText
+ placeholder={t`Only if it really needs it`}
+ initialValue={description}
+ onContentChange={onDescriptionChange}
+ maw={400}
+ className={S.descriptionInput}
+ aria-label={t`Give it a description`}
+ isMultiline
+ isMarkdown
+ isOptional
+ />
+ </Stack>
)}
{readOnly && description && (
<Stack gap="sm">
<Text fw="bold">{t`Description`}</Text>
- <Text c="text-secondary">{description}</Text>
+ <Markdown c="text-secondary">{description}</Markdown>
</Stack>
)}
</Stack>
diff --git a/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.unit.spec.tsx b/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.unit.spec.tsx
new file mode 100644
index 00000000000..9f6240d98f0
--- /dev/null
+++ b/frontend/src/metabase/data-studio/segments/components/SegmentEditor/SegmentEditor.unit.spec.tsx
@@ -0,0 +1,88 @@
+import { renderWithProviders, screen } from "__support__/ui";
+import * as Lib from "metabase-lib";
+import { DEFAULT_TEST_QUERY, SAMPLE_PROVIDER } from "metabase-lib/test-helpers";
+
+import { SegmentEditor } from "./SegmentEditor";
+
+const MARKDOWN_DESCRIPTION = "A **bold** [link](https://metabase.com)";
+
+function expectFormattedMarkdown() {
+ expect(screen.getByText("bold").tagName).toBe("STRONG");
+ expect(screen.getByRole("link", { name: "link" })).toHaveAttribute(
+ "href",
+ "https://metabase.com",
+ );
+}
+
+describe("SegmentEditor", () => {
+ const query = Lib.createTestQuery(SAMPLE_PROVIDER, DEFAULT_TEST_QUERY);
+ const description = "A sample description";
+ const onQueryChange = jest.fn();
+ const onDescriptionChange = jest.fn();
+
+ it("renders editable inputs when not read-only", () => {
+ renderWithProviders(
+ <SegmentEditor
+ query={query}
+ description={description}
+ onQueryChange={onQueryChange}
+ onDescriptionChange={onDescriptionChange}
+ />,
+ );
+
+ expect(screen.getByLabelText("Give it a description")).not.toHaveAttribute(
+ "readonly",
+ );
+ expect(
+ screen.getByText("Add filters to narrow your answer"),
+ ).toBeInTheDocument();
+ });
+
+ it("shows description when read-only", () => {
+ renderWithProviders(
+ <SegmentEditor
+ query={query}
+ description={description}
+ onQueryChange={onQueryChange}
+ onDescriptionChange={onDescriptionChange}
+ readOnly
+ />,
+ );
+
+ expect(
+ screen.queryByLabelText("Give it a description"),
+ ).not.toBeInTheDocument();
+ expect(screen.getByText("Description")).toBeInTheDocument();
+ expect(screen.getByText(description)).toBeInTheDocument();
+ expect(
+ screen.queryByText("Add filters to narrow your answer"),
+ ).not.toBeInTheDocument();
+ });
+
+ it("renders markdown formatting when editable", () => {
+ renderWithProviders(
+ <SegmentEditor
+ query={query}
+ description={MARKDOWN_DESCRIPTION}
+ onQueryChange={onQueryChange}
+ onDescriptionChange={onDescriptionChange}
+ />,
+ );
+
+ expectFormattedMarkdown();
+ });
+
+ it("renders markdown formatting when read-only", () => {
+ renderWithProviders(
+ <SegmentEditor
+ query={query}
+ description={MARKDOWN_DESCRIPTION}
+ onQueryChange={onQueryChange}
+ onDescriptionChange={onDescriptionChange}
+ readOnly
+ />,
+ );
+
+ expectFormattedMarkdown();
+ });
+});
diff --git a/frontend/src/metabase/data-studio/segments/pages/NewSegmentPage/NewSegmentPage.unit.spec.tsx b/frontend/src/metabase/data-studio/segments/pages/NewSegmentPage/NewSegmentPage.unit.spec.tsx
index c4ceb82884d..4162da07ec2 100644
--- a/frontend/src/metabase/data-studio/segments/pages/NewSegmentPage/NewSegmentPage.unit.spec.tsx
+++ b/frontend/src/metabase/data-studio/segments/pages/NewSegmentPage/NewSegmentPage.unit.spec.tsx
@@ -110,7 +110,9 @@ describe("NewSegmentPage", () => {
expect(screen.getByTestId("new-segment-page")).toBeInTheDocument();
expect(screen.getByPlaceholderText("New segment")).toHaveValue("");
- expect(screen.getByLabelText("Give it a description")).toHaveValue("");
+ expect(
+ screen.getByPlaceholderText("Only if it really needs it"),
+ ).toHaveValue("");
expect(
screen.queryByRole("button", { name: "Save" }),
).not.toBeInTheDocument();
@@ -139,7 +141,7 @@ describe("NewSegmentPage", () => {
setup();
await userEvent.type(
- screen.getByLabelText("Give it a description"),
+ screen.getByPlaceholderText("Only if it really needs it"),
"Test description",
);
diff --git a/frontend/src/metabase/data-studio/segments/pages/SegmentDetailPage/tests/common.unit.spec.tsx b/frontend/src/metabase/data-studio/segments/pages/SegmentDetailPage/tests/common.unit.spec.tsx
index fa98de5c8cd..b50ebc8fa0f 100644
--- a/frontend/src/metabase/data-studio/segments/pages/SegmentDetailPage/tests/common.unit.spec.tsx
+++ b/frontend/src/metabase/data-studio/segments/pages/SegmentDetailPage/tests/common.unit.spec.tsx
@@ -6,12 +6,17 @@ import { createMockSegment } from "metabase-types/api/mocks";
import { TEST_SEGMENT, setup } from "./setup";
+async function getDescriptionInput() {
+ await userEvent.click(screen.getByLabelText("Give it a description"));
+ return screen.getByPlaceholderText("Only if it really needs it");
+}
+
describe("SegmentDetailPage", () => {
it("renders page with segment data, tabs, and actions menu", async () => {
await setup();
expect(screen.getByDisplayValue("High Value Orders")).toBeInTheDocument();
- expect(screen.getByLabelText("Give it a description")).toHaveValue(
+ expect(screen.getByLabelText("Give it a description")).toHaveTextContent(
"Orders with total > 100",
);
expect(screen.getByText("Definition")).toBeInTheDocument();
@@ -33,7 +38,7 @@ describe("SegmentDetailPage", () => {
it("shows Save/Cancel buttons when description is modified", async () => {
await setup();
- const descriptionInput = screen.getByLabelText("Give it a description");
+ const descriptionInput = await getDescriptionInput();
await userEvent.clear(descriptionInput);
await userEvent.type(descriptionInput, "New description");
@@ -44,14 +49,16 @@ describe("SegmentDetailPage", () => {
it("resets form when Cancel is clicked after modifying description", async () => {
await setup();
- const descriptionInput = screen.getByLabelText("Give it a description");
+ const descriptionInput = await getDescriptionInput();
await userEvent.clear(descriptionInput);
await userEvent.type(descriptionInput, "Modified description");
expect(descriptionInput).toHaveValue("Modified description");
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
- expect(descriptionInput).toHaveValue("Orders with total > 100");
+ expect(screen.getByLabelText("Give it a description")).toHaveTextContent(
+ "Orders with total > 100",
+ );
expect(
screen.queryByRole("button", { name: "Save" }),
).not.toBeInTheDocument();
@@ -67,7 +74,7 @@ describe("SegmentDetailPage", () => {
await setup();
- const descriptionInput = screen.getByLabelText("Give it a description");
+ const descriptionInput = await getDescriptionInput();
await userEvent.clear(descriptionInput);
await userEvent.type(descriptionInput, "Updated description");
@@ -83,7 +90,9 @@ describe("SegmentDetailPage", () => {
expect(calls.length).toBeGreaterThan(0);
});
- expect(descriptionInput).toHaveValue("Updated description");
+ expect(screen.getByLabelText("Give it a description")).toHaveTextContent(
+ "Updated description",
+ );
await waitFor(() => {
expect(screen.getByText(/Total is greater than/)).toBeInTheDocument();
});
diff --git a/frontend/src/metabase/data-studio/segments/pages/SegmentDetailPage/tests/enterprise.unit.spec.tsx b/frontend/src/metabase/data-studio/segments/pages/SegmentDetailPage/tests/enterprise.unit.spec.tsx
index 72f77011e71..885bdaa64c7 100644
--- a/frontend/src/metabase/data-studio/segments/pages/SegmentDetailPage/tests/enterprise.unit.spec.tsx
+++ b/frontend/src/metabase/data-studio/segments/pages/SegmentDetailPage/tests/enterprise.unit.spec.tsx
@@ -49,7 +49,10 @@ describe("SegmentDetailPage", () => {
});
expect(screen.getByDisplayValue("High Value Orders")).toBeEnabled();
- expect(screen.getByLabelText("Give it a description")).toBeEnabled();
+ await userEvent.click(screen.getByLabelText("Give it a description"));
+ expect(
+ screen.getByPlaceholderText("Only if it really needs it"),
+ ).toBeEnabled();
await userEvent.click(screen.getByLabelText("Segment actions"));