1.0.2 • Published 4 months ago
react-tw-select v1.0.2
React Tailwind Select
A fully customizable, accessible, and lightweight select dropdown built with React and Tailwind CSS.
Features
- ✅ Fully customizable styles with Tailwind CSS
- ✅ Keyboard navigation (Arrow keys, Enter, Escape)
- ✅ Click outside to close dropdown
- ✅ Customizable icons
- ✅ Support for custom rendering of options
- ✅ TypeScript support
Installation
Install the package via npm:
npm install react-tw-select
Or using yarn:
yarn add react-tw-select
Usage
Basic Example
import { useState } from "react";
import { Select } from "react-tw-select";
const options = [
{ label: "Option 1", value: "1" },
{ label: "Option 2", value: "2" },
{ label: "Option 3", value: "3" },
];
export default function App() {
const [selected, setSelected] = useState(null);
return (
<Select options={options} selected={selected} onChange={setSelected} />
);
}
Customizing Styles
You can override default styles using Tailwind classes:
<Select
options={options}
selected={selected}
onChange={setSelected}
buttonClassName="bg-blue-500 text-white p-2 rounded"
dropdownClassName="bg-gray-100 border border-gray-300"
optionClassName="hover:bg-gray-300"
selectedClassName="bg-blue-700 text-white"
/>
Custom Option Rendering
If you want to customize how options are displayed:
<Select
options={options}
selected={selected}
onChange={setSelected}
renderOption={(option, isSelected, isHighlighted) => (
<div
className={`p-2 ${isSelected ? "bg-green-500" : ""} ${
isHighlighted ? "bg-gray-200" : ""
}`}
>
{option.label}
</div>
)}
/>
Props
Prop Name | Type | Description |
---|---|---|
options | { label: string, value: string }[] | The list of options to display in the dropdown. |
selected | { label: string, value: string } \| null | The currently selected option. |
onChange | (option: { label: string, value: string }) => void | Function called when an option is selected. |
placeholder | string | Placeholder text when no option is selected. |
buttonClassName | string | Custom styles for the button. |
dropdownClassName | string | Custom styles for the dropdown container. |
optionClassName | string | Custom styles for options. |
selectedClassName | string | Styles for the selected option. |
icon | ReactNode | Custom icon for the dropdown button. |
renderOption | (option, isSelected, isHighlighted) => ReactNode | Custom rendering function for options. |
License
MIT License. Feel free to use and modify as needed!
Contributing
If you find any issues or want to contribute, feel free to open an issue or a pull request on GitHub.
Happy coding! 🚀