-
Notifications
You must be signed in to change notification settings - Fork 0
Input #17
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
Open
GabrielTrindadeC
wants to merge
6
commits into
main
Choose a base branch
from
feature-input
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Input #17
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd61639
wip
GabrielTrindadeC d389cc9
refactor: enhance Input component structure and remove unused Label c…
GabrielTrindadeC 21450b9
refactor: clean up unused imports and improve component structure
GabrielTrindadeC 42cafc9
feat: add Input component with stories and tests
GabrielTrindadeC 0531cf3
fix: remove 'email' option from Input component type selection
GabrielTrindadeC b20f4f3
fix: correct spelling of 'rightComponent' in Input component and rela…
GabrielTrindadeC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,34 @@ | ||
| import { Meta, StoryObj } from "@storybook/react"; | ||
| import { Input } from "./Input"; | ||
| import { IconPhosphor } from "@/components/Icon/Icon"; | ||
|
|
||
| const meta: Meta<typeof Input> = { | ||
| title: "Components/Input", | ||
| component: Input, | ||
| argTypes: { | ||
| type: { | ||
| control: "select", | ||
| options: ["text", "password", "number"], | ||
| }, | ||
| status: { | ||
| control: "select", | ||
| options: ["success", "error", undefined], | ||
| }, | ||
|
|
||
| placeholder: { | ||
| control: "text", | ||
| }, | ||
| }, | ||
| args: { | ||
| type: "text", | ||
| status: undefined, | ||
|
|
||
| icon: <IconPhosphor name="User" weight="fill" />, | ||
| rightComponent: <IconPhosphor name="Acorn" />, | ||
|
|
||
| placeholder: "Placeholder", | ||
| }, | ||
| }; | ||
| export default meta; | ||
| type Story = StoryObj<typeof Input>; | ||
| export const Default: Story = {}; |
This file contains hidden or 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,62 @@ | ||
| import { cleanup, render, screen, fireEvent } from "@testing-library/react"; | ||
| import { describe, it, expect, afterEach } from "vitest"; | ||
| import "@testing-library/jest-dom/vitest"; | ||
| import { Input } from "./Input"; | ||
|
|
||
| const makeSut = (props: React.ComponentProps<typeof Input>) => { | ||
| render(<Input {...props} />); | ||
| }; | ||
|
|
||
| describe("Input", () => { | ||
| afterEach(() => { | ||
| cleanup(); | ||
| }); | ||
|
|
||
| it("renders the input with default props", () => { | ||
| makeSut({ placeholder: "Type something..." }); | ||
| const input = screen.getByPlaceholderText("Type something..."); | ||
| expect(input).toBeInTheDocument(); | ||
| expect(input).toHaveAttribute("type", "text"); | ||
| }); | ||
|
|
||
| it("renders the input with an icon passed via prop", () => { | ||
| const TestIcon = () => <span>Icon</span>; | ||
| makeSut({ placeholder: "Type something...", icon: <TestIcon /> }); | ||
| const icon = screen.getByText("Icon"); | ||
| expect(icon).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders the 'success' status icon when status is set to 'success'", () => { | ||
| makeSut({ placeholder: "Type something...", status: "success" }); | ||
| const container = | ||
| screen.getByPlaceholderText("Type something...").parentElement; | ||
| expect(container?.querySelector(".text-success-500")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("renders the 'error' status icon when status is set to 'error'", () => { | ||
| makeSut({ placeholder: "Type something...", status: "error" }); | ||
| const container = | ||
| screen.getByPlaceholderText("Type something...").parentElement; | ||
| expect(container?.querySelector(".text-danger-500")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("displays the 'rigthComponent' when the input is focused", () => { | ||
| const RightComponent = () => <span>Right</span>; | ||
| makeSut({ | ||
| placeholder: "Type something...", | ||
| rightComponent: <RightComponent />, | ||
| }); | ||
| const input = screen.getByPlaceholderText("Type something..."); | ||
| expect(screen.queryByText("Right")).not.toBeInTheDocument(); | ||
| fireEvent.focus(input); | ||
| expect(screen.getByText("Right")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("focuses the input when clicking on the container", () => { | ||
| makeSut({ placeholder: "Type something..." }); | ||
| const input = screen.getByPlaceholderText("Type something..."); | ||
| const container = input.parentElement; | ||
| fireEvent.click(container!); | ||
| expect(input).toHaveFocus(); | ||
| }); | ||
| }); | ||
This file contains hidden or 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,72 @@ | ||
| import { IconPhosphor } from "@/components/Icon/Icon"; | ||
| import { cn } from "@/lib/utils"; | ||
| import * as React from "react"; | ||
| import { InputProps } from "./Input.types"; | ||
|
|
||
| const Input = React.forwardRef<HTMLInputElement, InputProps>( | ||
| ( | ||
| { className, rightComponent, type = "text", icon, status, ...props }, | ||
| ref | ||
| ) => { | ||
| const [focused, setFocused] = React.useState(false); | ||
|
|
||
| const renderStatusIcon = () => { | ||
| if (status === "success") { | ||
| return ( | ||
| <IconPhosphor | ||
| name="CheckCircle" | ||
| className="text-success-500" | ||
| weight="fill" | ||
| /> | ||
| ); | ||
| } | ||
| if (status === "error") { | ||
| return ( | ||
| <IconPhosphor | ||
| name="Warning" | ||
| className="text-danger-500" | ||
| weight="fill" | ||
| /> | ||
| ); | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| return ( | ||
| <div | ||
| className={cn( | ||
| "flex items-center border px-4.5 py-3 w-full gap-[12px] cursor-text", | ||
| "hover:ring-1 hover:ring-primary-200 focus-within:ring-1 focus-within:ring-primary-500", | ||
| { | ||
| "border-green-200 bg-green-50": status === "success", | ||
| "border-red-200 bg-red-50": status === "error", | ||
| "border-gray-100": !status, | ||
| } | ||
| )} | ||
| onClick={(e) => { | ||
| const input = e.currentTarget.querySelector("input"); | ||
| input?.focus(); | ||
| }} | ||
| > | ||
| {icon} | ||
|
|
||
| <input | ||
| type={type} | ||
| ref={ref} | ||
| onFocus={() => setFocused(true)} | ||
| onBlur={() => setFocused(false)} | ||
| className={cn( | ||
| "flex-1 outline-none bg-transparent text-gray-500 placeholder:text-body-lg-400", | ||
| className | ||
| )} | ||
| {...props} | ||
| /> | ||
| {focused && rightComponent ? rightComponent : renderStatusIcon()} | ||
| </div> | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| Input.displayName = "Input"; | ||
|
|
||
| export { Input }; |
This file contains hidden or 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,6 @@ | ||
| export type Status = "success" | "error" | undefined; | ||
| export interface InputProps extends React.ComponentProps<"input"> { | ||
| icon?: React.ReactNode; | ||
| rightComponent?: React.ReactNode; | ||
| status?: Status; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.