Easy Dynamic react selector, it gives you the freedom to custom your selections according to your own style.
npm install ezdynamic-react-selectorbash
npm install ezdynamic-react-selector
`
Usage
you should always import styles before it:
`ts
import "ezdynamic-react-selector/dist/index.css";
import { Selector } from "ezdynamic-react-selector";
`
Here are examples of how you can use it.
$3
#### Examples
#### Set up the selected array as:
`ts
const selectedArr = {
title: ReactNode;
value: string;
}[]
`
##### First way:
`ts
import "ezdynamic-react-selector/dist/index.css";
import { Selector } from "ezdynamic-react-selector";
import styles from "./App.module.css";
import { useState } from "react";
const arr = [
{
title: "one",
value: "one",
},
{
title: "two",
value: "two",
},
{
title: "three",
value: "three",
},
{
title: "four",
value: "four",
},
{
title: "five",
value: "five",
},
{
title: "six ",
value: "six",
},
];
function App() {
const [open, setOpen] = useState(false);
const [selectedValue, setSelectedValue] = useState("");
const handleToggle = () => {
setOpen(!open);
};
return (
className={styles.App}
style={{
padding: "20px",
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
>
style={{
display: "flex",
alignItems: "center",
}}
>
placeholder="Select Value"
label={selectedValue}
openMenu={open}
onToggle={() => handleToggle()}
list={arr}
onSelect={(value) => setSelectedValue(value)}
stylesControl={{
selector: styles.selector,
dropdown: styles.dropdown,
placeholder: styles.placeholder,
}}
/>
`css
// As Example
.selector {
}
.dropdown {
}
.placeholder {
}
`
#### Seocnd way:
##### or you can simply wrap your code inside of it and customize the selection condition as follows:
NOTE This approach simplifies coding and allows you to fully customize it to suit your needs, which is the core value of this package.
in this case you have to write your own style for the dropdown menu
`ts
placeholder="Select Value"
label={selectedValue}
openMenu={open}
onToggle={() => handleToggle()}
>
{arr.map((item, index) => (
className={${styles.item} ${
selectedValue === item.value ? styles.selected : ""
}}
onClick={() => setSelectedValue(item.value)}
>
{item.title}
{selectedValue === item.value && (
onClick={(e) => {
e.stopPropagation();
setSelectedValue("");
}}
>
❌
)}
{arr.length - 1 !== index &&
}
))}
CSS code as:
`css
.item {
display: flex;
justify-content: space-between;
align-items: center;
cursor: pointer;
border-radius: 4px;
padding: 4px 7px;
}
.selected {
background-color: #a419161c;
}
`
$3
Same as before but the label is changed to the selections the user would picked.
`ts
import "ezdynamic-react-selector/dist/index.css";
import { Selector } from "ezdynamic-react-selector";
import styles from "./App.module.css";
import { Fragment, useState } from "react";
const arr = [
{
title: "one",
value: "one",
},
{
title: "two",
value: "two",
},
{
title: "three",
value: "three",
},
{
title: "four",
value: "four",
},
{
title: "five",
value: "five",
},
{
title: "six ",
value: "six",
},
];
function App() {
const [open, setOpen] = useState(false);
const [selectedList, setSelectedList] = useState([]);
const handleToggle = () => {
setOpen(!open);
};
const removeItemFromList = (e, value) => {
e.stopPropagation();
setSelectedList((prev) => prev.filter((item) => item !== value));
};
return (
className={styles.App}
style={{
padding: "20px",
display: "flex",
justifyContent: "center",
alignItems: "center",
}}
>
style={{
display: "flex",
alignItems: "center",
}}
>
placeholder="Select Value"
label={
selectedList.length > 0 && (
{selectedList.map((item, index) => (
{item}
))}
)
}
openMenu={open}
onToggle={() => handleToggle()}
>
{arr.map((item, index) => (
className={${styles.item} ${
selectedList.includes(item.value) ? styles.selected : ""
}}
onClick={() =>
!selectedList.includes(item.value) &&
setSelectedList((prev) => [...prev, item.value])
}
>
{item.title}
{selectedList.includes(item.value) && (
removeItemFromList(e, item.value)}>
❌
)}
{arr.length - 1 !== index &&
}
))}
`css
.selectedList {
display: flex;
gap: 10px;
}
``