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

simplified code by using the includes method to check if an option is… #677

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Shorifpatwary
Copy link

The !! operator is a way to convert a value to a boolean. In this case, it is being used to convert the result of the find method to a boolean.

The find method returns the first element in an array that satisfies the provided predicate function. If no element is found, it returns undefined. When !! is used to convert undefined to a boolean, it becomes false.

So, the checked prop is being set to true if find returns an element that satisfies the predicate, and false if no element is found.

One way to rewrite this code would be to use the Boolean function to explicitly convert the result of find to a boolean:

@Roeefl
Copy link

Roeefl commented Dec 27, 2022

Good one

stroke="currentColor"
strokeWidth="2"
className="dropdown-heading-dropdown-arrow gray"
className="rmsc__arrow-svg dropdown-heading-dropdown-arrow gray"
Copy link
Author

Choose a reason for hiding this comment

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

Instead of using inline styles for the fill and stroke attributes, consider using CSS classes to style the SVG. This would allow you to more easily customize the appearance of the arrow and make the component more reusable.

});

const handleHover = (iexpanded: boolean) => {
isInternalExpand && shouldToggleOnHover && setExpanded(iexpanded);
const handleHover = (isExpanded: boolean) => {
Copy link
Author

Choose a reason for hiding this comment

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

fixed spelling mistake

isInternalExpand && setExpanded(false);
},
[onChange, isInternalExpand]
);

Copy link
Author

Choose a reason for hiding this comment

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

This will avoid creating a new function on every render, which can improve performance

</span>
const getSelectedText = useMemo(
() => () => value.map((s) => s.label).join(", "),
[value]
Copy link
Author

Choose a reason for hiding this comment

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

using the useMemo hook to memoize the getSelectedText function, to avoid recomputing it on every render.

Copy link

Choose a reason for hiding this comment

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

Why are you overcomplicating this by returning a function from useMemo ?

Could be simply a string returned from useMemo with no invocation

return <span>{selectedText || t("allItemsAreSelected")}</span>;
default:
return <span>{selectedText || getSelectedText()}</span>;
}
Copy link
Author

Choose a reason for hiding this comment

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

Instead of using a ternary operator to determine the text to display, consider using a switch statement. This would make the code easier to read and understand, and would allow you to add additional cases more easily.

type="checkbox"
onChange={onClick}
checked={checked}
tabIndex={-1}
disabled={disabled}
/>
<span>{option.label}</span>
</div>
</label>
);
Copy link
Author

Choose a reason for hiding this comment

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

use a more semantic HTML element for the label, such as a label element. This would improve the accessibility of the component and make it easier to style

@@ -37,7 +37,7 @@ const SelectList = ({ options, onClick, skipIndex }: ISelectListProps) => {
tabIndex={tabIndex}
option={o}
onSelectionChanged={(c) => handleSelectionChanged(o, c)}
checked={!!value.find((s) => s.value === o.value)}
checked={value.includes(o)}
onClick={(e) => onClick(e, tabIndex)}
Copy link
Author

Choose a reason for hiding this comment

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

simplify code by using the include method.

stroke: currentColor;
stroke-width: 2;
}

Copy link
Author

Choose a reason for hiding this comment

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

style SVG element using CSS value instead of writing hardcoded code. now, the user could override this value if he wants.

@Shorifpatwary
Copy link
Author

Shorifpatwary commented Dec 30, 2022

Please let me know if it's need any changes

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.

2 participants