From 39f77745d7c35dc76aaefc2eb372cdd2b3e7e267 Mon Sep 17 00:00:00 2001 From: west270 <61320354+west270@users.noreply.github.com> Date: Thu, 13 Jul 2023 23:45:57 -0400 Subject: [PATCH] #433 - added field clear button (#434) --- .../FormComponents/FormAnyOrDict.tsx | 23 +++++++++++++-- .../FormComponents/FormTextField.tsx | 28 +++++++++++++------ .../ConnectionHttpFields.tsx | 2 +- .../ConnectionStompFields.tsx | 2 +- 4 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/components/FormComponents/FormAnyOrDict.tsx b/src/components/FormComponents/FormAnyOrDict.tsx index 5b2fdcb7..c875b695 100644 --- a/src/components/FormComponents/FormAnyOrDict.tsx +++ b/src/components/FormComponents/FormAnyOrDict.tsx @@ -1,4 +1,5 @@ -import { TextField, TextFieldProps } from '@mui/material' +import { Clear } from '@mui/icons-material' +import { IconButton, InputAdornment, TextField, TextFieldProps } from '@mui/material' import { defaultTextFieldProps } from 'components/FormComponents' import { ChangeEvent } from 'react' import { RegisterOptions, useFormContext } from 'react-hook-form' @@ -22,11 +23,29 @@ const FormAnyOrDict = ({ registerKey, registerOptions, ...textFieldProps }: Form if(error.message) textFieldProps.helperText = error.message } + const endAdornment = ( + + {!textFieldProps.disabled && + { + setValue(registerKey, undefined) + }} + > + + + } + + ) + if (textFieldProps.InputProps && !textFieldProps.disabled) textFieldProps.InputProps.endAdornment = endAdornment + else textFieldProps.InputProps = { + endAdornment: endAdornment + } + return ( ) => { if(event.target.value === '') { clearErrors(registerKey) diff --git a/src/components/FormComponents/FormTextField.tsx b/src/components/FormComponents/FormTextField.tsx index 78a3b620..b7f8460b 100644 --- a/src/components/FormComponents/FormTextField.tsx +++ b/src/components/FormComponents/FormTextField.tsx @@ -1,4 +1,4 @@ -import { Visibility, VisibilityOff } from '@mui/icons-material' +import { Clear, Visibility, VisibilityOff } from '@mui/icons-material' import { IconButton, InputAdornment, MenuItem, TextField, TextFieldProps } from '@mui/material' import { useMountedState } from 'hooks/useMountedState' import { DateTime } from 'luxon' @@ -20,7 +20,7 @@ const defaultTextFieldProps: TextFieldProps = { } const FormTextField = ({ registerKey, registerOptions, menuOptions, ...textFieldProps }: FormTextFieldProps) => { - const { register, getFieldState, watch } = useFormContext() + const { register, getFieldState, watch, setValue } = useFormContext() const [showPassword, setShowPassword] = useMountedState(false) const currentValue = watch(registerKey) @@ -31,14 +31,25 @@ const FormTextField = ({ registerKey, registerOptions, menuOptions, ...textField if(error.message) textFieldProps.helperText = error.message } - if(textFieldProps.type === 'password'){ + if(!menuOptions){ const endAdornment = ( - setShowPassword(!showPassword)} - > - {showPassword ? : } - + {textFieldProps.type === 'password' && + setShowPassword(!showPassword)} + sx={{mr: -2}} + > + {showPassword ? : } + + } + {!textFieldProps.disabled && + setValue(registerKey, '')} + sx={{mr: -2}} + > + + + } ) if (textFieldProps.InputProps) textFieldProps.InputProps.endAdornment = endAdornment @@ -72,6 +83,7 @@ const FormTextField = ({ registerKey, registerOptions, menuOptions, ...textField {...defaultTextFieldProps} value={(currentValue === 0 || currentValue) ? currentValue : ''} {...textFieldProps} + type={showPassword ? 'string' : textFieldProps.type} {...register(registerKey, registerOptions)} /> ) diff --git a/src/components/GardenConnectionForm/garden-connection-form-components/ConnectionHttpFields.tsx b/src/components/GardenConnectionForm/garden-connection-form-components/ConnectionHttpFields.tsx index af09be5e..ccb7f1ae 100644 --- a/src/components/GardenConnectionForm/garden-connection-form-components/ConnectionHttpFields.tsx +++ b/src/components/GardenConnectionForm/garden-connection-form-components/ConnectionHttpFields.tsx @@ -25,7 +25,7 @@ const ConnectionHttpFields = ({connectionType, textFieldProps, ...gridProps}: Co label="Host" /> - + - +