Skip to content

Commit

Permalink
fix: label placement when the select has a placeholder or description (
Browse files Browse the repository at this point in the history
  • Loading branch information
tianenpang authored Nov 20, 2024
1 parent 52822e9 commit 9e8beab
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-brooms-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@nextui-org/select": patch
---

Fix the label placement when the `Select` has a `placeholder` or `description`.
3 changes: 2 additions & 1 deletion packages/components/select/__tests__/select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -723,11 +723,12 @@ describe("Select", () => {
expect(onChange).toHaveBeenCalledTimes(1);
});

it("should place the label outside when labelPlacement is outside", () => {
it("should place the label outside when labelPlacement is outside and isMultiline enabled", () => {
const labelContent = "Favorite Animal Label";

render(
<Select
isMultiline
aria-label="Favorite Animal"
data-testid="select"
label={labelContent}
Expand Down
3 changes: 2 additions & 1 deletion packages/components/select/src/use-select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ export function useSelect<T extends object>(originalProps: UseSelectProps<T>) {
const hasPlaceholder = !!placeholder;
const shouldLabelBeOutside =
labelPlacement === "outside-left" ||
(labelPlacement === "outside" && (hasPlaceholder || !!originalProps.isMultiline));
(labelPlacement === "outside" &&
(!(hasPlaceholder || !!description) || !!originalProps.isMultiline));
const shouldLabelBeInside = labelPlacement === "inside";
const isOutsideLeft = labelPlacement === "outside-left";

Expand Down
37 changes: 37 additions & 0 deletions packages/components/select/stories/select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,43 @@ const LabelPlacementTemplate = ({color, variant, ...args}: SelectProps) => (
</Select>
</div>
</div>
<div className="w-full max-w-2xl flex flex-col gap-3">
<h3>With placeholder and description</h3>
<div className="w-full flex flex-row items-end gap-4">
<Select
color={color}
description="Select your favorite animal"
label="Favorite Animal"
placeholder="Select an animal"
variant={variant}
{...args}
>
{items}
</Select>
<Select
color={color}
description="Select your favorite animal"
label="Favorite Animal"
placeholder="Select an animal"
variant={variant}
{...args}
labelPlacement="outside"
>
{items}
</Select>
<Select
color={color}
description="Select your favorite animal"
label="Favorite Animal"
placeholder="Select an animal"
variant={variant}
{...args}
labelPlacement="outside-left"
>
{items}
</Select>
</div>
</div>
</div>
);

Expand Down

0 comments on commit 9e8beab

Please sign in to comment.