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: close dropdown on item click #111

Merged

Conversation

theflucs
Copy link
Contributor

@theflucs theflucs commented Feb 7, 2024

Added the functionality of closing the dropdown as soon as one of its item is clicked.

There are few repetitions in the code, though:
one is about the export dropdown, which can be resolved creating an ExportDropdownButton component.
The second one is about the use of the closeDropdownOnItemClick util function, which is attached to the dropdown anytime we use it. If we decide to have this functionality throughout the app, maybe a component wrapper could be helpful?

@Balastrong what do you think about these considerations?

I leave the PR on draft for now.
Thank you

Copy link

netlify bot commented Feb 7, 2024

👷 Deploy request for public-github-stats pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 68b8d3b

@Balastrong
Copy link
Member

Instead of writing an util to close the dropdown, which we have to remember to add on each new dropdown, I'd probably go with a generic Dropdown component that handles the logic by itself.

@theflucs
Copy link
Contributor Author

theflucs commented Feb 26, 2024

@Balastrong this refactoring has been quite a journey! At the end of which I am not sure it's worth to have it, because It added a level of complexity to the code base. Have a look and tell me what you think. Anyway it's been interesting to implement it for the sake of learning.
I also had to touch the ThemeSelector test, see the fix test error commit. It's not clear to me why it got broken after my changes.
Thank you

@theflucs theflucs marked this pull request as ready for review February 26, 2024 08:13
Copy link
Member

@Balastrong Balastrong left a comment

Choose a reason for hiding this comment

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

I think it overall looks good :)

@@ -0,0 +1,62 @@
import { FC, ReactElement } from "react";

export type DropdownProps = React.PropsWithChildren<{
Copy link
Member

Choose a reason for hiding this comment

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

Do you really need React.PropsWithChildren?

items: (React.HTMLProps<HTMLLIElement> & {
["data-testid"]?: string;
onClick?: () => void;
renderItem: string | ReactElement;
Copy link
Member

Choose a reason for hiding this comment

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

With ReactElement you can remove string

Suggested change
renderItem: string | ReactElement;
renderItem: ReactElement;

align?: "dropdown-end";
renderButton: ReactElement;
items: (React.HTMLProps<HTMLLIElement> & {
["data-testid"]?: string;
Copy link
Member

Choose a reason for hiding this comment

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

I don't think you need the square brackets here (and the other places where data-testid is used in ThemeSelector.jsx)

Comment on lines 44 to 45
{items.map((item, index) => {
const { onClick, renderItem, ...liProps } = item;
Copy link
Member

Choose a reason for hiding this comment

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

If you don't need item, you can already destructure it in the loop

Suggested change
{items.map((item, index) => {
const { onClick, renderItem, ...liProps } = item;
{items.map(({ onClick, renderItem, ...liProps }, index) => {

items: (HTMLProps<HTMLLIElement> & {
"data-testid"?: string;
onClick?: () => void;
renderItem: ReactElement;
Copy link
Member

Choose a reason for hiding this comment

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

Sorry I was wrong here, you can remove string only if you replace ReactElement with ReactNode.

With ReactNode you don't need to wrap in <span> the strings.

Copy link
Member

@Balastrong Balastrong left a comment

Choose a reason for hiding this comment

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

Thank you for all the changes, LGTM!

@Balastrong Balastrong merged commit d8f3b16 into DevLeonardoCommunity:main Feb 28, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants