Skip to content

Commit

Permalink
fix: Deprecate options.tooltip (#82)
Browse files Browse the repository at this point in the history
* Install Interweave

* Make tooltips render HTML

* Add test

* Feedback: update tooltips type

* fix: Deprecate options.tooltip

* Update test

* Address feedback

* Lint
  • Loading branch information
elenajdanova authored Jun 26, 2024
1 parent 27051e7 commit 0f47e6d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 7 deletions.
64 changes: 62 additions & 2 deletions src/controls/TextControl.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { test, expect } from "vitest"
import { test, expect, describe } from "vitest"
import { screen, waitFor } from "@testing-library/react"
import { userEvent } from "@testing-library/user-event"
import type { JSONSchema } from "json-schema-to-ts"

import { render } from "../common/test-render"
import type { UISchema } from "../ui-schema"
import type { TextControlOptions, UISchema } from "../ui-schema"
import type { JSONFormData } from "../common/schema-derived-types"

const textInputSchema = {
Expand Down Expand Up @@ -130,3 +130,63 @@ test("renders error messages from rule validation", async () => {

await screen.findByText("Only letters are allowed")
})

describe("tooltips", () => {
const getUiSchema = (options?: TextControlOptions) => ({
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/name",
label: "Name",
formItemProps: {
tooltip: {
title: (
<p>
Choose{" "}
<a
href="https://wheelofnames.com/"
target="_blank"
rel="noopener noreferrer"
>
a random name
</a>
.
</p>
),
placement: "right",
},
},
options,
},
],
})

const schema = {
type: "object",
properties: { name: { type: "string", title: "Name" } },
} satisfies JSONSchema

test("renders tooltip", async () => {
render({ schema, uischema: getUiSchema() })
await screen.findByPlaceholderText(schema.properties.name.title, {
exact: false,
})
const svgEl = screen.getByRole("img", { name: /question-circle/i })
await userEvent.hover(svgEl)

await screen.findByText("a random name", { exact: false })
})

test("renders right tooltip in case both are passed", async () => {
const options = { tooltip: "You should see this tooltip" }
render({ schema, uischema: getUiSchema(options) })
await screen.findByPlaceholderText(schema.properties.name.title, {
exact: false,
})
const svgEl = screen.getByRole("img", { name: /question-circle/i })
await userEvent.hover(svgEl)

await screen.findByText("You should see this tooltip")
})
})
10 changes: 7 additions & 3 deletions src/controls/TextControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { ControlUISchema, TextControlOptions } from "../ui-schema"
import { assertNever } from "../common/assert-never"
import { withJsonFormsControlProps } from "@jsonforms/react"
import { TextAreaProps } from "antd/es/input/TextArea"

type ControlProps = Omit<JSFControlProps, "uischema"> & {
data: string
handleChange(path: string, value: string): void
Expand Down Expand Up @@ -44,7 +45,10 @@ export function TextControl({
(uischema.options as TextControlOptions) ?? {}
const formItemProps =
"formItemProps" in uischema ? uischema.formItemProps : {}
const tooltip = options.tooltip ? options.tooltip : formItemProps?.tooltip
const { tooltip: formItemTooltip, ...formItemPropsWOTooltip } =
formItemProps ?? {}
const tooltip = options.tooltip ? options.tooltip : formItemTooltip ?? ""

const placeholderText = options.placeholderText
const form = Form.useFormInstance()
const rules: Rule[] = [
Expand All @@ -64,10 +68,10 @@ export function TextControl({
label={label}
id={id}
name={path}
rules={rules}
validateTrigger={["onBlur"]}
rules={rules}
tooltip={tooltip}
{...formItemProps}
{...formItemPropsWOTooltip}
>
<TextControlInput
aria-label={ariaLabel}
Expand Down
21 changes: 19 additions & 2 deletions src/stories/controls/TextControl.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export const RuleDefinedInUISchema: Story = {
},
}

export const Tooltip: Story = {
export const FormItemTooltip: Story = {
parameters: { controls: { expanded: true } },
tags: ["autodocs"],
args: {
Expand All @@ -137,7 +137,24 @@ export const Tooltip: Story = {
type: "Control",
scope: "#/properties/name",
label: "Name",
formItemProps: { tooltip: "It's what you call yourself" },
formItemProps: {
tooltip: {
title: (
<p>
Choose{" "}
<a
href="https://wheelofnames.com/"
target="_blank"
rel="noopener noreferrer"
>
a random name
</a>
.
</p>
),
placement: "right",
},
},
},
],
} satisfies UISchema<typeof schema>,
Expand Down
3 changes: 3 additions & 0 deletions src/ui-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export type AnyOfControlOptions = OneOfControlOptions
export type TextControlType = "multiline" | "password" | "singleline"

export type TextControlOptions = {
/**
* @deprecated Please use formItemProps.tooltip instead
*/
tooltip?: string
placeholderText?: string
required?: boolean
Expand Down

0 comments on commit 0f47e6d

Please sign in to comment.