Skip to content

Commit

Permalink
Merge branch 'feat/favourite_button' into alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
mguellsegarra committed Jan 25, 2024
2 parents 6d0d74b + 4f39c20 commit 79f7339
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/components/ui/Dropdown/Dropdown.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Default.args = {
await new Promise((resolve) => setTimeout(resolve, 1000));
return exampleData;
},
header: <div>This is a header</div>,
children: (
<button
style={{
Expand Down
45 changes: 31 additions & 14 deletions src/components/ui/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import { memo, useCallback, useState } from "react";
import {
memo,
useCallback,
useState,
useImperativeHandle,
forwardRef,
} from "react";
import { Dropdown as AntDropdown } from "antd";
import { DropdownProps } from "./Dropdown.types";
import { DropdownProps, DropdownRef } from "./Dropdown.types";
import { DropdownMenu, flattenDropdownItems } from "./DropdownMenu";
import ErrorBoundary from "antd/es/alert/ErrorBoundary";

export const Dropdown: React.FC<DropdownProps> = memo(
({
onRetrieveData,
onItemClick,
disabled = false,
searchable = "auto",
children,
trigger = ["click"],
placement,
maxHeight,
onOpenChange,
}: DropdownProps) => {
const DropdownComponent = forwardRef<DropdownRef, DropdownProps>(
(
{
onRetrieveData,
onItemClick,
disabled = false,
searchable = "auto",
children,
trigger = ["click"],
placement,
maxHeight,
header,
onOpenChange,
},
ref,
) => {
const [internalOpen, setInternalOpen] = useState(false);
const [emptyMenu, setEmptyMenu] = useState(false);

Expand All @@ -25,12 +35,17 @@ export const Dropdown: React.FC<DropdownProps> = memo(
return data;
}, [onRetrieveData]);

useImperativeHandle(ref, () => ({
close: () => setInternalOpen(false),
}));

return (
<ErrorBoundary>
<AntDropdown
dropdownRender={() => {
return (
<DropdownMenu
header={header}
maxHeight={maxHeight}
searchable={searchable}
onRetrieveData={onRetrieveDataCallback}
Expand Down Expand Up @@ -58,4 +73,6 @@ export const Dropdown: React.FC<DropdownProps> = memo(
},
);

export const Dropdown = memo(DropdownComponent);
DropdownComponent.displayName = "DropdownComponent";
Dropdown.displayName = "Dropdown";
5 changes: 5 additions & 0 deletions src/components/ui/Dropdown/Dropdown.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type BaseDropdownProps = {
searchable?: true | false | "auto";
onItemClick?: (item: DropdownMenuItem) => void;
onRetrieveData: () => Promise<DropdownMenuGroup[]>;
header?: ReactNode;
};

declare const Placements: readonly [
Expand Down Expand Up @@ -51,3 +52,7 @@ export type DropdownMenuItem = DropdownMenuItemType &
export type DropdownMenuItemType = {
type?: "item" | "divider";
};

export type DropdownRef = {
close: () => void;
};
2 changes: 2 additions & 0 deletions src/components/ui/Dropdown/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const DropdownMenu = ({
searchable,
onItemClick,
maxHeight,
header = null,
}: BaseDropdownProps) => {
const inputRef = useRef<any>(null);
const [searchValue, setSearchValue] = useState<string>();
Expand Down Expand Up @@ -99,6 +100,7 @@ export const DropdownMenu = ({

return (
<Root>
{header}
{mustShowSearch && (
<div style={{ padding: 5 }}>
<Search
Expand Down
23 changes: 23 additions & 0 deletions src/components/ui/FavouriteButton/FavouriteButton.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { FC } from "react";
import { FavouriteButton } from "./FavouriteButton";
import { FavouriteButtonProps } from "./FavouriteButton.types";
import { Default as DefaultStory } from "../Dropdown/Dropdown.stories";

export default {
title: "Components/FavouriteButton",
component: FavouriteButton,
};

const Template: FC<FavouriteButtonProps> = (args) => (
<FavouriteButton {...args} />
);

export const Default = Template.bind({});
Default.args = {
onRetrieveData: async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
return DefaultStory.exampleData;
},
header: <strong>Your shortcuts</strong>,
label: "FavouriteButton",
};
49 changes: 49 additions & 0 deletions src/components/ui/FavouriteButton/FavouriteButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { StarOutlined, StarFilled, DownOutlined } from "@ant-design/icons";
import { Button } from "antd";
import { FavouriteButtonProps } from "./FavouriteButton.types";
import { Fragment, forwardRef, useImperativeHandle, useRef } from "react";
import { Dropdown, DropdownRef } from "@/components";

export const FavouriteButton = forwardRef<DropdownRef, FavouriteButtonProps>(
({ isFavourite, onToggleFavourite, ...rest }, ref) => {
const dropdownRef = useRef<DropdownRef>(null);

useImperativeHandle(ref, () => ({
close: () => dropdownRef.current?.close(),
}));

return (
<Fragment>
<Button
type={isFavourite ? "primary" : "default"}
icon={
isFavourite ? (
<StarFilled style={{ color: "white" }} />
) : (
<StarOutlined />
)
}
style={{
width: 50,
borderTopRightRadius: 0,
borderBottomRightRadius: 0,
}}
onClick={onToggleFavourite}
></Button>
<Dropdown ref={dropdownRef} {...rest}>
<Button
style={{
width: 25,
borderTopLeftRadius: 0,
borderBottomLeftRadius: 0,
}}
icon={<DownOutlined style={{ fontSize: "0.5em" }} />}
onClick={(e) => e.preventDefault()}
></Button>
</Dropdown>
</Fragment>
);
},
);

FavouriteButton.displayName = "FavouriteButton";
6 changes: 6 additions & 0 deletions src/components/ui/FavouriteButton/FavouriteButton.types.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DropdownProps } from "@/components";

export type FavouriteButtonProps = {
isFavourite: boolean;
onToggleFavourite: () => void;
} & DropdownProps;
2 changes: 2 additions & 0 deletions src/components/ui/FavouriteButton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./FavouriteButton";
export * from "./FavouriteButton.types";
1 change: 1 addition & 0 deletions src/components/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./Label";
export * from "./Separator";
export * from "./FieldSet";
export * from "./Dropdown";
export * from "./FavouriteButton";

0 comments on commit 79f7339

Please sign in to comment.