From a3307765e91932a717281d136f18c38c6549b00d Mon Sep 17 00:00:00 2001 From: Sergio Betanzos Date: Thu, 19 May 2022 13:01:55 +0200 Subject: [PATCH] F #5422: Add accordion wrapper to form sections (#2058) --- .../src/client/components/Cards/NicCard.js | 2 +- .../client/components/Forms/FormWithSchema.js | 144 +++++++++++------- .../src/client/components/Forms/Legend.js | 31 ++-- .../context/configurationSection.js | 7 +- .../context/filesSection.js | 1 + .../Steps/ExtraConfiguration/context/index.js | 2 +- .../src/client/components/Tabs/Common/List.js | 2 +- .../Tabs/MarketplaceApp/Template.js | 2 +- .../components/Tabs/Vm/Configuration.js | 4 +- .../components/Tabs/VmTemplate/Template.js | 2 +- src/fireedge/src/client/theme/defaults.js | 30 ++-- 11 files changed, 134 insertions(+), 93 deletions(-) diff --git a/src/fireedge/src/client/components/Cards/NicCard.js b/src/fireedge/src/client/components/Cards/NicCard.js index e5f0d45bf1..ae8118a38c 100644 --- a/src/fireedge/src/client/components/Cards/NicCard.js +++ b/src/fireedge/src/client/components/Cards/NicCard.js @@ -144,7 +144,7 @@ const NicCard = memo( const rulesById = Object.entries(groupBy(SECURITY_GROUPS, 'ID')) return ( - + diff --git a/src/fireedge/src/client/components/Forms/FormWithSchema.js b/src/fireedge/src/client/components/Forms/FormWithSchema.js index eb976bc604..c6285ed2a6 100644 --- a/src/fireedge/src/client/components/Forms/FormWithSchema.js +++ b/src/fireedge/src/client/components/Forms/FormWithSchema.js @@ -14,11 +14,17 @@ * limitations under the License. * * ------------------------------------------------------------------------- */ /* eslint-disable jsdoc/require-jsdoc */ -import { createElement, useMemo, isValidElement } from 'react' +import { + Fragment, + createElement, + useMemo, + useCallback, + isValidElement, +} from 'react' import PropTypes from 'prop-types' -import { FormControl, Grid } from '@mui/material' import { useFormContext } from 'react-hook-form' +import { FormControl, Accordion, AccordionSummary, Grid } from '@mui/material' import * as FC from 'client/components/FormControl' import Legend from 'client/components/Forms/Legend' @@ -46,6 +52,7 @@ const INPUT_CONTROLLER = { } const FormWithSchema = ({ + accordion = false, id, cy, fields, @@ -59,6 +66,26 @@ const FormWithSchema = ({ const { sx: sxRoot, ...restOfRootProps } = rootProps ?? {} + const RootWrapper = useMemo( + () => + accordion && legend + ? ({ children }) => ( + + {children} + + ) + : Fragment, + [accordion, legend] + ) + + const LegendWrapper = useMemo( + () => (accordion && legend ? AccordionSummary : Fragment), + [accordion, legend] + ) + const getFields = useMemo( () => (typeof fields === 'function' ? fields() : fields), [fields?.length] @@ -66,12 +93,15 @@ const FormWithSchema = ({ if (!getFields || getFields?.length === 0) return null - const addIdToName = (name) => - name.startsWith('$') - ? name.slice(1) // removes character '$' and returns - : id - ? `${id}.${name}` - : name // concat form ID if exists + const addIdToName = useCallback( + (name) => + name.startsWith('$') + ? name.slice(1) // removes character '$' and returns + : id + ? `${id}.${name}` // concat form ID if exists + : name, + [id] + ) return ( - {legend && } - - {getFields?.map?.(({ dependOf, ...attributes }) => { - let valueOfDependField = null - let nameOfDependField = null + + + {legend && ( + + )} + + + {getFields?.map?.(({ dependOf, ...attributes }) => { + let valueOfDependField = null + let nameOfDependField = null - if (dependOf) { - nameOfDependField = Array.isArray(dependOf) - ? dependOf.map(addIdToName) - : addIdToName(dependOf) + if (dependOf) { + nameOfDependField = Array.isArray(dependOf) + ? dependOf.map(addIdToName) + : addIdToName(dependOf) - valueOfDependField = watch(nameOfDependField) - } + valueOfDependField = watch(nameOfDependField) + } - const { name, type, htmlType, grid, ...fieldProps } = Object.entries( - attributes - ).reduce((field, attribute) => { - const [key, value] = attribute - const isNotDependAttribute = NOT_DEPEND_ATTRIBUTES.includes(key) + const { name, type, htmlType, grid, ...fieldProps } = + Object.entries(attributes).reduce((field, attribute) => { + const [key, value] = attribute + const isNotDependAttribute = NOT_DEPEND_ATTRIBUTES.includes(key) - const finalValue = - typeof value === 'function' && - !isNotDependAttribute && - !isValidElement(value()) - ? value(valueOfDependField, formContext) - : value + const finalValue = + typeof value === 'function' && + !isNotDependAttribute && + !isValidElement(value()) + ? value(valueOfDependField, formContext) + : value - return { ...field, [key]: finalValue } - }, {}) + return { ...field, [key]: finalValue } + }, {}) - const dataCy = `${cy}-${name}`.replaceAll('.', '-') - const inputName = addIdToName(name) + const dataCy = `${cy}-${name}`.replaceAll('.', '-') + const inputName = addIdToName(name) - const isHidden = htmlType === INPUT_TYPES.HIDDEN + const isHidden = htmlType === INPUT_TYPES.HIDDEN - if (isHidden) return null + if (isHidden) return null - return ( - INPUT_CONTROLLER[type] && ( - - {createElement(INPUT_CONTROLLER[type], { - control, - cy: dataCy, - formContext, - dependencies: nameOfDependField, - name: inputName, - type: htmlType === false ? undefined : htmlType, - ...fieldProps, - })} - + return ( + INPUT_CONTROLLER[type] && ( + + {createElement(INPUT_CONTROLLER[type], { + control, + cy: dataCy, + formContext, + dependencies: nameOfDependField, + name: inputName, + type: htmlType === false ? undefined : htmlType, + ...fieldProps, + })} + + ) ) - ) - })} - + })} + + ) } FormWithSchema.propTypes = { + accordion: PropTypes.bool, id: PropTypes.string, cy: PropTypes.string, fields: PropTypes.oneOfType([ diff --git a/src/fireedge/src/client/components/Forms/Legend.js b/src/fireedge/src/client/components/Forms/Legend.js index 1e152e290c..487f648c1d 100644 --- a/src/fireedge/src/client/components/Forms/Legend.js +++ b/src/fireedge/src/client/components/Forms/Legend.js @@ -18,24 +18,30 @@ import PropTypes from 'prop-types' import { styled, Typography } from '@mui/material' import AdornmentWithTooltip from 'client/components/FormControl/Tooltip' -import { Translate, labelCanBeTranslated } from 'client/components/HOC' +import { Translate } from 'client/components/HOC' const StyledLegend = styled((props) => ( -))(({ theme, tooltip }) => ({ - marginBottom: '1em', - padding: '0em 1em 0.2em 0.5em', - borderBottom: `2px solid ${theme.palette.secondary.main}`, - ...(!!tooltip && { - display: 'inline-flex', - alignItems: 'center', +))( + ({ theme }) => ({ + padding: '0em 1em 0.2em 0.5em', + borderBottom: `2px solid ${theme.palette.secondary.main}`, }), -})) + ({ ownerState }) => ({ + ...(ownerState.tooltip && { + display: 'inline-flex', + alignItems: 'center', + }), + ...(!ownerState.disableGutters && { + marginBottom: '1em', + }), + }) +) const Legend = memo( - ({ title, tooltip }) => ( - - {labelCanBeTranslated(title) ? : title} + ({ title, tooltip, disableGutters }) => ( + + {!!tooltip && } ), @@ -45,6 +51,7 @@ const Legend = memo( Legend.propTypes = { title: PropTypes.any, tooltip: PropTypes.string, + disableGutters: PropTypes.bool, } Legend.displayName = 'FieldsetLegend' diff --git a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/configurationSection.js b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/configurationSection.js index fa2a113425..face7cf9d9 100644 --- a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/configurationSection.js +++ b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/configurationSection.js @@ -64,12 +64,17 @@ const ConfigurationSection = () => { - diff --git a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/filesSection.js b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/filesSection.js index f0aaedcd6b..129778f9d2 100644 --- a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/filesSection.js +++ b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/filesSection.js @@ -31,6 +31,7 @@ export const SECTION_ID = 'CONTEXT' */ const FilesSection = ({ hypervisor }) => ( FILES_FIELDS(hypervisor)} diff --git a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/index.js b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/index.js index 0cd2fae6f6..3eef08fe7e 100644 --- a/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/index.js +++ b/src/fireedge/src/client/components/Forms/VmTemplate/CreateForm/Steps/ExtraConfiguration/context/index.js @@ -30,8 +30,8 @@ export const TAB_ID = ['CONTEXT', USER_INPUTS_ID] const Context = (props) => ( <> - + ) diff --git a/src/fireedge/src/client/components/Tabs/Common/List.js b/src/fireedge/src/client/components/Tabs/Common/List.js index e761de18f0..2314369a5e 100644 --- a/src/fireedge/src/client/components/Tabs/Common/List.js +++ b/src/fireedge/src/client/components/Tabs/Common/List.js @@ -116,7 +116,7 @@ const AttributeList = ({ return ( - + {/* TITLE */} {title && ( diff --git a/src/fireedge/src/client/components/Tabs/MarketplaceApp/Template.js b/src/fireedge/src/client/components/Tabs/MarketplaceApp/Template.js index b3c93c8da7..0cae876fab 100644 --- a/src/fireedge/src/client/components/Tabs/MarketplaceApp/Template.js +++ b/src/fireedge/src/client/components/Tabs/MarketplaceApp/Template.js @@ -43,7 +43,7 @@ const AppTemplateTab = ({ id }) => { return ( <> - + }> diff --git a/src/fireedge/src/client/components/Tabs/Vm/Configuration.js b/src/fireedge/src/client/components/Tabs/Vm/Configuration.js index 75ba476d62..b75b808a57 100644 --- a/src/fireedge/src/client/components/Tabs/Vm/Configuration.js +++ b/src/fireedge/src/client/components/Tabs/Vm/Configuration.js @@ -34,7 +34,7 @@ const VmConfigurationTab = ({ id }) => { return (
- + @@ -46,7 +46,7 @@ const VmConfigurationTab = ({ id }) => { - + diff --git a/src/fireedge/src/client/components/Tabs/VmTemplate/Template.js b/src/fireedge/src/client/components/Tabs/VmTemplate/Template.js index 0d9a6b2d68..8b997522dc 100644 --- a/src/fireedge/src/client/components/Tabs/VmTemplate/Template.js +++ b/src/fireedge/src/client/components/Tabs/VmTemplate/Template.js @@ -30,7 +30,7 @@ const TemplateTab = ({ id }) => { const { data: vmTemplate = {} } = useGetTemplateQuery({ id }) return ( - + { }, }, MuiPaper: { + defaultProps: { + elevation: 0, + }, styleOverrides: { root: { backgroundImage: 'unset' }, }, + variants: [ + { + props: { variant: 'transparent' }, + style: { backgroundColor: 'transparent' }, + }, + ], }, MuiButtonBase: { defaultProps: { @@ -461,39 +470,18 @@ const createAppTheme = (appTheme, mode = SCHEMES.DARK) => { dense: true, }, }, - MuiChip: { - variants: [ - { - props: { variant: 'text' }, - style: { - border: 0, - backgroundColor: 'transparent', - }, - }, - ], - }, MuiAccordion: { defaultProps: { - elevation: 0, - square: true, disableGutters: true, TransitionProps: { unmountOnExit: true }, }, styleOverrides: { root: { flexBasis: '100%', - border: `1px solid ${defaultTheme.palette.divider}`, '&:before': { display: 'none' }, }, }, }, - MuiAccordionDetails: { - styleOverrides: { - root: { - borderTop: `1px solid ${defaultTheme.palette.divider}`, - }, - }, - }, MuiAccordionSummary: { defaultProps: { expandIcon: ,