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-custom-props

v1.0.0-beta

Published

Custom configurable props for React or HTML elements

Downloads

20

Readme

ReactCustomProps

Custom configurable props for React components / elements.

Add custom configurable props or HTML attributes to React components / elements, or any HTML elements.

NPM Package Stats

NPM Version Dependencies Dev Dependencies

Build Status Coverage Status

Install

Instal via npm: npm install --save react-custom-props

View the JSbin demo

Use case: Add custom props to elements

Say we have a customizable HelloWorld component which has:

  • a required name props
  • an optional custom props
  • an optional customOptions props

Use with custom props values

const custom = {
  // optional custom props can be a value
  className: 'hello-world'
}
const node = (
  <HelloWorld name="foo" custom={custom} />
);

Or if you prefer (further examples will use this notation):

const props = { name: 'foo', custom: {
  className: 'hello-world'
}};
const node = (
  <HelloWorld {...props} />
);

Use with custom props functions

Hanlders are not evaluated by default (props name matches /^on\w/ RegExp).

const custom = { custom: {
  // optional custom props can be a function
  className: (...args) => ('hello-world'),

  // handlers /^on\w/ are not evaluated by default
  onClick: (ev) => {
    ev.preventDefault(); return false;
  }
}};

Use with custom.children array (indexed child)

const custom = { custom: {
  // optional children custom props can be an array
  children: [
    // will be applied to the first child
    {
      style: {backgroundColor: 'yellow'}
    }
  ]
}};

Use with custom.children object (named child)

const custom = { custom: {
  // optional children custom props can be an object
  children: {
    // will be applied to the 'span' child
    span: {
      style: {backgroundColor: 'yellow'}
    }
  }
}};

Use with customOptions props

Available customOptions are:

  • ignore: A matcher to ignore some props. The matched props are ignored in the resulting custom props. Default is null.
  • raw: A matcher to copy as is some props. The matched props are copied as is in the resulting custom props. Default is /^on\w/, so that handlers are not evaluated.
  • bind: A value for this in the evaluated custom props function. Default is null

A matcher can be either:

  • a value: The custom props name matches if it equals the value.
  • null or undefined (void): The custom props name never matches.
  • a RegExp: The custom props name matches if it the RegExp test success (i.e. /^foo/.test(name) w/ name = 'foo' is a success).
  • a Function: The custom props name matches if the function returns a truthly value.
  • an Array of the previous matcher types: The custom props name matches if it matches every matchers.
import custom from 'react-custom-props';

const props = {
  custom: {
    shouldBeIgnored: 'value', // should be ignored
    ignoreThisOneToo: 'value', // ignored too
    onClick: (ev) => (false), // handlers are raw
    rawValue: 'rawValue', //
    rawFunc: () => ('rawFunc')
    propValue: 'propValue',
    propFunc: () => ('propFunc'), // should be evaluated to 'propFunc'
    prop: (value) => (value) // should be evaluated to the passed in value
  },
  customOptions: {
    ignore: ['shouldBeIgnored', /^ignore/],
    raw: [/^on\w/, (name) => (name.substr(0, 3) === 'raw')]
    // let `bind` be the default (null)
  }
};
const customProps = custom(props)('argument');
console.log(customProps);
/*
{
  onClick: (ev) => (false),
  rawValue: 'rawValue',
  rawFunc: () => ('rawFunc'),
  propValue: 'propValue',
  propFunc: 'propFunc',
  prop: 'argument'
}
*/

Use case: Design a fully configurable component

With stateless function

Custom props are evaluated with props in the following example.

import React, {PropTypes} from 'react';
import custom, {addPropTypesCustom} from 'react-custom-props';

export default function HelloWorld(props) {
  return (
    // inject optional root props
    <h1 {...custom(props)(props)}>
      Hello
      // inject optional named child props
      <span {...custom(props, 'span')(props)}>
        {props.name}
      </span> !
    </h1>
  );
}

HelloWorld.propTypes = {
  name: PropTypes.string.isRequired,
};

// add PropTypes Custom (not required)
addPropTypesCustom(HelloWorld);

With React Component class

Custom props are evaluated with this.props and this.state in the following example.

import React, {PropTypes, Component} from 'react';
import custom, {addPropTypesCustom} from 'react-custom-props';

class HelloWorld extends Component {
  render() {
    return (
      // inject optional root props
      <h1 {...custom(this.props)(this.props, this.state)}>
        Hello
        // inject optional named child props
        <span {...custom(this.props, 'span')(this.props, this.state)}>
          {props.name}
        </span> !
      </h1>
    );
  }
}

HelloWorld.propTypes = {
  name: PropTypes.string.isRequired
};

// add PropTypes Custom (not required)
addPropTypesCustom(HelloWorld);