diff --git a/superset-frontend/src/SqlLab/components/TemplateParamsEditor.jsx b/superset-frontend/src/SqlLab/components/TemplateParamsEditor.jsx index 863e994c1..20e80a171 100644 --- a/superset-frontend/src/SqlLab/components/TemplateParamsEditor.jsx +++ b/superset-frontend/src/SqlLab/components/TemplateParamsEditor.jsx @@ -16,11 +16,12 @@ * specific language governing permissions and limitations * under the License. */ -import React from 'react'; +import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import Badge from 'src/common/components/Badge'; import { t, styled } from '@superset-ui/core'; import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls'; +import { debounce } from 'lodash'; import ModalTrigger from 'src/components/ModalTrigger'; import { ConfigEditor } from 'src/components/AsyncAceEditor'; @@ -42,49 +43,29 @@ const StyledConfigEditor = styled(ConfigEditor)` } `; -export default class TemplateParamsEditor extends React.Component { - constructor(props) { - super(props); - const codeText = props.code || '{}'; - this.state = { - codeText, - parsedJSON: null, - isValid: true, - }; - this.onChange = this.onChange.bind(this); - } +function TemplateParamsEditor({ code, language, onChange }) { + const [parsedJSON, setParsedJSON] = useState(); + const [isValid, setIsValid] = useState(true); - componentDidMount() { - this.onChange(this.state.codeText); - } - - onChange(value) { - const codeText = value; - let isValid; - let parsedJSON = {}; + useEffect(() => { try { - parsedJSON = JSON.parse(value); - isValid = true; - } catch (e) { - isValid = false; + setParsedJSON(JSON.parse(code)); + setIsValid(true); + } catch { + setParsedJSON({}); + setIsValid(false); } - this.setState({ parsedJSON, isValid, codeText }); - const newValue = isValid ? codeText : '{}'; - if (newValue !== this.props.code) { - this.props.onChange(newValue); - } - } + }, [code]); - renderDoc() { - return ( + const modalBody = ( +
- {t('Assign a set of parameters as')}
+ Assign a set of parameters as
JSON
- {t('below (example:')}
+ below (example:
{'{"my_table": "foo"}'}
- {t('), and they become available in your SQL (example:')}
- SELECT * FROM {'{{ my_table }}'}
- {t(') by using')}
+ ), and they become available in your SQL (example:
+ SELECT * FROM {'{{ my_table }}'} ) by using
{' '}
syntax.