Skip to content

Commit 89568ab

Browse files
fix(core): derive onValueChange and onSelectionChange from onChange (#2700)
1 parent 45116fd commit 89568ab

2 files changed

Lines changed: 28 additions & 19 deletions

File tree

packages/core/src/react/hooks/useSlateProps.spec.tsx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,30 +56,16 @@ describe('useSlateProps', () => {
5656
expect(result.current.props.key).toBe(editor.meta.key);
5757

5858
act(() => {
59+
editor.children = nextValue as any;
60+
editor.selection = nextSelection;
5961
result.current.props.onChange(nextValue as any);
6062
});
6163

62-
expect(result.current.editorVersion).toBe(2);
63-
expect(result.current.selectionVersion).toBe(1);
64-
expect(result.current.valueVersion).toBe(1);
65-
expect(onChange).toHaveBeenCalledWith({ editor, value: nextValue });
66-
67-
act(() => {
68-
result.current.props.onValueChange(nextValue as any);
69-
});
70-
71-
expect(result.current.editorVersion).toBe(2);
72-
expect(result.current.selectionVersion).toBe(1);
73-
expect(result.current.valueVersion).toBe(2);
74-
expect(onValueChange).toHaveBeenCalledWith({ editor, value: nextValue });
75-
76-
act(() => {
77-
result.current.props.onSelectionChange(nextSelection);
78-
});
79-
8064
expect(result.current.editorVersion).toBe(2);
8165
expect(result.current.selectionVersion).toBe(2);
8266
expect(result.current.valueVersion).toBe(2);
67+
expect(onChange).toHaveBeenCalledWith({ editor, value: nextValue });
68+
expect(onValueChange).toHaveBeenCalledWith({ editor, value: nextValue });
8369
expect(onSelectionChange).toHaveBeenCalledWith({
8470
editor,
8571
selection: nextSelection,

packages/core/src/react/hooks/useSlateProps.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export const useSlateProps = ({ id }: { id?: string }): PlateSlateProps => {
2727
const updateVersionSelection = useIncrementVersion('versionSelection', id);
2828
const updateVersionValue = useIncrementVersion('versionValue', id);
2929

30+
const prevSelectionRef = React.useRef(editor.selection);
31+
const prevValueRef = React.useRef(editor.children);
32+
3033
const onChange = React.useCallback(
3134
(newValue: SlateComponentProps['initialValue']) => {
3235
updateVersionEditor();
@@ -35,8 +38,28 @@ export const useSlateProps = ({ id }: { id?: string }): PlateSlateProps => {
3538
if (!eventIsHandled) {
3639
onChangeProp?.({ editor, value: newValue as Value });
3740
}
41+
42+
if (editor.children !== prevValueRef.current) {
43+
prevValueRef.current = editor.children;
44+
updateVersionValue();
45+
onValueChangeProp?.({ editor, value: newValue as Value });
46+
}
47+
48+
if (editor.selection !== prevSelectionRef.current) {
49+
prevSelectionRef.current = editor.selection;
50+
updateVersionSelection();
51+
onSelectionChangeProp?.({ editor, selection: editor.selection });
52+
}
3853
},
39-
[editor, onChangeProp, updateVersionEditor]
54+
[
55+
editor,
56+
onChangeProp,
57+
onValueChangeProp,
58+
onSelectionChangeProp,
59+
updateVersionEditor,
60+
updateVersionValue,
61+
updateVersionSelection,
62+
]
4063
);
4164

4265
const onValueChange: SlateComponentProps['onValueChange'] = React.useMemo(

0 commit comments

Comments
 (0)