Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from FormidableLabs/issue/220-no-component-wi…
…ll-mount Move LiveProvider to hooks, fixes #220
- Loading branch information
Showing
7 changed files
with
96 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,89 @@ | ||
import React, { Component } from 'react'; | ||
import React, { useEffect, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import LiveContext from './LiveContext'; | ||
import { generateElement, renderElementAsync } from '../../utils/transpile'; | ||
|
||
export default class LiveProvider extends Component { | ||
static defaultProps = { | ||
code: '', | ||
noInline: false, | ||
language: 'jsx', | ||
disabled: false | ||
}; | ||
function LiveProvider({ | ||
children, | ||
code, | ||
language, | ||
theme, | ||
disabled, | ||
scope, | ||
transformCode, | ||
noInline = false | ||
}) { | ||
const [state, setState] = useState({ | ||
error: undefined, | ||
element: undefined | ||
}); | ||
|
||
static propTypes = { | ||
children: PropTypes.children, | ||
code: PropTypes.string, | ||
disabled: PropTypes.bool, | ||
language: PropTypes.string, | ||
noInline: PropTypes.bool, | ||
scope: PropTypes.object, | ||
theme: PropTypes.object, | ||
transformCode: PropTypes.node | ||
}; | ||
|
||
// eslint-disable-next-line camelcase | ||
UNSAFE_componentWillMount() { | ||
const { code, scope, transformCode, noInline } = this.props; | ||
|
||
this.transpile({ code, scope, transformCode, noInline }); | ||
} | ||
|
||
componentDidUpdate({ | ||
code: prevCode, | ||
scope: prevScope, | ||
noInline: prevNoInline, | ||
transformCode: prevTransformCode | ||
}) { | ||
const { code, scope, noInline, transformCode } = this.props; | ||
if ( | ||
code !== prevCode || | ||
scope !== prevScope || | ||
noInline !== prevNoInline || | ||
transformCode !== prevTransformCode | ||
) { | ||
this.transpile({ code, scope, transformCode, noInline }); | ||
} | ||
} | ||
|
||
onChange = code => { | ||
const { scope, transformCode, noInline } = this.props; | ||
this.transpile({ code, scope, transformCode, noInline }); | ||
}; | ||
|
||
onError = error => { | ||
this.setState({ error: error.toString() }); | ||
}; | ||
|
||
transpile = ({ code, scope, transformCode, noInline = false }) => { | ||
function transpile(newCode) { | ||
// Transpilation arguments | ||
const input = { | ||
code: transformCode ? transformCode(code) : code, | ||
code: transformCode ? transformCode(newCode) : newCode, | ||
scope | ||
}; | ||
|
||
const errorCallback = err => | ||
this.setState({ element: undefined, error: err.toString() }); | ||
const renderElement = element => this.setState({ ...state, element }); | ||
const errorCallback = error => | ||
setState({ error: error.toString(), element: undefined }); | ||
|
||
// State reset object | ||
const state = { unsafeWrapperError: undefined, error: undefined }; | ||
const renderElement = element => setState({ error: undefined, element }); | ||
|
||
try { | ||
if (noInline) { | ||
this.setState({ ...state, element: null }); // Reset output for async (no inline) evaluation | ||
setState({ error: undefined, element: null }); // Reset output for async (no inline) evaluation | ||
renderElementAsync(input, renderElement, errorCallback); | ||
} else { | ||
renderElement(generateElement(input, errorCallback)); | ||
} | ||
} catch (error) { | ||
this.setState({ ...state, error: error.toString() }); | ||
errorCallback(error); | ||
} | ||
}; | ||
} | ||
|
||
render() { | ||
const { children, code, language, theme, disabled } = this.props; | ||
useEffect(() => { | ||
transpile(code); | ||
}, [code, scope, noInline, transformCode]); | ||
|
||
return ( | ||
<LiveContext.Provider | ||
value={{ | ||
...this.state, | ||
code, | ||
language, | ||
theme, | ||
disabled, | ||
onError: this.onError, | ||
onChange: this.onChange | ||
}} | ||
> | ||
{children} | ||
</LiveContext.Provider> | ||
); | ||
} | ||
const onChange = newCode => transpile(newCode); | ||
|
||
const onError = error => setState({ error: error.toString() }); | ||
|
||
return ( | ||
<LiveContext.Provider | ||
value={{ | ||
...state, | ||
code, | ||
language, | ||
theme, | ||
disabled, | ||
onError, | ||
onChange | ||
}} | ||
> | ||
{children} | ||
</LiveContext.Provider> | ||
); | ||
} | ||
|
||
LiveProvider.propTypes = { | ||
children: PropTypes.node, | ||
code: PropTypes.string, | ||
disabled: PropTypes.bool, | ||
language: PropTypes.string, | ||
noInline: PropTypes.bool, | ||
scope: PropTypes.object, | ||
theme: PropTypes.object, | ||
transformCode: PropTypes.func | ||
}; | ||
|
||
LiveProvider.defaultProps = { | ||
code: '', | ||
noInline: false, | ||
language: 'jsx', | ||
disabled: false | ||
}; | ||
|
||
export default LiveProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters