Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(select): Create a generic Id type for Option interface #214

Closed
wants to merge 9 commits into from
1 change: 1 addition & 0 deletions src/select/Select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function SelectComponent<T extends Option = Option>(
return (
<div
ref={selectRef}
data-testid={props.testid}
Copy link
Contributor

Choose a reason for hiding this comment

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

In the ideal scenario, I think we should be able to get all "div" props in addition to the SelectProps and pass all of them to the div element. But it is fine for now. Maybe we can discuss it later on and use this logic on other components as well

className={selectClassName}
role={role}
onKeyDown={handleSelectKeyDown}
Expand Down
11 changes: 9 additions & 2 deletions src/select/item/SelectItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ function SelectItemComponent<T extends Option = Option>(
) {
const selectState = useSelectContext();
const dispatchSelectStateAction = useSelectDispatchContext();
const {onSelect, value, focusedOptionIndex, shouldCloseOnSelect, options} = selectState;
const {
onSelect,
value,
focusedOptionIndex,
shouldCloseOnSelect,
options,
isMenuOpen
} = selectState;
const optionIndex = options.findIndex((opt) => opt?.id === option?.id);
const isSelected = Array.isArray(value)
? Boolean(value.find((currentOption) => currentOption.id === option?.id))
Expand Down Expand Up @@ -83,7 +90,7 @@ function SelectItemComponent<T extends Option = Option>(
payload: optionIndex
});

if (shouldCloseOnSelect) {
if (shouldCloseOnSelect && isMenuOpen) {
dispatchSelectStateAction({type: "TOGGLE_MENU_VISIBILITY"});
}
}
Expand Down
60 changes: 21 additions & 39 deletions src/select/typeahead/TypeaheadSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import TypeaheadInput, {
} from "../../form/input/typeahead/TypeaheadInput";
import {mapOptionsToTagShapes} from "../../tag/util/tagUtils";
import {TagShape} from "../../tag/Tag";
import {filterOptionsByKeyword} from "./util/typeaheadSelectUtils";
import {filterOutItemsByKey} from "../../core/utils/array/arrayUtils";
import Spinner from "../../spinner/Spinner";
import {KEYBOARD_EVENT_KEY} from "../../core/utils/keyboard/keyboardEventConstants";
Expand All @@ -32,57 +31,54 @@ export interface TypeaheadSelectProps<
TypeaheadInputProps,
"id" | "placeholder" | "name" | "onFocus" | "type"
>;
contentRenderer: (option: T) => React.ReactNode;
onKeywordChange: (value: string) => void;
keyword: string;
testid?: string;
onKeywordChange?: (value: string) => void;
initialKeyword?: string;
controlledKeyword?: string;
onTagRemove?: (option: Option) => void;
selectedOptionLimit?: number;
customClassName?: string;
shouldDisplaySelectedOptions?: boolean;
shouldFilterOptionsByKeyword?: boolean;
isDisabled?: boolean;
customSpinner?: React.ReactNode;
shouldShowEmptyOptions?: boolean;
canOpenDropdownMenu?: boolean;
areOptionsFetching?: boolean;
}

/* eslint-disable complexity */
function TypeaheadSelect<T extends TypeaheadSelectOption = TypeaheadSelectOption>({
testid,
options,
selectedOptions,
typeaheadProps,
onTagRemove,
keyword,
onKeywordChange,
onSelect,
contentRenderer,
customClassName,
selectedOptionLimit,
shouldDisplaySelectedOptions = true,
shouldFilterOptionsByKeyword = true,
isDisabled,
shouldShowEmptyOptions = true,
canOpenDropdownMenu = true,
areOptionsFetching,
customSpinner,
initialKeyword = "",
controlledKeyword
customSpinner
}: TypeaheadSelectProps<T>) {
const typeaheadInputRef = useRef<HTMLInputElement | null>(null);

const [isMenuOpen, setMenuVisibility] = useState(false);
const [computedDropdownOptions, setComputedDropdownOptions] = useState(options);
const [shouldFocusOnInput, setShouldFocusOnInput] = useState(false);
const [keyword, setKeyword] = useState(initialKeyword);
const inputValue = typeof controlledKeyword === "string" ? controlledKeyword : keyword;

const tags = mapOptionsToTagShapes(selectedOptions);
const tags = mapOptionsToTagShapes(selectedOptions, contentRenderer);
const shouldDisplayOnlyTags = Boolean(
selectedOptionLimit && selectedOptions.length >= selectedOptionLimit
);

const canSelectMultiple = !selectedOptionLimit || selectedOptionLimit > 1;
const canSelectMultiple =
options.length > 1 && (!selectedOptionLimit || selectedOptionLimit > 1);

const shouldCloseOnSelect =
!canSelectMultiple ||
Boolean(selectedOptionLimit && selectedOptions.length >= selectedOptionLimit - 1);
Expand Down Expand Up @@ -124,6 +120,7 @@ function TypeaheadSelect<T extends TypeaheadSelectOption = TypeaheadSelectOption
return (
// TODO: Add isMenuOpenHook when we have it
<Select
testid={testid}
role={"listbox"}
onSelect={handleSelect}
options={computedDropdownOptions}
Expand All @@ -134,6 +131,7 @@ function TypeaheadSelect<T extends TypeaheadSelectOption = TypeaheadSelectOption
<TypeheadSelectTrigger
tags={shouldDisplaySelectedOptions ? tags : []}
handleTagRemove={handleRemove}
onClick={openDropdownMenu}
input={
!shouldDisplayOnlyTags && (
<TypeaheadInput
Expand All @@ -143,8 +141,8 @@ function TypeaheadSelect<T extends TypeaheadSelectOption = TypeaheadSelectOption
name={typeaheadProps.name}
type={typeaheadProps.type}
placeholder={typeaheadProps.placeholder}
value={inputValue}
onQueryChange={handleKeywordChange}
value={keyword}
onQueryChange={onKeywordChange}
onKeyDown={handleKeyDown}
rightIcon={
areOptionsFetching ? spinnerContent : <CaretDownIcon aria-hidden={true} />
Expand All @@ -159,9 +157,10 @@ function TypeaheadSelect<T extends TypeaheadSelectOption = TypeaheadSelectOption
<Select.Content>
{computedDropdownOptions.map((option) => (
<Select.Item key={option.id} option={option}>
{option.title}
{contentRenderer(option)}
</Select.Item>
))}

{shouldShowEmptyOptions && !computedDropdownOptions.length && (
<p
data-testid={`${testid}.empty-message`}
Expand Down Expand Up @@ -191,33 +190,17 @@ function TypeaheadSelect<T extends TypeaheadSelectOption = TypeaheadSelectOption
if (!shouldDisplayOnlyTags && !isDisabled) {
onSelect(option);
setComputedDropdownOptions(options);
setKeyword("");
setShouldFocusOnInput(true);
onKeywordChange("");
setMenuVisibility(!shouldCloseOnSelect);
setShouldFocusOnInput(shouldCloseOnSelect);
}
}

function handleRemove(tag: TagShape<Option>) {
if (onTagRemove) {
onTagRemove(tag.context!);
setShouldFocusOnInput(true);
}
}

function handleKeywordChange(value: string) {
if (shouldFilterOptionsByKeyword) {
const unselectedOptions = options.filter(
(option) => selectedOptions.indexOf(option) < 0
);

setComputedDropdownOptions(filterOptionsByKeyword(unselectedOptions, value));
}

if (onKeywordChange) {
onKeywordChange(value);
}

if (typeof controlledKeyword === "undefined") {
setKeyword(value);
setMenuVisibility(false);
}
}

Expand All @@ -226,7 +209,7 @@ function TypeaheadSelect<T extends TypeaheadSelectOption = TypeaheadSelectOption

if (
key === KEYBOARD_EVENT_KEY.BACKSPACE &&
!inputValue &&
keyword === "" &&
onTagRemove &&
selectedOptions.length
) {
Expand All @@ -235,6 +218,5 @@ function TypeaheadSelect<T extends TypeaheadSelectOption = TypeaheadSelectOption
}
}
}
/* eslint-enable complexity */

export default TypeaheadSelect;
10 changes: 0 additions & 10 deletions src/select/typeahead/_typeahead-select.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@
width: 100%;
}

.typeahead-select__header-container {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: flex-start;

border: 1px solid var(--default-border-color);
border-radius: var(--small-border-radius);
}

.typeahead-select--can-select-multiple {
.input {
height: auto;
Expand Down
8 changes: 5 additions & 3 deletions src/select/typeahead/trigger/TypeheadSelectTrigger.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ export interface TypeheadSelectTriggerProps {
handleTagRemove: (tag: TagShape) => void;
customClassName?: string;
input?: React.ReactNode;
onClick?: VoidFunction;
}
function TypeheadSelectTrigger({
handleTagRemove,
tags,
customClassName,
input
input,
onClick
}: TypeheadSelectTriggerProps) {
return (
<Select.Trigger customClassName={"typeahead-select-trigger"}>
<Select.Trigger customClassName={"typeahead-select-trigger"} onClick={onClick}>
<List
customClassName={classNames(
"typeahead-select-trigger__tag-list",
customClassName
)}
testid={"TypeaheadSelectTrigger.list"}
items={tags}>
{(tag: TagShape) => (
<ListItem
Expand All @@ -41,6 +42,7 @@ function TypeheadSelectTrigger({
</ListItem>
)}
</List>

{input}
</Select.Trigger>
);
Expand Down
8 changes: 6 additions & 2 deletions src/select/typeahead/trigger/_typehead-select-trigger.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

padding: 0;

border: 1px solid var(--default-border-color);
border-radius: var(--small-border-radius);
.typeahead-select__input {
.input {
border: none;
border-radius: 8px;
}
}
}

.typeahead-select-trigger__tag-list {
Expand Down
Loading