Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix RichTextInput doesn't update when record field updates #8314

Merged
merged 5 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions packages/ra-input-rich-text/src/RichTextInput.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as React from 'react';
import expect from 'expect';
import { render, waitFor } from '@testing-library/react';
import { Basic } from './RichTextInput.stories';

describe('<RichTextInput />', () => {
it('should update its content when fields value changes', async () => {
const record = { id: 123, body: '<h1>Hello world!</h1>' };
const { container, rerender } = render(<Basic record={record} />);

await waitFor(() => {
expect(container.querySelector('#body').innerHTML).toEqual(
'<h1>Hello world!</h1>'
);
});

const newRecord = { id: 123, body: '<h1>Goodbye world!</h1>' };
rerender(<Basic record={newRecord} />);

await waitFor(() => {
expect(container.querySelector('#body').innerHTML).toEqual(
'<h1>Goodbye world!</h1>'
);
});
});
});
8 changes: 8 additions & 0 deletions packages/ra-input-rich-text/src/RichTextInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ export const RichTextInput = (props: RichTextInputProps) => {

const { error, invalid, isTouched } = fieldState;

useEffect(() => {
if (!editor) return;

editor.commands.setContent(field.value, false, {
WiXSL marked this conversation as resolved.
Show resolved Hide resolved
preserveWhitespace: true,
});
}, [editor, field.value]);

useEffect(() => {
if (!editor) return;

Expand Down