Skip to content

Commit

Permalink
fix: pass string onChange when value is string for selects (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztofzuraw authored Jan 19, 2024
1 parent 8748279 commit 2d47594
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/components/Combobox/Common/useCombobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { FocusEvent, useState } from "react";

import { Option, SingleChangeHandler } from "~/components/BaseSelect";
import { isString } from "~/utils";

const getItemsFilter = <T extends Option>(
inputValue: string | undefined,
Expand All @@ -26,13 +25,15 @@ const getItemsFilter = <T extends Option>(
export const useCombobox = <T extends Option, V extends string | Option>({
selectedItem,
options,
isValuePassedAsString,
onChange,
onInputValueChange,
onFocus,
onBlur,
}: {
selectedItem: T | null | undefined;
options: T[];
isValuePassedAsString: boolean;
onChange?: SingleChangeHandler<V | null>;
onInputValueChange?: (value: string) => void;
onFocus?: (e: FocusEvent<HTMLInputElement, Element>) => void;
Expand All @@ -58,7 +59,7 @@ export const useCombobox = <T extends Option, V extends string | Option>({
selectedItem,
onSelectedItemChange: ({ selectedItem }) => {
if (selectedItem) {
const selectedValue = isString(selectedItem)
const selectedValue = isValuePassedAsString
? selectedItem.value
: selectedItem;
onChange?.(selectedValue as V);
Expand Down
1 change: 1 addition & 0 deletions src/components/Combobox/Dynamic/DynamicCombobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const DynamicComboboxInner = <T extends Option>(
} = useCombobox({
selectedItem: value,
options,
isValuePassedAsString: false,
onChange,
onInputValueChange,
onFocus,
Expand Down
5 changes: 4 additions & 1 deletion src/components/Combobox/Static/Combobox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ const ComboboxInner = <T extends Option, V extends Option | string>(
}: ComboboxProps<T, V>,
ref: ForwardedRef<HTMLInputElement>
) => {
const isValuePassedAsString = isString(value);

const {
active,
typed,
Expand All @@ -85,9 +87,10 @@ const ComboboxInner = <T extends Option, V extends Option | string>(
itemsToSelect,
hasItemsToSelect,
} = useCombobox({
selectedItem: isString(value)
selectedItem: isValuePassedAsString
? options.find((option) => option.value === value)
: value,
isValuePassedAsString,
options,
onChange,
onFocus,
Expand Down
4 changes: 2 additions & 2 deletions src/components/Multiselect/Common/useMultiselect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const useMultiselect = <T extends Option, V extends Option | string>({
case useMultipleSelection.stateChangeTypes.DropdownKeyDownBackspace:
case useMultipleSelection.stateChangeTypes
.FunctionRemoveSelectedItem: {
const selected = isStringArray(selectedItems)
const selected = isStringArray(selectedValues)
? newSelectedItems?.map((item) => item.value)
: newSelectedItems;
onChange?.(selected as V[]);
Expand Down Expand Up @@ -130,7 +130,7 @@ export const useMultiselect = <T extends Option, V extends Option | string>({
case useCombobox.stateChangeTypes.ItemClick:
case useCombobox.stateChangeTypes.InputBlur:
if (newSelectedItem) {
const selected = isStringArray(selectedItems)
const selected = isStringArray(selectedValues)
? [...selectedItems.map((i) => i.value), newSelectedItem.value]
: [...selectedItems, newSelectedItem];
onChange?.(selected as V[]);
Expand Down
4 changes: 3 additions & 1 deletion src/components/Select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const SelectInner = <T extends Option, V extends Option | string>(
}: SelectProps<T, V>,
ref: ForwardedRef<HTMLElement>
) => {
const isValuePassedAsString = isString(value);
const {
active,
typed,
Expand All @@ -91,9 +92,10 @@ const SelectInner = <T extends Option, V extends Option | string>(
highlightedIndex,
hasItemsToSelect,
} = useSelect({
value: isString(value)
value: isValuePassedAsString
? options.find((item) => item.value === value)
: value,
isValuePassedAsString,
options,
onChange,
onFocus,
Expand Down
6 changes: 3 additions & 3 deletions src/components/Select/useSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import {
} from "downshift";
import { FocusEvent, useState } from "react";

import { isString } from "~/utils";

import { Option } from "../BaseSelect";
import { SelectProps } from "./Select";

export const useSelect = <T extends Option, V extends string | Option>({
value,
isValuePassedAsString,
options,
onChange,
onFocus,
onBlur,
}: {
value: T | null | undefined;
isValuePassedAsString: boolean;
options: T[];
onChange?: SelectProps<T, V>["onChange"];
onFocus?: (e: FocusEvent<HTMLElement, Element>) => void;
Expand All @@ -40,7 +40,7 @@ export const useSelect = <T extends Option, V extends string | Option>({
itemToString: (item) => item?.label ?? "",
onSelectedItemChange: ({ selectedItem }) => {
if (selectedItem) {
const selectedValue = isString(selectedItem)
const selectedValue = isValuePassedAsString
? selectedItem.value
: selectedItem;
onChange?.(selectedValue as V);
Expand Down

1 comment on commit 2d47594

@vercel
Copy link

@vercel vercel bot commented on 2d47594 Jan 19, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.