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

cerebral-selectr

v0.14.0

Published

A tags selector module for Cerebral

Downloads

34

Readme

cerebral-selectr

cerebral-selectr module is built to only be used with Cerebral and React.

install

yarn add cerebral-selectr

demo

You can use the webpackbin https://www.webpackbin.com/bins/-KjrQdO5r08RepJqEA1W or the demo inside.

setup

import {Selectr} from 'cerebral-selectr'

const controller = Controller({
  modules: {
    selectr: Selectr({})
  }
})

To instantiate a Select you can just do the following. The name is mandatory

import React, { Component } from 'react'
import { Select } from 'cerebral-selectr'

class SomeComponent extends Component {
  render() {
    return (
      <div>
        <Select name={'tag1'} />
      </div>
    )
  }
}

export default SomeComponent

Additionally you can provide options to the component. If you don't set selected the option will be searchable. You can add extra payload if you need to get more data from hooks. You can also add a tagClicked function that is triggered when a tag is clicked. You will get the namespace and label as payload.

import React, { Component } from 'react'
import { Select } from 'cerebral-selectr'

class SomeComponent extends Component {
  render() {
    return (
      <div>
        <Select
          payload={{ moreStuff: 1, stuff: 2 }}
          tagClicked={(payload) => {}}
          name={'tag1'}
          options=[
            {label: 'Some option', selected: true}
          ]/>
      </div>
    )
  }
}

export default SomeComponent

Sometimes you want to make a tag pinned so that it can't be deleted.

import React, { Component } from 'react'
import { Select } from 'cerebral-selectr'

class SomeComponent extends Component {
  render() {
    return (
      <div>
        <Select
          name={'tag1'}
          options=[
            {label: 'Some option', selected: true, pinned: true}
          ]/>
      </div>
    )
  }
}

export default SomeComponent

You can make the whole select uneditable by passing editable: false

import React, { Component } from 'react'
import { Select } from 'cerebral-selectr'

class SomeComponent extends Component {
  render() {
    return (
      <div>
        <Select
          editable={false}
          name={'tag1'}
          options=[
            {label: 'Some option', selected: true}
          ]
        />
      </div>
    )
  }
}

export default SomeComponent

hooks

If you want to hook into the lifecycle of cerebral-selectr you have the following options.

  • onOptionCreated - When a new option is created that wasn't in the list before. Props passed is the namespace and the new label.
  • onSelectOption - Select an option. Props passed is the namespace and the label.
  • unSelectOption - unselect an option

const controller = Controller({
  modules: {
    selectr: Selectr({
      hooks: {
        onOptionCreated: [
          ({ props }) => {
            console.log('tag is created')
          }
        ],
        onSelectOption: [
          ({ props }) => {
            console.log('Option is selected')
          }
        ],
        unSelectOption: [
          ({ props }) => {
            console.log('options is unselected')
          }
        ],
      }
    })
  }
})

override styles

You can override styles if you want another.

<Select
  name={'tag1'}
  overrideStyles={{
    container: {
      borderRadius: 0
    },
    tags: {
      tag: {
        color: '#fff',
        borderRadius: 0,
        backgroundColor: '#588a00'
      },
      deleteEnter: {
        backgroundColor: '#008040',
        color: '#fff'
      }
    },
    options: {
      selected: {
        background: '#008040',
        color: '#fff'
      }
    }
  }}
/>

This is the default style of tag. Wrap it in the tags object.

tag: {
  color: '#fff',
  padding: 4,
  fontSize: 9,
  margin: 2,
  borderRadius: 2,
  backgroundColor: 'rgb(51, 122, 183)',
  display: 'inline-block',
  cursor: 'pointer',
  outline: 0,
  maxWidth: 'calc(100% - 12px)',
  wordWrap: 'break-word'
},
pinnedTag: {
  paddingRight: 2
},
flash: {
  backgroundColor: 'orange',
  transition: 'background 0.5s ease'
},
deleteEnter: {
  backgroundColor: '#d9534f'
},
pinnedEnter: {
  backgroundColor: 'rgb(20, 81, 135)'
},
cross: {
  marginLeft: 1,
  color: '#fff',
  padding: 2,
  fontSize: 11,
  cursor: 'pointer'
},
pinnedCross: {
  width: 0,
  fontSize: 11,
  display: 'inline-block'
}

Default styles for container.

container: {
  borderRadius: 2,
  fontFamily: 'verdana',
  width: 300,
  background: '#fff',
  padding: 3,
  border: '1px solid #ccc',
  cursor: 'pointer',
  display: 'inline-block'
},
noneEditableContainer: {
  fontFamily: 'verdana',
  width: 300,
  background: '#fff',
  display: 'inline-block'
}

Default styles for options

container: {
  height: 200,
  width: 308,
  overflow: 'hidden',
  boxSizing: 'border-box',
  backgroundColor: '#fff',
  position: 'absolute',
  borderRadius: 2,
  lineHeight: 1.3,
  border: '1px solid #aaa',
  boxShadow: '0 1px 6px rgba(0,0,0,.25)',
  outline: 0,
  cursor: 'pointer'
},
option: {
  padding: 7,
  cursor: 'pointer',
  fontFamily: 'verdana',
  color: '#000',
  height: 14,
  fontSize: 10,
  overflow: 'hidden',
  whiteSpace: 'nowrap',
  textOverflow: 'ellipsis'
},
selected: {
  background: 'rgb(51, 122, 183)',
  color: '#fff'
}