The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(front-component-renderer): forward React 18 camelCase attributes on remote elements (#26015)
Abdul Rahman1749958b215e
diff --git a/packages/twenty-front-component-renderer/src/remote/elements/utils/__tests__/patchRemoteElementAttributes.test.ts b/packages/twenty-front-component-renderer/src/remote/elements/utils/__tests__/patchRemoteElementAttributes.test.tsindex c9f54ec5ac6..c4b90ba912c 100644--- a/packages/twenty-front-component-renderer/src/remote/elements/utils/__tests__/patchRemoteElementAttributes.test.ts+++ b/packages/twenty-front-component-renderer/src/remote/elements/utils/__tests__/patchRemoteElementAttributes.test.ts@@ -5,9 +5,21 @@ import { resolveRemoteElementPrototypes } from '@/remote/elements/utils/resolveRimport { patchRemoteElementAttributes } from '../patchRemoteElementAttributes';-const createHtmlDivElement = (): HTMLElement =>+type RemoteElementWithPropertyUpdater = HTMLElement & {+ updateRemoteProperty: (propertyName: string, value?: unknown) => void;+};++type RemoteSvgElement = RemoteElementWithPropertyUpdater & {+ viewBox?: string;+ strokeWidth?: string;+};++const createHtmlDivElement = (): RemoteElementWithPropertyUpdater =>document.createElement('html-div');+const createHtmlSvgElement = (): RemoteSvgElement =>+ document.createElement('html-svg');+describe('patchRemoteElementAttributes', () => {beforeAll(() => {patchRemoteElementAttributes();@@ -161,6 +173,91 @@ describe('patchRemoteElementAttributes', () => {});});+ describe('className attribute alias', () => {+ it('should store a className attribute as the class attribute', () => {+ const element = createHtmlDivElement();++ element.setAttribute('className', 'from-react-18');++ expect(element.getAttribute('class')).toBe('from-react-18');+ expect(element.getAttribute('className')).toBe('from-react-18');+ expect(element.hasAttribute('className')).toBe(true);+ expect(element.className).toBe('from-react-18');+ expect(element.getAttributeNames()).toEqual(['class']);+ });++ it('should forward a className attribute to the host as the className property', () => {+ const element = createHtmlDivElement();+ const updateRemoteProperty = jest.spyOn(element, 'updateRemoteProperty');++ element.setAttribute('className', 'from-react-18');++ expect(updateRemoteProperty).toHaveBeenCalledTimes(1);+ expect(updateRemoteProperty).toHaveBeenCalledWith(+ 'className',+ 'from-react-18',+ );+ });++ it('should clear the class attribute when removing the className alias', () => {+ const element = createHtmlDivElement();++ element.setAttribute('className', 'from-react-18');++ const updateRemoteProperty = jest.spyOn(element, 'updateRemoteProperty');++ element.removeAttribute('className');++ expect(element.getAttribute('class')).toBeNull();+ expect(element.hasAttribute('className')).toBe(false);+ expect(updateRemoteProperty).toHaveBeenCalledTimes(1);+ expect(updateRemoteProperty).toHaveBeenCalledWith('className', undefined);+ });+ });++ describe('camelCase remote property names written as attributes', () => {+ it('should store a camelCase property name under its kebab-case attribute', () => {+ const element = createHtmlSvgElement();++ element.setAttribute('viewBox', '0 0 24 24');++ expect(element.getAttribute('view-box')).toBe('0 0 24 24');+ expect(element.getAttribute('viewBox')).toBe('0 0 24 24');+ expect(element.hasAttribute('viewBox')).toBe(true);+ expect(element.viewBox).toBe('0 0 24 24');+ expect(element.getAttributeNames()).toEqual(['view-box']);+ });++ it('should assign the camelCase remote property that remote-dom forwards', () => {+ const element = createHtmlSvgElement();++ element.setAttribute('strokeWidth', '2');++ expect(element.strokeWidth).toBe('2');+ expect(element.getAttribute('stroke-width')).toBe('2');+ });++ it('should clear the property when removing the camelCase attribute', () => {+ const element = createHtmlSvgElement();++ element.setAttribute('viewBox', '0 0 24 24');+ element.removeAttribute('viewBox');++ expect(element.getAttribute('viewBox')).toBeNull();+ expect(element.hasAttribute('view-box')).toBe(false);+ expect(element.viewBox).toBeUndefined();+ });++ it('should leave attributes that are not remote properties untouched', () => {+ const element = createHtmlSvgElement();++ element.setAttribute('data-icon', 'lock');++ expect(element.getAttribute('data-icon')).toBe('lock');+ expect(element.getAttributeNames()).toEqual(['data-icon']);+ });+ });+describe('attribute names colliding with Object prototype keys', () => {it('should store them as real attributes instead of mapping them to a property', () => {const element = createHtmlDivElement();diff --git a/packages/twenty-front-component-renderer/src/remote/elements/utils/patchRemoteElementAttributes.ts b/packages/twenty-front-component-renderer/src/remote/elements/utils/patchRemoteElementAttributes.tsindex f372f547576..3237046fffa 100644--- a/packages/twenty-front-component-renderer/src/remote/elements/utils/patchRemoteElementAttributes.ts+++ b/packages/twenty-front-component-renderer/src/remote/elements/utils/patchRemoteElementAttributes.ts@@ -1,3 +1,4 @@+import { type RemoteElementConstructor as RemoteDomElementConstructor } from '@remote-dom/core/elements';import { isDefined } from 'twenty-shared/utils';import { ALLOWED_HTML_ELEMENTS } from '@/constants/AllowedHtmlElements';@@ -24,10 +25,11 @@ type RemoteElementWithAttributeUpdater = Element &updateRemoteProperty: (propertyName: string, value?: unknown) => void;};-type RemoteElementConstructor = CustomElementConstructor & {- observedAttributes?: string[];- prototype: RemoteElementWithAttributeUpdater;-};+type RemoteElementConstructor = CustomElementConstructor &+ Partial<Pick<RemoteDomElementConstructor, 'remotePropertyDefinitions'>> & {+ observedAttributes?: string[];+ prototype: RemoteElementWithAttributeUpdater;+ };export const patchRemoteElementAttributes = (): void => {for (const allowedHtmlElement of ALLOWED_HTML_ELEMENTS) {@@ -49,12 +51,32 @@ export const patchRemoteElementAttributes = (): void => {isAriaOrDataAttribute(attributeName) &&!attributeNamesAlreadySyncedByRemoteDom.has(attributeName);+ const toCanonicalAttributeName = (attributeName: string): string => {+ if (attributeName === 'className') {+ return 'class';+ }++ if (ATTRIBUTE_NAME_TO_ELEMENT_PROPERTY_NAME.has(attributeName)) {+ return attributeName;+ }++ const remotePropertyAttributeName =+ elementConstructor.remotePropertyDefinitions?.get(+ attributeName,+ )?.attribute;++ return isDefined(remotePropertyAttributeName)+ ? remotePropertyAttributeName+ : attributeName;+ };+const originalGetAttribute = elementConstructor.prototype.getAttribute;elementConstructor.prototype.getAttribute = function (this: RemoteElementWithAttributeUpdater,- attributeName: string,+ rawAttributeName: string,) {+ const attributeName = toCanonicalAttributeName(rawAttributeName);const mappedElementPropertyName =ATTRIBUTE_NAME_TO_ELEMENT_PROPERTY_NAME.get(attributeName);@@ -73,8 +95,9 @@ export const patchRemoteElementAttributes = (): void => {elementConstructor.prototype.hasAttribute = function (this: RemoteElementWithAttributeUpdater,- attributeName: string,+ rawAttributeName: string,) {+ const attributeName = toCanonicalAttributeName(rawAttributeName);const mappedElementPropertyName =ATTRIBUTE_NAME_TO_ELEMENT_PROPERTY_NAME.get(attributeName);@@ -102,9 +125,10 @@ export const patchRemoteElementAttributes = (): void => {elementConstructor.prototype.setAttribute = function (this: RemoteElementWithAttributeUpdater,- attributeName: string,+ rawAttributeName: string,attributeValue: string,) {+ const attributeName = toCanonicalAttributeName(rawAttributeName);const mappedElementPropertyName =ATTRIBUTE_NAME_TO_ELEMENT_PROPERTY_NAME.get(attributeName);@@ -130,8 +154,9 @@ export const patchRemoteElementAttributes = (): void => {elementConstructor.prototype.removeAttribute = function (this: RemoteElementWithAttributeUpdater,- attributeName: string,+ rawAttributeName: string,) {+ const attributeName = toCanonicalAttributeName(rawAttributeName);const mappedElementPropertyName =ATTRIBUTE_NAME_TO_ELEMENT_PROPERTY_NAME.get(attributeName);