Skip to content

Commit

Permalink
When calling onSelect with a keyboard event, pass in the event
Browse files Browse the repository at this point in the history
  • Loading branch information
hoorayimhelping committed Feb 12, 2025
1 parent 49d3dda commit 4547713
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/components/Select/MultiSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { useCallback, useState } from "react";
import { KeyboardEvent, useCallback, useState } from "react";

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

import { SelectContainerProps, SelectOptionProp, SelectionType } from "./common/types";
import {
SelectGroup,
SelectItem,
InternalSelect
} from "./common/InternalSelect";
import { SelectGroup, SelectItem, InternalSelect } from "./common/InternalSelect";

export interface MultiSelectProps
extends Omit<
SelectContainerProps,
"onChange" | "value" | "open" | "onOpenChange" | "onSelect"
> {
defaultValue?: Array<string>;
onSelect?: (value: Array<string>, type?: SelectionType) => void;
onSelect?: (
value: Array<string>,
type?: SelectionType,
evt?: KeyboardEvent<HTMLElement>
) => void;
value?: Array<string>;
defaultOpen?: boolean;
onOpenChange?: (open: boolean) => void;
Expand Down Expand Up @@ -50,10 +50,10 @@ export const MultiSelect = ({
}, [valueProp]);

const onChange = useCallback(
(values: Array<string>, type?: SelectionType) => {
(values: Array<string>, type?: SelectionType, evt?: KeyboardEvent<HTMLElement>) => {
setSelectedValues(values);
if (typeof onSelectProp === "function") {
onSelectProp(values, type);
onSelectProp(values, type, evt);
}
},
[onSelectProp]
Expand Down
12 changes: 8 additions & 4 deletions src/components/Select/SingleSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useState } from "react";
import { KeyboardEvent, useCallback, useState } from "react";

import { useUpdateEffect } from "@/hooks";

Expand All @@ -11,7 +11,11 @@ export interface SelectProps
"onChange" | "value" | "sortable" | "open" | "onOpenChange" | "onSelect"
> {
defaultValue?: string;
onSelect?: (value: string, type?: SelectionType) => void;
onSelect?: (
value: string,
type?: SelectionType,
evt?: KeyboardEvent<HTMLElement>
) => void;
value?: string;
placeholder?: string;
onOpenChange?: (open: boolean) => void;
Expand Down Expand Up @@ -48,7 +52,7 @@ export const Select = ({
);

const onSelect = useCallback(
(value: string, type?: SelectionType) => {
(value: string, type?: SelectionType, evt?: KeyboardEvent<HTMLElement>) => {
setSelectedValues(values => {
if (values[0] !== value) {
return [value];
Expand All @@ -57,7 +61,7 @@ export const Select = ({
});
onOpenChange(false);
if (typeof onSelectProp === "function") {
onSelectProp(value, type);
onSelectProp(value, type, evt);
}
},
[onSelectProp, onOpenChange]
Expand Down
4 changes: 2 additions & 2 deletions src/components/Select/common/InternalSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ export const InternalSelect = ({
if (e.key === "Enter") {
e.preventDefault();
if (highlighted) {
onSelect(highlighted);
onSelect(highlighted, "default", e);
} else if (visibleList.current.length === 0 && allowCreateOption) {
onSelect(search, "custom");
onSelect(search, "custom", e);
}
} else if (["ArrowUp", "ArrowDown", "Home", "End"].includes(e.key)) {
e.preventDefault();
Expand Down
8 changes: 6 additions & 2 deletions src/components/Select/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HTMLAttributes, ReactNode } from "react";
import { HTMLAttributes, KeyboardEvent, ReactNode } from "react";
import { HorizontalDirection, IconName } from "@/components";
import { PopoverProps } from "@radix-ui/react-popover";

Expand Down Expand Up @@ -74,7 +74,11 @@ interface InternalSelectProps
onOpenChange: (open: boolean) => void;
value: Array<string>;
sortable?: boolean;
onSelect: (value: string, type?: SelectionType) => void;
onSelect: (
value: string,
type?: SelectionType,
evt?: KeyboardEvent<HTMLElement>
) => void;
multiple?: boolean;
checkbox?: boolean;
selectLabel?: string;
Expand Down

0 comments on commit 4547713

Please sign in to comment.