Giter Club home page Giter Club logo

react-easy-dropdown's Introduction

React Easy Dropdown

Contains minimum no of dependencies !

A simple select component for the React Library.

In the v1.0.0 provided basic select component with powerfull customization options.



Two Steps :

  1. Install it
npm i react-easy-dropdown
  1. 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;


To see all prop details of <BasicSelect/> component refer below table.


Props Definitions :

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 #

Contribution

Open for contributions

License

MIT Licensed. Copyright (c) Sashika Suraweera 2022.

react-easy-dropdown's People

Contributors

sashiksu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.