Skip to content

Commit

Permalink
fix: broken rich text onChange prop (#1740)
Browse files Browse the repository at this point in the history
* fix: broken rich text onChange prop

* chore: remove accidental code

* refactor: resolve comments

* refactor: make handler look nicer
  • Loading branch information
MayaGillilan committed Sep 3, 2024
1 parent 472a50e commit d62535c
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions packages/rich-text/src/RichTextEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,38 @@ import { SyncEditorChanges } from './SyncEditorChanges';
import Toolbar from './Toolbar';
import StickyToolbarWrapper from './Toolbar/components/StickyToolbarWrapper';

type ConnectedProps = {
type RichTextProps = {
sdk: FieldAppSDK;
isInitiallyDisabled: boolean;
onAction?: RichTextTrackingActionHandler;
restrictedMarks?: string[];
// For passing down to connected editor, some refactoring needed
minHeight?: string | number;
maxHeight?: string | number;
value?: Contentful.Document;
isDisabled?: boolean;
onChange?: (doc: Contentful.Document) => unknown;
isToolbarHidden?: boolean;
actionsDisabled?: boolean;
/**
* @deprecated Use `sdk.field.onValueChanged` instead
*/
onChange?: (doc: Contentful.Document) => unknown;
};

type ConnectedRichTextProps = {
sdk: FieldAppSDK;
onAction?: RichTextTrackingActionHandler;
onChange?: (doc: Contentful.Document) => unknown;
restrictedMarks?: string[];
minHeight?: string | number;
maxHeight?: string | number;
value?: Contentful.Document;
isDisabled?: boolean;
isToolbarHidden?: boolean;
actionsDisabled?: boolean;
};

export const ConnectedRichTextEditor = (props: ConnectedProps) => {
export const ConnectedRichTextEditor = (props: ConnectedRichTextProps) => {
const { sdk, onAction, restrictedMarks } = props;

const id = getContentfulEditorId(sdk);
Expand Down Expand Up @@ -90,14 +108,18 @@ export const ConnectedRichTextEditor = (props: ConnectedProps) => {
);
};

type Props = ConnectedProps & { isInitiallyDisabled: boolean };

const RichTextEditor = (props: Props) => {
const { sdk, isInitiallyDisabled, onAction, restrictedMarks, ...otherProps } = props;
const RichTextEditor = (props: RichTextProps) => {
const { sdk, isInitiallyDisabled, onAction, restrictedMarks, onChange, ...otherProps } = props;
const isEmptyValue = React.useCallback(
(value) => !value || deepEquals(value, Contentful.EMPTY_DOCUMENT),
[]
);
React.useEffect(() => {
if (!onChange) {
return;
}
return sdk.field.onValueChanged(onChange);
}, [onChange, sdk.field]);

const id = getContentfulEditorId(props.sdk);
return (
Expand Down

0 comments on commit d62535c

Please sign in to comment.