Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onError prop to LiveProvider #163

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ It supports these props, while passing any others through to the `children`:
|scope|PropTypes.object|Accepts custom globals that the `code` can use
|noInline|PropTypes.bool|Doesn’t evaluate and mount the inline code (Default: `false`). Note: when using `noInline` whatever code you write must be a single expression (function, class component or some `jsx`) that can be returned immediately. If you'd like to render multiple components, use `noInline={true}`
|transformCode|PropTypes.func|Accepts and returns the code to be transpiled, affording an opportunity to first transform it
|onError|PropTypes.func|Called with `Error` object when render fails. Allows you to listen for errors and implement custom logic.|
|onRender|PropTypes.func|Called when component is rendered successfully. Can be used alongside `onError` to clear own error state.|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could use onError for this same purpose? If onRender is called really only when render is called successfully (presumably after error) could we instead just call onError again with the default state such as { error: undefined }. If I understand the point of this prop correctly I could imagine just resetting the error state would be sufficient for clearing any external error states?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's definitely doable!

I'm thinking it might be a tad confusing to call it onError when it doesn't explicitly emit errors though. Maybe having the function signature be onRender(error?: Error) => void would make more sense? From first glance you'd be able to see what the event is about, and that it can emit both error and non-error state.

Let me know what you'd prefer and I'll make the updates. 🙂

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about onChange(code: string, error?: Error)

|language|PropTypes.string|What language you're writing for correct syntax highlighting. (Default: `jsx`)
|disabled|PropTypes.bool|Disable editing on the `<LiveEditor />` (Default: `false`)
|theme|PropTypes.object|A `prism-react-renderer` theme object. See more [here](https://github.com/FormidableLabs/prism-react-renderer#theming)
Expand Down Expand Up @@ -243,5 +245,5 @@ Here are the various factors at play:

## Maintenance Status

**Active:** Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.
**Active:** Formidable is actively working on this project, and we expect to continue for work for the foreseeable future. Bug reports, feature requests and pull requests are welcome.

21 changes: 19 additions & 2 deletions src/components/Live/LiveProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export default class LiveProvider extends Component {
disabled: PropTypes.bool,
language: PropTypes.string,
noInline: PropTypes.bool,
onError: PropTypes.func,
onRender: PropTypes.func,
scope: PropTypes.object,
theme: PropTypes.object,
transformCode: PropTypes.node
Expand Down Expand Up @@ -54,6 +56,9 @@ export default class LiveProvider extends Component {

onError = error => {
this.setState({ error: error.toString() });
if (this.props.onError) {
this.props.onError(error);
}
};

transpile = ({ code, scope, transformCode, noInline = false }) => {
Expand All @@ -63,9 +68,18 @@ export default class LiveProvider extends Component {
scope
};

const errorCallback = err =>
const errorCallback = err => {
this.setState({ element: undefined, error: err.toString() });
const renderElement = element => this.setState({ ...state, element });
if (this.props.onError) {
this.props.onError(err);
}
};
const renderElement = element => {
this.setState({ ...state, element });
if (this.props.onRender) {
this.props.onRender();
}
};

// State reset object
const state = { unsafeWrapperError: undefined, error: undefined };
Expand All @@ -79,6 +93,9 @@ export default class LiveProvider extends Component {
}
} catch (error) {
this.setState({ ...state, error: error.toString() });
if (this.props.onError) {
this.props.onError();
}
}
};

Expand Down
4 changes: 3 additions & 1 deletion typings/react-live.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ type DivProps = HTMLProps<HTMLDivElement>
type PreProps = HTMLProps<HTMLPreElement>

// LiveProvider
export type LiveProviderProps = Omit<DivProps, 'scope'> & {
export type LiveProviderProps = Omit<DivProps, 'scope' | 'onError'> & {
scope?: { [key: string]: any };
code?: string;
noInline?: boolean;
transformCode?: (code: string) => string;
language?: Language;
disabled?: boolean;
theme?: PrismTheme;
onError?: (error: Error) => void;
onRender?: () => void;
}

export const LiveProvider: ComponentClass<LiveProviderProps>
Expand Down