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

feat(input): add outside-top prop #3660

Open
wants to merge 12 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
6 changes: 6 additions & 0 deletions .changeset/little-crabs-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/input": major
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
"@nextui-org/theme": patch
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
---

Fixing Issue 3058. Initially, you cannot present label outside the input component if there is no placeholder. To fix this, I have added a "outside-top" prop which displays the label outside regardless of the placeholder, just like "outside-left" prop.
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 12 additions & 7 deletions packages/components/input/src/input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const Input = forwardRef<"input", InputProps>((props, ref) => {
labelPlacement,
hasHelper,
isOutsideLeft,
isOutsideTop,
shouldLabelBeOutside,
errorMessage,
isInvalid,
Expand Down Expand Up @@ -48,9 +49,13 @@ const Input = forwardRef<"input", InputProps>((props, ref) => {
return (
<div {...getHelperWrapperProps()}>
{isInvalid && errorMessage ? (
<div {...getErrorMessageProps()}>{errorMessage}</div>
<div className="bg-green-500" {...getErrorMessageProps()}>
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
{errorMessage}
</div>
) : description ? (
<div {...getDescriptionProps()}>{description}</div>
<div className="bg-green-500" {...getDescriptionProps()}>
{description}
</div>
) : null}
</div>
);
Expand All @@ -66,7 +71,7 @@ const Input = forwardRef<"input", InputProps>((props, ref) => {

const innerWrapper = useMemo(() => {
return (
<div {...getInnerWrapperProps()}>
<div className="bg-yellow-700" {...getInnerWrapperProps()}>
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
{startContent}
<input {...getInputProps()} />
{end}
Expand All @@ -77,9 +82,9 @@ const Input = forwardRef<"input", InputProps>((props, ref) => {
const mainWrapper = useMemo(() => {
if (shouldLabelBeOutside) {
return (
<div {...getMainWrapperProps()}>
<div className="bg-pink-600" {...getMainWrapperProps()}>
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
<div {...getInputWrapperProps()}>
{!isOutsideLeft ? labelContent : null}
{!isOutsideLeft && !isOutsideTop ? labelContent : null}
{innerWrapper}
</div>
{helperWrapper}
Expand All @@ -89,7 +94,7 @@ const Input = forwardRef<"input", InputProps>((props, ref) => {

return (
<>
<div {...getInputWrapperProps()}>
<div className="bg-blue-900" {...getInputWrapperProps()}>
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
{labelContent}
{innerWrapper}
</div>
Expand All @@ -112,7 +117,7 @@ const Input = forwardRef<"input", InputProps>((props, ref) => {

return (
<Component {...getBaseProps()}>
{isOutsideLeft ? labelContent : null}
{isOutsideLeft || isOutsideTop ? labelContent : null}
{mainWrapper}
</Component>
);
Expand Down
19 changes: 16 additions & 3 deletions packages/components/input/src/use-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,22 +250,34 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
const hasPlaceholder = !!props.placeholder;
const hasLabel = !!label;
const hasHelper = !!description || !!errorMessage;
const shouldLabelBeOutside = labelPlacement === "outside" || labelPlacement === "outside-left";
const shouldLabelBeOutside =
labelPlacement === "outside" ||
labelPlacement === "outside-left" ||
labelPlacement === "outside-top";
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
const shouldLabelBeInside = labelPlacement === "inside";
const isPlaceholderShown = domRef.current
? (!domRef.current.value || domRef.current.value === "" || !inputValue || inputValue === "") &&
hasPlaceholder
: false;

const isOutsideLeft = labelPlacement === "outside-left";
const isOutsideTop = labelPlacement === "outside-top";
wingkwong marked this conversation as resolved.
Show resolved Hide resolved

const hasStartContent = !!startContent;

/*
outside -> label is outside only when some placeholder is there
outside-top, outside-left-> label is outside regardless of placeholder
*/
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
const isLabelOutside = shouldLabelBeOutside
? labelPlacement === "outside-left" ||
labelPlacement === "outside-top" ||
wingkwong marked this conversation as resolved.
Show resolved Hide resolved
hasPlaceholder ||
(labelPlacement === "outside" && hasStartContent)
: false;
const isLabelOutsideAsPlaceholder =
labelPlacement === "outside" && !hasPlaceholder && !hasStartContent;
const isLabelOutsideAsPlaceholder = labelPlacement === "outside" && !hasPlaceholder;
// &&
// !hasStartContent;
wingkwong marked this conversation as resolved.
Show resolved Hide resolved

const slots = useMemo(
() =>
Expand Down Expand Up @@ -523,6 +535,7 @@ export function useInput<T extends HTMLInputElement | HTMLTextAreaElement = HTML
hasStartContent,
isLabelOutside,
isOutsideLeft,
isOutsideTop,
isLabelOutsideAsPlaceholder,
shouldLabelBeOutside,
shouldLabelBeInside,
Expand Down
9 changes: 8 additions & 1 deletion packages/components/input/stories/input.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default {
control: {
type: "select",
},
options: ["inside", "outside", "outside-left"],
options: ["inside", "outside", "outside-left", "outside-top"],
},
isDisabled: {
control: {
Expand Down Expand Up @@ -178,6 +178,7 @@ const LabelPlacementTemplate = (args) => (
<Input {...args} description="inside" />
<Input {...args} description="outside" labelPlacement="outside" />
<Input {...args} description="outside-left" labelPlacement="outside-left" />
<Input {...args} description="outside-top" labelPlacement="outside-top" />
</div>
</div>
<div className="flex flex-col gap-3">
Expand All @@ -196,6 +197,12 @@ const LabelPlacementTemplate = (args) => (
labelPlacement="outside-left"
placeholder="Enter your email"
/>
<Input
{...args}
description="outside-top"
labelPlacement="outside-top"
placeholder="Enter your email"
/>
</div>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions packages/core/theme/src/components/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,12 @@ const input = tv({
outside: {
mainWrapper: "flex flex-col",
},
"outside-top": {
base: "flex-col items-center flex-nowrap data-[has-helper=true]:items-start",
inputWrapper: "flex-1",
mainWrapper: "flex flex-col",
label: "relative text-foreground pb-2",
},
abhinav700 marked this conversation as resolved.
Show resolved Hide resolved
abhinav700 marked this conversation as resolved.
Show resolved Hide resolved
"outside-left": {
base: "flex-row items-center flex-nowrap data-[has-helper=true]:items-start",
inputWrapper: "flex-1",
Expand Down