Skip to content

Commit 2df6e2a

Browse files
Select: when calling onSelect with a keyboard event, pass in the event (#544)
1 parent 49d3dda commit 2df6e2a

File tree

5 files changed

+49
-28
lines changed

5 files changed

+49
-28
lines changed

Diff for: src/components/Select/MultiSelect.tsx

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { useCallback, useState } from "react";
1+
import { KeyboardEvent, MouseEvent, useCallback, useState } from "react";
22

3-
import {useUpdateEffect} from "@/hooks";
3+
import { useUpdateEffect } from "@/hooks";
44

55
import { SelectContainerProps, SelectOptionProp, SelectionType } from "./common/types";
6-
import {
7-
SelectGroup,
8-
SelectItem,
9-
InternalSelect
10-
} from "./common/InternalSelect";
6+
import { SelectGroup, SelectItem, InternalSelect } from "./common/InternalSelect";
117

128
export interface MultiSelectProps
139
extends Omit<
1410
SelectContainerProps,
1511
"onChange" | "value" | "open" | "onOpenChange" | "onSelect"
1612
> {
1713
defaultValue?: Array<string>;
18-
onSelect?: (value: Array<string>, type?: SelectionType) => void;
14+
onSelect?: (
15+
value: Array<string>,
16+
type?: SelectionType,
17+
evt?: KeyboardEvent<HTMLElement> | MouseEvent<HTMLElement>
18+
) => void;
1919
value?: Array<string>;
2020
defaultOpen?: boolean;
2121
onOpenChange?: (open: boolean) => void;
@@ -50,10 +50,10 @@ export const MultiSelect = ({
5050
}, [valueProp]);
5151

5252
const onChange = useCallback(
53-
(values: Array<string>, type?: SelectionType) => {
53+
(values: Array<string>, type?: SelectionType, evt?: KeyboardEvent<HTMLElement>) => {
5454
setSelectedValues(values);
5555
if (typeof onSelectProp === "function") {
56-
onSelectProp(values, type);
56+
onSelectProp(values, type, evt);
5757
}
5858
},
5959
[onSelectProp]

Diff for: src/components/Select/SingleSelect.tsx

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useCallback, useState } from "react";
1+
import { KeyboardEvent, MouseEvent, useCallback, useState } from "react";
22

33
import { useUpdateEffect } from "@/hooks";
44

@@ -11,7 +11,11 @@ export interface SelectProps
1111
"onChange" | "value" | "sortable" | "open" | "onOpenChange" | "onSelect"
1212
> {
1313
defaultValue?: string;
14-
onSelect?: (value: string, type?: SelectionType) => void;
14+
onSelect?: (
15+
value: string,
16+
type?: SelectionType,
17+
evt?: KeyboardEvent<HTMLElement> | MouseEvent<HTMLElement>
18+
) => void;
1519
value?: string;
1620
placeholder?: string;
1721
onOpenChange?: (open: boolean) => void;
@@ -48,7 +52,11 @@ export const Select = ({
4852
);
4953

5054
const onSelect = useCallback(
51-
(value: string, type?: SelectionType) => {
55+
(
56+
value: string,
57+
type?: SelectionType,
58+
evt?: KeyboardEvent<HTMLElement> | MouseEvent<HTMLElement>
59+
) => {
5260
setSelectedValues(values => {
5361
if (values[0] !== value) {
5462
return [value];
@@ -57,7 +65,7 @@ export const Select = ({
5765
});
5866
onOpenChange(false);
5967
if (typeof onSelectProp === "function") {
60-
onSelectProp(value, type);
68+
onSelectProp(value, type, evt);
6169
}
6270
},
6371
[onSelectProp, onOpenChange]

Diff for: src/components/Select/common/InternalSelect.tsx

+9-9
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ export const InternalSelect = ({
250250
if (e.key === "Enter") {
251251
e.preventDefault();
252252
if (highlighted) {
253-
onSelect(highlighted);
253+
onSelect(highlighted, undefined, e);
254254
} else if (visibleList.current.length === 0 && allowCreateOption) {
255-
onSelect(search, "custom");
255+
onSelect(search, "custom", e);
256256
}
257257
} else if (["ArrowUp", "ArrowDown", "Home", "End"].includes(e.key)) {
258258
e.preventDefault();
@@ -306,7 +306,7 @@ export const InternalSelect = ({
306306
e.preventDefault();
307307
e.stopPropagation();
308308
if (allowCreateOption) {
309-
onSelect(search, "custom");
309+
onSelect(search, "custom", e);
310310
}
311311
};
312312

@@ -547,11 +547,11 @@ export const SelectItem = forwardRef<HTMLDivElement, SelectItemProps>(
547547
) => {
548548
const { highlighted, updateHighlighted, isHidden, selectedValues, onSelect } =
549549
useOption();
550-
const onSelectValue = () => {
550+
const onSelectValue = (evt: MouseEvent<HTMLElement>) => {
551551
if (!disabled) {
552-
onSelect(value);
552+
onSelect(value, undefined, evt);
553553
if (typeof onSelectProp == "function") {
554-
onSelectProp(value);
554+
onSelectProp(value, undefined, evt);
555555
}
556556
}
557557
};
@@ -629,11 +629,11 @@ export const MultiSelectCheckboxItem = forwardRef<
629629
) => {
630630
const { highlighted, updateHighlighted, isHidden, selectedValues, onSelect } =
631631
useOption();
632-
const onSelectValue = () => {
632+
const onSelectValue = (evt: MouseEvent<HTMLElement>) => {
633633
if (!disabled) {
634-
onSelect(value);
634+
onSelect(value, undefined, evt);
635635
if (typeof onSelectProp == "function") {
636-
onSelectProp(value);
636+
onSelectProp(value, undefined, evt);
637637
}
638638
}
639639
};

Diff for: src/components/Select/common/OptionContext.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import { createContext } from "react";
1+
import { createContext, KeyboardEvent, MouseEvent } from "react";
2+
import { SelectionType } from "./types";
23

34
type OptionContextProps = {
45
search: string;
56
highlighted?: string;
67
updateHighlighted: (value: string) => void;
78
isHidden: (value?: string) => boolean;
89
selectedValues: Array<string>;
9-
onSelect: (value: string) => void;
10+
onSelect: (
11+
value: string,
12+
type?: SelectionType,
13+
evt?: KeyboardEvent<HTMLElement> | MouseEvent<HTMLElement>
14+
) => void;
1015
showCheck?: boolean;
1116
};
1217

Diff for: src/components/Select/common/types.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HTMLAttributes, ReactNode } from "react";
1+
import { HTMLAttributes, KeyboardEvent, MouseEvent, ReactNode } from "react";
22
import { HorizontalDirection, IconName } from "@/components";
33
import { PopoverProps } from "@radix-ui/react-popover";
44

@@ -8,7 +8,11 @@ interface SelectItemComponentProps
88
extends Omit<DivProps, "disabled" | "onSelect" | "value" | "children"> {
99
separator?: boolean;
1010
disabled?: boolean;
11-
onSelect?: (value: string) => void;
11+
onSelect?: (
12+
value: string,
13+
type?: SelectionType,
14+
evt?: KeyboardEvent<HTMLElement> | MouseEvent<HTMLElement>
15+
) => void;
1216
value: string;
1317
icon?: IconName;
1418
iconDir?: HorizontalDirection;
@@ -74,7 +78,11 @@ interface InternalSelectProps
7478
onOpenChange: (open: boolean) => void;
7579
value: Array<string>;
7680
sortable?: boolean;
77-
onSelect: (value: string, type?: SelectionType) => void;
81+
onSelect: (
82+
value: string,
83+
type?: SelectionType,
84+
evt?: KeyboardEvent<HTMLElement> | MouseEvent<HTMLElement>
85+
) => void;
7886
multiple?: boolean;
7987
checkbox?: boolean;
8088
selectLabel?: string;

0 commit comments

Comments
 (0)