-
-
Notifications
You must be signed in to change notification settings - Fork 90
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
base: master
Are you sure you want to change the base?
Conversation
… in the value array
Good one |
stroke="currentColor" | ||
strokeWidth="2" | ||
className="dropdown-heading-dropdown-arrow gray" | ||
className="rmsc__arrow-svg dropdown-heading-dropdown-arrow gray" |
There was a problem hiding this comment.
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) => { |
There was a problem hiding this comment.
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] | ||
); | ||
|
There was a problem hiding this comment.
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] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>; | ||
} |
There was a problem hiding this comment.
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> | ||
); |
There was a problem hiding this comment.
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)} |
There was a problem hiding this comment.
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; | ||
} | ||
|
There was a problem hiding this comment.
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.
Please let me know if it's need any changes |
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: