Skip to content

Commit 368c9ee

Browse files
authored
InputBoxWrapper automatically add hide/show behavior for password fields (#2327)
Signed-off-by: Lenin Alevski <alevsk.8772@gmail.com>
1 parent 3513a01 commit 368c9ee

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

portal-ui/src/screens/Console/Common/FormComponents/InputBoxWrapper/InputBoxWrapper.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//
1414
// You should have received a copy of the GNU Affero General Public License
1515
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
import React from "react";
16+
import React, { useState } from "react";
1717
import {
1818
Grid,
1919
IconButton,
@@ -24,6 +24,8 @@ import {
2424
} from "@mui/material";
2525
import { OutlinedInputProps } from "@mui/material/OutlinedInput";
2626
import { InputProps as StandardInputProps } from "@mui/material/Input";
27+
import VisibilityOffIcon from "@mui/icons-material/VisibilityOff";
28+
import RemoveRedEyeIcon from "@mui/icons-material/RemoveRedEye";
2729
import { Theme } from "@mui/material/styles";
2830
import createStyles from "@mui/styles/createStyles";
2931
import makeStyles from "@mui/styles/makeStyles";
@@ -137,6 +139,7 @@ const InputBoxWrapper = ({
137139
onFocus,
138140
}: InputBoxProps) => {
139141
let inputProps: any = { "data-index": index, ...extraInputProps };
142+
const [toggleTextInput, setToggleTextInput] = useState<boolean>(false);
140143

141144
if (type === "number" && min) {
142145
inputProps["min"] = min;
@@ -150,6 +153,18 @@ const InputBoxWrapper = ({
150153
inputProps["pattern"] = pattern;
151154
}
152155

156+
let inputBoxWrapperIcon = overlayIcon;
157+
let inputBoxWrapperType = type;
158+
159+
if (type === "password" && overlayIcon === null) {
160+
inputBoxWrapperIcon = toggleTextInput ? (
161+
<VisibilityOffIcon />
162+
) : (
163+
<RemoveRedEyeIcon />
164+
);
165+
inputBoxWrapperType = toggleTextInput ? "text" : "password";
166+
}
167+
153168
return (
154169
<React.Fragment>
155170
<Grid
@@ -191,7 +206,7 @@ const InputBoxWrapper = ({
191206
autoFocus={autoFocus}
192207
disabled={disabled}
193208
onChange={onChange}
194-
type={type}
209+
type={inputBoxWrapperType}
195210
multiline={multiline}
196211
autoComplete={autoComplete}
197212
inputProps={inputProps}
@@ -202,7 +217,7 @@ const InputBoxWrapper = ({
202217
onKeyPress={onKeyPress}
203218
onFocus={onFocus}
204219
/>
205-
{overlayIcon && (
220+
{inputBoxWrapperIcon && (
206221
<div
207222
className={`${classes.overlayAction} ${
208223
label !== "" ? "withLabel" : ""
@@ -214,15 +229,15 @@ const InputBoxWrapper = ({
214229
? () => {
215230
overlayAction();
216231
}
217-
: () => null
232+
: () => setToggleTextInput(!toggleTextInput)
218233
}
219234
id={overlayId}
220235
size={"small"}
221236
disableFocusRipple={false}
222237
disableRipple={false}
223238
disableTouchRipple={false}
224239
>
225-
{overlayIcon}
240+
{inputBoxWrapperIcon}
226241
</IconButton>
227242
</div>
228243
)}

0 commit comments

Comments
 (0)