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

react-role-manager

v1.0.10

Published

A simple react component to manage users roles/teams

Downloads

12

Readme

react-role-manager

Simple React component to manage users roles/teams based on Semantic UI for React

Install

npm i --save @techexmachina/react-role-manager

Example

import React, { Component } from 'react';
import autobind from 'react-autobind';
import PropTypes from 'prop-types';
import RoleManager from '@techexmachina/react-role-manager';
import { Segment, Checkbox, Grid, Header, Table, Search, Dropdown } from 'semantic-ui-react';
import 'semantic-ui-css/semantic.min.css';

const roles = [
  "super-admin",
  "admin",
  "author",
  "subscriber",
  "user",
];

const users = [
  {
    _id: 1,
    name: "david",
    roles: ['admin'],
  },
  {
    _id: 2,
    name: "simon",
    roles: ['subscriber'],
  },
  {
    _id: 3,
    name: "sylvain",
    roles: ['author'],
  },
  {
    _id: 4,
    name: "francois",
    roles: ['subscriber'],
  },
  {
    _id: 5,
    name: "jimmy",
    roles: ['admin'],
  },
];

/********************************************************/

export class RoleManagerExample extends Component {
  constructor( props ){
    super( props );
    autobind( this );
    this.state = {
      loading: false,
      results: [],
      columns: [
        '_id',
        'name',
      ],
    };
  }

  handeOptionChange( roles, searchValue ){
    this.setState({ loading: true });

    searchValue = searchValue.trim();

    if ( !roles.length && !searchValue ) this.setState({ loading: false, results: [] });
    else setTimeout( f => {
      const regexp = new RegExp( searchValue, 'i' );
      const results = users.filter( user => (
        ( !roles.length && regexp.test( user.name ) ) ||
        ( roles.length && roles.indexOf( user.roles[0] ) >= 0 && regexp.test( user.name ) ) ||
        ( !searchValue && roles.length && roles.indexOf( user.roles[0] ) >= 0 )
      ) );
      
      this.setState({ loading: false, results });
    }, 500 );
  }

  handleRoleChange( newRoles, userId ){
    console.log("Attributing new roles to " + userId, newRoles);
    // Update your databse here
  }

  render(){
    const { loading, results, columns } = this.state;

    return (
      <RoleManager
        loading={loading}
        results={results}
        onOptionChange={this.handeOptionChange}
        roles={roles}
        columns={columns}
        onRoleChange={this.handleRoleChange}
      />
    );
  }
}


Props

RoleManager props

| |Format|Required|What it does ?| |-------|-------|-------|-------| |loading|boolean| NO | Set to true if entries are being fetched / computed |onRoleChange|function| YES | A callback used to catch role changes on a specific user. Receives an array of roles (strings) & a user _id as parameters |onOptionChange|function| YES | A callback used to be called when the Search field or a role Checkbox is updated. Receives an array of roles (strings) & a search string as parameters |roles|[string]| YES | An array of strings to display the different roles/teams that can be (un)checked for changing search options |results|[object]| NO | An array of user objects to fill the TableGenerator. Each object must have at least a roles attribute (array of string) and an _id (string or number) |columns|[string or object]| YES | An array of string or object representing the different columns displayed by the TableGenerator. Strings should match of the user object property. Objects must contains at least a name (string) property for naming the column, and a as property, which is a React.Component prototype of your choice to be displayed in the cell. Objects can also have an arbitrary additionalProps property, which will be passed to the as component.

Notes

The RoleManager is based on react-table-generator module. It will automaticaly add an additional column to the Table with a semantic Dropdown in order to let you remove and add roles to users. The dropdown will automatically receive the onRoleChange prop and the user roles and _id

Contributors

  • David Panart (@Fen747)

MIT Licensed