Skip to content

Commit fded710

Browse files
authored
fix(CheckboxMultiSelect): properly handle option clicks (#612)
1 parent ef775eb commit fded710

File tree

3 files changed

+21
-81
lines changed

3 files changed

+21
-81
lines changed
Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
import { ReactNode } from "react";
2+
23
import { HorizontalDirection, IconName } from "@/components";
3-
import { styled } from "styled-components";
4-
import { EllipsisContent } from "../EllipsisContent/EllipsisContent";
4+
import { Container, GapOptions } from "@/components/Container/Container";
5+
import { EllipsisContent } from "@/components/EllipsisContent/EllipsisContent";
56
import { Icon } from "@/components/Icon/Icon";
67
import { IconSize } from "@/components/Icon/types";
78

8-
const LabelContainer = styled.div<{ $hasIcon: boolean; $iconDir: HorizontalDirection }>`
9-
display: grid;
10-
grid-template-columns: ${({ $hasIcon, $iconDir }) =>
11-
`${$hasIcon && $iconDir === "start" ? "auto " : ""}1fr${
12-
$hasIcon && $iconDir === "end" ? " auto" : ""
13-
}`};
14-
align-items: center;
15-
justify-content: flex-start;
16-
17-
width: 100%;
18-
width: -webkit-fill-available;
19-
width: fill-available;
20-
width: stretch;
21-
gap: ${({ theme }) => theme.click.sidebar.navigation.item.default.space.gap};
22-
`;
23-
249
interface IconWrapperProps {
2510
icon?: IconName;
2611
iconDir?: HorizontalDirection;
@@ -29,6 +14,7 @@ interface IconWrapperProps {
2914
height?: number | string;
3015
children: ReactNode;
3116
ellipsisContent?: boolean;
17+
gap?: GapOptions;
3218
}
3319

3420
const IconWrapper = ({
@@ -39,13 +25,14 @@ const IconWrapper = ({
3925
height,
4026
children,
4127
ellipsisContent = true,
28+
gap = "sm",
4229
...props
4330
}: IconWrapperProps) => {
4431
const TextWrapper = ellipsisContent ? EllipsisContent : "div";
4532
return (
46-
<LabelContainer
47-
$hasIcon={typeof icon === "string"}
48-
$iconDir={iconDir}
33+
<Container
34+
orientation="horizontal"
35+
gap={gap}
4936
{...props}
5037
>
5138
{icon && iconDir === "start" && (
@@ -69,7 +56,7 @@ const IconWrapper = ({
6956
height={height}
7057
/>
7158
)}
72-
</LabelContainer>
59+
</Container>
7360
);
7461
};
7562
export default IconWrapper;

src/components/Select/SingleSelectValue.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const SingleSelectValue = ({
3131
<IconWrapper
3232
icon={icon}
3333
iconDir={iconDir}
34+
gap="xxs"
3435
>
3536
{label ?? children ?? value}
3637
</IconWrapper>

src/components/Select/common/InternalSelect.tsx

Lines changed: 11 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { Portal } from "@radix-ui/react-popover";
2525
import {
2626
Checkbox,
2727
CheckboxVariants,
28+
Container,
2829
Icon,
2930
IconButton,
3031
Label,
@@ -585,6 +586,7 @@ export const SelectItem = forwardRef<HTMLDivElement, SelectItemProps>(
585586
<IconWrapper
586587
icon={icon}
587588
iconDir={iconDir}
589+
gap="xxs"
588590
>
589591
{label ?? children}
590592
</IconWrapper>
@@ -630,7 +632,7 @@ export const MultiSelectCheckboxItem = forwardRef<
630632
const { highlighted, updateHighlighted, isHidden, selectedValues, onSelect } =
631633
useOption();
632634

633-
const handleSelect = (evt: MouseEvent<HTMLElement>) => {
635+
const handleMenuItemClick = (evt: MouseEvent<HTMLElement>) => {
634636
if (!disabled) {
635637
onSelect(value, undefined, evt);
636638

@@ -640,21 +642,6 @@ export const MultiSelectCheckboxItem = forwardRef<
640642
}
641643
};
642644

643-
const handleMenuItemClick = (evt: MouseEvent<HTMLElement>) => {
644-
// Clicking checkbox label fires another click event on the checkbox input.
645-
// They cancel each other out, so we handle checkbox clicks separately,
646-
// and this covers the outside area.
647-
if (evt.target !== evt.currentTarget) {
648-
return;
649-
}
650-
651-
handleSelect(evt);
652-
};
653-
654-
const handleCheckboxClick = (evt: MouseEvent<HTMLButtonElement>): void => {
655-
handleSelect(evt);
656-
};
657-
658645
const handleMenuItemMouseOver = (e: MouseEvent<HTMLDivElement>) => {
659646
if (onMouseOverProp) {
660647
onMouseOverProp(e);
@@ -683,59 +670,24 @@ export const MultiSelectCheckboxItem = forwardRef<
683670
data-testid={`multi-select-checkbox-${value}`}
684671
cui-select-item=""
685672
>
686-
{icon && iconDir === "start" && (
673+
<Container
674+
orientation="horizontal"
675+
gap="xs"
676+
>
687677
<Checkbox
688678
checked={isChecked}
689679
data-testid="multi-select-checkbox"
690680
disabled={disabled}
691-
label={
692-
label ? (
693-
<div style={{ display: "flex", justifyContent: "center" }}>
694-
<Icon
695-
name={icon}
696-
size="sm"
697-
/>
698-
{label}
699-
</div>
700-
) : (
701-
<div style={{ display: "flex", justifyContent: "center" }}>
702-
<Icon
703-
name={icon}
704-
size="sm"
705-
/>
706-
{children}
707-
</div>
708-
)
709-
}
710-
onClick={handleCheckboxClick}
711681
variant={variant ?? "default"}
712682
/>
713-
)}
714-
{icon && iconDir === "end" && (
715683
<IconWrapper
716684
icon={icon}
717-
iconDir="end"
685+
iconDir={iconDir}
686+
gap="xxs"
718687
>
719-
<Checkbox
720-
checked={isChecked}
721-
data-testid="multi-select-checkbox"
722-
disabled={disabled}
723-
label={label ?? children}
724-
onClick={handleCheckboxClick}
725-
variant={variant ?? "default"}
726-
/>
688+
{label ?? children}
727689
</IconWrapper>
728-
)}
729-
{!icon && (
730-
<Checkbox
731-
checked={isChecked}
732-
data-testid="multi-select-checkbox"
733-
disabled={disabled}
734-
label={label ?? children}
735-
onClick={handleCheckboxClick}
736-
variant={variant ?? "default"}
737-
/>
738-
)}
690+
</Container>
739691
</GenericMenuItem>
740692
{separator && <Separator size="sm" />}
741693
</>

0 commit comments

Comments
 (0)