From dd2353062445ef32f6afd9114a079e87f5f73007 Mon Sep 17 00:00:00 2001 From: Rhys Date: Mon, 9 Dec 2024 10:04:43 -0500 Subject: [PATCH] fix(components): fix element key editor tooltip and revert not updating validity COMPASS-8586 (#6548) --- .../document-list/element-editors.tsx | 12 +++++++++++- .../src/components/document-list/element.tsx | 17 ++++++++++++++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/packages/compass-components/src/components/document-list/element-editors.tsx b/packages/compass-components/src/components/document-list/element-editors.tsx index 5a031109f81..3082496d17c 100644 --- a/packages/compass-components/src/components/document-list/element-editors.tsx +++ b/packages/compass-components/src/components/document-list/element-editors.tsx @@ -82,6 +82,7 @@ export const KeyEditor: React.FunctionComponent<{ enabled={!valid} trigger={({ className, + children, // Having a tooltip connected to the input elements is not the most // accessible thing ever and so a lot of event listeners of the // tooltip conflict with the textarea default behavior (due to @@ -93,6 +94,8 @@ export const KeyEditor: React.FunctionComponent<{ onPointerUp, onPointerDown, onMouseDown, + /* eslint-enable @typescript-eslint/no-unused-vars */ + ...triggerProps }: React.HTMLProps) => { return (
@@ -118,7 +121,9 @@ export const KeyEditor: React.FunctionComponent<{ )} style={{ width }} spellCheck="false" + {...triggerProps} > + {children}
); }} @@ -185,6 +190,7 @@ export const ValueEditor: React.FunctionComponent<{ onBlur, }) => { const val = String(value); + const darkMode = useDarkMode(); const inputStyle = useMemo(() => { if (type === 'String') { @@ -280,7 +286,11 @@ export const ValueEditor: React.FunctionComponent<{ className={cx( editorReset, editorOutline, - !valid && editorInvalid + !valid && editorInvalid, + !valid && + (darkMode + ? editorInvalidDarkMode + : editorInvalidLightMode) )} style={inputStyle} spellCheck="false" diff --git a/packages/compass-components/src/components/document-list/element.tsx b/packages/compass-components/src/components/document-list/element.tsx index 85197f9e709..cecbe23e8c8 100644 --- a/packages/compass-components/src/components/document-list/element.tsx +++ b/packages/compass-components/src/components/document-list/element.tsx @@ -92,6 +92,17 @@ function useHadronElement(el: HadronElementType) { [el, forceUpdate] ); + const onElementReverted = useCallback( + (changedElement: HadronElementType) => { + if (el.uuid === changedElement.uuid) { + // When an element is reverted we check again if the key is a duplicate. + setIsDuplicateKey(el.isDuplicateKey(el.key)); + forceUpdate(); + } + }, + [el, forceUpdate] + ); + useEffect(() => { if (prevEl && prevEl !== el) { forceUpdate(); @@ -101,7 +112,7 @@ function useHadronElement(el: HadronElementType) { useEffect(() => { el.on(ElementEvents.Converted, onElementChanged); el.on(ElementEvents.Edited, onElementChanged); - el.on(ElementEvents.Reverted, onElementChanged); + el.on(ElementEvents.Reverted, onElementReverted); el.on(ElementEvents.Invalid, onElementChanged); el.on(ElementEvents.Valid, onElementChanged); el.on(ElementEvents.Added, onElementAddedOrRemoved); @@ -113,7 +124,7 @@ function useHadronElement(el: HadronElementType) { return () => { el.off(ElementEvents.Converted, onElementChanged); el.off(ElementEvents.Edited, onElementChanged); - el.off(ElementEvents.Reverted, onElementChanged); + el.off(ElementEvents.Reverted, onElementReverted); el.off(ElementEvents.Valid, onElementChanged); el.off(ElementEvents.Added, onElementAddedOrRemoved); el.off(ElementEvents.Removed, onElementAddedOrRemoved); @@ -121,7 +132,7 @@ function useHadronElement(el: HadronElementType) { el.off(ElementEvents.Collapsed, onElementChanged); el.off(ElementEvents.VisibleElementsChanged, onElementChanged); }; - }, [el, onElementChanged, onElementAddedOrRemoved]); + }, [el, onElementChanged, onElementAddedOrRemoved, onElementReverted]); const isValid = el.isCurrentTypeValid();