InputBoxWrapper automatically add hide/show behavior for password fields (#2327)

Signed-off-by: Lenin Alevski <alevsk.8772@gmail.com>
This commit is contained in:
Lenin Alevski
2022-09-20 21:13:34 -07:00
committed by GitHub
parent 3513a01711
commit 368c9ee3d7

View File

@@ -13,7 +13,7 @@
// //
// You should have received a copy of the GNU Affero General Public License // You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>. // along with this program. If not, see <http://www.gnu.org/licenses/>.
import React from "react"; import React, { useState } from "react";
import { import {
Grid, Grid,
IconButton, IconButton,
@@ -24,6 +24,8 @@ import {
} from "@mui/material"; } from "@mui/material";
import { OutlinedInputProps } from "@mui/material/OutlinedInput"; import { OutlinedInputProps } from "@mui/material/OutlinedInput";
import { InputProps as StandardInputProps } from "@mui/material/Input"; import { InputProps as StandardInputProps } from "@mui/material/Input";
import VisibilityOffIcon from "@mui/icons-material/VisibilityOff";
import RemoveRedEyeIcon from "@mui/icons-material/RemoveRedEye";
import { Theme } from "@mui/material/styles"; import { Theme } from "@mui/material/styles";
import createStyles from "@mui/styles/createStyles"; import createStyles from "@mui/styles/createStyles";
import makeStyles from "@mui/styles/makeStyles"; import makeStyles from "@mui/styles/makeStyles";
@@ -137,6 +139,7 @@ const InputBoxWrapper = ({
onFocus, onFocus,
}: InputBoxProps) => { }: InputBoxProps) => {
let inputProps: any = { "data-index": index, ...extraInputProps }; let inputProps: any = { "data-index": index, ...extraInputProps };
const [toggleTextInput, setToggleTextInput] = useState<boolean>(false);
if (type === "number" && min) { if (type === "number" && min) {
inputProps["min"] = min; inputProps["min"] = min;
@@ -150,6 +153,18 @@ const InputBoxWrapper = ({
inputProps["pattern"] = pattern; inputProps["pattern"] = pattern;
} }
let inputBoxWrapperIcon = overlayIcon;
let inputBoxWrapperType = type;
if (type === "password" && overlayIcon === null) {
inputBoxWrapperIcon = toggleTextInput ? (
<VisibilityOffIcon />
) : (
<RemoveRedEyeIcon />
);
inputBoxWrapperType = toggleTextInput ? "text" : "password";
}
return ( return (
<React.Fragment> <React.Fragment>
<Grid <Grid
@@ -191,7 +206,7 @@ const InputBoxWrapper = ({
autoFocus={autoFocus} autoFocus={autoFocus}
disabled={disabled} disabled={disabled}
onChange={onChange} onChange={onChange}
type={type} type={inputBoxWrapperType}
multiline={multiline} multiline={multiline}
autoComplete={autoComplete} autoComplete={autoComplete}
inputProps={inputProps} inputProps={inputProps}
@@ -202,7 +217,7 @@ const InputBoxWrapper = ({
onKeyPress={onKeyPress} onKeyPress={onKeyPress}
onFocus={onFocus} onFocus={onFocus}
/> />
{overlayIcon && ( {inputBoxWrapperIcon && (
<div <div
className={`${classes.overlayAction} ${ className={`${classes.overlayAction} ${
label !== "" ? "withLabel" : "" label !== "" ? "withLabel" : ""
@@ -214,7 +229,7 @@ const InputBoxWrapper = ({
? () => { ? () => {
overlayAction(); overlayAction();
} }
: () => null : () => setToggleTextInput(!toggleTextInput)
} }
id={overlayId} id={overlayId}
size={"small"} size={"small"}
@@ -222,7 +237,7 @@ const InputBoxWrapper = ({
disableRipple={false} disableRipple={false}
disableTouchRipple={false} disableTouchRipple={false}
> >
{overlayIcon} {inputBoxWrapperIcon}
</IconButton> </IconButton>
</div> </div>
)} )}