Skip to content

Commit

Permalink
Controlled: support transforming value
Browse files Browse the repository at this point in the history
  • Loading branch information
Hypnosphi committed Jul 24, 2019
1 parent a633e7d commit 49b0dcc
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 18 deletions.
9 changes: 7 additions & 2 deletions .ts/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/app.min.js

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 30 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
});
}
if (!this.hydrated) {
this.deferred ? this.resolveChange() : this.initChange(props.value || '')
this.deferred ? this.resolveChange(props.value) : this.initChange(props.value || '')
}
this.hydrated = true;
}
Expand All @@ -442,7 +442,7 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
}

/** @internal */
private resolveChange() {
private resolveChange(value) {

this.emulating = true;

Expand All @@ -456,6 +456,12 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
doc.replaceRange(this.deferred.text, this.deferred.from, this.deferred.to, this.deferred.origin);
}

if (value && value !== doc.getValue()) {
const cursor = doc.getCursor();
doc.setValue(value);
doc.setCursor(cursor);
}

this.emulating = false;
this.deferred = null;
}
Expand Down
17 changes: 17 additions & 0 deletions test/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,23 @@ describe('Change', () => {
const doc = wrapper.instance().editor.getDoc();
doc.replaceRange('bar', {line: 1, ch: 1});
});

it('[Controlled] transform value', done => {
const wrapper = Enzyme.mount(
<Controlled
value='foo'
onBeforeChange={(editor, data, value) => {
wrapper.setProps({value: value.replace(/o/g, 'p')});
}}
onChange={(editor, data, value) => {
expect(value).toEqual('fppfpp');
expect(editor.getValue()).toEqual('fppfpp');
done();
}}
/>);
const doc = wrapper.instance().editor.getDoc();
doc.replaceRange('foo', {line: 1, ch: 1});
});
});

describe('Props', () => {
Expand Down

0 comments on commit 49b0dcc

Please sign in to comment.