Skip to content

Commit

Permalink
Merge pull request #8314 from marmelab/fix-rti-value-update
Browse files Browse the repository at this point in the history
Fix `RichTextInput` doesn't update when record field updates
  • Loading branch information
fzaninotto authored Oct 27, 2022
2 parents 7d8d881 + be90159 commit 44fa491
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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, {
preserveWhitespace: true,
});
}, [editor, field.value]);

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

Expand Down

0 comments on commit 44fa491

Please sign in to comment.