Skip to content

Commit

Permalink
themable selected items
Browse files Browse the repository at this point in the history
  • Loading branch information
jitunayak committed Nov 28, 2022
1 parent ad17531 commit b99e349
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
Binary file added public/snapshot2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/snapshot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 11 additions & 5 deletions src/components/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ export type Item = { label: string; key: string };
export type IProps = {
placeholder?: string;
list: Item[];
// selectedItems: Item[];
// setselectedItems: any;
selectedItems: Item[];
setSelectedItems: any;
enableSearch?: boolean;
colorSelectedItem?: { border: string; background: string };
};

function MultiSelect({
Expand All @@ -23,18 +22,17 @@ function MultiSelect({
selectedItems,
setSelectedItems: setselectedItems,
enableSearch = true,
colorSelectedItem = { border: "red", background: "rgb(254 242 242)" },
}: IProps) {
// const [selectedItems, setselectedItems] = useState<Item[]>([]);
const [showDropDownBox, setshowDropDownBox] = useState(false);
const [filtertext, setFiltertext] = useState("");
const [allItem, setAllItem] = useState(list);

const handleAddItem = (itemToBeAdded: Item) => {
const doesItemAlreadyExist = selectedItems.find(
(item) => item.key === itemToBeAdded.key
);

if (doesItemAlreadyExist) return;

setselectedItems([...selectedItems, itemToBeAdded]);
};

Expand All @@ -50,6 +48,10 @@ function MultiSelect({
{selectedItems.length === 0 && (
<div
className="selectedItem"
style={{
borderColor: `${colorSelectedItem.border}`,
backgroundColor: `${colorSelectedItem.background}`,
}}
onClick={() => setshowDropDownBox(!showDropDownBox)}
>
{placeholder} ...{" "}
Expand All @@ -59,6 +61,10 @@ function MultiSelect({
<div
key={item.key}
className="selectedItem"
style={{
borderColor: `${colorSelectedItem.border}`,
backgroundColor: `${colorSelectedItem.background}`,
}}
onClick={() => handleRemoveItem(item)}
>
{item.label}{" "}
Expand Down
23 changes: 23 additions & 0 deletions src/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,26 @@ yarn add slick-react-multiselect-dropdown # yarn
setSelectedItems={setSelectedItem}
/>
```

## 💄 Themable

color values for selected Items could be in all css accepted formats. such as `#cece` `rgb(254 242 242)` `green`

```ts

const colorSelectedItem = {
{ border: "red", background: "rgb(254 242 242)" }
}

<MultiSelect
placeholder="Select from list"
list= {listItems}
selectedItems= {selectedItem}
setSelectedItems= {setSelectedItem}
colorSelectedItem= {colorSelectedItem}
/>
```

## Result

!["demo snapshot"](https://github.com/jitunayak/slick-react-multiselect-dropdown/blob/main/public/snapshot2.jpeg?raw=true)

0 comments on commit b99e349

Please sign in to comment.