Skip to content

Commit

Permalink
fix(autocomplete): endContent prop (#1902)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgarciadev authored Nov 6, 2023
1 parent 6a6d426 commit 85a820e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/beige-tips-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@nextui-org/autocomplete": patch
"@nextui-org/theme": patch
---

Fix #1893, `endContent` prop fixed.
3 changes: 2 additions & 1 deletion apps/docs/content/docs/components/autocomplete.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ properties to customize the popover, listbox and input components.
| scrollShadowProps | [ScrollShadowProps](/docs/components/scroll-shadow#api) | Props to be passed to the ScrollShadow component. | - |
| selectorButtonProps | [ButtonProps](/docs/components/button#api) | Props to be passed to the selector button. | - |
| clearButtonProps | [ButtonProps](/docs/components/button#api) | Props to be passed to the clear button. | - |
| disableClearable | `boolean` | Whether the clear button should be hidden. | `false` |
| isClearable | `boolean` | Whether the clear button should be shown. | `true` |
| disableClearable | `boolean` | Whether the clear button should be hidden. (**Deprecated**) Use `isClearable` instead. | `false` |
| disableAnimation | `boolean` | Whether the Autocomplete should be animated. | `true` |
| disableSelectorIconRotation | `boolean` | Whether the select should disable the rotation of the selector icon. | `false` |
| classNames | `Record<"base"| "listboxWrapper"| "listbox"| "popoverContent" | "endContentWrapper"| "clearButton" | "selectorButton", string>` | Allows to set custom class names for the Autocomplete slots. | - |
Expand Down
3 changes: 2 additions & 1 deletion packages/components/autocomplete/src/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function Autocomplete<T extends object>(props: Props<T>, ref: ForwardedRef<HTMLI
disableAnimation,
selectorIcon = <ChevronDownIcon />,
clearIcon = <CloseIcon />,
endContent,
getBaseProps,
getSelectorButtonProps,
getInputProps,
Expand All @@ -44,7 +45,7 @@ function Autocomplete<T extends object>(props: Props<T>, ref: ForwardedRef<HTMLI
{...getInputProps()}
endContent={
<div {...getEndContentWrapperProps()}>
<Button {...getClearButtonProps()}>{clearIcon}</Button>
{endContent || <Button {...getClearButtonProps()}>{clearIcon}</Button>}
<Button {...getSelectorButtonProps()}>{selectorIcon}</Button>
</div>
}
Expand Down
20 changes: 18 additions & 2 deletions packages/components/autocomplete/src/use-autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ interface Props<T> extends Omit<HTMLNextUIProps<"input">, keyof ComboBoxProps<T>
* @default { disableAnimation: false }
*/
inputProps?: Partial<InputProps>;
/**
* Whether the clear button should be hidden.
* @default false
* @deprecated Use `isClearable` instead.
*/
disableClearable?: boolean;
/**
* Props to be passed to the selector button component.
* @default { size: "sm", variant: "light", radius: "full", isIconOnly: true }
Expand Down Expand Up @@ -104,7 +110,7 @@ interface Props<T> extends Omit<HTMLNextUIProps<"input">, keyof ComboBoxProps<T>
}

export type UseAutocompleteProps<T> = Props<T> &
Omit<InputProps, "children" | "value" | "defaultValue" | "classNames"> &
Omit<InputProps, "children" | "value" | "isClearable" | "defaultValue" | "classNames"> &
ComboBoxProps<T> &
AsyncLoadable &
AutocompleteVariantProps;
Expand All @@ -113,6 +119,12 @@ export function useAutocomplete<T extends object>(originalProps: UseAutocomplete
const [props, variantProps] = mapPropsVariants(originalProps, autocomplete.variantKeys);
const disableAnimation = originalProps.disableAnimation ?? false;

// TODO: Remove disableClearable prop in the next minor release.
const isClearable =
originalProps.disableClearable !== undefined
? !originalProps.disableClearable
: originalProps.isClearable;

const {
ref,
as,
Expand All @@ -127,6 +139,7 @@ export function useAutocomplete<T extends object>(originalProps: UseAutocomplete
clearIcon,
scrollRef: scrollRefProp,
defaultFilter,
endContent,
allowsEmptyCollection = true,
shouldCloseOnBlur = true,
popoverProps = {},
Expand Down Expand Up @@ -286,10 +299,11 @@ export function useAutocomplete<T extends object>(originalProps: UseAutocomplete
() =>
autocomplete({
...variantProps,
isClearable,
disableAnimation,
className,
}),
[...Object.values(variantProps), disableAnimation, className],
[...Object.values(variantProps), isClearable, disableAnimation, className],
);

const onClear = useCallback(() => {
Expand Down Expand Up @@ -413,6 +427,8 @@ export function useAutocomplete<T extends object>(originalProps: UseAutocomplete
isLoading,
clearIcon,
isOpen,
endContent,
isClearable,
disableAnimation,
allowsCustomValue,
selectorIcon,
Expand Down
22 changes: 22 additions & 0 deletions packages/components/autocomplete/stories/autocomplete.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,20 @@ const StartContentTemplate = ({color, variant, ...args}: AutocompleteProps) => (
</Autocomplete>
);

const EndContentTemplate = ({color, variant, ...args}: AutocompleteProps) => (
<Autocomplete
className="max-w-xs"
color={color}
defaultSelectedKey={"cat"}
endContent={<PetBoldIcon className="text-xl" />}
label="Favorite Animal"
variant={variant}
{...args}
>
{items}
</Autocomplete>
);

const DynamicTemplateWithDescriptions = ({color, variant, ...args}: AutocompleteProps<Animal>) => (
<Autocomplete
className="max-w-xs"
Expand Down Expand Up @@ -693,6 +707,14 @@ export const StartContent = {
},
};

export const EndContent = {
render: EndContentTemplate,

args: {
...defaultProps,
},
};

export const WithoutScrollShadow = {
render: Template,

Expand Down
8 changes: 4 additions & 4 deletions packages/core/theme/src/components/autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const autocomplete = tv({
selectorButton: "text-medium",
},
variants: {
disableClearable: {
true: {
isClearable: {
true: {},
false: {
clearButton: "hidden",
},
false: {},
},
disableAnimation: {
true: {
Expand All @@ -47,7 +47,7 @@ const autocomplete = tv({
},
defaultVariants: {
disableAnimation: false,
disableClearable: false,
isClearable: true,
disableSelectorIconRotation: false,
},
});
Expand Down

2 comments on commit 85a820e

@vercel
Copy link

@vercel vercel bot commented on 85a820e Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 85a820e Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.