Using kintone UI Component v1 in React with @lit/react #1608
Pinned
h000780
started this conversation in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Background
createComponent() is the API provided by @lit/react, which allows Web Components to be directly used within React.
Based on this API, KUC Components can also be seamlessly integrated and utilized within React applications.
Here is an unofficial guide on how to create React wrapper of KUC v1.
Unofficial means that we don't test the behavior and support it.
So please develop on your own responsibility.
Please give us feedback if you have any opinions or feedback.
Alternative Integration Approach
This guide presents a method using @lit/react to create React wrappers for KUC components, which is particularly beneficial for TypeScript projects. Before developing this approach, we documented a more direct method using DOM manipulation and refs, which you can find here: Using kintone UI Component v1 in React.
The previous approach may be easier to implement initially but can become challenging when working with complex components or in TypeScript environments.
Overview
This article introduces how to create React wrapper components for KUC Components without modifying the KUC source code itself. This approach allows you to use KUC components in your React customization projects.
While most KUC components can be successfully wrapped for React, there is a limitation to be aware of:
Preparation
Before creating React wrappers for KUC components, follow these general steps:
Simple components
This method can be applied following components:
We will use the createComponent() API to wrap some simple components from @lit/react to achieve this conversion.
Create React wrapper components using the createComponent() API (as shown in the examples below)
In this example, we created a new
ReactText
component based on the KUC Text component. When the value of theReactText
component changes, it triggers anonChange/onFocus/onInput
event, which is consistent with the behavior of the KUC Text component.Import and use these wrapper components in your React application
Components that can contain HTML elements
This method can be applied following components:
However, some KUC Components have custom components set directly through properties. These cannot be used directly after conversion and need further wrapping. The wrapping method will be explained in conjunction with the sample of the button component.
We create a
KucReactButton
class through thecreateComponent()
API.Then, we need to rewrite the ButtonProps type declaration, changing the type of the content property from
content?: string | HTMLElement
tostring | ReactElement
, and adding theonClick
property.Note that the event name here must be consistent with the key of the events parameter in createComponent() :
Finally, we create a new
ReactButton
component that uses theKucReactButton
component internally and handles updates to custom components:In this example, the
ReactButton
component uses theKucReactButton
component internally and updates the custom component when the content property changes.This is achieved by calling the
transformContent
method within auseEffect
hook that executes whenever the content prop changes.In the
transformContent
method:createRoot
method to create a new React root on a newly created div element and renders the content prop within itThen, in the component's return statement, we pass the transformed content (the
transformedContent
state) to theKucReactButton
component as its content property, while passing all other props through the spread operator.Through this approach, we have successfully converted the
KucButton
component into aReactButton
component that can accept React elements as content.In React, we can use it like this:
Components with method
This method can be applied following components:
In KUC, there are 4 components that expose methods for programmatic control: Dialog, Notification, Spinner, and MobileNotification. These components provide methods like open() and close() that need special handling when used in React.
We use Dialog for our example.
Method: open(), close()
The wrapper created by
createComponent()
can not call the methods of the custom element directly.We need to use a ref to call the methods of KUC components.
open
method of the dialog in react directly throughref
andcreateComponent
.Here is the suggested usage:
createComponent()
Demo:

open
method into theisOpen
propertyWe will create a property
isOpen
because the open() and close() methods are used to control whether the dialog is open or not.Here is the sample:
createComponent()
to create a Dialog Component.Then use the Dialog Component to create a React wrapper with the property
isOpen
.State
to open the dialog.Complex components
This method can be applied following components:
Finally, let's look at how to handle those KUC Components that set custom components in callback methods.
In the table, custom components are set in the render property, and they can be wrapped in React in another way. Here is an example of wrapping a table in React.
First, we create a
KucReactTable
component usingcreateComponent
:Then, we rewrite the type declaration, change the HTMLElement in the type declaration to
ReactElement
, and add an event inReactTableProps
->onChange?: (event: any) => void;
Next, we create a new
ReactTable
component that uses theKucReactTable
component internally and handles updates to child components.The process starts by grabbing the columns from the
ReactTable
component's props. It checks each column for a render function. If it finds one, it wraps the function's output in a new DOM container, similar to how we wrapped a Button before. These updated columns are then passed to theKucReactTable
for display.In React, we can use it like this:
These are the detailed steps on how to convert KUC components to React components. Hope this article will be helpful for you!
Beta Was this translation helpful? Give feedback.
All reactions