Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Deprecate options.tooltip #82

Merged
merged 8 commits into from
Jun 26, 2024
Merged
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
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}
elenajdanova marked this conversation as resolved.
Show resolved Hide resolved
>
<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
Loading