Files
test-repo/website/playground/PrettierFormat.js
T

66 lines
1.1 KiB
JavaScript

import * as React from "react";
export default class PrettierFormat extends React.Component {
constructor() {
super();
this.state = { formatted: "", debug: {} };
}
componentDidMount() {
this.format();
}
componentDidUpdate(prevProps) {
for (const key of [
"enabled",
"code",
"options",
"debugAst",
"debugPreprocessedAst",
"debugDoc",
"debugComments",
"reformat",
"rethrowEmbedErrors",
]) {
if (prevProps[key] !== this.props[key]) {
this.format();
break;
}
}
}
async format() {
const {
enabled,
worker,
code,
options,
debugAst: ast,
debugPreprocessedAst: preprocessedAst,
debugDoc: doc,
debugComments: comments,
reformat,
rethrowEmbedErrors,
} = this.props;
if (!enabled) {
return;
}
const result = await worker.format(code, options, {
ast,
preprocessedAst,
doc,
comments,
reformat,
rethrowEmbedErrors,
});
this.setState(result);
}
render() {
return this.props.children(this.state);
}
}