@lockmondan/select-custom
v0.2.10
Published
A select custom for react and next
Downloads
2
Readme
select-custom
Customizable select component for React and Next.js.
Understanding the Elements
In this package, you will find 2 elements, Select
and Option
, which allow you to customize them as you wish.
These elements use classes generated by Tailwind CSS for their default styling. To replace these generated classes with your own Tailwind classes, you can use the tailwind-merge
package, enabling you to style them however you like.
How to Style Them?
To style these elements, both have a className
property. However, there is a difference: the Select
component works a bit differently from the Option
component.
Select
The className
property uses an interface named ClassNameSelect
, which consists of three attributes: classNameRoot
, classNameView
, and classNameOptions
. Each of these attributes is responsible for a specific part of styling the Select
component.
classNameRoot
: It takes astring
type and is responsible for wrapping the entireSelect
element.classNameView
: It can receive either theClassNameSelectView
interface or astring
, and it is responsible for the display of the selected value. TheClassNameSelectView
interface has four attributes:style
: Styles the entire view area of theSelect
.whenShowOptions
: Modifies styling when the options list is shown.whenHiddenOptions
: Modifies styling when the options list is hidden.input
: Styles the input component.
classNameOptions
: It can receive theClassNameSelectComponent
type or astring
, and it is responsible for styling the options list. TheClassNameSelectComponent
interface has three attributes:style
: Styles the entire options list.whenShowOptions
: Modifies styling when the options list is shown.whenHiddenOptions
: Modifies styling when the options list is hidden.
Option
The className
property accepts only the string
type, allowing you to style the entire component as you prefer.
How to Use Them?
To use these components, the process is similar to using traditional select
and option
elements. The Select
component should be the parent element that wraps all Option
elements. See the example below:
<Select name="car" value="0">
<Option value="0" text="Ferrari" />
<Option value="1" text="Porsche" />
</Select>
In this example, Select
is the main container, and each Option
represents a choice available within the Select
.
Select
The Select
component has some required attributes: name
, value
, and children
.
name
: Typestring
, is required as it is the key to capture the selected value, especially useful in aform
with server actions.value
: Typestring
, sets the initially selected value from one of the options. If the value is not found, it will default to an empty string and display a default placeholder "Select an Option".children
: TheSelect
element must contain at least oneOption
element.
Optional Attributes
showOptionsIcon
andhiddenOptionsIcon
: TypeReact.ElementType
, responsible for changing the icon that appears in the component's view. It is recommended to use thereact-icons
package for swapping icons.sizeIcons
: Typenumber
, changes the size of the icons in the view.hiddenIcons
: Typeboolean
, hides the icons in the view.
Option
The Option
component has 2 required attributes: value
and text
.
value
: Typestring
. This property is directly tied to thevalue
attribute of theSelect
element. When the page renders, theOption
with the samevalue
will be pre-selected by theSelect
.Example:
<Select name="car" value="0"> <Option value="0" text="Ferrari" /> {/* Option pre-selected by Select */} <Option value="1" text="Porsche" /> </Select>
text
: Typestring
. This property defines the text that will be displayed by theSelect
element when one of the options is selected.
Optional Attributes:
placeholder
: Typeboolean
. Whentrue
, this property specifies that the text of theOption
will be used as a placeholder in theSelect
element. Whenever thisOption
is selected, theSelect
value will be an empty string.Example:
<Select name="car" value="placeholder"> <Option value="placeholder" text="Choose your car" placeholder /> <Option value="0" text="Ferrari" /> <Option value="1" text="Porsche" /> </Select>
icon
: TypeReact.ElementType
. This attribute adds an icon to theOption
element. It is recommended to use thereact-icons
package to add the icon.
Well, that's all and feel free to use it.