Skip to content

Commit a439235

Browse files
Niklas Kieferfake-join[bot]
authored andcommitted
feat(playground): emit formPlayground.rendered event
1 parent 64edf52 commit a439235

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

packages/form-js-playground/src/Playground.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default function Playground(options) {
5959
data={ data }
6060
onStateChanged={ (_state) => state = _state }
6161
onInit={ _ref => ref = _ref }
62+
emit={ emitter.emit }
6263
{ ...rest }
6364
/>,
6465
container
@@ -85,6 +86,10 @@ export default function Playground(options) {
8586
return ref.setSchema(schema);
8687
};
8788

89+
this.get = function(name, strict) {
90+
return ref.get(name, strict);
91+
};
92+
8893
this.destroy = function() {
8994
this.emit('destroy');
9095
};

packages/form-js-playground/src/components/PlaygroundRoot.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import './PlaygroundRoot.css';
2424
export function PlaygroundRoot(props) {
2525

2626
const {
27-
editor: editorConfig = {}
27+
editor: editorConfig = {},
28+
emit
2829
} = props;
2930

3031
const {
@@ -53,8 +54,10 @@ export function PlaygroundRoot(props) {
5354

5455
const [ resultData, setResultData ] = useState(props.data || {});
5556

57+
// pipe to playground API
5658
useEffect(() => {
5759
props.onInit({
60+
get: (name, strict) => formEditorRef.current.get(name, strict),
5861
setSchema: setInitialSchema
5962
});
6063
});
@@ -90,6 +93,12 @@ export function PlaygroundRoot(props) {
9093
setSchema(formEditor.getSchema());
9194
});
9295

96+
formEditor.on('formEditor.rendered', () => {
97+
98+
// notifiy interested parties after render
99+
emit('formPlayground.rendered');
100+
});
101+
93102
form.on('changed', event => {
94103
setResultData(event.data);
95104
});

packages/form-js-playground/test/spec/Playground.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,30 @@ describe('playground', function() {
181181

182182
// then
183183
expect(playground.getState().schema).to.deep.include(otherSchema);
184+
});
185+
186+
187+
it('should emit <formPlayground.rendered>', async function() {
188+
189+
// given
190+
const spy = sinon.spy();
184191

192+
await act(() => {
193+
playground = new Playground({
194+
container,
195+
schema
196+
});
197+
});
198+
199+
playground.on('formPlayground.rendered', spy);
200+
201+
const eventBus = playground.get('eventBus');
202+
203+
// when
204+
eventBus.fire('formEditor.rendered');
205+
206+
// then
207+
expect(spy).to.have.been.called;
185208
});
186209

187210
});

0 commit comments

Comments
 (0)