Contains minimum no of dependencies !
In the v1.0.0 provided basic select component with powerfull customization options.
- Install it
npm i react-easy-dropdown
- Import it to your React Component and use.
(See below example with example form)
import React, { useState } from "react";
import { BasicSelect } from "react-easy-dropdown";
const SampleOptionsData = [
{ id: 1, value: "Option 1" },
{ id: 2, value: "Option 2" },
{ id: 3, value: "Option 3" },
];
const LivePlayground = () => {
const [formInfo, setFormInfo] = useState({
text: "",
select: "",
textArea: "",
});
const handleChange = (event) => {
setFormInfo({ ...formInfo, [event.target.name]: event.target.value });
};
const handleSubmit = (event) => {
event.preventDefault();
console.log(formInfo);
};
return (
<div className='container mb-5 mt-5'>
<div className='row'>
<div className='col-md-6'></div>
<div className='col-md-6'>
<form onSubmit={(event) => handleSubmit(event)}>
<div className='mb-3'>
<label htmlFor='simpleTextInput' className='form-label'>
Type Something
</label>
<input
type='text'
id='simpleTextInput'
className='form-control'
name='text'
value={formInfo.text}
placeholder='Simple text input'
autoComplete='off'
onChange={(event) => handleChange(event)}
required={true}
//disabled
/>
</div>
<BasicSelect
name='select'
required={true}
options={SampleOptionsData}
onSelect={(event) => {
handleChange(event);
}}
wrapperStyles={{ backgroundColor: "#f2f2f2" }}
value={SampleOptionsData[2]}
hasLabelText={true}
labelWrapperStyles={{ marginBottom: "0" }}
labelText={"Please select a option :"}
labelTextStyles={{ fontWeight: "700" }}
hasSecondarTextForLabel={true}
secondaryTextWrapperStyle={{ backgroundColor: "#eee" }}
secondaryText={
"This secondary text comes under the label text of component"
}
onFocus={() => {
console.log("user focused !");
}}
onBlur={() => {
console.log("user blur !");
}}
/>
<div className='mb-3'>
<label htmlFor='simpleTextArea' className='form-label'>
Type Something long text
</label>
<textarea
name='textArea'
className='form-control'
id='simpleTextArea'
placeholder='Simple text area'
value={formInfo.textArea}
onChange={(event) => handleChange(event)}
></textarea>
</div>
<div className='d-grid gap-2 d-md-block'>
<button type='submit' className='btn btn-primary'>
Save
</button>
<button type='reset' className='btn btn-danger'>
Reset
</button>
</div>
</form>
</div>
</div>
</div>
);
};
export default LivePlayground;
Prop | Definition | Required | Default | Example |
---|---|---|---|---|
Basic Props | ||||
options |
sturctured objects of array where each object have id and name as properties | No | [] |
[{ id: 1, value: "Option 1" },{ id: 2, value: "Option 2" },] |
name |
name attribute of component | Yes | "select-comp" |
"country-dropdown" |
value |
Default value/ selected value | No | undefined |
{ id: 1, value: "Option 1" } |
id |
id attribute of component | No | "select-comp" |
"country-dropdown" |
required |
whether required field or not | No | false |
true |
disabled |
whether component is in disabled mode or not | No | false |
true |
userTriedSubmit |
this prop can use for validation purpose, when user tries submit and got validation errors can mark component with red outline along with setting required to true |
No | false |
true |
onSelect |
function to run when user select option from dropdown. this will return object similar to {target: { name: string; value: string }} |
Yes | (event) => {handleChange(event) |
|
onBlur |
function to excute when user going away from select component. have no return type | No | () => console.log('blur event occurs) |
|
onFocus |
function to execute when user focus to select component. have no return type | No | () => console.log('focus event occurs) |
|
Additionaly Introduced Props | ||||
wrapperStyles |
jsx style object to style entire wrapper of select component with label, secondary label and select component area | No | { backgroundColor: "#f2f2f2" } |
|
hasLabelText |
can show or hide label text using this prop | No | false |
true |
labelWrapperStyles |
jsx style object for style label component except label text | No |
|
{ backgroundColor: "#e2f2f2" } |
labelText |
text string for lebel | No | "Select a option:" |
"Select a Country :" |
labelTextStyles |
jsx style object to style label text. this won't affect to labelWrapperStyles wrapper styles |
No | { fontWeight: "700" } |
|
hasSecondarTextForLabel |
can show or hide secondary text for label | No | false |
true |
secondaryTextWrapperStyle |
jsx style object to style secondary label text wrapper | No | { backgroundColor: "#a2f2f2" } |
|
secondaryTextStyles |
jsx style object to style secondary label text. this won't affect to secondaryTextWrapperStyle wrapper styles |
No | { marginBottom: "0.8em", fontWeight: "100", fontSize: ".9rem" } |
|
selectContainerStyles |
jsx style object to style select component itself | No | { backgroundColor: "#f2f2f2", borderRadius: ".6rem" } |
|
showDropdownOnClickOfValue |
if you want to open dropdown when click on value of select component, then use this prop | No | false |
true |
hasClear |
show or hide select component clear button | No | false |
true |
clearControlEle |
you can pass your own icon,text or HTML element to appear as clear component. but use this prop appropriately | No | close SVG icon | X |
hasDivider |
show or hide divider line in between clear and dropdpwn icon | No | true |
false |
hasDropdown |
show or hide dropdown icon. Hint : if you hide both divider and dropdown icon, make sure to enable showDropdownOnClickOfValue so you can get new look and feel of select component |
No | true |
false |
dropdownEle |
you can pass your own icon,text or HTML element to appear as dropdown component. but use this prop appropriately | No | dropdown SVG icon | # |
Open for contributions
MIT Licensed. Copyright (c) Sashika Suraweera 2022.