Skip to content

Commit

Permalink
Fix unnecessary update of ra-input-rich-text on edit
Browse files Browse the repository at this point in the history
  • Loading branch information
Roy Choo committed Apr 11, 2019
1 parent 198fdf3 commit 661b8a1
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"jest": "^23.6.0",
"lerna": "~2.9.1",
"lolex": "~2.3.2",
"mutationobserver-shim": "^0.3.3",
"prettier": "~1.16.4",
"raf": "~3.4.0",
"ts-jest": "^23.10.5",
Expand Down
9 changes: 8 additions & 1 deletion packages/ra-input-rich-text/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { withStyles } from '@material-ui/core/styles';
import styles from './styles';

export class RichTextInput extends Component {
lastValueChange = null;

static propTypes = {
addLabel: PropTypes.bool.isRequired,
classes: PropTypes.object,
Expand Down Expand Up @@ -56,6 +58,10 @@ export class RichTextInput extends Component {
this.quill.on('text-change', debounce(this.onTextChange, 500));
}

shouldComponentUpdate(nextProps) {
return nextProps.input.value !== this.lastValueChange;
}

componentDidUpdate(prevProps) {
if (prevProps.input.value !== this.props.input.value) {
const selection = this.quill.getSelection();
Expand All @@ -76,6 +82,7 @@ export class RichTextInput extends Component {
onTextChange = () => {
const value =
this.editor.innerHTML == '<p><br></p>' ? '' : this.editor.innerHTML;
this.lastValueChange = value;
this.props.input.onChange(value);
};

Expand All @@ -91,7 +98,7 @@ export class RichTextInput extends Component {
fullWidth={this.props.fullWidth}
className="ra-rich-text-input"
>
<div ref={this.updateDivRef} />
<div data-testid="quill" ref={this.updateDivRef} />
{error && <FormHelperText error>{error}</FormHelperText>}
{helperText && <FormHelperText>{helperText}</FormHelperText>}
</FormControl>
Expand Down
52 changes: 52 additions & 0 deletions packages/ra-input-rich-text/src/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react';
import debounce from 'lodash/debounce';
import {render, fireEvent, waitForElement, cleanup, getByTestId} from 'react-testing-library'

import { RichTextInput } from './index';

let container;

jest.mock('lodash/debounce');
describe('RichTextInput', () => {
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
document.getSelection = () => {
return {
removeAllRanges: () => {},
getRangeAt: function() {},
};
};
jest.mock('lodash.debounce', (fn) => fn);
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

it('should call text-change event only once when editing', async () => {
const mockFn = jest.fn();
debounce.mockImplementation(fn => fn);
const { getByTestId, rerender } = render(
<RichTextInput
input={{
value: '<p>test</p>',
onChange: mockFn
}}
meta={{error: null}} />);
const quillNode = await waitForElement(() => {
return getByTestId('quill')
});
quillNode.__quill.setText('test1');
rerender(
<RichTextInput
input={{
value: '<p>test1</p>',
onChange: mockFn
}}
meta={{error: null}} />)

expect(mockFn).toHaveBeenCalledTimes(1);
});
})
1 change: 1 addition & 0 deletions test-setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('raf/polyfill');
require('mutationobserver-shim');
var enzyme = require('enzyme');
var Adapter = require('enzyme-adapter-react-16');

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10181,6 +10181,11 @@ multimatch@^2.0.0:
arrify "^1.0.0"
minimatch "^3.0.0"

mutationobserver-shim@^0.3.3:
version "0.3.3"
resolved "https://registry.yarnpkg.com/mutationobserver-shim/-/mutationobserver-shim-0.3.3.tgz#65869630bc89d7bf8c9cd9cb82188cd955aacd2b"
integrity sha512-gciOLNN8Vsf7YzcqRjKzlAJ6y7e+B86u7i3KXes0xfxx/nfLmozlW1Vn+Sc9x3tPIePFgc1AeIFhtRgkqTjzDQ==

mute-stream@0.0.7:
version "0.0.7"
resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab"
Expand Down

0 comments on commit 661b8a1

Please sign in to comment.