Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Aizad/ Improvement for Password Input #51

Merged
merged 2 commits into from
Feb 1, 2024
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
2 changes: 1 addition & 1 deletion lib/components/PasswordInput/PasswordMeter.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ $ERROR: #ec3f3f;
.deriv-password__meter__bar {
height: 100%;
border-radius: 0px 0px 4px 4px;
transition: "width 0.25s ease-in-out";
transition: width 0.25s ease-in-out;

&__initial {
background-color: $NEUTRAL;
Expand Down
32 changes: 15 additions & 17 deletions lib/components/PasswordInput/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {
ChangeEvent,
ComponentProps,
FocusEvent,
useCallback,
useMemo,
useState,
Expand Down Expand Up @@ -40,11 +41,7 @@ export const validatePassword = (password: string) => {

type InputProps = ComponentProps<typeof Input>;

interface PasswordInputProps
extends Pick<
InputProps,
"value" | "onChange" | "label" | "id" | "autoComplete"
> {
interface PasswordInputProps extends Omit<InputProps, "rightPlaceholder"> {
hidePasswordMeter?: boolean;
hint?: string;
}
Expand All @@ -58,13 +55,12 @@ const PasswordVariant: Record<TScore, InputProps["variant"]> = {
};

export const PasswordInput = ({
autoComplete,
id,
label,
value,
onBlur,
onChange,
hint,
hidePasswordMeter,
...rest
}: PasswordInputProps) => {
const [isTouched, setIsTouched] = useState(false);
const [showPassword, setShowPassword] = useState(false);
Expand All @@ -84,20 +80,21 @@ export const PasswordInput = ({
[isTouched, onChange]
);

const handleBlur = useCallback(() => {
if (!isTouched) {
setIsTouched(true);
}
}, [isTouched]);
const handleBlur = useCallback(
(e: FocusEvent<HTMLInputElement>) => {
onBlur?.(e);
if (!isTouched) {
setIsTouched(true);
}
},
[isTouched, onBlur]
);

return (
<div className="deriv-password">
<Input
autoComplete={autoComplete}
id={id}
label={label}
type={showPassword ? "text" : "password"}
value={value}
type={showPassword ? "text" : "password"}
message={isTouched ? errorMessage : "" || hint}
onChange={handleChange}
onBlur={handleBlur}
Expand All @@ -110,6 +107,7 @@ export const PasswordInput = ({
{showPassword ? <EyeIcon /> : <EyeIconSlash />}
</button>
}
{...rest}
/>
{!hidePasswordMeter && <PasswordMeter score={score as TScore} />}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
import { StoryObj, Meta } from "@storybook/react";
import { PasswordInput } from "../../lib/main";
import { useState } from "react";

const meta = {
title: "Components/PasswordInput",
component: PasswordInput,
parameters: { layout: "centered" },
tags: ["autodocs"],
args: {
autoComplete: "password",
id: "password",
label: "Enter Password",
value: "",
onChange: () => {},
hidePasswordMeter: false,
hint: "Password should have lower and uppercase English letters with numbers.",
hint: "This is a hint message",
},
argTypes: {
autoComplete: {
control: {
disable: true,
},
},
id: {
value: {
control: {
disable: true,
},
Expand Down Expand Up @@ -49,5 +43,17 @@ export const Default: Story = {
value: "",
onChange: () => {},
hidePasswordMeter: false,
hint: "This is a hint message",
},
render: (args) => {
const [value, setValue] = useState("");

return (
<PasswordInput
{...args}
value={value}
onChange={(e) => setValue(e.target.value)}
/>
);
},
};