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

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1c3714a
feat(input): add outside-top prop to the input component
abhinav700 Aug 16, 2024
0dc983c
feat(input): adding changeset
abhinav700 Aug 16, 2024
8f07cc8
docs(input): updated docs for label placement section of input component
abhinav700 Aug 16, 2024
d618395
Merge branch 'nextui-org:canary' into fix-3058/adding_outside-top_prop
abhinav700 Aug 17, 2024
5d977dc
fix(input.tsx): remove bg classes that I added for testing and debugg…
abhinav700 Aug 17, 2024
c9de014
fix(changeset): update changeset as advised in review for PR 3660
abhinav700 Aug 17, 2024
d2748cf
fix(use-input.ts): change comment position as advised in review for …
abhinav700 Aug 17, 2024
612100c
fix(use-input.ts): remove comment as advised in review for PR 3660
abhinav700 Aug 17, 2024
43715c0
fix(use-input.ts): fixed typo
abhinav700 Aug 17, 2024
11b515b
Merge branch 'nextui-org:canary' into fix-3058/adding_outside-top_prop
abhinav700 Oct 7, 2024
1de9a26
chore(input): refactoring
abhinav700 Oct 11, 2024
2d9031c
chore(input): bump nextui-org/theme to next target version and make l…
abhinav700 Oct 13, 2024
0871105
chore(changeset): change to patch instead
wingkwong Nov 22, 2024
feb0f8f
chore(deps): update peerDependencies
wingkwong Nov 22, 2024
efeca94
chore(deps): update peerDependencies
wingkwong Nov 22, 2024
f88b474
Merge branch 'beta/release-next' into pr/3660
wingkwong Nov 22, 2024
f662fb2
refactor(input): revise layout
wingkwong Nov 22, 2024
120f5e2
Merge branch 'beta/release-next' into pr/3660
wingkwong Nov 29, 2024
ee34d49
feat(docs): add outside-top
wingkwong Nov 29, 2024
c7246b1
docs(input): notes for the labelplacement properties
abhinav700 Nov 29, 2024
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
1 change: 1 addition & 0 deletions apps/docs/content/components/input/label-placements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export default function App() {
"inside",
"outside",
"outside-left",
"outside-top"
];

return (
Expand Down
2 changes: 1 addition & 1 deletion apps/docs/content/docs/components/input.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ the end of the label and the input will be required.

### Label Placements

You can change the position of the label by setting the `labelPlacement` property to `inside`, `outside` or `outside-left`.
You can change the position of the label by setting the `labelPlacement` property to `inside`, `outside`, `outside-left` or `outside-top`.

<CodeDemo title="Label Placements" files={inputContent.labelPlacements} />

Expand Down
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