mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-20 06:01:24 +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:
@@ -443,7 +443,6 @@ export const ReactEditor: ReactEditorInterface = {
|
||||
// Create a new selection in the top of the document if missing
|
||||
if (!editor.selection) {
|
||||
Transforms.select(editor, Editor.start(editor, []))
|
||||
editor.onChange()
|
||||
}
|
||||
// IS_FOCUSED should be set before calling el.focus() to ensure that
|
||||
// FocusedContext is updated to the correct value
|
||||
|
@@ -85,6 +85,43 @@ describe('slate-react', () => {
|
||||
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