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 3 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"vitest": "^1.5.0"
},
"dependencies": {
"interweave": "^13.1.0",
"lodash.isempty": "^4.4.0",
"lodash.merge": "^4.6.2",
"lodash.range": "^3.2.0",
Expand Down
16 changes: 12 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/controls/TextControl.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,33 @@ test("renders error messages from rule validation", async () => {

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

test("renders tooltips", async () => {
const uischema = {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/name",
label: "Name",
options: {
tooltip:
"Choose <a href='https://wheelofnames.com/' target='_blank' rel='noopener noreferrer'>a random name</a>.",
elenajdanova marked this conversation as resolved.
Show resolved Hide resolved
},
},
],
}
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 })
await userEvent.hover(svgEl)

await screen.findByText("a random name", { exact: false })
})
36 changes: 30 additions & 6 deletions src/controls/TextControl.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import type { ChangeEvent } from "react"
import { useCallback, useEffect } from "react"
import React, { 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,
} from "@jsonforms/core"
import { Interweave } from "interweave"
import ReactDOMServer from "react-dom/server"

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 +47,10 @@
(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 tooltipProps = getTooltipProps(mergedTooltip)

const placeholderText = options.placeholderText
const form = Form.useFormInstance()
const rules: Rule[] = [
Expand All @@ -64,10 +70,13 @@
label={label}
id={id}
name={path}
rules={rules}
validateTrigger={["onBlur"]}
tooltip={tooltip}
{...formItemProps}
rules={rules}
tooltip={{
title: <Interweave content={tooltipProps.title} />,
...tooltipProps.rest,
}}
{...formItemPropsWOTooltip}
elenajdanova marked this conversation as resolved.
Show resolved Hide resolved
>
<TextControlInput
aria-label={ariaLabel}
Expand Down Expand Up @@ -162,4 +171,19 @@
return value.toString()
}

const getTooltipProps = (tooltip: string | FormItemProps["tooltip"]) => {
if (!tooltip) {
return { title: "", rest: null }
} else if (typeof tooltip === "string") {
return { title: tooltip, rest: null }
} else if (React.isValidElement(tooltip)) {
return { title: ReactDOMServer.renderToString(tooltip), rest: null }

Check warning on line 180 in src/controls/TextControl.tsx

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L180

Added line #L180 was not covered by tests
} else if (typeof tooltip === "object" && "title" in tooltip) {
const { title, ...rest } = tooltip
return { title: String(title), rest }

Check warning on line 183 in src/controls/TextControl.tsx

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L182-L183

Added lines #L182 - L183 were not covered by tests
} else {
return { title: String(tooltip), rest: null }

Check warning on line 185 in src/controls/TextControl.tsx

View check run for this annotation

Codecov / codecov/patch

src/controls/TextControl.tsx#L185

Added line #L185 was not covered by tests
}
}

export const TextRenderer = withJsonFormsControlProps(TextControl)
31 changes: 29 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,34 @@ export const Tooltip: Story = {
type: "Control",
scope: "#/properties/name",
label: "Name",
formItemProps: { tooltip: "It's what you call yourself" },
formItemProps: {
tooltip: {
title: "It's what you call <b>yourself</b>",
placement: "right",
},
},
},
],
} satisfies UISchema<typeof schema>,
},
}

export const OptionsTooltip: Story = {
parameters: { controls: { expanded: true } },
tags: ["autodocs"],
args: {
jsonSchema: schema,
uiSchema: {
type: "VerticalLayout",
elements: [
{
type: "Control",
scope: "#/properties/name",
label: "Name",
options: {
tooltip:
"Choose <a href='https://wheelofnames.com/' target='_blank' rel='noopener noreferrer'>a random name</a>.",
},
},
],
} satisfies UISchema<typeof schema>,
Expand Down
Loading