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

Document how to wrap a field React component into Flow component #3600

Merged
merged 17 commits into from
Aug 22, 2024
Merged
Changes from 14 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
81 changes: 81 additions & 0 deletions articles/flow/integrations/react.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,84 @@

customElements.define('react-router-layout', ReactRouterLayoutElement);
----

[[wrap-react-component]]
== Wrapping a React component into Flow component

Check warning on line 213 in articles/flow/integrations/react.adoc

View workflow job for this annotation

GitHub Actions / lint

[vale] reported by reviewdog 🐶 [Vaadin.HeadingCase] 'Wrapping a React component into Flow component' should be in title case. Raw Output: {"message": "[Vaadin.HeadingCase] 'Wrapping a React component into Flow component' should be in title case.", "location": {"path": "articles/flow/integrations/react.adoc", "range": {"start": {"line": 213, "column": 4}}}, "severity": "WARNING"}
AlainaFaisal marked this conversation as resolved.
Show resolved Hide resolved

When integrating React components into Vaadin applications, one common requirement is to enable these components to participate in Vaadin's data binding and form handling.
This can be accomplished by wrapping the React component in a Vaadin component that extends `AbstractSinglePropertyField`.
This allows the React component to be used like any other field in Vaadin, making it compatible with the `Binder` API.

This integration process involves two main parts:
- creating a Java class for the server-side adapter component
- developing a client-side adapter using TypeScript and React.
Next this process is demonstrated using a simple React text input component as an example.

=== Example Implementation:

=== Create the Client-Side React Component
First, define your React component. For this example, assume a simple text input component.
[source,jsx]
----
// File: frontend/react-text-input.tsx

import React, { useState } from 'react';
import {ReactAdapterElement, type RenderHooks} from "Frontend/generated/flow/ReactAdapter";

class ReactTextInputElement extends ReactAdapterElement {
constructor() {
super();
}
AlainaFaisal marked this conversation as resolved.
Show resolved Hide resolved

protected override render(hooks: RenderHooks): React.ReactElement | null {
const [value, setValue] = hooks.useState<string>("value");

const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setValue(event.target.value);
this.dispatchEvent(new CustomEvent('value-changed', { detail: { value: event.target.value } }));
};

return (
<div>
<input type="text" value={value} onChange={handleChange} />
<span>{value?.length} characters</span>
</div>
);
}
}
AlainaFaisal marked this conversation as resolved.
Show resolved Hide resolved

customElements.define('react-text-input', ReactTextInputElement);
----

=== Server-Side Vaadin Component Wrapping the React Component
Next, create a Vaadin component that wraps the React component and extends `AbstractSinglePropertyField`.

[source,java]
----
// Package: com.example.application.ui

import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.AbstractSinglePropertyField;

@Tag("react-text-input")
@JsModule("./react-text-input.tsx")
public class ReactTextField extends AbstractSinglePropertyField<ReactTextField, String> {
public ReactTextField() {
super("value", "", false);
}
}
AlainaFaisal marked this conversation as resolved.
Show resolved Hide resolved
----

=== Using the React Component in a Vaadin Form
Now, you can use `ReactTextField` like any other Vaadin component within a form:

[source,java]
----
Binder<Person> binder = new Binder<>(Person.class);
ReactTextField reactTextField = new ReactTextField();

binder.forField(reactTextField).bind(Person::getName, Person::setName);

add(reactTextField);
----
Loading