Repository navigation
feat(du): support React 18.3 hosts in the DU widgets #223
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
sergiubologa
wants to merge
3
commits into
develop
Choose a base branch
from
feat/react-18-support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| "@uipath/ui-widgets-document-viewer": minor | ||
| "@uipath/ui-widgets-validation-station": minor | ||
| "@uipath/ui-widgets-classification-station": minor | ||
| "@uipath/ui-widgets-datatable": minor | ||
| "@uipath/ui-widgets-conversational-agent-chat": minor | ||
| "@uipath/ui-widgets-decisions-and-reasons": minor | ||
| "@uipath/ui-widgets-external-auth": minor | ||
| "@uipath/ui-widgets-multi-file-upload": minor | ||
| "@uipath/ui-widgets-pdf-viewer": minor | ||
| --- | ||
|
|
||
| Support React 18.3 hosts. The peer range is now `react@^18.3.0 || ^19.0.0`. On React 18, the Document Viewer, Validation Station (and its subcomponents) and Classification Station assign their web component's inputs as properties, as React 19 does, and remove their event listeners (and tear down a `persistent` element) on unmount. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { | ||
| useCallback, | ||
| useLayoutEffect, | ||
| useRef, | ||
| version, | ||
| type ElementType, | ||
| type ReactElement, | ||
| } from "react"; | ||
| import type { WcElement, WcRefCallback } from "./bindWcEvents.js"; | ||
|
|
||
| /** | ||
| * React 19 assigns a custom element's props as element properties; React 18 | ||
| * assigns them as attributes, which turns every object input (`dom`, | ||
| * `options`, the references) into the string `"[object Object]"`. | ||
| */ | ||
| const REACT_SETS_PROPERTIES = Number.parseInt(version, 10) >= 19; | ||
|
|
||
| /** | ||
| * Props that stay on the rendered tag under every React version: `className`, | ||
| * `style`, and dashed names, which are attributes by design (`instance-id`). | ||
| * React 18 maps `style` on custom elements but not `className`, which it | ||
| * would write as a `classname` attribute, so it is passed as `class` there. | ||
| */ | ||
| function isAttributeProp(key: string): boolean { | ||
| return key === "className" || key === "style" || key.includes("-"); | ||
| } | ||
|
|
||
| /** | ||
| * The inputs last assigned to each element, keyed by the element itself, so a | ||
| * ref that detaches and re-attaches to the same element (a new `elementRef`) | ||
| * does not re-assign unchanged inputs. | ||
| */ | ||
| const assignedInputs = new WeakMap<Element, Record<string, unknown>>(); | ||
|
|
||
| export interface WcHostProps { | ||
| tag: string; | ||
| props: Record<string, unknown>; | ||
| elementRef: WcRefCallback; | ||
| } | ||
|
|
||
| /** | ||
| * Renders a custom element so that its inputs arrive as properties on React | ||
| * 18 and 19. | ||
| * | ||
| * On React 19 every prop goes on the tag, and React assigns it as a property | ||
| * before the element connects. On React 18 only the attribute props go on the | ||
| * tag; the rest are assigned as properties in a layout effect, in the order of | ||
| * `props`' keys, after the element connects and before the browser paints. | ||
| * Only values that changed since the last commit are assigned, and a prop the | ||
| * host stops passing goes back to `undefined`, as React 19 does. | ||
| */ | ||
| export function WcHost({ tag, props, elementRef }: WcHostProps): ReactElement { | ||
| const elementState = useRef<WcElement | null>(null); | ||
|
|
||
| const ref = useCallback( | ||
| (el: WcElement | null) => { | ||
| elementState.current = el; | ||
| elementRef(el); | ||
| }, | ||
| [elementRef], | ||
| ); | ||
|
|
||
| useLayoutEffect(() => { | ||
| if (REACT_SETS_PROPERTIES) return; | ||
| const el = elementState.current; | ||
| if (!el) return; | ||
| let assigned = assignedInputs.get(el); | ||
| if (!assigned) { | ||
| assigned = {}; | ||
| assignedInputs.set(el, assigned); | ||
| } | ||
| const target = el as unknown as Record<string, unknown>; | ||
| for (const [key, value] of Object.entries(props)) { | ||
| if (isAttributeProp(key) || assigned[key] === value) continue; | ||
| target[key] = value; | ||
| assigned[key] = value; | ||
| } | ||
| for (const key of Object.keys(assigned)) { | ||
| if (!(key in props)) { | ||
| target[key] = undefined; | ||
| delete assigned[key]; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| // The tag is a custom element name, which JSX cannot type statically. | ||
| const Tag = tag as unknown as ElementType; | ||
| if (REACT_SETS_PROPERTIES) return <Tag {...props} ref={ref} />; | ||
| const attributes = Object.fromEntries( | ||
| Object.entries(props) | ||
| .filter(([key]) => isAttributeProp(key)) | ||
| .map(([key, value]) => [key === "className" ? "class" : key, value]), | ||
| ); | ||
| return <Tag {...attributes} ref={ref} />; | ||
| } | ||
102 changes: 102 additions & 0 deletions
102
packages/classification-station/src/__tests__/WcHost.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
| import { render } from "@testing-library/react"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| // Take WcHost's React 18 path on the workspace's React 19: it branches on | ||
| // React's `version`, read once at import. `npm run test:react18` runs the same | ||
| // path on a real React 18. | ||
| vi.mock("react", async (importOriginal) => ({ | ||
| ...(await importOriginal<typeof import("react")>()), | ||
| version: "18.3.1", | ||
| })); | ||
|
|
||
| const { WcHost } = await import("../WcHost"); | ||
|
|
||
| const TAG = "wc-host-test-element"; | ||
| const OTHER_TAG = "wc-host-test-other-element"; | ||
|
|
||
| class Recorder extends HTMLElement { | ||
| assigned: Array<[string, unknown]> = []; | ||
| } | ||
| for (const key of ["dom", "options", "documentId", "theme"]) { | ||
| Object.defineProperty(Recorder.prototype, key, { | ||
| get(this: any) { | ||
| return this[`_${key}`]; | ||
| }, | ||
| set(this: any, value: unknown) { | ||
| this.assigned.push([key, value]); | ||
| this[`_${key}`] = value; | ||
| }, | ||
| }); | ||
| } | ||
| for (const tag of [TAG, OTHER_TAG]) { | ||
| if (!customElements.get(tag)) | ||
| customElements.define(tag, class extends Recorder {}); | ||
| } | ||
|
|
||
| const host = ( | ||
| tag: string, | ||
| props: Record<string, unknown>, | ||
| elementRef = vi.fn(), | ||
| ) => <WcHost tag={tag} props={props} elementRef={elementRef} />; | ||
|
|
||
| describe("WcHost on React 18", () => { | ||
| it("assigns inputs as properties in key order, keeping className, style and dashed names on the tag", () => { | ||
| const dom = { Pages: [] }; | ||
| const { container } = render( | ||
| host(TAG, { | ||
| className: "c", | ||
| style: { height: "10px" }, | ||
| "instance-id": "i1", | ||
| dom, | ||
| theme: "dark", | ||
| documentId: "d1", | ||
| }), | ||
| ); | ||
| const el = container.querySelector(TAG) as Recorder; | ||
|
|
||
| expect(el.assigned).toEqual([ | ||
| ["dom", dom], | ||
| ["theme", "dark"], | ||
| ["documentId", "d1"], | ||
| ]); | ||
| expect(el.getAttribute("class")).toBe("c"); | ||
| expect(el.style.height).toBe("10px"); | ||
| expect(el.getAttribute("instance-id")).toBe("i1"); | ||
| expect(el.hasAttribute("dom")).toBe(false); | ||
| }); | ||
|
|
||
| it("re-assigns only changed inputs, and resets a removed one to undefined", () => { | ||
| const dom = { Pages: [] }; | ||
| const { container, rerender } = render(host(TAG, { dom, theme: "dark" })); | ||
| const el = container.querySelector(TAG) as Recorder; | ||
| el.assigned = []; | ||
|
|
||
| const options = { a: 1 }; | ||
| rerender(host(TAG, { dom, options })); | ||
|
|
||
| expect(el.assigned).toEqual([ | ||
| ["options", options], | ||
| ["theme", undefined], | ||
| ]); | ||
| }); | ||
|
|
||
| it("assigns every input afresh to a new element when the tag changes", () => { | ||
| const dom = { Pages: [] }; | ||
| const { container, rerender } = render(host(TAG, { dom })); | ||
| rerender(host(OTHER_TAG, { dom })); | ||
| const el = container.querySelector(OTHER_TAG) as Recorder; | ||
|
|
||
| expect(el.assigned).toEqual([["dom", dom]]); | ||
| }); | ||
|
|
||
| it("hands the element, then null on unmount, to the element ref", () => { | ||
| const elementRef = vi.fn(); | ||
| const { container, unmount } = render(host(TAG, {}, elementRef)); | ||
| const el = container.querySelector(TAG); | ||
|
|
||
| expect(elementRef).toHaveBeenLastCalledWith(el); | ||
| unmount(); | ||
| expect(elementRef).toHaveBeenLastCalledWith(null); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.