-
Notifications
You must be signed in to change notification settings - Fork 70
feat(prefixedIpInput): add prefixedInput, prefixedIpInput WD-32589 #1301
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
omarelkashef
wants to merge
1
commit into
main
Choose a base branch
from
WD-32589-add-prefixedInput-component
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
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| @import "vanilla-framework"; | ||
|
|
||
| .prefixed-input { | ||
| position: relative; | ||
|
|
||
| .prefixed-input__input { | ||
| padding-top: 0.25rem; | ||
| } | ||
|
|
||
| .prefixed-input__text { | ||
| padding-left: $spv--small; | ||
| padding-top: 0.3rem; | ||
| pointer-events: none; | ||
| position: absolute; | ||
| } | ||
|
|
||
| &--with-label { | ||
| .prefixed-input__text { | ||
| top: 2.5rem; | ||
| } | ||
| } | ||
| } | ||
omarelkashef marked this conversation as resolved.
Show resolved
Hide resolved
|
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 type { Meta, StoryObj } from "@storybook/react"; | ||
| import PrefixedInput from "./PrefixedInput"; | ||
|
|
||
| const meta: Meta<typeof PrefixedInput> = { | ||
| component: PrefixedInput, | ||
| tags: ["autodocs"], | ||
| }; | ||
|
|
||
| export default meta; | ||
|
|
||
| type Story = StoryObj<typeof PrefixedInput>; | ||
|
|
||
| export const Default: Story = { | ||
| args: { | ||
| immutableText: "https://", | ||
| placeholder: "example.com", | ||
| }, | ||
| }; | ||
|
|
||
| export const WithLabel: Story = { | ||
| args: { | ||
| immutableText: "https://", | ||
| label: "Website URL", | ||
| placeholder: "example.com", | ||
| }, | ||
| }; | ||
|
|
||
| export const Disabled: Story = { | ||
| args: { | ||
| immutableText: "@", | ||
| label: "Username", | ||
| placeholder: "username", | ||
| disabled: true, | ||
| }, | ||
| }; | ||
|
|
||
| export const WithError: Story = { | ||
| args: { | ||
| immutableText: "https://", | ||
| label: "Website URL", | ||
| placeholder: "example.com", | ||
| error: "Invalid URL format", | ||
| }, | ||
| }; | ||
|
|
||
| export const WithHelpText: Story = { | ||
| args: { | ||
| immutableText: "User ID:", | ||
| label: "User Identifier", | ||
| placeholder: " Enter user ID", | ||
| help: "This will be used to identify your account", | ||
| }, | ||
| }; | ||
|
|
||
| export const Required: Story = { | ||
| args: { | ||
| immutableText: "https://", | ||
| label: "Website URL", | ||
| placeholder: "example.com", | ||
| required: true, | ||
| }, | ||
| }; |
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,120 @@ | ||
| import React from "react"; | ||
| import { render } from "@testing-library/react"; | ||
|
|
||
| import PrefixedInput, { PrefixedInputProps } from "./PrefixedInput"; | ||
|
|
||
| // Mock the Input component | ||
| jest.mock("components/Input", () => { | ||
| return function MockInput(props: PrefixedInputProps) { | ||
| return <input data-testid="mock-input" type="text" {...props} />; | ||
| }; | ||
| }); | ||
|
|
||
| jest.mock("classnames", () => { | ||
| return jest.fn((...args) => { | ||
| return args | ||
| .filter(Boolean) | ||
| .map((arg) => { | ||
| if (typeof arg === "string") { | ||
| return arg; | ||
| } | ||
| if (typeof arg === "object" && arg !== null) { | ||
| return Object.keys(arg) | ||
| .filter((key) => arg[key]) | ||
| .join(" "); | ||
| } | ||
| return ""; | ||
| }) | ||
| .filter(Boolean) | ||
| .join(" "); | ||
| }); | ||
| }); | ||
|
|
||
| describe("PrefixedInput", () => { | ||
| beforeEach(() => { | ||
| // Mock getBoundingClientRect | ||
| HTMLElement.prototype.getBoundingClientRect = jest.fn(() => ({ | ||
| width: 50, | ||
| height: 20, | ||
| top: 0, | ||
| left: 0, | ||
| bottom: 20, | ||
| right: 50, | ||
| x: 0, | ||
| y: 0, | ||
| toJSON: () => {}, | ||
| })); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it("renders the immutable text", () => { | ||
| const { container } = render(<PrefixedInput immutableText="https://" />); | ||
| expect(container).toContainHTML("https://"); | ||
| }); | ||
|
|
||
| it("passes extra classes to the input element", () => { | ||
| const { container } = render( | ||
| <PrefixedInput immutableText="prefix" className="extra-class" />, | ||
| ); | ||
| const input = container.querySelector("input") as HTMLInputElement; | ||
| expect(input).toHaveClass("prefixed-input__input extra-class"); | ||
| }); | ||
|
|
||
| it("renders with label class when label is provided", () => { | ||
| const { container } = render( | ||
| <PrefixedInput immutableText="prefix" label="Test Label" />, | ||
| ); | ||
| const element = container.querySelector(".prefixed-input"); | ||
| expect(element).toHaveClass("prefixed-input prefixed-input--with-label"); | ||
| }); | ||
|
|
||
| it("renders without label class when label is not provided", () => { | ||
| const { container } = render(<PrefixedInput immutableText="prefix" />); | ||
| expect(container.querySelector(".prefixed-input--with-label")).toBeNull(); | ||
| }); | ||
|
|
||
| it("updates padding on window resize", () => { | ||
| const { container } = render(<PrefixedInput immutableText="https://" />); | ||
| const input = container.querySelector("input"); | ||
|
|
||
| expect(input?.style.paddingLeft).toBe("50px"); | ||
|
|
||
| HTMLElement.prototype.getBoundingClientRect = jest.fn(() => ({ | ||
| width: 100, | ||
| height: 20, | ||
| top: 0, | ||
| left: 0, | ||
| bottom: 20, | ||
| right: 100, | ||
| x: 0, | ||
| y: 0, | ||
| toJSON: () => {}, | ||
| })); | ||
|
|
||
| window.dispatchEvent(new Event("resize")); | ||
|
|
||
| expect(input?.style.paddingLeft).toBe("100px"); | ||
| }); | ||
|
|
||
| it("passes additional props to the Input component", () => { | ||
| const { container } = render( | ||
| <PrefixedInput | ||
| immutableText="prefix" | ||
| placeholder="Enter text" | ||
| disabled | ||
| />, | ||
| ); | ||
| const input = container.querySelector("input"); | ||
| expect(input).toHaveAttribute("placeholder", "Enter text"); | ||
| expect(input).toHaveAttribute("disabled"); | ||
| }); | ||
|
|
||
| it("sets input type to text", () => { | ||
| const { container } = render(<PrefixedInput immutableText="prefix" />); | ||
| const input = container.querySelector("input"); | ||
| expect(input).toHaveAttribute("type", "text"); | ||
| }); | ||
| }); |
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,78 @@ | ||
| import React, { type ReactElement } from "react"; | ||
| import { useLayoutEffect, useRef, useCallback } from "react"; | ||
|
|
||
| import Input, { type InputProps } from "components/Input"; | ||
| import classNames from "classnames"; | ||
| import "./PrefixedInput.scss"; | ||
| import { PropsWithSpread } from "types"; | ||
|
|
||
| // export type PrefixedInputProps = Omit<InputProps, "type"> & { | ||
| // /** | ||
| // * The immutable text that appears at the beginning of the input field. | ||
| // * This text is not editable by the user and visually appears inside the input. | ||
| // */ | ||
| // immutableText: string; | ||
| // }; | ||
|
|
||
| export type PrefixedInputProps = PropsWithSpread< | ||
| { | ||
| /** | ||
| * The immutable text that appears at the beginning of the input field. | ||
| * This text is not editable by the user and visually appears inside the input. | ||
| */ | ||
| immutableText: string; | ||
| }, | ||
| Omit<InputProps, "type"> | ||
| >; | ||
|
|
||
| const PrefixedInput = ({ | ||
| immutableText, | ||
| ...props | ||
| }: PrefixedInputProps): ReactElement => { | ||
| const prefixTextRef = useRef<HTMLDivElement>(null); | ||
| const inputWrapperRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| const updatePadding = useCallback(() => { | ||
| const prefixElement = prefixTextRef.current; | ||
| const inputElement = inputWrapperRef.current?.querySelector("input"); | ||
|
|
||
| if (prefixElement && inputElement) { | ||
| // Adjust the left padding of the input to be the same width as the immutable text. | ||
| // This displays the user input and the unchangeable text together as one combined string. | ||
| const prefixWidth = prefixElement.getBoundingClientRect().width; | ||
| inputElement.style.paddingLeft = `${prefixWidth}px`; | ||
| } | ||
| }, []); | ||
|
|
||
| useLayoutEffect(() => { | ||
| updatePadding(); | ||
|
|
||
| // Listen for window resize events (includes zoom changes) | ||
| window.addEventListener("resize", updatePadding); | ||
| }, [immutableText, props.label, updatePadding]); | ||
|
|
||
| return ( | ||
| <div | ||
| className={classNames("prefixed-input", { | ||
| "prefixed-input--with-label": !!props.label, | ||
| })} | ||
| > | ||
| <div className="prefixed-input__text" ref={prefixTextRef}> | ||
| {immutableText} | ||
| </div> | ||
| <div ref={inputWrapperRef}> | ||
| <Input | ||
| {...props} | ||
| className={classNames("prefixed-input__input", props.className)} | ||
| type="text" | ||
| wrapperClassName={classNames( | ||
| "prefixed-input__wrapper", | ||
| props.wrapperClassName, | ||
| )} | ||
| /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default PrefixedInput; |
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 @@ | ||
| export { default, type PrefixedInputProps } from "./PrefixedInput"; |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Percy, there is too much spacing between the prefix and the editable part for the ip input. Though I can't reproduce it locally with Firefox and the dev server running.