From 07cbbfd7ab6981ed76c5016cb7c190412496b7ef Mon Sep 17 00:00:00 2001 From: Alejandro Date: Tue, 20 Oct 2026 09:47:38 +0200 Subject: [PATCH] F OpenNebula/one#7441: Add new Card Button component (#4572) Add new Card Buttons to Toggle controller --- .../Forms/FormControl/ToggleController.js | 130 ++++++++++-------- .../primitives/Buttons/Card/index.js | 118 ++++++++++++++++ .../primitives/Buttons/Card/styles.js | 96 +++++++++++++ .../components/primitives/Buttons/index.js | 1 + .../Steps/General/informationSchema.js | 1 + 5 files changed, 286 insertions(+), 60 deletions(-) create mode 100644 src/fireedge/src/modules/components/primitives/Buttons/Card/index.js create mode 100644 src/fireedge/src/modules/components/primitives/Buttons/Card/styles.js diff --git a/src/fireedge/src/modules/components/composed/Forms/FormControl/ToggleController.js b/src/fireedge/src/modules/components/composed/Forms/FormControl/ToggleController.js index 9ee651573c..238b41534c 100644 --- a/src/fireedge/src/modules/components/composed/Forms/FormControl/ToggleController.js +++ b/src/fireedge/src/modules/components/composed/Forms/FormControl/ToggleController.js @@ -16,17 +16,10 @@ import { memo, useCallback, useEffect } from 'react' import PropTypes from 'prop-types' import { TEXT_VARIANTS, TEXT_WEIGHTS } from '@ConstantsModule' -import { - styled, - FormControl, - ToggleButtonGroup, - ToggleButton, - FormHelperText, - Box, - alpha, -} from '@mui/material' +import { styled, FormControl, FormHelperText, Box } from '@mui/material' import { useController } from 'react-hook-form' import { Text } from '@modules/components/primitives/Text' +import { CardButton } from '@modules/components/primitives/Buttons/Card' import { ErrorHelper } from '@modules/components/composed/Forms/FormControl/ErrorHelper' import { AdornmentWithTooltip as Tooltip } from '@modules/components/composed/Forms/FormControl/Tooltip' import { generateKey } from '@UtilsModule' @@ -65,14 +58,23 @@ export const ToggleController = memo( useEffect(() => { if (optionSelected) { - const exists = values?.find((option) => option.value === optionSelected) + const selectedValues = Array.isArray(optionSelected) + ? optionSelected + : [optionSelected] + const exists = selectedValues.every((selectedValue) => + values?.some((option) => option.value === selectedValue) + ) !exists && onChange() } }, []) const handleChange = useCallback( - (_, newValues) => { + (newValues) => { onBlur() - if (!readOnly && (!notNull || newValues)) { + const hasSelection = Array.isArray(newValues) + ? newValues.length > 0 + : newValues !== null && newValues !== undefined + + if (!readOnly && (!notNull || hasSelection)) { onChange(newValues) if (typeof onConditionChange === 'function') { onConditionChange(newValues) @@ -82,6 +84,29 @@ export const ToggleController = memo( [onChange, onConditionChange, readOnly, notNull] ) + const handleCardChange = useCallback( + (optionValue) => { + const selectedValues = Array.isArray(optionSelected) + ? optionSelected + : [] + + const isSelected = multiple + ? selectedValues.includes(optionValue) + : optionSelected === optionValue + + const newValues = multiple + ? isSelected + ? selectedValues.filter((value) => value !== optionValue) + : [...selectedValues, optionValue] + : isSelected + ? null + : optionValue + + handleChange(newValues) + }, + [handleChange, multiple, optionSelected] + ) + // Safe loading of default value useEffect(() => { if ( @@ -106,52 +131,41 @@ export const ToggleController = memo( {tooltip && } )} - ({ + display: 'flex', + gap: `${theme.scale[500]}px`, + })} > - {values?.map(({ text, value = '', description }) => ( - ({ - p: 1, - '&.Mui-selected': { - borderColor: theme.palette.primary.main, - backgroundColor: alpha(theme.palette.primary.main, 0.18), - boxShadow: `inset 0 0 0 1px ${theme.palette.primary.main}`, - color: theme.palette.primary.main, - }, - '&.Mui-selected:hover': { - backgroundColor: alpha(theme.palette.primary.main, 0.26), - }, - })} - > - - - - {description && ( - - )} - - - ))} - + {values?.map( + ( + { text, value = '', description, startIcon, isDisabled = false }, + index + ) => ( + handleCardChange(value)} + /> + ) + )} + {Boolean(message) && ( @@ -159,11 +173,7 @@ export const ToggleController = memo( )} ) - }, - (prevProps, nextProps) => - prevProps.values.length === nextProps.values.length && - prevProps.label === nextProps.label && - prevProps.tooltip === nextProps.tooltip + } ) ToggleController.propTypes = { diff --git a/src/fireedge/src/modules/components/primitives/Buttons/Card/index.js b/src/fireedge/src/modules/components/primitives/Buttons/Card/index.js new file mode 100644 index 0000000000..f6c6c4513d --- /dev/null +++ b/src/fireedge/src/modules/components/primitives/Buttons/Card/index.js @@ -0,0 +1,118 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2026, OpenNebula Project, OpenNebula Systems * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); you may * + * not use this file except in compliance with the License. You may obtain * + * a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * ------------------------------------------------------------------------- */ + +import { Box } from '@mui/material' +import { Component, forwardRef } from 'react' +import PropTypes from 'prop-types' +import clsx from 'clsx' +import { getStyles } from '@modules/components/primitives/Buttons/Card/styles' +import { Text } from '@modules/components/primitives/Text' +import { Tooltip } from '@modules/components/primitives/Tooltip/Default' +import { renderIcon } from '@UtilsModule' + +/** + * Card-shaped selectable button. + * + * @param {object} root0 - Params + * @param {string|Component} root0.text - Button text + * @param {string|Component} root0.description - Optional supporting text + * @param {Component} root0.startIcon - Icon displayed above the text + * @param {string} root0.startIconSize - Size of the start icon (either 'small' or 'big') + * @param {*} root0.value - Option value + * @param {boolean} root0.isSelected - Whether the button is selected + * @param {boolean} root0.isDisabled - Whether the button is disabled + * @param {Function} root0.onClick - Click handler + * @returns {Component} Card button component + */ +export const CardButton = forwardRef( + ( + { + text = '', + description, + startIcon, + startIconSize = 'small', + value, + isSelected = false, + isDisabled = false, + onClick, + className, + ...opts + }, + ref + ) => { + const handleClick = (event) => { + if (isDisabled) return + + onClick?.(event, value) + } + + return ( + + + getStyles({ theme })} + {...opts} + > + {startIcon && ( + + {renderIcon(startIcon)} + + )} + + {text && ( + + )} + + + + ) + } +) + +CardButton.propTypes = { + text: PropTypes.node, + description: PropTypes.node, + startIcon: PropTypes.oneOfType([PropTypes.node, PropTypes.elementType]), + startIconSize: PropTypes.oneOf(['small', 'big']), + value: PropTypes.any, + isSelected: PropTypes.bool, + isDisabled: PropTypes.bool, + isReadOnly: PropTypes.bool, + onClick: PropTypes.func, + className: PropTypes.string, +} + +CardButton.displayName = 'CardButton' diff --git a/src/fireedge/src/modules/components/primitives/Buttons/Card/styles.js b/src/fireedge/src/modules/components/primitives/Buttons/Card/styles.js new file mode 100644 index 0000000000..aff5b7a846 --- /dev/null +++ b/src/fireedge/src/modules/components/primitives/Buttons/Card/styles.js @@ -0,0 +1,96 @@ +/* ------------------------------------------------------------------------- * + * Copyright 2002-2026, OpenNebula Project, OpenNebula Systems * + * * + * Licensed under the Apache License, Version 2.0 (the "License"); you may * + * not use this file except in compliance with the License. You may obtain * + * a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, software * + * distributed under the License is distributed on an "AS IS" BASIS, * + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * + * See the License for the specific language governing permissions and * + * limitations under the License. * + * ------------------------------------------------------------------------- */ + +/** + * @param {object} root0 - Params + * @param {object} root0.theme - Current theme in use + * @returns {object} - Button SX style + */ +export const getStyles = ({ theme }) => { + const icon = { + color: theme.palette.icon.primary, + + '& svg': { + width: `${theme.scale[600]}px`, + height: `${theme.scale[600]}px`, + strokeWidth: 1.5, + }, + + '&.big svg': { + width: `${theme.scale[800]}px`, + height: `${theme.scale[800]}px`, + }, + } + + const text = { + textAlign: 'center', + color: theme.palette.text.body, + fontSize: { + xs: theme.fontSize.body.sm.mobile, + sm: theme.fontSize.body.sm.tablet, + md: theme.fontSize.body.sm.desktop, + }, + fontWeight: 500, + lineHeight: { + xs: theme.lineHeight.body.sm.mobile, + sm: theme.lineHeight.body.sm.tablet, + md: theme.lineHeight.body.sm.desktop, + }, + } + + return { + cursor: 'pointer', + display: 'flex', + minWidth: `${theme.scale[1500]}px`, + flexDirection: 'column', + alignItems: 'center', + gap: `${theme.scale[200]}px`, + padding: `${theme.scale[150]}px ${theme.scale[400]}px`, + borderRadius: `${theme.borderRadius.xlg}px`, + border: `${theme.borderWidth.sm}px solid ${theme.palette.border.primary}`, + backgroundColor: theme.palette.surface.primary, + + '& .card-button-icon': icon, + '& .card-button-text': text, + + '&:hover': { + backgroundColor: theme.palette.surface.actionHover4, + }, + + '&.disabled': { + backgroundColor: theme.palette.surface.actionHover4, + + '& .card-button-icon': { + color: theme.palette.icon.onDisabled, + }, + + '& .card-button-text': { + color: theme.palette.text.onDisabled, + }, + }, + + '&:active': { + backgroundColor: theme.palette.surface.primary, + outlineOffset: `${theme.borderWidth.sm}px`, + outline: `${theme.borderWidth.sm}px solid ${theme.palette.border.focus2}`, + }, + + '&.selected': { + padding: `${theme.scale[150] - 1}px ${theme.scale[400] - 1}px`, + border: `${theme.borderWidth.md}px solid ${theme.palette.border.focus2}`, + }, + } +} diff --git a/src/fireedge/src/modules/components/primitives/Buttons/index.js b/src/fireedge/src/modules/components/primitives/Buttons/index.js index e8de8e48e7..b05441e548 100644 --- a/src/fireedge/src/modules/components/primitives/Buttons/index.js +++ b/src/fireedge/src/modules/components/primitives/Buttons/index.js @@ -14,6 +14,7 @@ * limitations under the License. * * ------------------------------------------------------------------------- */ +export * from '@modules/components/primitives/Buttons/Card' export * from '@modules/components/primitives/Buttons/Checkbox' export * from '@modules/components/primitives/Buttons/Default' export * from '@modules/components/primitives/Buttons/Group' diff --git a/src/fireedge/src/modules/resources/VmTemplate/Forms/CreateForm/Steps/General/informationSchema.js b/src/fireedge/src/modules/resources/VmTemplate/Forms/CreateForm/Steps/General/informationSchema.js index da6f9d5a67..bd0da24f14 100644 --- a/src/fireedge/src/modules/resources/VmTemplate/Forms/CreateForm/Steps/General/informationSchema.js +++ b/src/fireedge/src/modules/resources/VmTemplate/Forms/CreateForm/Steps/General/informationSchema.js @@ -61,6 +61,7 @@ export const DESCRIPTION = { export const HYPERVISOR_FIELD = (isUpdate) => ({ name: 'HYPERVISOR', type: INPUT_TYPES.TOGGLE, + notNull: true, values: arrayToOptions(Object.values(HYPERVISORS), { addEmpty: false, getText: (hypervisor) => hypervisor.toUpperCase(),