The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Fix lost keystrokes in Preact front component text inputs (#28178)
Raphaël Bosic099a425e061
diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime-remote-wrapper-plugin.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime-remote-wrapper-plugin.tsindex d02bf382229..97ed9d9554f 100644--- a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime-remote-wrapper-plugin.ts+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime-remote-wrapper-plugin.ts@@ -34,18 +34,15 @@ export const createJsxRuntimeRemoteWrapperPlugin = (loader: 'js' as const,}));- build.onResolve({ filter: /^__reads_element_ref_from_vnode__$/ }, () => ({- path: '__reads_element_ref_from_vnode__',- namespace: 'reads-element-ref-from-vnode',+ build.onResolve({ filter: /^__uses_preact__$/ }, () => ({+ path: '__uses_preact__',+ namespace: 'uses-preact',}));- build.onLoad(- { filter: /.*/, namespace: 'reads-element-ref-from-vnode' },- () => ({- contents: `export const READS_ELEMENT_REF_FROM_VNODE = ${usePreact};`,- loader: 'js' as const,- }),- );+ build.onLoad({ filter: /.*/, namespace: 'uses-preact' }, () => ({+ contents: `export const USES_PREACT = ${usePreact};`,+ loader: 'js' as const,+ }));build.onResolve({ filter: /^react\/jsx-runtime$/ }, async (args) => {if (args.pluginData?.skipJsxWrapper) {diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/__tests__/jsx-shared-helpers.spec.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/__tests__/jsx-shared-helpers.spec.tsindex 2114ac1db35..9037b809d38 100644--- a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/__tests__/jsx-shared-helpers.spec.ts+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/__tests__/jsx-shared-helpers.spec.ts@@ -11,17 +11,29 @@ import { type SyntheticLikeEvent } from '@/cli/utilities/build/common/front-comptype SharedHelpers = typeof JsxSharedHelpers;-const loadSharedHelpers = (): SharedHelpers => {+const loadSharedHelpers = ({+ usesPreact = false,+}: { usesPreact?: boolean } = {}): SharedHelpers => {const { code } = esbuild.transformSync(jsxSharedHelpersSource, {format: 'cjs',loader: 'js',});const helpersModule = { exports: {} };+ const virtualModules: Record<string, unknown> = {+ __uses_preact__: { USES_PREACT: usesPreact },+ };- new Function('module', 'exports', 'globalThis', code)(+ new Function('module', 'exports', 'require', 'globalThis', code)(helpersModule,helpersModule.exports,- { __HTML_TAG_TO_CUSTOM_ELEMENT_TAG__: { button: 'html-button' } },+ (modulePath: string) => virtualModules[modulePath],+ {+ __HTML_TAG_TO_CUSTOM_ELEMENT_TAG__: {+ button: 'html-button',+ input: 'html-input',+ textarea: 'html-textarea',+ },+ },);return helpersModule.exports as SharedHelpers;@@ -139,6 +151,86 @@ describe('jsx runtime shared helpers', () => {]);});+ it('should run onChange of text entry controls on input in Preact builds, as preact/compat does', () => {+ const { makeEventRef } = loadSharedHelpers({ usesPreact: true });+ const searchInput = Object.assign(new EventTarget(), {+ tagName: 'HTML-INPUT',+ type: 'search',+ });+ const textarea = Object.assign(new EventTarget(), {+ tagName: 'HTML-TEXTAREA',+ });+ const handleInput = vi.fn();+ const handleChange = vi.fn();+ const handleTextareaChange = vi.fn();++ makeEventRef(+ { onInput: handleInput, onChange: handleChange },+ null,+ 'jsx',+ )(searchInput);+ makeEventRef({ onChange: handleTextareaChange }, null, 'jsx')(textarea);+ dispatchSyntheticLikeEvents(searchInput, ['input', 'change']);+ dispatchSyntheticLikeEvents(textarea, ['input', 'change']);++ expect(handleInput).toHaveBeenCalledTimes(1);+ expect(handleInput.mock.calls[0][0].type).toBe('input');+ expect(handleChange).toHaveBeenCalledTimes(1);+ expect(handleChange.mock.calls[0][0].type).toBe('input');+ expect(handleTextareaChange).toHaveBeenCalledTimes(1);+ expect(handleTextareaChange.mock.calls[0][0].type).toBe('input');+ });++ it('should keep onChange of text entry controls on change in React builds', () => {+ const { makeEventRef } = loadSharedHelpers();+ const searchInput = Object.assign(new EventTarget(), {+ tagName: 'HTML-INPUT',+ type: 'search',+ });+ const handleChange = vi.fn();++ makeEventRef({ onChange: handleChange }, null, 'jsx')(searchInput);+ dispatchSyntheticLikeEvents(searchInput, ['input', 'change']);++ expect(handleChange).toHaveBeenCalledTimes(1);+ expect(handleChange.mock.calls[0][0].type).toBe('change');+ });++ it('should keep onChange on change for checkbox, radio and file inputs in Preact builds', () => {+ const { makeEventRef } = loadSharedHelpers({ usesPreact: true });++ for (const type of ['checkbox', 'radio', 'file']) {+ const input = Object.assign(new EventTarget(), {+ tagName: 'HTML-INPUT',+ type,+ });+ const handleChange = vi.fn();++ makeEventRef({ onChange: handleChange }, null, 'jsx')(input);+ dispatchSyntheticLikeEvents(input, ['input', 'change']);++ expect(handleChange).toHaveBeenCalledTimes(1);+ expect(handleChange.mock.calls[0][0].type).toBe('change');+ }+ });++ it('should move onChange back to change when a text input becomes a checkbox in Preact builds', () => {+ const { makeEventRef } = loadSharedHelpers({ usesPreact: true });+ const input = Object.assign(new EventTarget(), {+ tagName: 'HTML-INPUT',+ type: 'text',+ });+ const handleChange = vi.fn();++ makeEventRef({ onChange: handleChange }, null, 'jsx')(input);+ input.type = 'checkbox';+ makeEventRef({ onChange: handleChange }, null, 'jsx')(input);+ dispatchSyntheticLikeEvents(input, ['input', 'change']);++ expect(handleChange).toHaveBeenCalledTimes(1);+ expect(handleChange.mock.calls[0][0].type).toBe('change');+ });+it('should let the element handler prevent the handler a clone added, as Base UI merging does', () => {const { makeEventRef, withCloneEventRef } = loadSharedHelpers();const element = new EventTarget();diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/jsx-runtime-virtual-modules.d.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/jsx-runtime-virtual-modules.d.tsindex b5181013992..75919e2ac0d 100644--- a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/jsx-runtime-virtual-modules.d.ts+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/jsx-runtime-virtual-modules.d.ts@@ -21,6 +21,6 @@ declare module '__jsx_shared_helpers__' {export * from '@/cli/utilities/build/common/front-component-build/jsx-runtime/jsx-shared-helpers';}-declare module '__reads_element_ref_from_vnode__' {- export const READS_ELEMENT_REF_FROM_VNODE: boolean;+declare module '__uses_preact__' {+ export const USES_PREACT: boolean;}diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/react-wrapper.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/react-wrapper.tsindex 7ee94568045..01522aaddc2 100644--- a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/react-wrapper.ts+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/react-wrapper.ts@@ -7,7 +7,7 @@ import {withCloneEventRef,withJsxEventRef,} from '__jsx_shared_helpers__';-import { READS_ELEMENT_REF_FROM_VNODE } from '__reads_element_ref_from_vnode__';+import { USES_PREACT } from '__uses_preact__';import { type ClonedElement } from '@/cli/utilities/build/common/front-component-build/jsx-runtime/types/cloned-element.type';import { type ElementProps } from '@/cli/utilities/build/common/front-component-build/jsx-runtime/types/element-props.type';@@ -76,7 +76,7 @@ function cloneElement(const propsWithCloneEventRef = withCloneEventRef(element,getPropsArgument(argumentsAfterElement),- READS_ELEMENT_REF_FROM_VNODE,+ USES_PREACT,);return originalCloneElement.apply(diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/is-text-entry-element.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/is-text-entry-element.tsnew file mode 100644index 00000000000..2eb2daff3b6--- /dev/null+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/is-text-entry-element.ts@@ -0,0 +1,26 @@+import { CUSTOM_ELEMENT_TAG_BY_HTML_TAG } from '@/cli/utilities/build/common/front-component-build/jsx-runtime/constants/custom-element-tag-by-html-tag';++const INPUT_TYPES_WITHOUT_TEXT_ENTRY = new Set(['checkbox', 'radio', 'file']);++export const isTextEntryElement = (element: EventTarget) => {+ if (!('tagName' in element) || typeof element.tagName !== 'string') {+ return false;+ }++ const lowerCaseTagName = element.tagName.toLowerCase();++ if (lowerCaseTagName === CUSTOM_ELEMENT_TAG_BY_HTML_TAG.textarea) {+ return true;+ }++ if (lowerCaseTagName !== CUSTOM_ELEMENT_TAG_BY_HTML_TAG.input) {+ return false;+ }++ const inputType =+ 'type' in element && typeof element.type === 'string'+ ? element.type.toLowerCase()+ : '';++ return !INPUT_TYPES_WITHOUT_TEXT_ENTRY.has(inputType);+};diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/set-event-handlers-of-source.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/set-event-handlers-of-source.tsindex 8681e2d11e4..e3fb37d5c40 100644--- a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/set-event-handlers-of-source.ts+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/set-event-handlers-of-source.ts@@ -17,7 +17,10 @@ export const setEventHandlersOfSource = ({}) => {const currentListenerKeys: Record<string, boolean> = {};for (const eventPropName in events) {- const listenerDescriptor = toEventListenerDescriptor(eventPropName);+ const listenerDescriptor = toEventListenerDescriptor({+ eventPropName,+ element,+ });currentListenerKeys[listenerDescriptor.key] = true;const listenerEntry =listenerEntriesByKey[listenerDescriptor.key] ||diff --git a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/to-event-listener-descriptor.ts b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/to-event-listener-descriptor.tsindex 26b200b1e2a..0aa0d6b52ab 100644--- a/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/to-event-listener-descriptor.ts+++ b/packages/twenty-sdk/src/cli/utilities/build/common/front-component-build/jsx-runtime/utils/to-event-listener-descriptor.ts@@ -1,6 +1,9 @@+import { USES_PREACT } from '__uses_preact__';+import { CAPTURE_PHASE_PROP_NAME_SUFFIX } from '@/cli/utilities/build/common/front-component-build/jsx-runtime/constants/capture-phase-prop-name-suffix';import { type EventListenerDescriptor } from '@/cli/utilities/build/common/front-component-build/jsx-runtime/types/event-listener-descriptor.type';import { isCapturePhaseEventProp } from '@/cli/utilities/build/common/front-component-build/jsx-runtime/utils/is-capture-phase-event-prop';+import { isTextEntryElement } from '@/cli/utilities/build/common/front-component-build/jsx-runtime/utils/is-text-entry-element';const EVENT_PROP_NAME_PREFIX = 'on';const DOM_EVENT_PROP_NAME_BY_LOWER_CASE_REACT_EVENT_PROP_NAME: Record<@@ -10,9 +13,13 @@ const DOM_EVENT_PROP_NAME_BY_LOWER_CASE_REACT_EVENT_PROP_NAME: Record<ondoubleclick: 'ondblclick',};-export const toEventListenerDescriptor = (- eventPropName: string,-): EventListenerDescriptor => {+export const toEventListenerDescriptor = ({+ eventPropName,+ element,+}: {+ eventPropName: string;+ element: EventTarget;+}): EventListenerDescriptor => {const isCapturePhase = isCapturePhaseEventProp(eventPropName);const bubblePhaseEventPropName = isCapturePhase? eventPropName.slice(0, -CAPTURE_PHASE_PROP_NAME_SUFFIX.length)@@ -23,10 +30,14 @@ export const toEventListenerDescriptor = (lowerCaseEventPropName] || lowerCaseEventPropName;const eventType = domEventPropName.slice(EVENT_PROP_NAME_PREFIX.length);+ const isTextEntryChange =+ USES_PREACT && eventType === 'change' && isTextEntryElement(element);+ const listenerType = isTextEntryChange ? 'input' : eventType;+ const listenerKey = isTextEntryChange ? 'change:input' : eventType;return {- key: isCapturePhase ? eventType + ':capture' : eventType,- type: eventType,+ key: isCapturePhase ? listenerKey + ':capture' : listenerKey,+ type: listenerType,capture: isCapturePhase,};};