Skip to content

Commit 49b0dcc

Browse files
committed
Controlled: support transforming value
1 parent a633e7d commit 49b0dcc

File tree

6 files changed

+71
-18
lines changed

6 files changed

+71
-18
lines changed

.ts/index.js

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/app.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
416416
});
417417
}
418418
if (!this.hydrated) {
419-
this.deferred ? this.resolveChange() : this.initChange(props.value || '')
419+
this.deferred ? this.resolveChange(props.value) : this.initChange(props.value || '')
420420
}
421421
this.hydrated = true;
422422
}
@@ -442,7 +442,7 @@ export class Controlled extends React.Component<IControlledCodeMirror, any> {
442442
}
443443

444444
/** @internal */
445-
private resolveChange() {
445+
private resolveChange(value) {
446446

447447
this.emulating = true;
448448

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

459+
if (value && value !== doc.getValue()) {
460+
const cursor = doc.getCursor();
461+
doc.setValue(value);
462+
doc.setCursor(cursor);
463+
}
464+
459465
this.emulating = false;
460466
this.deferred = null;
461467
}

test/index.spec.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,23 @@ describe('Change', () => {
362362
const doc = wrapper.instance().editor.getDoc();
363363
doc.replaceRange('bar', {line: 1, ch: 1});
364364
});
365+
366+
it('[Controlled] transform value', done => {
367+
const wrapper = Enzyme.mount(
368+
<Controlled
369+
value='foo'
370+
onBeforeChange={(editor, data, value) => {
371+
wrapper.setProps({value: value.replace(/o/g, 'p')});
372+
}}
373+
onChange={(editor, data, value) => {
374+
expect(value).toEqual('fppfpp');
375+
expect(editor.getValue()).toEqual('fppfpp');
376+
done();
377+
}}
378+
/>);
379+
const doc = wrapper.instance().editor.getDoc();
380+
doc.replaceRange('foo', {line: 1, ch: 1});
381+
});
365382
});
366383

367384
describe('Props', () => {

0 commit comments

Comments
 (0)