I am working on a react project and have built a form framework which wraps material ui, around the redux form.
** sandbox https://codesandbox.io/s/romantic-pasteur-nmw92
https://material-ui.com/components/text-fields/
I am unsure how to add the states or a variable to get changed to toggle the field -- I tried to add a constructor but it came back with an error.
Here is the current code base - at the moment it will produce a password field - but I can't get toggling processed.
import React from 'react';
import TextField from '@material-ui/core/TextField';
import FormControl from '@material-ui/core/FormControl';
import FormHelperText from '@material-ui/core/FormHelperText';
import Visibility from '@material-ui/icons/Visibility';
import VisibilityOff from '@material-ui/icons/VisibilityOff';
import IconButton from '@material-ui/core/IconButton';
import InputAdornment from '@material-ui/core/InputAdornment';
import TooltipInfoHandler from '../_SharedGlobalComponents/TooltipInfoHandler/TooltipInfoHandler';
const renderPasswordField = ({input, rows, multiline, label, required, type, placeholder, fieldRef, onClick, disabled, helper, charLimit, startAdornment, endAdornment, meta: { touched, error, warning } }) => {
let showPassword = false;
function handleClickShowPassword(){
console.log("showpass?")
showPassword = !showPassword
}
/*
const handleClickShowPassword = () => {
setValues({ ...values, showPassword: !values.showPassword });
};
const handleMouseDownPassword = (event) => {
event.preventDefault();
};
*/
/*
constructor(props, context) {
super(props, context);
this.state = {
showPassword: false
};
}*/
return (
<FormControl component="fieldset" fullWidth={true} className={multiline === true? "has-multiline": null}>
<TextField
label={required ? label + " *": label}
multiline={multiline}
rows={rows}
type={showPassword ? 'text' : 'password'}
//type={'text'}
error={touched && (error && error.length > 0 ? true : false)}
helperText={touched &&
((error && error.length > 0 ? error : null) ||
(warning && warning.length > 0 ? warning : null))
}
placeholder={placeholder}
InputLabelProps={placeholder? {shrink: true} : {}}
inputRef={fieldRef}
onClick={onClick}
disabled={disabled}
InputProps={{
endAdornment: <IconButton
aria-label="toggle password visibility"
onClick={handleClickShowPassword}
//onMouseDown={handleMouseDownPassword}
>
{showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
}}
{...input}
/>
{helper && helper.length > 0 &&
<TooltipInfoHandler helper={helper} />
}
{charLimit && multiline &&
<FormHelperText
error={false}
>
{charLimit - input.value.length} characters remaining
</FormHelperText>
}
</FormControl>
)
}
export default renderPasswordField;