-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/dev-madhurendra/short-my-url …
…into FE_Image
- Loading branch information
Showing
9 changed files
with
230 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
describe("App Component", () => { | ||
it("should renders the correct heading", () => { | ||
expect(true).toBe(true); | ||
expect(true).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import React from "react"; | ||
import TextField from "./index"; | ||
import { Meta, StoryObj } from "@storybook/react"; | ||
|
||
export default { | ||
title: "atoms/TextField", | ||
component: TextField, | ||
} as Meta<typeof TextField>; | ||
|
||
export const Default: StoryObj<typeof TextField> = { | ||
args: { | ||
placeholder: "Enter your text", | ||
value: "", | ||
onChange: (value: string) => console.log(value), | ||
width: "20%", | ||
height: 40, | ||
size: "medium", | ||
borderRadius: "8px", | ||
}, | ||
}; | ||
|
||
export const Password: StoryObj<typeof TextField> = { | ||
args: { | ||
placeholder: "Enter Password", | ||
value: "", | ||
onChange: (value: string) => console.log(value), | ||
isPassword: true, | ||
width: "20%", | ||
height: 40, | ||
size: "medium", | ||
borderRadius: "8px", | ||
}, | ||
}; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { render, fireEvent, screen } from "@testing-library/react"; | ||
import CustomTextField from "./index"; | ||
|
||
describe("CustomTextField", () => { | ||
it("should render with the correct initial value and call onChange handler", () => { | ||
const handleChange = jest.fn(); | ||
const { getByPlaceholderText } = render( | ||
<CustomTextField | ||
placeholder="Enter text" | ||
value="Initial value" | ||
onChange={handleChange} | ||
isPassword={false} | ||
/> | ||
); | ||
|
||
const input = getByPlaceholderText("Enter text") as HTMLInputElement; | ||
expect(input.value).toBe("Initial value"); | ||
|
||
fireEvent.change(input, { target: { value: "Updated value" } }); | ||
expect(handleChange).toHaveBeenCalledWith("Updated value"); | ||
}); | ||
|
||
it("should handles text change correctly", () => { | ||
let value = ""; | ||
|
||
const handleChange = (newValue: string) => { | ||
value = newValue; | ||
}; | ||
|
||
render( | ||
<CustomTextField | ||
placeholder="Enter your text" | ||
value={value} | ||
onChange={handleChange} | ||
isPassword={false} | ||
width="100%" | ||
height={40} | ||
size="medium" | ||
borderRadius="8px" | ||
/> | ||
); | ||
|
||
const textField = screen.getByPlaceholderText("Enter your text"); | ||
fireEvent.change(textField, { target: { value: "Hello" } }); | ||
|
||
expect(value).toBe("Hello"); | ||
}); | ||
|
||
it(" should toggles password visibility correctly", () => { | ||
let value = ""; | ||
|
||
const handleChange = (newValue: string) => { | ||
value = newValue; | ||
}; | ||
|
||
render( | ||
<CustomTextField | ||
placeholder="Enter your password" | ||
value={value} | ||
onChange={handleChange} | ||
isPassword | ||
width="100%" | ||
height={40} | ||
size="medium" | ||
borderRadius="8px" | ||
/> | ||
); | ||
|
||
const textField = screen.getByPlaceholderText("Enter your password"); | ||
const toggleButton = screen.getByRole("button"); | ||
|
||
fireEvent.change(textField, { target: { value: "Secret123" } }); | ||
fireEvent.click(toggleButton); | ||
|
||
expect(textField.getAttribute("type")).toBe("text"); | ||
|
||
fireEvent.click(toggleButton); | ||
|
||
expect(textField.getAttribute("type")).toBe("password"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { useState } from 'react'; | ||
import { TextField, IconButton, InputAdornment } from '@mui/material'; | ||
import EyeOn from "../../../../public/assets/images/eye.svg"; | ||
import EyeOff from "../../../../public/assets/images/Vector.svg"; | ||
import IconComponent from '../icon'; | ||
|
||
export interface TextFieldProps { | ||
placeholder?: string; | ||
value: string; | ||
onChange: (value: string) => void; | ||
isPassword: boolean; | ||
width?: string; | ||
height?: string | number; | ||
size?: "small" | "medium"; | ||
borderRadius?: string | number; | ||
} | ||
|
||
const CustomTextField: React.FC<TextFieldProps> = ({ | ||
placeholder, | ||
value, | ||
onChange, | ||
isPassword, | ||
width, | ||
height, | ||
size, | ||
borderRadius, | ||
}) => { | ||
const [showPassword, setShowPassword] = useState(false); | ||
|
||
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
onChange(event.target.value); | ||
}; | ||
|
||
const handleTogglePasswordVisibility = () => { | ||
setShowPassword((prevShowPassword) => !prevShowPassword); | ||
}; | ||
|
||
const textFieldStyle = { | ||
width: width ?? "100%", | ||
height: height ?? 40, | ||
borderRadius: borderRadius ?? "8px", | ||
}; | ||
|
||
return ( | ||
<TextField | ||
placeholder={placeholder} | ||
type={isPassword && !showPassword ? "password" : "text"} | ||
value={value} | ||
onChange={handleChange} | ||
fullWidth | ||
margin="normal" | ||
size={size} | ||
InputProps={{ | ||
sx: textFieldStyle, | ||
endAdornment: isPassword && ( | ||
<InputAdornment position="end"> | ||
<IconButton onClick={handleTogglePasswordVisibility} edge="end"> | ||
{showPassword ? ( | ||
<IconComponent height={"19.41px"} width={"20px"} src={EyeOff} /> | ||
) : ( | ||
<IconComponent height={"19.41px"} width={"20px"} src={EyeOn} /> | ||
)} | ||
</IconButton> | ||
</InputAdornment> | ||
), | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
export default CustomTextField; |