Merge pull request #1173 from bluewave-labs/fix/fe/field-refactor

Fix/fe/field refactor
This commit is contained in:
Alexander Holliday authored and GitHub committed 2024-11-21 17:33:18 -08:00
commit a84916f0b0
2 files changed
+83 -73

No files matched your search

+28 -18
View File
@@ -7,23 +7,31 @@ import Visibility from "@mui/icons-material/Visibility";
import "./index.css"; import "./index.css";
/** /**
* Field component for rendering various types of input fields with customizable properties
*
* @param {Object} props * @param {Object} props
* @param {string} [props.type] - Type of input field (e.g., 'text', 'password'). * @param {string} [props.type='text'] - Type of input field (text, password, url, email, description, number).
* @param {string} props.id - ID of the input field. * @param {string} props.id - Unique identifier for the input field.
* @param {string} props.name - Name of the input field. * @param {string} props.name - Name attribute for the input field.
* @param {string} [props.label] - Label for the input field. * @param {string} [props.label] - Label text displayed above the input field.
* @param {boolean} [props.https] - Indicates if it should display http or https. * @param {boolean} [props.https=true] - For URL type, determines whether to show https:// or http://.
* @param {boolean} [props.isRequired] - Indicates if the field is required, will display a red asterisk. * @param {boolean} [props.isRequired=false] - Displays a red asterisk if the field is required.
* @param {boolean} [props.isOptional] - Indicates if the field is optional, will display optional text. * @param {boolean} [props.isOptional=false] - Displays an optional label next to the field.
* @param {string} [props.optionalLabel] - Optional label for the input field. * @param {string} [props.optionalLabel='(optional)'] - Custom text for optional label.
* @param {string} [props.autoComplete] - Autocomplete value for the input field. * @param {string} [props.autoComplete] - Autocomplete attribute for the input.
* @param {string} [props.placeholder] - Placeholder text for the input field. * @param {string} [props.placeholder] - Placeholder text for the input field.
* @param {string} props.value - Value of the input field. * @param {string} props.value - Current value of the input field.
* @param {function} props.onChange - Function called on input change. * @param {function} props.onChange - Callback function triggered on input value change.
* @param {string} [props.error] - Error message to display for the input field. * @param {function} [props.onBlur] - Callback function triggered when input loses focus.
* @param {boolean} [props.disabled] - Indicates if the input field is disabled. * @param {function} [props.onInput] - Callback function triggered on input event.
* @param {boolean} [props.hidden] - Indicates if the input field is hidden. * @param {string} [props.error] - Error message to display below the input field.
* @param {React.Ref} [ref] - Ref forwarded to the underlying `TextField` component. Allows for direct interactions such as focusing. * @param {boolean} [props.disabled=false] - Disables the input field if true.
* @param {boolean} [props.hidden=false] - Hides the entire input field if true.
* @param {string} [props.className] - Additional CSS class names for the input container.
* @param {boolean} [props.hideErrorText=false] - Hides the error message if true.
* @param {React.Ref} [ref] - Ref forwarded to the underlying TextField component.
*
* @returns {React.ReactElement} Rendered input field component
*/ */
const Field = forwardRef( const Field = forwardRef(
@@ -46,7 +54,8 @@ const Field = forwardRef(
error, error,
disabled, disabled,
hidden, hidden,
className className,
hideErrorText = false,
}, },
ref ref
) => { ) => {
@@ -190,7 +199,7 @@ const Field = forwardRef(
<Typography <Typography
component="span" component="span"
className="input-error" className="input-error"
hidden={className ? true : false} hidden={hideErrorText}
color={theme.palette.error.main} color={theme.palette.error.main}
mt={theme.spacing(2)} mt={theme.spacing(2)}
sx={{ sx={{
@@ -225,7 +234,8 @@ Field.propTypes = {
error: PropTypes.string, error: PropTypes.string,
disabled: PropTypes.bool, disabled: PropTypes.bool,
hidden: PropTypes.bool, hidden: PropTypes.bool,
className: PropTypes.string className: PropTypes.string,
hideErrorText: PropTypes.bool,
}; };
export default Field; export default Field;
@@ -4,7 +4,6 @@ import Checkbox from "../../../../Components/Inputs/Checkbox";
import { useTheme } from "@emotion/react"; import { useTheme } from "@emotion/react";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
/** /**
* `CustomThreshold` is a functional React component that displays a * `CustomThreshold` is a functional React component that displays a
* group of CheckBox with a label and its correspondant threshold input field. * group of CheckBox with a label and its correspondant threshold input field.
@@ -24,60 +23,61 @@ import PropTypes from "prop-types";
*/ */
export const CustomThreshold = ({ export const CustomThreshold = ({
checkboxId, checkboxId,
checkboxLabel, checkboxLabel,
onCheckboxChange, onCheckboxChange,
fieldId, fieldId,
onFieldChange, onFieldChange,
onFieldBlur, onFieldBlur,
alertUnit, alertUnit,
infrastructureMonitor, infrastructureMonitor,
errors errors,
}) => }) => {
{ const theme = useTheme();
const theme = useTheme(); return (
return ( <Stack
<Stack direction={"row"}
direction={"row"} sx={{
sx={{ width: "50%",
width: "50%", justifyContent: "space-between",
justifyContent: "space-between", flexWrap: "wrap",
flexWrap: "wrap" }}
}} >
> <Box>
<Box> <Checkbox
<Checkbox id={checkboxId}
id={checkboxId} label={checkboxLabel}
label={checkboxLabel} isChecked={infrastructureMonitor[checkboxId]}
isChecked={infrastructureMonitor[checkboxId]} onChange={onCheckboxChange}
onChange={onCheckboxChange} />
/> </Box>
</Box> <Stack
<Stack direction={"row"}
direction={"row"} sx={{
sx={{ justifyContent: "flex-end",
justifyContent: "flex-end", }}
}} >
> <Field
<Field type="number"
type="number" className="field-infrastructure-alert"
className="field-infrastructure-alert" id={fieldId}
id={fieldId} value={infrastructureMonitor[fieldId]}
value={infrastructureMonitor[fieldId]} onBlur={onFieldBlur}
onBlur={onFieldBlur} onChange={onFieldChange}
onChange={onFieldChange} error={errors[fieldId]}
error={errors[fieldId]} disabled={!infrastructureMonitor[checkboxId]}
disabled={!infrastructureMonitor[checkboxId]} hideErrorText={true}
></Field> ></Field>
<Typography <Typography
component="p" component="p"
m={theme.spacing(3)} m={theme.spacing(3)}
> >
{alertUnit} {alertUnit}
</Typography> </Typography>
</Stack> </Stack>
</Stack> </Stack>
)} );
};
CustomThreshold.propTypes = { CustomThreshold.propTypes = {
checkboxId: PropTypes.string.isRequired, checkboxId: PropTypes.string.isRequired,