npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rootre/forms-dropdown

v1.3.3

Published

Dropdown component AKA selectbox for various usages

Downloads

11

Readme

Dropdown component

Generic and lightweight React dropdown component.

Installation

With yarn

yarn add @rootre/forms-dropdown

or with npm

npm install @rootre/forms-dropdown

Usage

Basic usage:

import React from 'react';
import Dropdown from '@rootre/forms-dropdown';
import '@rootre/forms-dropdown/styles.css';

export default function App() {
    const items = [
        {label: 'Cat'},
        {label: 'Dog'},
        {label: 'Rabbit'},
        {label: 'Parrot'},
    ];

    return (
        <div>
            <Dropdown 
                items={items}
                afterChange={item => {
                    console.log('selected item:', item);
                }}
            />
        </div>
    );
}

Your item object can be anything, only mandatory field is label key that is shown in component HTML.

Usage with controllers:

You can pass react useState instances to control dropdown's behavior:

import React, {useState} from 'react';
import Dropdown from '@rootre/forms-dropdown';
import '@rootre/forms-dropdown/styles.css';

export default function App() {
    const items = [
        {label: 'Cat'},
        {label: 'Dog'},
        {label: 'Rabbit'},
        {label: 'Parrot'},
    ];

    const activeItemController = useState(items[0]); // bare in mind that controllers override initials
    const openController = useState(false);
    const [, setActiveItem] = activeItemController;
    const [, setOpen] = openController;

    return (
        <div>
            <button onClick={() => setActiveItem(items[2])}>Set Rabbit</button>
            <button onClick={() => setOpen(true)}>Open dropdown</button>
            <Dropdown
                items={items}
                controllers={{
                    active: activeItemController,
                    open: openController,
                }}
            />
        </div>
    );
}

Note

Controllers override initials, so if you want set controller for active item (controllers.active) you set initial active item in useState(initialItem)

Styling

Component classes are bundled via CSS modules and uses local ident name: dropdown_[local]

CSS file:

import '@rootre/forms-dropdown/styles.css';

Importing whole css file get you all basic formatting.

Optionally, if you use overrides: itemTemplate or itemsTemplate you could use defaults from original sass file:

Sass file:

import '@rootre/forms-dropdown/styles.scss';

It gets you following defaults:

.activeItem {} /* selected item */
.content {} /* wrapping div of opened dropdown contents */
.dropdown {} /* the whole dropdown */
.item {} /* item in opened dropdown list */
.list {} /* wrapping div of opened dropdown list */

Demo

Check out basic demo page

Props

activeItemTemplate: (label: string) => React.Component

default:

function activeItemTemplate(label) {
    return <span>{label}</span>;
}

afterChange: (item: object) => void

default: () => {}

afterOpen: (isOpen: boolean) => void

default: () => {}

controllers: object

controllers.active: Array.<function>

default: React.useState(null)

Controller for getting/setting active item

controllers.open: Array.<function>

default: React.useState(false)

Controller for opening/closing dropdown

disabled: boolean

default: false

Passing true renders the component disabled/unclickable

hasError: boolean

default: false

If true, an error class is added. Default styling paints borders in red

initialIsOpened: boolean

default: false

If dropdown should be rendered open or not

initialItem: object

default: null

Item that will be shown on first render

itemTemplate: (item: object, handleSelect: function, index: number, labelKey: string) => React.Component

default:

function itemTemplate(item, handleSelect, index, labelKey) {
    return (
        <div key={index} onClick={() => handleSelect(item)}>
            <span>{item[labelKey]}</span>
        </div>
    );
}

Use for custom formatting items in dropdown

itemsTemplate: (items: object[], handleSelect: function, labelKey: string, itemTemplate: function) => React.Component

default:

function itemsTemplate(items, handleSelect, labelKey, itemTemplate) {
    return (
        <div className={styles.list}>
            {items.map((item, index) => itemTemplate(item, handleSelect, index, labelKey))}
        </div>
    )
}

Use if you want to override the whole list of items in dropdown

items: Array.<object>

default: []

Can be array of any objects you want, but each object has to have labelKey property set

labelKey: string

default: label

Determines which item's object property will be used for rendering label inside dropdown

placeholder: string

default: ''

A placeholder text for dropdown