From fb29d62fe242d7a1a46f35c82d3e6f522e81d017 Mon Sep 17 00:00:00 2001 From: brenopolanski Date: Fri, 9 Oct 2020 16:24:12 -0300 Subject: [PATCH 01/13] refactor: filter options in the select dropdown --- .../SelectDropdown/SelectDropdown.tsx | 112 +++++++++++++----- .../SelectDropdown/SelectDropdownInput.tsx | 25 ++++ src/app/components/SelectDropdown/styles.ts | 8 +- 3 files changed, 112 insertions(+), 33 deletions(-) mode change 100644 => 100755 src/app/components/SelectDropdown/SelectDropdown.tsx create mode 100755 src/app/components/SelectDropdown/SelectDropdownInput.tsx diff --git a/src/app/components/SelectDropdown/SelectDropdown.tsx b/src/app/components/SelectDropdown/SelectDropdown.tsx old mode 100644 new mode 100755 index 3804297255..bff6ce1cec --- a/src/app/components/SelectDropdown/SelectDropdown.tsx +++ b/src/app/components/SelectDropdown/SelectDropdown.tsx @@ -1,10 +1,11 @@ import { useFormField } from "app/components/Form/useFormField"; import { Icon } from "app/components/Icon"; import { Input, InputAddonEnd } from "app/components/Input"; -import { useSelect } from "downshift"; -import React, { useEffect, useState } from "react"; +import { SelectDropdownInput } from "app/components/SelectDropdown/SelectDropdownInput"; +import { useCombobox } from "downshift"; +import React, { useEffect, useMemo, useState } from "react"; -import { SelectOptionsList, SelectToggleButton } from "./styles"; +import { SelectOptionsList } from "./styles"; type Option = { label: string; @@ -12,48 +13,75 @@ type Option = { }; type SelectProps = { + options: Option[]; defaultValue?: string; isInvalid?: boolean; - disabled?: any; - options: Option[]; + disabled?: boolean; onChange?: (selected: Option) => void; } & React.InputHTMLAttributes; type SelectDropdownProps = { - placeholder?: string; options: Option[]; + placeholder?: string; onSelectedItemChange: any; disabled?: boolean; isInvalid?: boolean; defaultSelectedItem?: Option; } & React.InputHTMLAttributes; +export const itemToString = (item: Option | null) => item?.label || ""; + const SelectDropdown = ({ - placeholder, options, + placeholder, onSelectedItemChange, disabled, isInvalid, defaultSelectedItem, }: SelectDropdownProps) => { + const [items, setItems] = useState([...options]); + + const isMatch = (inputValue: string, option: Option) => + inputValue && option.label.toLowerCase().startsWith(inputValue.toLowerCase()); + const { isOpen, - selectedItem, - selectItem, - getToggleButtonProps, + closeMenu, + openMenu, + getComboboxProps, + getLabelProps, + getInputProps, + getItemProps, getMenuProps, + selectItem, + selectedItem, + inputValue, highlightedIndex, - getItemProps, - } = useSelect