Skip to content

Commit a1c5da3

Browse files
authored
Merge pull request #51 from deriv-com/aizad/password-improvements
Aizad/ Improvement for Password Input
2 parents bac22be + c3726d9 commit a1c5da3

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

lib/components/PasswordInput/PasswordMeter.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $ERROR: #ec3f3f;
55
.deriv-password__meter__bar {
66
height: 100%;
77
border-radius: 0px 0px 4px 4px;
8-
transition: "width 0.25s ease-in-out";
8+
transition: width 0.25s ease-in-out;
99

1010
&__initial {
1111
background-color: $NEUTRAL;

lib/components/PasswordInput/index.tsx

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {
22
ChangeEvent,
33
ComponentProps,
4+
FocusEvent,
45
useCallback,
56
useMemo,
67
useState,
@@ -40,11 +41,7 @@ export const validatePassword = (password: string) => {
4041

4142
type InputProps = ComponentProps<typeof Input>;
4243

43-
interface PasswordInputProps
44-
extends Pick<
45-
InputProps,
46-
"value" | "onChange" | "label" | "id" | "autoComplete"
47-
> {
44+
interface PasswordInputProps extends Omit<InputProps, "rightPlaceholder"> {
4845
hidePasswordMeter?: boolean;
4946
hint?: string;
5047
}
@@ -58,13 +55,12 @@ const PasswordVariant: Record<TScore, InputProps["variant"]> = {
5855
};
5956

6057
export const PasswordInput = ({
61-
autoComplete,
62-
id,
63-
label,
6458
value,
59+
onBlur,
6560
onChange,
6661
hint,
6762
hidePasswordMeter,
63+
...rest
6864
}: PasswordInputProps) => {
6965
const [isTouched, setIsTouched] = useState(false);
7066
const [showPassword, setShowPassword] = useState(false);
@@ -84,20 +80,21 @@ export const PasswordInput = ({
8480
[isTouched, onChange]
8581
);
8682

87-
const handleBlur = useCallback(() => {
88-
if (!isTouched) {
89-
setIsTouched(true);
90-
}
91-
}, [isTouched]);
83+
const handleBlur = useCallback(
84+
(e: FocusEvent<HTMLInputElement>) => {
85+
onBlur?.(e);
86+
if (!isTouched) {
87+
setIsTouched(true);
88+
}
89+
},
90+
[isTouched, onBlur]
91+
);
9292

9393
return (
9494
<div className="deriv-password">
9595
<Input
96-
autoComplete={autoComplete}
97-
id={id}
98-
label={label}
99-
type={showPassword ? "text" : "password"}
10096
value={value}
97+
type={showPassword ? "text" : "password"}
10198
message={isTouched ? errorMessage : "" || hint}
10299
onChange={handleChange}
103100
onBlur={handleBlur}
@@ -110,6 +107,7 @@ export const PasswordInput = ({
110107
{showPassword ? <EyeIcon /> : <EyeIconSlash />}
111108
</button>
112109
}
110+
{...rest}
113111
/>
114112
{!hidePasswordMeter && <PasswordMeter score={score as TScore} />}
115113
</div>

src/stories/PasswordInput.stories.ts src/stories/PasswordInput.stories.tsx

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
import { StoryObj, Meta } from "@storybook/react";
22
import { PasswordInput } from "../../lib/main";
3+
import { useState } from "react";
34

45
const meta = {
56
title: "Components/PasswordInput",
67
component: PasswordInput,
78
parameters: { layout: "centered" },
89
tags: ["autodocs"],
910
args: {
10-
autoComplete: "password",
11-
id: "password",
1211
label: "Enter Password",
1312
value: "",
1413
onChange: () => {},
1514
hidePasswordMeter: false,
16-
hint: "Password should have lower and uppercase English letters with numbers.",
15+
hint: "This is a hint message",
1716
},
1817
argTypes: {
19-
autoComplete: {
20-
control: {
21-
disable: true,
22-
},
23-
},
24-
id: {
18+
value: {
2519
control: {
2620
disable: true,
2721
},
@@ -49,5 +43,17 @@ export const Default: Story = {
4943
value: "",
5044
onChange: () => {},
5145
hidePasswordMeter: false,
46+
hint: "This is a hint message",
47+
},
48+
render: (args) => {
49+
const [value, setValue] = useState("");
50+
51+
return (
52+
<PasswordInput
53+
{...args}
54+
value={value}
55+
onChange={(e) => setValue(e.target.value)}
56+
/>
57+
);
5258
},
5359
};

0 commit comments

Comments
 (0)