From 58b0571edf2327911cde48f142720a4c440bf6e0 Mon Sep 17 00:00:00 2001 From: Tushar Balar Date: Thu, 15 Apr 2021 13:39:15 +0000 Subject: [PATCH] feat: Custom menu implementation --- ui/src/main/webapp/components/CustomMenu.jsx | 70 ++++++++++++++++++++ ui/src/main/webapp/pages/Input/InputPage.jsx | 22 +++++- ui/src/main/webapp/pages/style.scss | 2 +- 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 ui/src/main/webapp/components/CustomMenu.jsx diff --git a/ui/src/main/webapp/components/CustomMenu.jsx b/ui/src/main/webapp/components/CustomMenu.jsx new file mode 100644 index 000000000..238887d20 --- /dev/null +++ b/ui/src/main/webapp/components/CustomMenu.jsx @@ -0,0 +1,70 @@ +import React, { Component } from 'react'; +import PropTypes from 'prop-types'; +import { _ } from '@splunk/ui-utils/i18n'; +import { getUnifiedConfigs } from '../util/util'; + +class CustomMenu extends Component { + constructor(props) { + super(props); + this.state = { + loading: true, + }; + this.shouldRender = true; + } + + componentDidMount() { + const globalConfig = getUnifiedConfigs(); + this.setState({ loading: true }); + this.loadCustomControl().then((Control) => { + const customControl = new Control(globalConfig, this.el, this.setValue); + customControl.render(); + this.setState({ loading: false }); + }); + } + + shouldComponentUpdate(nextProps, nextState) { + if (!nextState.loading && this.shouldRender) { + this.shouldRender = false; + return true; + } + return false; + } + + setValue = (newValue) => { + this.props.handleChange(newValue); + }; + + loadCustomControl = () => { + const globalConfig = getUnifiedConfigs(); + const appName = globalConfig.meta.name; + return new Promise((resolve) => { + __non_webpack_require__( + [`app/${appName}/js/build/custom/${this.props.fileName}`], + (Control) => resolve(Control) + ); + }); + }; + + render() { + return ( + <> + {this.state.loading && _('Loading...')} + { + { + this.el = el; + }} + style={{ visibility: this.state.loading ? 'hidden' : 'visible' }} + /> + } + + ); + } +} + +CustomMenu.propTypes = { + fileName: PropTypes.string.isRequired, + handleChange: PropTypes.func, +}; + +export default CustomMenu; diff --git a/ui/src/main/webapp/pages/Input/InputPage.jsx b/ui/src/main/webapp/pages/Input/InputPage.jsx index 80990b865..acbd6f44c 100644 --- a/ui/src/main/webapp/pages/Input/InputPage.jsx +++ b/ui/src/main/webapp/pages/Input/InputPage.jsx @@ -15,6 +15,7 @@ import { TableContextProvider } from '../../context/TableContext'; import { MODE_CREATE, MODE_CLONE, MODE_EDIT } from '../../constants/modes'; import { PAGE_INPUT } from '../../constants/pages'; import { STYLE_PAGE } from '../../constants/dialogStyles'; +import CustomMenu from '../../components/CustomMenu'; import TableWrapper from '../../components/table/TableWrapper'; import EntityModal from '../../components/EntityModal'; import ErrorBoundary from '../../components/ErrorBoundary'; @@ -39,7 +40,7 @@ function InputPage() { const [entity, setEntity] = useState({ open: false }); const unifiedConfigs = getUnifiedConfigs(); - const { services, title, description } = unifiedConfigs.pages.inputs; + const { services, title, description, menu: customMenuField } = unifiedConfigs.pages.inputs; const toggle =