mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-22 15:02:51 +02:00
fix: unexpected event triggered when using ReactEditor.focus (#5677)
* chore: reproduce the problem env * fix: unexpected event triggered * feat: onValueChange unit test
This commit is contained in:
5
.changeset/mighty-trainers-juggle.md
Normal file
5
.changeset/mighty-trainers-juggle.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'slate-react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
fix unexpected event triggered when using `ReactEditor.focus`
|
@@ -443,7 +443,6 @@ export const ReactEditor: ReactEditorInterface = {
|
|||||||
// Create a new selection in the top of the document if missing
|
// Create a new selection in the top of the document if missing
|
||||||
if (!editor.selection) {
|
if (!editor.selection) {
|
||||||
Transforms.select(editor, Editor.start(editor, []))
|
Transforms.select(editor, Editor.start(editor, []))
|
||||||
editor.onChange()
|
|
||||||
}
|
}
|
||||||
// IS_FOCUSED should be set before calling el.focus() to ensure that
|
// IS_FOCUSED should be set before calling el.focus() to ensure that
|
||||||
// FocusedContext is updated to the correct value
|
// FocusedContext is updated to the correct value
|
||||||
|
@@ -85,6 +85,43 @@ describe('slate-react', () => {
|
|||||||
expect(windowSelection?.focusOffset).toBe(testSelection.focus.offset)
|
expect(windowSelection?.focusOffset).toBe(testSelection.focus.offset)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test('should not trigger onValueChange when focus is called', async () => {
|
||||||
|
const editor = withReact(createEditor())
|
||||||
|
const initialValue = [{ type: 'block', children: [{ text: 'test' }] }]
|
||||||
|
const onChange = jest.fn()
|
||||||
|
const onValueChange = jest.fn()
|
||||||
|
const onSlectionChange = jest.fn()
|
||||||
|
|
||||||
|
act(() => {
|
||||||
|
render(
|
||||||
|
<Slate
|
||||||
|
editor={editor}
|
||||||
|
initialValue={initialValue}
|
||||||
|
onValueChange={onValueChange}
|
||||||
|
onChange={onChange}
|
||||||
|
onSelectionChange={onSlectionChange}
|
||||||
|
>
|
||||||
|
<Editable />
|
||||||
|
</Slate>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(editor.selection).toBe(null)
|
||||||
|
|
||||||
|
await act(async () => {
|
||||||
|
ReactEditor.focus(editor)
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(editor.selection).toEqual({
|
||||||
|
anchor: { path: [0, 0], offset: 0 },
|
||||||
|
focus: { path: [0, 0], offset: 0 },
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(onChange).toHaveBeenCalled()
|
||||||
|
expect(onSlectionChange).toHaveBeenCalled()
|
||||||
|
expect(onValueChange).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user