Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

433 added field clear button #434

Merged
merged 1 commit into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/components/FormComponents/FormAnyOrDict.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -22,11 +23,29 @@ const FormAnyOrDict = ({ registerKey, registerOptions, ...textFieldProps }: Form
if(error.message) textFieldProps.helperText = error.message
}

const endAdornment = (
<InputAdornment position="end">
{!textFieldProps.disabled &&
<IconButton
onClick={() => {
setValue(registerKey, undefined)
}}
>
<Clear />
</IconButton>
}
</InputAdornment>
)
if (textFieldProps.InputProps && !textFieldProps.disabled) textFieldProps.InputProps.endAdornment = endAdornment
else textFieldProps.InputProps = {
endAdornment: endAdornment
}

return (
<TextField
{...defaultTextFieldProps}
{...textFieldProps}
value={ invalid ? currentValue : JSON.stringify(currentValue)}
value={ currentValue === undefined ? '' : ( invalid ? currentValue : JSON.stringify(currentValue))}
onChange={(event: ChangeEvent<HTMLInputElement>) => {
if(event.target.value === '') {
clearErrors(registerKey)
Expand Down
28 changes: 20 additions & 8 deletions src/components/FormComponents/FormTextField.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
Expand All @@ -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 = (
<InputAdornment position="end">
<IconButton
onClick={() => setShowPassword(!showPassword)}
>
{showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
{textFieldProps.type === 'password' &&
<IconButton
onClick={() => setShowPassword(!showPassword)}
sx={{mr: -2}}
>
{showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
}
{!textFieldProps.disabled &&
<IconButton
onClick={() => setValue(registerKey, '')}
sx={{mr: -2}}
>
<Clear />
</IconButton>
}
</InputAdornment>
)
if (textFieldProps.InputProps) textFieldProps.InputProps.endAdornment = endAdornment
Expand Down Expand Up @@ -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)}
/>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const ConnectionHttpFields = ({connectionType, textFieldProps, ...gridProps}: Co
label="Host"
/>
</Grid>
<Grid item minWidth="125px" maxWidth="125px" xs={1}>
<Grid item minWidth="150px" maxWidth="150px" xs={1}>
<FormTextField
{...textFieldProps}
registerKey="connection_params.http.port"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const ConnectionStompFields = ({connectionType, textFieldProps, ...gridProps}: C
label="Host"
/>
</Grid>
<Grid item minWidth="125px" maxWidth="125px" xs={1}>
<Grid item minWidth="150px" maxWidth="150px" xs={1}>
<FormTextField
{...textFieldProps}
registerKey="connection_params.stomp.port"
Expand Down