-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: LimitedInput component WIP #134
Draft
ndv99
wants to merge
3
commits into
canonical:main
Choose a base branch
from
ndv99:feat-limited-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.
Draft
Changes from 2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,15 @@ | ||
@import "vanilla-framework"; | ||
|
||
.limited-input { | ||
position: relative; | ||
|
||
&__wrapper::before { | ||
position: absolute; | ||
pointer-events: none; | ||
padding-left: $spv--small; | ||
padding-bottom: calc(0.4rem - 1px); | ||
padding-top: calc(0.4rem - 1px); | ||
content: var(--immutable, ""); | ||
top: var(--top, "inherit"); | ||
} | ||
} |
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,20 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { LimitedInput } from "@/lib/elements/LimitedInput/LimitedInput"; | ||
|
||
const meta: Meta<typeof LimitedInput> = { | ||
title: "elements/LimitedInput", | ||
component: LimitedInput, | ||
tags: ["autodocs"], | ||
}; | ||
|
||
export default meta; | ||
|
||
export const Example: StoryObj<typeof LimitedInput> = { | ||
args: { | ||
help: "The valid range for this subnet is 192.168.0.[1-254]", | ||
label: "IP address", | ||
immutableText: "192.168.0.", | ||
placeholder: "[1-254]", | ||
}, | ||
}; |
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,38 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
|
||
import { LimitedInput } from "./LimitedInput"; | ||
|
||
const { getComputedStyle } = window; | ||
|
||
beforeAll(() => { | ||
// getComputedStyle is not implemeneted in jsdom, so we need to do this. | ||
window.getComputedStyle = (elt) => getComputedStyle(elt); | ||
}); | ||
|
||
afterAll(() => { | ||
// Reset to original implementation | ||
window.getComputedStyle = getComputedStyle; | ||
}); | ||
|
||
it("renders without crashing", async () => { | ||
render(<LimitedInput aria-label="Limited input" immutableText="Some text" />); | ||
|
||
expect( | ||
screen.getByRole("textbox", { name: "Limited input" }), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it("sets the --immutable css variable to the provided immutable text", async () => { | ||
const { rerender } = render( | ||
<LimitedInput aria-label="Limited input" immutableText="Some text" />, | ||
); | ||
|
||
rerender( | ||
<LimitedInput aria-label="Limited input" immutableText="Some text" />, | ||
); | ||
|
||
expect( | ||
screen.getByRole("textbox", { name: "Limited input" }).parentElement | ||
?.parentElement, | ||
).toHaveStyle(`--immutable: "Some text";`); | ||
}); |
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,58 @@ | ||
import { RefObject, useEffect, useRef } from "react"; | ||
|
||
import { Input, InputProps } from "@canonical/react-components"; | ||
import "./LimitedInput.scss"; | ||
import classNames from "classnames"; | ||
|
||
export type LimitedInputProps = Omit<InputProps, "type"> & { | ||
immutableText: string; | ||
}; | ||
|
||
/** | ||
* An input component with a fixed prefix that cannot be edited. | ||
* | ||
* @param immutableText The prefixed text that cannot be edited. | ||
* @returns A LimitedInput component. | ||
*/ | ||
export const LimitedInput = ({ | ||
immutableText, | ||
...props | ||
}: LimitedInputProps) => { | ||
const limitedInputRef: RefObject<HTMLDivElement> = useRef(null); | ||
|
||
const inputWrapper = limitedInputRef.current?.firstElementChild; | ||
|
||
useEffect(() => { | ||
if (inputWrapper) { | ||
if (props.label) { | ||
inputWrapper.setAttribute( | ||
"style", | ||
`--top: 2.5rem; --immutable: "${immutableText}"`, | ||
); | ||
} else { | ||
inputWrapper.setAttribute("style", `--immutable: "${immutableText}"`); | ||
} | ||
|
||
const width = window.getComputedStyle(inputWrapper, ":before").width; | ||
|
||
inputWrapper.lastElementChild?.firstElementChild?.setAttribute( | ||
"style", | ||
`padding-left: ${width}`, | ||
); | ||
} | ||
}, [inputWrapper]); | ||
|
||
return ( | ||
<div className="limited-input" ref={limitedInputRef}> | ||
<Input | ||
className={classNames("limited-input__input", props.className)} | ||
type="text" | ||
wrapperClassName={classNames( | ||
"limited-input__wrapper", | ||
props.wrapperClassName, | ||
)} | ||
{...props} | ||
/> | ||
</div> | ||
); | ||
}; |
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 @@ | ||
export * from "./LimitedInput"; |
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
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.
I'm aware that this isn't a perfect solution, but for now it's workable as advised by @petermakowski. I'll create a jira ticket to investigate this implementation and find something better, but for now this should unblock canonical/maas-ui#5420