Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Fix grid json and text editor to save after losing focus

Joshen Lime6bd6c8130ef
diff --git a/studio/components/grid/components/common/BlockKeys.tsx b/studio/components/grid/components/common/BlockKeys.tsx
index 9ffb31af1ca..388fb92703c 100644
--- a/studio/components/grid/components/common/BlockKeys.tsx
+++ b/studio/components/grid/components/common/BlockKeys.tsx
@@ -1,4 +1,5 @@
-import { FC, useCallback, KeyboardEvent, ReactNode } from 'react'
+import { useClickedOutside } from 'hooks'
+import { FC, useRef, useEffect, useCallback, KeyboardEvent, ReactNode } from 'react'
interface BlockKeysProps {
value: string | null
@@ -13,6 +14,9 @@ interface BlockKeysProps {
* Example: press enter to add newline on textEditor
*/
export const BlockKeys: FC<BlockKeysProps> = ({ value, children, onEscape, onEnter }) => {
+ const ref = useRef(null)
+ const isClickedOutside = useClickedOutside(ref)
+
const handleKeyDown = useCallback(
(ev: KeyboardEvent<HTMLDivElement>) => {
switch (ev.key) {
@@ -32,15 +36,12 @@ export const BlockKeys: FC<BlockKeysProps> = ({ value, children, onEscape, onEnt
[value]
)
- function onBlur() {
- // [Joshen] Commenting this out for now as its causing some odd behavior
- // where its triggering when a children of the Popover overlay component is clicked
- // Also to make the save action a bit more deliberate
- // if (onEnter) onEnter(value)
- }
+ useEffect(() => {
+ if (isClickedOutside && onEnter !== undefined) onEnter(value)
+ }, [isClickedOutside])
return (
- <div onKeyDown={handleKeyDown} onBlur={onBlur}>
+ <div ref={ref} onKeyDown={handleKeyDown}>
{children}
</div>
)
diff --git a/studio/components/grid/components/editor/TextEditor.tsx b/studio/components/grid/components/editor/TextEditor.tsx
index ac375f9178c..2b0fa077dc3 100644
--- a/studio/components/grid/components/editor/TextEditor.tsx
+++ b/studio/components/grid/components/editor/TextEditor.tsx
@@ -44,9 +44,21 @@ export function TextEditor<TRow, TSummaryRow = unknown>({
value={value ?? ''}
onChange={onChange}
/>
- <div className="flex items-center justify-end p-2 bg-scale-400 space-x-2">
- <p className="text-xs text-scale-1100">Save changes</p>
- <code className="text-xs">⏎</code>
+ <div className="flex items-start justify-between p-2 bg-scale-400 space-x-2">
+ <div className="space-y-1">
+ <div className="flex items-center space-x-2">
+ <div className="px-1.5 py-[2.5px] rounded bg-scale-600 border border-scale-700 flex items-center justify-center">
+ <span className="text-[10px]">⏎</span>
+ </div>
+ <p className="text-xs text-scale-1100">Save changes</p>
+ </div>
+ <div className="flex items-center space-x-2">
+ <div className="px-1 py-[2.5px] rounded bg-scale-600 border border-scale-700 flex items-center justify-center">
+ <span className="text-[10px]">Esc</span>
+ </div>
+ <p className="text-xs text-scale-1100">Cancel changes</p>
+ </div>
+ </div>
</div>
</BlockKeys>
}
diff --git a/studio/hooks/ui/index.ts b/studio/hooks/ui/index.ts
index a62409a29de..42c5f109a3c 100644
--- a/studio/hooks/ui/index.ts
+++ b/studio/hooks/ui/index.ts
@@ -1,3 +1,4 @@
export * from './useFlag'
export * from './useIsActive'
export * from './useUrlState'
+export * from './useClickedOutside'
diff --git a/studio/hooks/ui/useClickedOutside.ts b/studio/hooks/ui/useClickedOutside.ts
new file mode 100644
index 00000000000..8e4086f83cc
--- /dev/null
+++ b/studio/hooks/ui/useClickedOutside.ts
@@ -0,0 +1,25 @@
+import { useEffect, useState } from 'react'
+
+/**
+ * Hook that alerts clicks outside of the passed ref
+ */
+export const useClickedOutside = (ref: any) => {
+ const [active, setActive] = useState<boolean>(false)
+
+ useEffect(() => {
+ const handleClickOutside = (event: any) => {
+ if (ref.current && !ref.current.contains(event.target)) {
+ setActive(true)
+ } else {
+ setActive(false)
+ }
+ }
+
+ document.addEventListener('mousedown', handleClickOutside)
+ return () => {
+ document.removeEventListener('mousedown', handleClickOutside)
+ }
+ }, [ref])
+
+ return active
+}