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

prop-types-required-nodes

v0.1.2

Published

Prop-types helper for strict runtime checking React nodes

Downloads

1

Readme

prop-types-required-nodes ⛔️

npm version

Prop-types helper for strict runtime checking React nodes.

This library relevant for React developers who used prop-types or developers who build UI-kits.

Idea

if you have some component that accepts other ReactNode components via props, for example:

interface ButtonProps {
  icon: React.ReactNode;
  children: React.ReactNode;
}

export function Button({ icon, children }: ButtonProps) {
  return (
    <div>
      {icon}
      {children}
    </div>
  );
}

in this case icon can be any ReactNode and it's normal situation. But sometimes we would like specify in props concrete ReactNode.

Let's consider next code:

interface IconProps {
  src: string;
}

export function Icon({ src }: IconProps) {
  return <>{src} </>;
}

interface ButtonProps {
  icon: React.ReactNode;
  children: React.ReactNode;
}

export function Button({ icon, children }: ButtonProps) {
  return (
    <div>
      {icon}
      {children}
    </div>
  );
}

// usage:
<Button icon={<Icon src="*" />}>My Button</Button>;

In my idea i want to allow receive in icon prop only <Icon /> component and nothing more.

// Valid case:
<Button icon={<Icon src="*" />}>My Button</Button>

// Possible case, but not in my opinion:
<Button icon={<span>*</span>}>My Button</Button>

By static types we can't effective infer ReactNode, but we can do it in runtime.

Inside prop-types-required-nodes using prop-types customProp:

MyComponent.propTypes = {
  customProp: function (props, propName, componentName) {
    if (!/matchme/.test(props[propName])) {
      return new Error(
        'Invalid prop `' +
          propName +
          '` supplied to' +
          ' `' +
          componentName +
          '`. Validation failed.',
      );
    }
  },
};

finally for strict checking nodes we can engage it!

Installation

Make sure you have installed prop-types

NPM

npm install prop-types-required-nodes --save-dev

Yarn

yarn add prop-types-required-nodes -D

Usage

import requiredNodes from 'prop-types-required-nodes';

interface IconProps {
  src: string;
}

export function Icon({ src }: IconProps) {
  return <>{src} </>;
}

interface ButtonProps {
  icon: React.ReactNode;
  children: React.ReactNode;
}

export function Button({ icon, children }: ButtonProps) {
  return (
    <div>
      {icon}
      {children}
    </div>
  );
}

Button.propTypes = {
  icon: requiredNodes([Icon]),
  children: PropTypes.node,
};

// console.error: Warning: Failed prop type: Invalid prop `icon` supplied to `Icon`. Validation failed.
<Button icon={<span>*</span>}>My Button</Button>;

Also you can pass to requiredNodes several nodes:

import SvgIcon from '@mui/material/SvgIcon';
import Icon from '@ant-design/icons';

import MessageSvg from 'path/to/message.svg';

Button.propTypes = {
  icon: requiredNodes([Icon, SvgIcon]),
  children: PropTypes.node,
};

// console.error: Warning: Failed prop type: Invalid prop `icon` supplied to `Icon`. Validation failed.
<Button icon={<span>*</span>}>My Button</Button>;

// no errors:
<Button icon={<Icon component={MessageSvg} />}>My Button</Button>;

// no errors:
<Button icon={<SvgIcon component={MessageSvg} viewBox="0 0 600 476.6" />}>
  My Button
</Button>;

Contributing

Your feedback and contributions are welcome. If you have a suggestion, please raise an issue. Prior to that, please search through the issues first in case your suggestion has been made already. If you decide to work on an issue, or feel like taking initiative and contributing anything at all, feel free to create a pull request and I will get back to you shortly.