Skip to content

Render html #285

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import {withTheme, withLang} from './decorators';

export const decorators = [withTheme, withLang];

export const parameters = {
options: {
storySort: {
order: ['Docs', 'Array', 'Boolean', 'Other', 'Number', 'Object', 'String', '*'],
},
},
};

export const globalTypes = {
theme: {
defaultValue: 'light',
Expand Down
2 changes: 2 additions & 0 deletions docs/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ This component serves as the primary entry point for drawing dynamic forms.
| destroyOnUnregister | `boolean` | | If true, the value of a field will be destroyed when that field is unregistered. Defaults to true |
| generateRandomValue | `function` | | Function that is necessary to generate a random value |
| storeSubscriber | `(storeValue: FieldValue) => void` | | Subscriber function will be called when internal store of dynamic field is changed |
| renderHtml | `function` | | Function for custom rendering of HTML and markdown content in descriptions |

### Controller

Expand Down Expand Up @@ -48,6 +49,7 @@ This component serves as the primary entry point for creating an overview of for
| Link | `React.ComponentType<{value: FormValue; link: Spec['viewSpec']['link'];}>` | | [Component](./spec.md#link) for converting values to links |
| Monaco | `React.ComponentType<MonacoEditorProps>` | | [MonacoEditor](https://github.com/react-monaco-editor/react-monaco-editor) component for Monaco [Input](./config.md#inputs) |
| showLayoutDescription | boolean | | enable to show viewSpec.layoutDescription hint |
| renderHtml | `function` | | Function for custom rendering of HTML and markdown content in descriptions |

### ViewController

Expand Down
52 changes: 52 additions & 0 deletions docs/renderHtml.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# renderHtml

## Description

`renderHtml` is an optional property intended for custom rendering of HTML and markdown content in descriptions.
This property is useful when you need to render formatted text (markdown or HTML) keeping it consistent with your project's design and styling.

Type of the property:

```ts
renderHtml?: (text: string) => React.ReactNode;
```

---

## How to use renderHtml?

To use this prop, pass your custom render function to the [DynamicField](./lib.md#dynamicfield) or [DynamicView](./lib.md#dynamicview) components.

Example for [DynamicField](./lib.md#dynamicfield):

```tsx
// Use any third-party library or your custom implementation for HTML rendering
const renderHtml = (text: string) => {
return <div dangerouslySetInnerHTML={{__html: text}} />;
};

export const MyForm = () => {
return <DynamicField name="input" spec={spec} config={dynamicConfig} renderHtml={renderHtml} />;
};
```

Example for [DynamicView](./lib.md#dynamicview):

```tsx
const renderHtml = (text: string) => {
return <div dangerouslySetInnerHTML={{__html: text}} />;
};

export const MyView = () => {
return (
<DynamicView value={value} spec={spec} config={dynamicViewConfig} renderHtml={renderHtml} />
);
};
```

---

### Tips for using renderHtml:

- You can use any third-party libraries (for example, `@gravity-ui/markdown-editor`, `@diplodoc/transform`, etc.) to ensure safer and more effective rendering of markdown or HTML content.
- If your custom components (for instance, custom layout or custom input components) also require markdown or HTML rendering capabilities, be sure to leverage the built-in hook `useRenderHtml()` for convenient access to the rendering function within child components.
Loading