Skip to content

Commit 0f47e6d

Browse files
authored
fix: Deprecate options.tooltip (#82)
* Install Interweave * Make tooltips render HTML * Add test * Feedback: update tooltips type * fix: Deprecate options.tooltip * Update test * Address feedback * Lint
1 parent 27051e7 commit 0f47e6d

4 files changed

Lines changed: 91 additions & 7 deletions

File tree

src/controls/TextControl.test.tsx

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { test, expect } from "vitest"
1+
import { test, expect, describe } from "vitest"
22
import { screen, waitFor } from "@testing-library/react"
33
import { userEvent } from "@testing-library/user-event"
44
import type { JSONSchema } from "json-schema-to-ts"
55

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

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

131131
await screen.findByText("Only letters are allowed")
132132
})
133+
134+
describe("tooltips", () => {
135+
const getUiSchema = (options?: TextControlOptions) => ({
136+
type: "VerticalLayout",
137+
elements: [
138+
{
139+
type: "Control",
140+
scope: "#/properties/name",
141+
label: "Name",
142+
formItemProps: {
143+
tooltip: {
144+
title: (
145+
<p>
146+
Choose{" "}
147+
<a
148+
href="https://wheelofnames.com/"
149+
target="_blank"
150+
rel="noopener noreferrer"
151+
>
152+
a random name
153+
</a>
154+
.
155+
</p>
156+
),
157+
placement: "right",
158+
},
159+
},
160+
options,
161+
},
162+
],
163+
})
164+
165+
const schema = {
166+
type: "object",
167+
properties: { name: { type: "string", title: "Name" } },
168+
} satisfies JSONSchema
169+
170+
test("renders tooltip", async () => {
171+
render({ schema, uischema: getUiSchema() })
172+
await screen.findByPlaceholderText(schema.properties.name.title, {
173+
exact: false,
174+
})
175+
const svgEl = screen.getByRole("img", { name: /question-circle/i })
176+
await userEvent.hover(svgEl)
177+
178+
await screen.findByText("a random name", { exact: false })
179+
})
180+
181+
test("renders right tooltip in case both are passed", async () => {
182+
const options = { tooltip: "You should see this tooltip" }
183+
render({ schema, uischema: getUiSchema(options) })
184+
await screen.findByPlaceholderText(schema.properties.name.title, {
185+
exact: false,
186+
})
187+
const svgEl = screen.getByRole("img", { name: /question-circle/i })
188+
await userEvent.hover(svgEl)
189+
190+
await screen.findByText("You should see this tooltip")
191+
})
192+
})

src/controls/TextControl.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { ControlUISchema, TextControlOptions } from "../ui-schema"
1111
import { assertNever } from "../common/assert-never"
1212
import { withJsonFormsControlProps } from "@jsonforms/react"
1313
import { TextAreaProps } from "antd/es/input/TextArea"
14+
1415
type ControlProps = Omit<JSFControlProps, "uischema"> & {
1516
data: string
1617
handleChange(path: string, value: string): void
@@ -44,7 +45,10 @@ export function TextControl({
4445
(uischema.options as TextControlOptions) ?? {}
4546
const formItemProps =
4647
"formItemProps" in uischema ? uischema.formItemProps : {}
47-
const tooltip = options.tooltip ? options.tooltip : formItemProps?.tooltip
48+
const { tooltip: formItemTooltip, ...formItemPropsWOTooltip } =
49+
formItemProps ?? {}
50+
const tooltip = options.tooltip ? options.tooltip : formItemTooltip ?? ""
51+
4852
const placeholderText = options.placeholderText
4953
const form = Form.useFormInstance()
5054
const rules: Rule[] = [
@@ -64,10 +68,10 @@ export function TextControl({
6468
label={label}
6569
id={id}
6670
name={path}
67-
rules={rules}
6871
validateTrigger={["onBlur"]}
72+
rules={rules}
6973
tooltip={tooltip}
70-
{...formItemProps}
74+
{...formItemPropsWOTooltip}
7175
>
7276
<TextControlInput
7377
aria-label={ariaLabel}

src/stories/controls/TextControl.stories.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ export const RuleDefinedInUISchema: Story = {
125125
},
126126
}
127127

128-
export const Tooltip: Story = {
128+
export const FormItemTooltip: Story = {
129129
parameters: { controls: { expanded: true } },
130130
tags: ["autodocs"],
131131
args: {
@@ -137,7 +137,24 @@ export const Tooltip: Story = {
137137
type: "Control",
138138
scope: "#/properties/name",
139139
label: "Name",
140-
formItemProps: { tooltip: "It's what you call yourself" },
140+
formItemProps: {
141+
tooltip: {
142+
title: (
143+
<p>
144+
Choose{" "}
145+
<a
146+
href="https://wheelofnames.com/"
147+
target="_blank"
148+
rel="noopener noreferrer"
149+
>
150+
a random name
151+
</a>
152+
.
153+
</p>
154+
),
155+
placement: "right",
156+
},
157+
},
141158
},
142159
],
143160
} satisfies UISchema<typeof schema>,

src/ui-schema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ export type AnyOfControlOptions = OneOfControlOptions
155155
export type TextControlType = "multiline" | "password" | "singleline"
156156

157157
export type TextControlOptions = {
158+
/**
159+
* @deprecated Please use formItemProps.tooltip instead
160+
*/
158161
tooltip?: string
159162
placeholderText?: string
160163
required?: boolean

0 commit comments

Comments
 (0)