From c64d0e29eb9e47a2291baaac5ca90a9c3b781ff9 Mon Sep 17 00:00:00 2001 From: Per-Kristian Nordnes Date: Thu, 2 Nov 2023 15:09:32 +0100 Subject: [PATCH] fix(form/inputs): fix bug with onRemove cb for TextBlock (#5061) This will fix a issue with the wrong path being sent to the delete function and focus can now be called without a setTimeout because of a bugfix in the PortableTextEditor. --- .../core/form/inputs/PortableText/text/TextBlock.tsx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/packages/sanity/src/core/form/inputs/PortableText/text/TextBlock.tsx b/packages/sanity/src/core/form/inputs/PortableText/text/TextBlock.tsx index 10edf0a4288..e8cf732dc34 100644 --- a/packages/sanity/src/core/form/inputs/PortableText/text/TextBlock.tsx +++ b/packages/sanity/src/core/form/inputs/PortableText/text/TextBlock.tsx @@ -130,11 +130,14 @@ export function TextBlock(props: TextBlockProps) { }, [onItemOpen, memberItem]) const onRemove = useCallback(() => { - const sel: EditorSelection = {focus: {path, offset: 0}, anchor: {path, offset: 0}} + const point = {path: path.slice(-1), offset: 0} + const sel: EditorSelection = { + focus: point, + anchor: point, + } PortableTextEditor.delete(editor, sel, {mode: 'blocks'}) - // Focus will not stick unless this is done through a timeout when deleted through clicking the menu button. - setTimeout(() => PortableTextEditor.focus(editor)) - }, [editor, path]) + PortableTextEditor.focus(editor) + }, [path, editor]) const text = useMemo(() => { return (