Skip to content

Commit

Permalink
fix(content-read-only): priorize explicit prop over redux state (#4977)
Browse files Browse the repository at this point in the history
Refs #4976
  • Loading branch information
char0n authored Apr 24, 2024
1 parent ce4b058 commit 91cc57c
Showing 1 changed file with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
import { useEffect } from 'react';
import PropTypes from 'prop-types';

const EditorWrapper = (Original, system) => {
const Editor = (props) => {
const isReadOnly = system.editorSelectors.selectContentIsReadOnly();
/**
* `isReadOnly` prop has always priority over the state.
* If `isReadOnly` prop is set, it will override the state.
*/
const Editor = ({ isReadOnly: isReadOnlyFromProp, ...props }) => {
const isReadOnlyFromState = system.editorSelectors.selectContentIsReadOnly();
const isReadOnly = isReadOnlyFromProp ?? isReadOnlyFromState;

useEffect(() => {
if (typeof isReadOnlyFromProp === 'boolean' && isReadOnlyFromProp !== isReadOnlyFromState) {
if (isReadOnlyFromProp) system.editorActions.setContentReadOnly();
if (!isReadOnlyFromProp) system.editorActions.setContentReadWrite();
}
}, [isReadOnlyFromProp, isReadOnlyFromState]);

// eslint-disable-next-line react/jsx-props-no-spreading
return <Original {...props} isReadOnly={isReadOnly} />;
};

Editor.propTypes = {
isReadOnly: PropTypes.bool,
};
Editor.defaultProps = {
isReadOnly: null,
};

return Editor;
};

Expand Down

0 comments on commit 91cc57c

Please sign in to comment.