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 5 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
46 changes: 46 additions & 0 deletions src/controls/TextControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,49 @@

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

test("renders tooltips", async () => {
const uischema = {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/name",
label: "Name",
options: {
formItemProps: {
tooltip: {
title: (
<p>
Choose{" "}
<a
href="https://wheelofnames.com/"
target="_blank"
rel="noopener noreferrer"
>
a random name
</a>
.
</p>
),
placement: "right",
},
},
},
},
],
}
const schema = {
type: "object",
properties: { name: { type: "string", title: "Name" } },
} satisfies JSONSchema

render({ schema, uischema })
await screen.findByPlaceholderText(schema.properties.name.title, {
exact: false,
})
const svgEl = screen.getByRole("img", { name: /question-circle/i })

Check failure on line 174 in src/controls/TextControl.test.tsx

View workflow job for this annotation

GitHub Actions / cache-and-install-deps-and-run-tests

src/controls/TextControl.test.tsx > renders tooltips

TestingLibraryElementError: Unable to find an accessible element with the role "img" and name `/question-circle/i` Here are the accessible roles: textbox: Name "Name": <input aria-label="Name" autocomplete="off" class="ant-input css-dev-only-do-not-override-1rqnfsa ant-input-outlined" id="name" placeholder="name" type="text" value="" /> -------------------------------------------------- button: Name "Submit": <button class="ant-btn css-dev-only-do-not-override-1rqnfsa ant-btn-primary" type="button" /> -------------------------------------------------- Ignored nodes: comments, script, style <body> <div> <form class="ant-form ant-form-horizontal css-dev-only-do-not-override-1rqnfsa" > <div class="ant-form ant-form-horizontal css-dev-only-do-not-override-1rqnfsa" > <div /> <div class="ant-form-item css-dev-only-do-not-override-1rqnfsa" > <div class="ant-row ant-form-item-row css-dev-only-do-not-override-1rqnfsa" > <div class="ant-col ant-form-item-label css-dev-only-do-not-override-1rqnfsa" > <label class="" for="name" title="Name" > Name </label> </div> <div class="ant-col ant-form-item-control css-dev-only-do-not-override-1rqnfsa" > <div class="ant-form-item-control-input" > <div class="ant-form-item-control-input-content" > <input aria-label="Name" autocomplete="off" class="ant-input css-dev-only-do-not-override-1rqnfsa ant-input-outlined" id="name" placeholder="name" type="text" value="" /> </div> </div> </div> </div> </div> </div> <div class="ant-form-item css-dev-only-do-not-override-1rqnfsa" > <div class="ant-row ant-form-item-row css-dev-only-do-not-override-1rqnfsa" > <div class="ant-col ant-form-item-control css-dev-only-do-not-override-1rqnfsa" > <div class="ant-form-item-control-input" > <div class="ant-form-item-control-input-content" > <button class="ant-btn css-dev-only-do-not-override-1rqnfsa ant-btn-primary" type="button" > <span> Submit </span> </button> </div> </div> </div> </div> </div> </form> </div> </body> ❯ Object.getElementError node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/config.js:37:19 ❯ node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:76:38 ❯ node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:52:17 ❯ node_modules/.pnpm/@testing-library[email protected]/node_modules/@testing-library/dom/dist/query-helpers.js:95:19 ❯ src/controls/TextControl.test.tsx:174:24
await userEvent.hover(svgEl)

await screen.findByText("a random name", { exact: false })
})
13 changes: 8 additions & 5 deletions src/controls/TextControl.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ChangeEvent } from "react"
import { useCallback, useEffect } from "react"
import { Input, Form, InputProps } from "antd"
import type { Rule } from "antd/es/form"
import type { FormItemProps, Rule } from "antd/es/form"
import type {
ControlElement,
ControlProps as JSFControlProps,
Expand All @@ -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,9 @@ export function TextControl({
(uischema.options as TextControlOptions) ?? {}
const formItemProps =
"formItemProps" in uischema ? uischema.formItemProps : {}
const tooltip = options.tooltip ? options.tooltip : formItemProps?.tooltip
const { tooltip, ...formItemPropsWOTooltip } = formItemProps as FormItemProps
elenajdanova marked this conversation as resolved.
Show resolved Hide resolved
const mergedTooltip = options.tooltip ? options.tooltip : tooltip ?? ""
elenajdanova marked this conversation as resolved.
Show resolved Hide resolved

const placeholderText = options.placeholderText
const form = Form.useFormInstance()
const rules: Rule[] = [
Expand All @@ -64,10 +67,10 @@ export function TextControl({
label={label}
id={id}
name={path}
rules={rules}
validateTrigger={["onBlur"]}
tooltip={tooltip}
{...formItemProps}
rules={rules}
tooltip={mergedTooltip}
{...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