Skip to content

Commit

Permalink
Combobox fixes (#140)
Browse files Browse the repository at this point in the history
  • Loading branch information
MosheZemah authored Jun 7, 2021
1 parent d15de1f commit 37f5b42
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@

&.disabled {
@include disabled;
pointer-events: none;
}
}
11 changes: 7 additions & 4 deletions src/components/Combobox/Combobox.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const renderOption = (index, option, isActive, isActiveByKeyboard, onOptionClick
role="listitem"
ariaLabel={ariaLabel}
id={`combox-item-${id}`}
onMouseEnter={onOptionHover}
onMouseEnter={!disabled && onOptionHover}
onClick={event => onOptionClick(event, index, option, true)}
className={cx("combobox-option", {
disabled,
Expand Down Expand Up @@ -73,6 +73,7 @@ const Combobox = forwardRef(

const onOptionClick = useCallback(
(_event, index, option, mouseClick) => {
if (option.disabled) return;
onClick && onClick(option);
setActiveItemIndex(index);
if (mouseClick) {
Expand All @@ -99,7 +100,7 @@ const Combobox = forwardRef(
return filterdOptions.map((option, index) => {
return renderOption(index, option, activeItemIndex === index, isActiveByKeyboard, onOptionClick, onOptionHover);
});
}, [options, filterValue, activeItemIndex, isActiveByKeyboard, onOptionClick, onOptionHover]);
}, [filterdOptions, activeItemIndex, isActiveByKeyboard, onOptionClick, onOptionHover]);

const onChangeCallback = useCallback(
value => {
Expand All @@ -114,7 +115,9 @@ const Combobox = forwardRef(

const onAddNewCallback = useCallback(() => {
onAddNew && onAddNew(filterValue);
}, [onAddNew, filterValue]);
// clear filter after adding
setFilterValue("");
}, [onAddNew, filterValue, setFilterValue]);

useListKeyboardNavigation(
inputRef,
Expand Down Expand Up @@ -156,7 +159,7 @@ const Combobox = forwardRef(
ref={mergedRef}
role="listbox"
aria-activedescendant={`combobox-item-${id}`}
className={cx("combobox--wrapper", className)}
className={cx("combobox--wrapper", className, { empty: !hasResults })}
id={id}
>
<Search
Expand Down
6 changes: 5 additions & 1 deletion src/components/Combobox/Combobox.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ $icon-margin: 4px;
flex-direction: column;
@include font-general-text();

&.empty {
max-height: 200px;
}

&-search {
flex: 0 0 auto;
}
Expand Down Expand Up @@ -68,7 +72,7 @@ $icon-margin: 4px;
@include theme-prop(background-color, primary-selected-color);
}

&:hover:not(.disabled),
&:hover:not(.disabled):not(.selected),
&.active {
@include theme-prop(color, primary-text-color);
@include theme-prop(background-color, primary-background-hover-color);
Expand Down
35 changes: 25 additions & 10 deletions src/components/Combobox/__stories__/combobox.stories.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState, useCallback } from "react";
import { action } from "@storybook/addon-actions";
import { text, boolean, number, select } from "@storybook/addon-knobs";
import { withPerformance } from "storybook-addon-performance";
Expand All @@ -20,29 +20,44 @@ export const Sandbox = () => (
</div>
);

const getOptions = selectedId => {
return [
{ id: "1", label: "first", leftIcon: "fa fa-star-o", selected: selectedId === "1" },
{ id: "2", label: "second", rightIcon: "fa fa-star-o", selected: selectedId === "2" },
{ id: "3", label: "disabled", disabled: true, rightIcon: "fa fa-star-o", selected: selectedId === "3" },
{ id: "4", label: "fourth", selected: selectedId === "4" }
];
const getOptions = (selectedId, additionalOptions = []) => {
const options = [
{ id: "1", label: "first", leftIcon: "fa fa-star-o" },
{ id: "2", label: "second", rightIcon: "fa fa-star-o" },
{ id: "3", label: "disabled", disabled: true, rightIcon: "fa fa-star-o" },
{ id: "4", label: "fourth" }
].concat(additionalOptions);

options.forEach(option => {
option.selected = option.id === selectedId;
});
return options;
};

const ComboboxWrapper = () => {
const [selectedId, setSelectedId] = useState("2");
const [addedItems, setAddedItems] = useState([]);

const addNewItem = useCallback(
filterValue => {
const id = (5 + addedItems.length).toString();
setAddedItems(addedItems.concat([{ id, label: filterValue }]));
setSelectedId(id);
},
[addedItems, setAddedItems, setSelectedId]
);

return (
<div className="combobox-wrapper">
<Combobox
placeholder="Search for content"
onAddNew={value => console.log("Add new ", value)}
onAddNew={addNewItem}
addNewLabel={value => `+ Add new ${value}`}
onClick={option => {
console.log("Clicked on ", option.label);
setSelectedId(option.id);
}}
options={getOptions(selectedId)}
options={getOptions(selectedId, addedItems)}
/>
</div>
);
Expand Down
1 change: 0 additions & 1 deletion src/styles/states.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
@include theme-prop(border-color, disabled-background-color);
@include theme-prop(color, disabled-text-color);
cursor: not-allowed;
pointer-events: none;
&:hover {
background-color: transparent;
}
Expand Down

0 comments on commit 37f5b42

Please sign in to comment.