Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.

onUpdate prop can be passed to Playground. #131

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,19 @@ Annotation map for the docClass. They key is the prop to annotate, the value is
scope={{React}}
/>
```

### `onUpdate`
*PropTypes.func*

A function which gets called whenever the code updates after the initial render. The callback is fired with a single hash argument which looks like:

```js
{
code,
evalError
}
```

`code` is the new code text.

`evalError` will either be `null` (if there was no error) or the error object caught during attempted compilation and execution of the new code.
5 changes: 4 additions & 1 deletion src/components/es6-preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ class EsPreview extends Component {

static propTypes = {
code: PropTypes.string.isRequired,
scope: PropTypes.object.isRequired
scope: PropTypes.object.isRequired,
onError: PropTypes.func.isRequired
};

_compileCode = () => {
Expand Down Expand Up @@ -152,7 +153,9 @@ class EsPreview extends Component {
}
}
render(<Comp />, mountNode);
this.props.onError(null);
} catch (err) {
this.props.onError(err);
this._setTimeout(() => {
render(
<div className="playgroundError">{err.toString()}</div>,
Expand Down
20 changes: 19 additions & 1 deletion src/components/playground.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class ReactPlayground extends Component {
es6Console: PropTypes.bool,
context: PropTypes.object,
initiallyExpanded: PropTypes.bool,
previewComponent: PropTypes.node
previewComponent: PropTypes.node,
onUpdate: PropTypes.func
};

state = {
Expand All @@ -46,13 +47,28 @@ class ReactPlayground extends Component {
});
};

componentDidUpdate = (prevProps, prevState) => {
if (this.state.code !== prevState.code && this.props.onUpdate) {
this.props.onUpdate({
code: this.state.code,
evalError: this.previewError
});
}
};

_handleCodeChange = (code) => {
this.setState({
code,
external: false
});
};

_handlePreviewError = (error) => {
// error may be an error object, or null (if there was no problem).
// this callback is expected to run before componentDidUpdate.
this.previewError = error;
};

_toggleCode = () => {
this.setState({
expandedCode: !this.state.expandedCode
Expand Down Expand Up @@ -107,13 +123,15 @@ class ReactPlayground extends Component {
<EsPreview
code={code}
scope={scope}
onError={this._handlePreviewError}
/> :
<Preview
context={context}
code={code}
scope={scope}
noRender={noRender}
previewComponent={previewComponent}
onError={this._handlePreviewError}
/>
}
</div>
Expand Down
3 changes: 3 additions & 0 deletions src/components/preview.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Preview extends Component {
scope: PropTypes.object.isRequired,
previewComponent: PropTypes.node,
noRender: PropTypes.bool,
onError: PropTypes.func.isRequired,
context: PropTypes.object
};

Expand Down Expand Up @@ -90,10 +91,12 @@ class Preview extends Component {
}
/* eslint-enable no-eval, max-len */
clearTimeout(this.timeoutID);
this.props.onError(null);
this.setState({ error: null });
} catch (err) {
const error = err.toString();
clearTimeout(this.timeoutID); //eslint-disable-line no-undef
this.props.onError(err);
this.timeoutID = setTimeout(() => {
this.setState({ error });
}, 500);
Expand Down