项目作者: kettanaito

项目描述 :
A utility function to create custom prop type validators for React with ".isRequired" chaining built in.
高级语言: JavaScript
项目地址: git://github.com/kettanaito/create-prop-types.git
创建时间: 2018-03-07T10:08:25Z
项目社区:https://github.com/kettanaito/create-prop-types

开源协议:MIT License

下载


create-prop-types

A lightweight helper library to create custom React prop types.

Getting started

Install

  1. npm install create-prop-types --save-dev

Create a custom prop type

  1. // ./prop-types/Iterable.js
  2. import createPropType from 'create-prop-types';
  3. import { Iterable } from 'immutable';
  4. const IterablePropType = createPropType({
  5. predicate: propValue => Iterable.isIterable(propValue)
  6. });
  7. export default IterablePropType;

Use the custom prop type

  1. import Iterable from './prop-types/Iterable';
  2. export default class MyComponent extends React.Component {
  3. static propTypes = {
  4. propA: Iterable,
  5. propB: Iterable.isRequired
  6. }
  7. }

API

predicate: (propValue: any) => boolean

A predicate function to satisfy in order to consider the prop value as valid.

Example

  1. import createPropType from 'create-prop-types';
  2. const CustomPropType = createPropType({
  3. predicate: propValue => (propValue !== 'foo')
  4. });
  5. export default CustomPropType;

warnings?: Warnings

Optional custom warning messages.

  1. type Warnings = {
  2. missing?: WarningResolver,
  3. invalid?: WarningResolver
  4. }
  5. type WarningResolver = (propName: string, propValue: any, componentName: string) => string

Example

  1. import createPropType from 'create-prop-types';
  2. const CustomPropType = createPropType({
  3. predicate: propValue => ...,
  4. warnings: {
  5. missing: (propName, propValue, componentName) => {
  6. return `Required prop "${propName}" is missing in "${componentName}" component.`;
  7. }
  8. }
  9. });
  10. export default CustomPropType;

License

This project is licensed under MIT License. See the license file for more details.